-
Notifications
You must be signed in to change notification settings - Fork 314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Credentials implementation supplying an ID token. #234
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -230,7 +230,7 @@ def requires_scopes(self): | |
|
||
@_helpers.copy_docstring(credentials.Scoped) | ||
def with_scopes(self, scopes): | ||
return Credentials( | ||
return self.__class__( | ||
self._signer, | ||
service_account_email=self._service_account_email, | ||
scopes=scopes, | ||
|
@@ -249,7 +249,7 @@ def with_subject(self, subject): | |
google.auth.service_account.Credentials: A new credentials | ||
instance. | ||
""" | ||
return Credentials( | ||
return self.__class__( | ||
self._signer, | ||
service_account_email=self._service_account_email, | ||
scopes=self._scopes, | ||
|
@@ -273,7 +273,7 @@ def with_claims(self, additional_claims): | |
new_additional_claims = copy.deepcopy(self._additional_claims) | ||
new_additional_claims.update(additional_claims or {}) | ||
|
||
return Credentials( | ||
return self.__class__( | ||
self._signer, | ||
service_account_email=self._service_account_email, | ||
scopes=self._scopes, | ||
|
@@ -336,3 +336,207 @@ def signer(self): | |
@_helpers.copy_docstring(credentials.Signing) | ||
def signer_email(self): | ||
return self._service_account_email | ||
|
||
|
||
class IDTokenCredentials(credentials.Signing, credentials.Credentials): | ||
"""Open ID Connect ID Token-based service account credentials. | ||
|
||
These credentials are largely similar to :class:`.Credentials`, but instead | ||
of using an OAuth 2.0 Access Token as the bearer token, they use an Open | ||
ID Connect ID Token as the bearer token. These credentials are useful when | ||
communicating to services that require ID Tokens and can not accept access | ||
tokens. | ||
|
||
Usually, you'll create these credentials with one of the helper | ||
constructors. To create credentials using a Google service account | ||
private key JSON file:: | ||
|
||
credentials = ( | ||
service_account.IDTokenCredentials.from_service_account_file( | ||
'service-account.json')) | ||
|
||
Or if you already have the service account file loaded:: | ||
|
||
service_account_info = json.load(open('service_account.json')) | ||
credentials = ( | ||
service_account.IDTokenCredentials.from_service_account_info( | ||
service_account_info)) | ||
|
||
Both helper methods pass on arguments to the constructor, so you can | ||
specify additional scopes and a subject if necessary:: | ||
|
||
credentials = ( | ||
service_account.IDTokenCredentials.from_service_account_file( | ||
'service-account.json', | ||
scopes=['email'], | ||
subject='[email protected]')) | ||
` | ||
The credentials are considered immutable. If you want to modify the scopes | ||
or the subject used for delegation, use :meth:`with_scopes` or | ||
:meth:`with_subject`:: | ||
|
||
scoped_credentials = credentials.with_scopes(['email']) | ||
delegated_credentials = credentials.with_subject(subject) | ||
|
||
""" | ||
def __init__(self, signer, service_account_email, token_uri, | ||
target_audience, additional_claims=None): | ||
""" | ||
Args: | ||
signer (google.auth.crypt.Signer): The signer used to sign JWTs. | ||
service_account_email (str): The service account's email. | ||
token_uri (str): The OAuth 2.0 Token URI. | ||
target_audience (str): The intended audience for these credentials, | ||
used when requesting the ID Token. The ID Token's ``aud`` claim | ||
will be set to this string. | ||
additional_claims (Mapping[str, str]): Any additional claims for | ||
the JWT assertion used in the authorization grant. | ||
|
||
.. note:: Typically one of the helper constructors | ||
:meth:`from_service_account_file` or | ||
:meth:`from_service_account_info` are used instead of calling the | ||
constructor directly. | ||
""" | ||
super(IDTokenCredentials, self).__init__() | ||
self._signer = signer | ||
self._service_account_email = service_account_email | ||
self._token_uri = token_uri | ||
self._target_audience = target_audience | ||
|
||
if additional_claims is not None: | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
self._additional_claims = additional_claims | ||
else: | ||
self._additional_claims = {} | ||
|
||
@classmethod | ||
def _from_signer_and_info(cls, signer, info, **kwargs): | ||
"""Creates a credentials instance from a signer and service account | ||
info. | ||
|
||
Args: | ||
signer (google.auth.crypt.Signer): The signer used to sign JWTs. | ||
info (Mapping[str, str]): The service account info. | ||
kwargs: Additional arguments to pass to the constructor. | ||
|
||
Returns: | ||
google.auth.jwt.IDTokenCredentials: The constructed credentials. | ||
|
||
Raises: | ||
ValueError: If the info is not in the expected format. | ||
""" | ||
kwargs.setdefault('service_account_email', info['client_email']) | ||
kwargs.setdefault('token_uri', info['token_uri']) | ||
return cls(signer, **kwargs) | ||
|
||
@classmethod | ||
def from_service_account_info(cls, info, **kwargs): | ||
"""Creates a credentials instance from parsed service account info. | ||
|
||
Args: | ||
info (Mapping[str, str]): The service account info in Google | ||
format. | ||
kwargs: Additional arguments to pass to the constructor. | ||
|
||
Returns: | ||
google.auth.service_account.IDTokenCredentials: The constructed | ||
credentials. | ||
|
||
Raises: | ||
ValueError: If the info is not in the expected format. | ||
""" | ||
signer = _service_account_info.from_dict( | ||
info, require=['client_email', 'token_uri']) | ||
return cls._from_signer_and_info(signer, info, **kwargs) | ||
|
||
@classmethod | ||
def from_service_account_file(cls, filename, **kwargs): | ||
"""Creates a credentials instance from a service account json file. | ||
|
||
Args: | ||
filename (str): The path to the service account json file. | ||
kwargs: Additional arguments to pass to the constructor. | ||
|
||
Returns: | ||
google.auth.service_account.IDTokenCredentials: The constructed | ||
credentials. | ||
""" | ||
info, signer = _service_account_info.from_filename( | ||
filename, require=['client_email', 'token_uri']) | ||
return cls._from_signer_and_info(signer, info, **kwargs) | ||
|
||
def with_target_audience(self, target_audience): | ||
"""Create a copy of these credentials with the specified target | ||
audience. | ||
|
||
Args: | ||
target_audience (str): The intended audience for these credentials, | ||
used when requesting the ID Token. | ||
|
||
Returns: | ||
google.auth.service_account.IDTokenCredentials: A new credentials | ||
instance. | ||
""" | ||
return self.__class__( | ||
self._signer, | ||
service_account_email=self._service_account_email, | ||
token_uri=self._token_uri, | ||
target_audience=target_audience, | ||
additional_claims=self._additional_claims.copy()) | ||
|
||
def _make_authorization_grant_assertion(self): | ||
"""Create the OAuth 2.0 assertion. | ||
|
||
This assertion is used during the OAuth 2.0 grant to acquire an | ||
ID token. | ||
|
||
Returns: | ||
bytes: The authorization grant assertion. | ||
""" | ||
now = _helpers.utcnow() | ||
lifetime = datetime.timedelta(seconds=_DEFAULT_TOKEN_LIFETIME_SECS) | ||
expiry = now + lifetime | ||
|
||
payload = { | ||
'iat': _helpers.datetime_to_secs(now), | ||
'exp': _helpers.datetime_to_secs(expiry), | ||
# The issuer must be the service account email. | ||
'iss': self.service_account_email, | ||
# The audience must be the auth token endpoint's URI | ||
'aud': self._token_uri, | ||
# The target audience specifies which service the ID token is | ||
# intended for. | ||
'target_audience': self._target_audience | ||
} | ||
|
||
payload.update(self._additional_claims) | ||
|
||
token = jwt.encode(self._signer, payload) | ||
|
||
return token | ||
|
||
@_helpers.copy_docstring(credentials.Credentials) | ||
def refresh(self, request): | ||
assertion = self._make_authorization_grant_assertion() | ||
access_token, expiry, _ = _client.id_token_jwt_grant( | ||
request, self._token_uri, assertion) | ||
self.token = access_token | ||
self.expiry = expiry | ||
|
||
@property | ||
def service_account_email(self): | ||
"""The service account email.""" | ||
return self._service_account_email | ||
|
||
@_helpers.copy_docstring(credentials.Signing) | ||
def sign_bytes(self, message): | ||
return self._signer.sign(message) | ||
|
||
@property | ||
@_helpers.copy_docstring(credentials.Signing) | ||
def signer(self): | ||
return self._signer | ||
|
||
@property | ||
@_helpers.copy_docstring(credentials.Signing) | ||
def signer_email(self): | ||
return self._service_account_email |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.