Check if key is defined before checking key name
This allows an access token to be generated in some cases even
when the default key is not defined.
@@ -140,12 +140,20 @@ class AppIdentityService(BaseService):
# account definition.
default_audience = 'https://www.googleapis.com/oauth2/v4/token'
+ if (service_account_name is None or
+ (self._key is not None and
+ self._key.key_name == service_account_name)):
+ if self._key is None:
+ raise UnknownError('A private key is not configured')
+ else:
+ assertion = self._key.generate_assertion(default_audience, scopes)
+ # TODO: Generate access token from assertion.
+ return AccessToken(assertion, int(time.time() + 3600))
+
if service_account_id is not None:
raise UnknownError(
'{} is not configured'.format(service_account_id))
- if (service_account_name is not None and
- service_account_name != self._key.key_name):
service_account_node = '/'.join([self._service_accounts_node,
service_account_name])
try:
@@ -179,13 +187,6 @@ class AppIdentityService(BaseService):
expiration_time = int(time.time()) + token_details['expires_in']
return AccessToken(token_details['access_token'], expiration_time)
- if self._key is None:
- raise UnknownError('A private key is not configured')
-
- assertion = self._key.generate_assertion(default_audience, scopes)
- # TODO: Generate access token from assertion.
- return AccessToken(assertion, int(time.time() + 3600))
-
def sign(self, blob):
""" Signs a message with the project's key.
