Example #1
0
    def testCacheFileName(self):
        cache = oauth2_client.FileSystemTokenCache(
            path_pattern='/var/run/ccache/token.%(uid)s.%(key)s')
        self.assertEquals('/var/run/ccache/token.%d.abc123' % os.getuid(),
                          cache.CacheFileName('abc123'))

        cache = oauth2_client.FileSystemTokenCache(
            path_pattern='/var/run/ccache/token.%(key)s')
        self.assertEquals('/var/run/ccache/token.abc123',
                          cache.CacheFileName('abc123'))
Example #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'
def OAuth2ClientFromBotoConfig(config):
    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 = None
    if (config.has_option('Boto', 'proxy')
            and config.has_option('Boto', 'proxy_port')):
        proxy = "%s:%s" % (config.get(
            'Boto', 'proxy'), config.get('Boto', 'proxy_port'))

    provider_label = config.get('OAuth2', 'provider_label',
                                GOOGLE_OAUTH2_PROVIDER_LABEL)
    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)

    client_id = config.get('OAuth2', 'client_id', GSUTIL_CLIENT_ID)
    client_secret = config.get('OAuth2', 'client_secret',
                               GSUTIL_CLIENT_NOTSOSECRET)

    return oauth2_client.OAuth2Client(oauth2_client.OAuth2Provider(
        provider_label, provider_authorization_uri, provider_token_uri),
                                      client_id,
                                      client_secret,
                                      proxy=proxy,
                                      access_token_cache=token_cache)
Example #4
0
def OAuth2ClientFromBotoConfig(config, 
    cred_type=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
  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'))
  
  provider_label = config.get(  
      'OAuth2', 'provider_label', GOOGLE_OAUTH2_PROVIDER_LABEL)  
  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 == CredTypes.OAUTH2_SERVICE_ACCOUNT:
    service_client_id = config.get('Credentials', 'gs_service_client_id', '')
    private_key_filename = config.get('Credentials', 'gs_service_key_file', '')
    key_file_pass = config.get('Credentials', 'gs_service_key_file_password',
                               GOOGLE_OAUTH2_DEFAULT_FILE_PASSWORD)
    with open(private_key_filename, 'rb') as private_key_file:
      private_key = private_key_file.read()
    
    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)

  elif cred_type == CredTypes.OAUTH2_USER_ACCOUNT:
    client_id = config.get('OAuth2', 'client_id', GSUTIL_CLIENT_ID)
    client_secret = config.get(
        'OAuth2', 'client_secret', GSUTIL_CLIENT_NOTSOSECRET)
    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,
            ca_certs_file=config.get_value('Boto', 'ca_certificates_file'))
  else:
    raise Exception('You have attempted to create an OAuth2 client without '
        'setting up OAuth2 credentials. Please run "gsutil config" to set up '
        'your credentials correctly or see "gsutil help config" for more '
        'information.')