Exemplo n.º 1
0
  def testCacheFileName(self):
    """Tests configuring the cache with a specific file name."""
    cache = oauth2_client.FileSystemTokenCache(
        path_pattern='/var/run/ccache/token.%(uid)s.%(key)s')
    if IS_WINDOWS:
      uid = '_'
    else:
      uid = os.getuid()
    self.assertEqual('/var/run/ccache/token.%s.abc123' % uid,
                     cache.CacheFileName('abc123'))

    cache = oauth2_client.FileSystemTokenCache(
        path_pattern='/var/run/ccache/token.%(key)s')
    self.assertEqual('/var/run/ccache/token.abc123',
                     cache.CacheFileName('abc123'))
Exemplo n.º 2
0
 def setUp(self):
   self.cache = oauth2_client.FileSystemTokenCache()
   self.start_time = datetime.datetime(2011, 3, 1, 10, 25, 13, 300826)
   self.token_1 = oauth2_client.AccessToken('token1', self.start_time)
   self.token_2 = oauth2_client.AccessToken(
       'token2', self.start_time + datetime.timedelta(seconds=492))
   self.key = 'token1key'
Exemplo n.º 3
0
def OAuth2ClientFromBotoConfig(
    config, cred_type=oauth2_client.CredTypes.OAUTH2_USER_ACCOUNT):
  token_cache = None
  token_cache_type = config.get('OAuth2', 'token_cache', 'file_system')
  if token_cache_type == 'file_system':
    if config.has_option('OAuth2', 'token_cache_path_pattern'):
      token_cache = oauth2_client.FileSystemTokenCache(
          path_pattern=config.get('OAuth2', 'token_cache_path_pattern'))
    else:
      token_cache = oauth2_client.FileSystemTokenCache()
  elif token_cache_type == 'in_memory':
    token_cache = oauth2_client.InMemoryTokenCache()
  else:
    raise Exception(
        "Invalid value for config option OAuth2/token_cache: %s" %
        token_cache_type)

  proxy_host = None
  proxy_port = None
  proxy_user = None
  proxy_pass = None
  if (config.has_option('Boto', 'proxy')
      and config.has_option('Boto', 'proxy_port')):
    proxy_host = config.get('Boto', 'proxy')
    proxy_port = int(config.get('Boto', 'proxy_port'))
    proxy_user = config.get('Boto', 'proxy_user', None)
    proxy_pass = config.get('Boto', 'proxy_pass', None)

  provider_authorization_uri = config.get(
      'OAuth2', 'provider_authorization_uri',
      GOOGLE_OAUTH2_PROVIDER_AUTHORIZATION_URI)
  provider_token_uri = config.get(
      'OAuth2', 'provider_token_uri', GOOGLE_OAUTH2_PROVIDER_TOKEN_URI)

  if cred_type == oauth2_client.CredTypes.OAUTH2_SERVICE_ACCOUNT:
    service_client_id = config.get('Credentials', 'gs_service_client_id', '')
    private_key_filename = config.get('Credentials', 'gs_service_key_file', '')
    with open(private_key_filename, 'rb') as private_key_file:
      private_key = private_key_file.read()

    json_key = None
    try:
      json_key = json.loads(private_key)
    except ValueError:
      pass
    if json_key:
      for json_entry in ('client_id', 'client_email', 'private_key_id',
                         'private_key'):
        if json_entry not in json_key:
          raise Exception('The JSON private key file at %s '
                          'did not contain the required entry: %s' %
                          (private_key_filename, json_entry))

      return oauth2_client.OAuth2JsonServiceAccountClient(
          json_key['client_id'], json_key['client_email'],
          json_key['private_key_id'], json_key['private_key'],
          access_token_cache=token_cache, auth_uri=provider_authorization_uri,
          token_uri=provider_token_uri,
          disable_ssl_certificate_validation=not(config.getbool(
              'Boto', 'https_validate_certificates', True)),
          proxy_host=proxy_host, proxy_port=proxy_port,
          proxy_user=proxy_user, proxy_pass=proxy_pass)
    else:
      key_file_pass = config.get('Credentials', 'gs_service_key_file_password',
                                 GOOGLE_OAUTH2_DEFAULT_FILE_PASSWORD)

      return oauth2_client.OAuth2ServiceAccountClient(
          service_client_id, private_key, key_file_pass,
          access_token_cache=token_cache, auth_uri=provider_authorization_uri,
          token_uri=provider_token_uri,
          disable_ssl_certificate_validation=not(config.getbool(
              'Boto', 'https_validate_certificates', True)),
          proxy_host=proxy_host, proxy_port=proxy_port,
          proxy_user=proxy_user, proxy_pass=proxy_pass)

  elif cred_type == oauth2_client.CredTypes.OAUTH2_USER_ACCOUNT:
    client_id = config.get('OAuth2', 'client_id',
                           os.environ.get('OAUTH2_CLIENT_ID', CLIENT_ID))
    if not client_id:
      raise Exception(
          'client_id for your application obtained from '
          'https://console.developers.google.com must be set in a boto config '
          'or with OAUTH2_CLIENT_ID environment variable or with '
          'gcs_oauth2_boto_plugin.SetFallbackClientIdAndSecret function.')

    client_secret = config.get('OAuth2', 'client_secret',
                               os.environ.get('OAUTH2_CLIENT_SECRET',
                                              CLIENT_SECRET))
    ca_certs_file=config.get_value('Boto', 'ca_certificates_file')
    if ca_certs_file == 'system':
      ca_certs_file = None

    if not client_secret:
      raise Exception(
          'client_secret for your application obtained from '
          'https://console.developers.google.com must be set in a boto config '
          'or with OAUTH2_CLIENT_SECRET environment variable or with '
          'gcs_oauth2_boto_plugin.SetFallbackClientIdAndSecret function.')
    return oauth2_client.OAuth2UserAccountClient(
        provider_token_uri, client_id, client_secret,
        config.get('Credentials', 'gs_oauth2_refresh_token'),
        auth_uri=provider_authorization_uri, access_token_cache=token_cache,
        disable_ssl_certificate_validation=not(config.getbool(
            'Boto', 'https_validate_certificates', True)),
        proxy_host=proxy_host, proxy_port=proxy_port,
        proxy_user=proxy_user, proxy_pass=proxy_pass,
        ca_certs_file=ca_certs_file)
  else:
    raise Exception('You have attempted to create an OAuth2 client without '
                    'setting up OAuth2 credentials.')