コード例 #1
0
    def test_multistore_file_get_all_keys(self):
        # start with no keys
        keys = multistore_file.get_all_credential_keys(FILENAME)
        self.assertEquals([], keys)

        # store credentials
        credentials = self.create_test_credentials(client_id='client1')
        custom_key = {'myapp': 'testing', 'clientid': 'client1'}
        store1 = multistore_file.get_credential_storage_custom_key(
            FILENAME, custom_key)
        store1.put(credentials)

        keys = multistore_file.get_all_credential_keys(FILENAME)
        self.assertEquals([custom_key], keys)

        # store more credentials
        credentials = self.create_test_credentials(client_id='client2')
        string_key = 'string_key'
        store2 = multistore_file.get_credential_storage_custom_string_key(
            FILENAME, string_key)
        store2.put(credentials)

        keys = multistore_file.get_all_credential_keys(FILENAME)
        self.assertEquals(2, len(keys))
        self.assertTrue(custom_key in keys)
        self.assertTrue({'key': string_key} in keys)

        # back to no keys
        store1.delete()
        store2.delete()
        keys = multistore_file.get_all_credential_keys(FILENAME)
        self.assertEquals([], keys)
コード例 #2
0
    def test_multistore_file_get_all_keys(self):
        # start with no keys
        keys = multistore_file.get_all_credential_keys(FILENAME)
        self.assertEquals([], keys)

        # store credentials
        credentials = self._create_test_credentials(client_id='client1')
        custom_key = {'myapp': 'testing', 'clientid': 'client1'}
        store1 = multistore_file.get_credential_storage_custom_key(
            FILENAME, custom_key)
        store1.put(credentials)

        keys = multistore_file.get_all_credential_keys(FILENAME)
        self.assertEquals([custom_key], keys)

        # store more credentials
        credentials = self._create_test_credentials(client_id='client2')
        string_key = 'string_key'
        store2 = multistore_file.get_credential_storage_custom_string_key(
            FILENAME, string_key)
        store2.put(credentials)

        keys = multistore_file.get_all_credential_keys(FILENAME)
        self.assertEquals(2, len(keys))
        self.assertTrue(custom_key in keys)
        self.assertTrue({'key': string_key} in keys)

        # back to no keys
        store1.delete()
        store2.delete()
        keys = multistore_file.get_all_credential_keys(FILENAME)
        self.assertEquals([], keys)
コード例 #3
0
    def test_multistore_file_get_all_keys(self):
        # start with no keys
        keys = multistore_file.get_all_credential_keys(FILENAME)
        self.assertEquals([], keys)

        # store credentials
        credentials = self.create_test_credentials(client_id="client1")
        custom_key = {"myapp": "testing", "clientid": "client1"}
        store1 = multistore_file.get_credential_storage_custom_key(FILENAME, custom_key)
        store1.put(credentials)

        keys = multistore_file.get_all_credential_keys(FILENAME)
        self.assertEquals([custom_key], keys)

        # store more credentials
        credentials = self.create_test_credentials(client_id="client2")
        string_key = "string_key"
        store2 = multistore_file.get_credential_storage_custom_string_key(FILENAME, string_key)
        store2.put(credentials)

        keys = multistore_file.get_all_credential_keys(FILENAME)
        self.assertEquals(2, len(keys))
        self.assertTrue(custom_key in keys)
        self.assertTrue({"key": string_key} in keys)

        # back to no keys
        store1.delete()
        store2.delete()
        keys = multistore_file.get_all_credential_keys(FILENAME)
        self.assertEquals([], keys)
