Ejemplo n.º 1
0
 def MakeServiceAccountCredentialsGoogleAuth(self):
     expiry = datetime.datetime(2001, 2, 3, 14, 15, 16)
     credentials = creds.FromJsonGoogleAuth(
         self.EXTENDED_SERVICE_ACCOUNT_CREDENTIALS_JSON)
     credentials.token = 'access_token'
     credentials.expiry = expiry
     credentials.id_tokenb64 = 'id-token'
     return credentials
Ejemplo n.º 2
0
 def testFromJson_UserAccountGoogleAuth(self):
     json_blob = textwrap.dedent("""\
     {
       "client_id": "foo.apps.googleusercontent.com",
       "client_secret": "file-secret",
       "refresh_token": "file-token",
       "scopes": [
         "scope1"
       ],
       "token_uri": "https://oauth2.googleapis.com/token",
       "type": "authorized_user"
     }""")
     expected_credentials = creds.FromJsonGoogleAuth(json_blob)
     expected_credentials_dict = json.loads(json_blob)
     del expected_credentials_dict['type']
     self.assertIsInstance(expected_credentials,
                           c_google_auth.UserCredWithReauth)
     self.AssertCredentialsEqual(expected_credentials,
                                 expected_credentials_dict)
Ejemplo n.º 3
0
    def testFromJson_ServiceAccountGoogleAuth(self):
        self.StartObjectPatch(google_auth_crypt.RSASigner,
                              'from_service_account_info')
        credentials = creds.FromJsonGoogleAuth(
            self.EXTENDED_SERVICE_ACCOUNT_CREDENTIALS_JSON)

        self.AssertCredentialsEqual(
            credentials, {
                'client_id': 'bar.apps.googleusercontent.com',
                'service_account_email': '*****@*****.**',
                'private_key_id': 'key-id',
                'private_key':
                '-----BEGIN PRIVATE KEY-----\nasdf\n-----END PRIVATE KEY-----\n',
                'project_id': 'bar-test',
                '_token_uri': 'https://oauth2.googleapis.com/token',
            })

        creds_type = creds.CredentialTypeGoogleAuth.FromCredentials(
            credentials)
        self.assertEqual(creds.CredentialTypeGoogleAuth.SERVICE_ACCOUNT,
                         creds_type)
def CredentialsFromAdcDictGoogleAuth(external_config):
    """Creates external account creds from a dict of application default creds.

  Args:
    external_config (Mapping): The configuration dictionary representing the
      credentials. This is loaded from the ADC file typically.

  Returns:
    google.auth.external_account.Credentials: The initialized external account
      credentials.

  Raises:
    BadCredentialJsonFileException: If the config format is invalid.
    googlecloudsdk.core.credentials.creds.InvalidCredentialsError: If the
      provided configuration is invalid or unsupported.
  """
    if ('type' not in external_config
            or external_config['type'] != _EXTERNAL_ACCOUNT_TYPE):
        raise BadCredentialJsonFileException(
            'The provided credentials configuration is not in a valid format.')

    return c_creds.FromJsonGoogleAuth(json.dumps(external_config))