4747
4848_GOOGLE_OAUTH2_TOKEN_ENDPOINT = "https://oauth2.googleapis.com/token"
4949
50+ _SOURCE_CREDENTIAL_AUTHORIZED_USER_TYPE = "authorized_user"
51+ _SOURCE_CREDENTIAL_SERVICE_ACCOUNT_TYPE = "service_account"
52+ _SOURCE_CREDENTIAL_EXTERNAL_ACCOUNT_AUTHORIZED_USER_TYPE = (
53+ "external_account_authorized_user"
54+ )
55+
5056
5157def _make_iam_token_request (
5258 request ,
@@ -411,39 +417,45 @@ def with_scopes(self, scopes, default_scopes=None):
411417 return cred
412418
413419 @classmethod
414- def _source_credentials_from_impersonated_account_info (cls , info ):
415- """Creates a Credentials instance from parsed authorized user info.
420+ def from_impersonated_service_account_info (cls , info , scopes = None ):
421+ """Creates a Credentials instance from parsed impersonated service account credentials info.
416422
417423 Args:
418- info (Mapping[str, str]): The authorized user info in Google
424+ info (Mapping[str, str]): The impersonated service account credentials info in Google
419425 format.
426+ scopes (Sequence[str]): Optional list of scopes to include in the
427+ credentials.
420428
421429 Returns:
422430 google.oauth2.credentials.Credentials: The constructed
423431 credentials.
424432
425433 Raises:
426- InvalidType: If the source_credentials are not a support impersonation type
427- ValueError: If the source_credentials info is not in the expected format.
434+ InvalidType: If the info["source_credentials"] are not a supported impersonation type
435+ InvalidValue: If the info["service_account_impersonation_url"] is not in the expected format.
436+ ValueError: If the info is not in the expected format.
428437 """
429- _AUTHORIZED_USER_TYPE = "authorized_user"
430- _SERVICE_ACCOUNT_TYPE = "service_account"
431- _EXTERNAL_ACCOUNT_AUTHORIZED_USER_TYPE = "external_account_authorized_user"
432438
433439 source_credentials_info = info .get ("source_credentials" )
434440 source_credentials_type = source_credentials_info .get ("type" )
435- if source_credentials_type == _AUTHORIZED_USER_TYPE :
441+ if source_credentials_type == _SOURCE_CREDENTIAL_AUTHORIZED_USER_TYPE :
436442 from google .oauth2 import credentials
443+
437444 source_credentials = credentials .Credentials .from_authorized_user_info (
438445 source_credentials_info
439446 )
440- elif source_credentials_type == _SERVICE_ACCOUNT_TYPE :
447+ elif source_credentials_type == _SOURCE_CREDENTIAL_SERVICE_ACCOUNT_TYPE :
441448 from google .oauth2 import service_account
449+
442450 source_credentials = service_account .Credentials .from_service_account_info (
443451 source_credentials_info
444452 )
445- elif source_credentials_type == _EXTERNAL_ACCOUNT_AUTHORIZED_USER_TYPE :
453+ elif (
454+ source_credentials_type
455+ == _SOURCE_CREDENTIAL_EXTERNAL_ACCOUNT_AUTHORIZED_USER_TYPE
456+ ):
446457 from google .auth import external_account_authorized_user
458+
447459 source_credentials = external_account_authorized_user .Credentials .from_info (
448460 source_credentials_info
449461 )
@@ -454,28 +466,6 @@ def _source_credentials_from_impersonated_account_info(cls, info):
454466 )
455467 )
456468
457- return source_credentials
458-
459- @classmethod
460- def from_impersonated_account_info (cls , info , scopes = None ):
461- """Creates a Credentials instance from parsed authorized user info.
462-
463- Args:
464- info (Mapping[str, str]): The authorized user info in Google
465- format.
466- scopes (Sequence[str]): Optional list of scopes to include in the
467- credentials.
468-
469- Returns:
470- google.oauth2.credentials.Credentials: The constructed
471- credentials.
472-
473- Raises:
474- InvalidType: If the source_credentials are not a support impersonation type
475- ValueError: If the info is not in the expected format.
476- """
477- source_credentials = cls ._source_credentials_from_impersonated_account_info (info )
478-
479469 impersonation_url = info .get ("service_account_impersonation_url" )
480470 start_index = impersonation_url .rfind ("/" )
481471 end_index = impersonation_url .find (":generateAccessToken" )
@@ -484,7 +474,6 @@ def from_impersonated_account_info(cls, info, scopes=None):
484474 "Cannot extract target principal from {}" .format (impersonation_url )
485475 )
486476 target_principal = impersonation_url [start_index + 1 : end_index ]
487-
488477 delegates = info .get ("delegates" )
489478 quota_project_id = info .get ("quota_project_id" )
490479
@@ -493,7 +482,7 @@ def from_impersonated_account_info(cls, info, scopes=None):
493482 target_principal ,
494483 scopes ,
495484 delegates ,
496- quota_project_id = quota_project_id
485+ quota_project_id = quota_project_id ,
497486 )
498487
499488
0 commit comments