Skip to content
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 service_account.Credentials.with_claims #140

Merged
merged 2 commits into from
Mar 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions google/auth/jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import base64
import collections
import copy
import datetime
import json

Expand Down Expand Up @@ -426,13 +427,15 @@ def with_claims(self, issuer=None, subject=None, audience=None,
Returns:
google.auth.jwt.Credentials: A new credentials instance.
"""
new_additional_claims = copy.deepcopy(self._additional_claims)
new_additional_claims.update(additional_claims or {})

return Credentials(
self._signer,
issuer=issuer if issuer is not None else self._issuer,
subject=subject if subject is not None else self._subject,
audience=audience if audience is not None else self._audience,
additional_claims=self._additional_claims.copy().update(
additional_claims or {}))
additional_claims=new_additional_claims)

def _make_jwt(self):
"""Make a signed JWT.
Expand Down
24 changes: 24 additions & 0 deletions google/oauth2/service_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
.. _RFC 7523: https://tools.ietf.org/html/rfc7523
"""

import copy
import datetime

from google.auth import _helpers
Expand Down Expand Up @@ -246,6 +247,29 @@ def with_subject(self, subject):
subject=subject,
additional_claims=self._additional_claims.copy())

def with_claims(self, additional_claims):

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

"""Returns a copy of these credentials with modified claims.

Args:
additional_claims (Mapping[str, str]): Any additional claims for
the JWT payload. This will be merged with the current
additional claims.

Returns:
google.auth.service_account.Credentials: A new credentials
instance.
"""
new_additional_claims = copy.deepcopy(self._additional_claims)
new_additional_claims.update(additional_claims or {})

return Credentials(
self._signer,
service_account_email=self._service_account_email,
scopes=self._scopes,
token_uri=self._token_uri,
subject=self._subject,
additional_claims=new_additional_claims)

def _make_authorization_grant_assertion(self):
"""Create the OAuth 2.0 assertion.

Expand Down
4 changes: 4 additions & 0 deletions tests/oauth2/test_service_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ def test_create_scoped(self):
credentials = self.credentials.with_scopes(scopes)
assert credentials._scopes == scopes

def test_with_claims(self):
new_credentials = self.credentials.with_claims({'meep': 'moop'})
assert new_credentials._additional_claims == {'meep': 'moop'}

def test__make_authorization_grant_assertion(self):
token = self.credentials._make_authorization_grant_assertion()
payload = jwt.decode(token, PUBLIC_CERT_BYTES)
Expand Down