コード例 #4
0
def _FindStorageKeyForAccount(account):
  """Scans credential file for keys matching given account.

  If such key(s) is found it checks that current set of scopes is a subset of
  scopes associated with the key.

  Args:
    account: str, The account tied to the storage key being fetched.

  Returns:
    dict, key to be used in the credentials store.
  """
  storage_path = config.Paths().credentials_path
  current_scopes = set(config.CLOUDSDK_SCOPES)
  equivalent_keys = [key for key in
                     multistore_file.get_all_credential_keys(
                         filename=storage_path)
                     if (key.get('type') == 'google-cloud-sdk' and
                         key.get('account') == account and (
                             'scope' not in key or
                             set(key.get('scope').split()) >= current_scopes))]

  preferred_key = _GetStorageKeyForAccount(account)
  if preferred_key in equivalent_keys:
    equivalent_keys.remove(preferred_key)
  elif equivalent_keys:  # Migrate credentials over to new key format.
    storage = multistore_file.get_credential_storage_custom_key(
        filename=storage_path,
        key_dict=equivalent_keys[0])
    creds = storage.get()
    storage = multistore_file.get_credential_storage_custom_key(
        filename=storage_path,
        key_dict=preferred_key)
    storage.put(creds)

  # Remove all other entries.
  for key in equivalent_keys:
    storage = multistore_file.get_credential_storage_custom_key(
        filename=storage_path,
        key_dict=key)
    storage.delete()

  return preferred_key
コード例 #5
0
    def test_multistore_file_backwards_compatibility(self):
        credentials = self.create_test_credentials()
        scopes = ["scope1", "scope2"]

        # store the credentials using the legacy key method
        store = multistore_file.get_credential_storage(FILENAME, "client_id", "user_agent", scopes)
        store.put(credentials)

        # retrieve the credentials using a custom key that matches the legacy key
        key = {"clientId": "client_id", "userAgent": "user_agent", "scope": util.scopes_to_string(scopes)}
        store = multistore_file.get_credential_storage_custom_key(FILENAME, key)
        stored_credentials = store.get()

        self.assertEqual(credentials.access_token, stored_credentials.access_token)
コード例 #6
0
    def test_multistore_file_custom_key(self):
        credentials = self.create_test_credentials()

        custom_key = {"myapp": "testing", "clientid": "some client"}
        store = multistore_file.get_credential_storage_custom_key(FILENAME, custom_key)

        store.put(credentials)
        stored_credentials = store.get()

        self.assertNotEquals(None, stored_credentials)
        self.assertEqual(credentials.access_token, stored_credentials.access_token)

        store.delete()
        stored_credentials = store.get()

        self.assertEquals(None, stored_credentials)
コード例 #7
0
def _StorageForAccount(account):
    """Get the oauth2client.multistore_file storage.

  Args:
    account: str, The account tied to the storage being fetched.

  Returns:
    oauth2client.client.Storage, A credentials store.
  """
    storage_path = config.Paths().credentials_path
    parent_dir, unused_name = os.path.split(storage_path)
    files.MakeDir(parent_dir)

    storage = multistore_file.get_credential_storage_custom_key(
        filename=storage_path, key_dict=_FindStorageKeyForAccount(account))
    return storage
コード例 #8
0
ファイル: store.py プロジェクト: flgiordano/netcash
def _StorageForAccount(account):
  """Get the oauth2client.multistore_file storage.

  Args:
    account: str, The account tied to the storage being fetched.

  Returns:
    oauth2client.client.Storage, A credentials store.
  """
  storage_path = config.Paths().credentials_path
  parent_dir, unused_name = os.path.split(storage_path)
  files.MakeDir(parent_dir)

  storage = multistore_file.get_credential_storage_custom_key(
      filename=storage_path,
      key_dict=_FindStorageKeyForAccount(account))
  return storage
コード例 #9
0
    def test_multistore_file_custom_key(self):
        credentials = self.create_test_credentials()

        custom_key = {'myapp': 'testing', 'clientid': 'some client'}
        store = multistore_file.get_credential_storage_custom_key(
            FILENAME, custom_key)

        store.put(credentials)
        stored_credentials = store.get()

        self.assertNotEquals(None, stored_credentials)
        self.assertEqual(credentials.access_token,
                         stored_credentials.access_token)

        store.delete()
        stored_credentials = store.get()

        self.assertEquals(None, stored_credentials)
コード例 #10
0
    def test_multistore_file_backwards_compatibility(self):
        credentials = self._create_test_credentials()
        scopes = ['scope1', 'scope2']

        # store the credentials using the legacy key method
        store = multistore_file.get_credential_storage(
            FILENAME, 'client_id', 'user_agent', scopes)
        store.put(credentials)

        # retrieve the credentials using a custom key that matches the
        # legacy key
        key = {'clientId': 'client_id', 'userAgent': 'user_agent',
               'scope': util.scopes_to_string(scopes)}
        store = multistore_file.get_credential_storage_custom_key(
            FILENAME, key)
        stored_credentials = store.get()

        self.assertEqual(credentials.access_token,
                         stored_credentials.access_token)
コード例 #11
0
    def test_multistore_file_backwards_compatibility(self):
        credentials = self.create_test_credentials()
        scopes = ['scope1', 'scope2']

        # store the credentials using the legacy key method
        store = multistore_file.get_credential_storage(FILENAME, 'client_id',
                                                       'user_agent', scopes)
        store.put(credentials)

        # retrieve the credentials using a custom key that matches the legacy key
        key = {
            'clientId': 'client_id',
            'userAgent': 'user_agent',
            'scope': util.scopes_to_string(scopes)
        }
        store = multistore_file.get_credential_storage_custom_key(
            FILENAME, key)
        stored_credentials = store.get()

        self.assertEqual(credentials.access_token,
                         stored_credentials.access_token)
コード例 #12
0
def _StorageForAccount(account):
    """Get the oauth2client.multistore_file storage.

  Args:
    account: str, The account tied to the storage being fetched.

  Returns:
    oauth2client.client.Storage, A credentials store.
  """
    storage_path = config.Paths().credentials_path
    parent_dir, unused_name = os.path.split(storage_path)
    files.MakeDir(parent_dir)

    storage_key = {
        "type": "google-cloud-sdk",
        "account": account,
        "clientId": properties.VALUES.auth.client_id.Get(required=True),
        "scope": " ".join(config.CLOUDSDK_SCOPES),
    }

    storage = multistore_file.get_credential_storage_custom_key(filename=storage_path, key_dict=storage_key)
    return storage
コード例 #13
0
def _StorageForAccount(account):
    """Get the oauth2client.multistore_file storage.

  Args:
    account: str, The account tied to the storage being fetched.

  Returns:
    oauth2client.client.Storage, A credentials store.
  """
    storage_path = config.Paths().credentials_path
    parent_dir, unused_name = os.path.split(storage_path)
    files.MakeDir(parent_dir)

    storage_key = {
        'type': 'google-cloud-sdk',
        'account': account,
        'clientId': properties.VALUES.auth.client_id.Get(required=True),
        'scope': ' '.join(config.CLOUDSDK_SCOPES),
    }

    storage = multistore_file.get_credential_storage_custom_key(
        filename=storage_path, key_dict=storage_key)
    return storage
コード例 #14
0
ファイル: store.py プロジェクト: squarethecircle/snapBetter
def _StorageForAccount(account):
  """Get the oauth2client.multistore_file storage.

  Args:
    account: str, The account tied to the storage being fetched.

  Returns:
    oauth2client.client.Storage, A credentials store.
  """
  storage_path = config.Paths().credentials_path
  parent_dir, unused_name = os.path.split(storage_path)
  files.MakeDir(parent_dir)

  storage_key = {
      'type': 'google-cloud-sdk',
      'account': account,
      'clientId': config.CLOUDSDK_CLIENT_ID,
      'scope': ' '.join(config.CLOUDSDK_SCOPES),
  }

  storage = multistore_file.get_credential_storage_custom_key(
      filename=storage_path,
      key_dict=storage_key)
  return storage