Skip to content

Commit 27b9d76

Browse files
author
Jon Wayne Parrott
authored
Add service_account.Credentials.to_jwt_credentials (#45)
1 parent 6ac0443 commit 27b9d76

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

packages/google-auth/google/oauth2/service_account.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,33 @@ def from_service_account_file(cls, filename, **kwargs):
204204
filename, require=['client_email', 'token_uri'])
205205
return cls._from_signer_and_info(signer, info, **kwargs)
206206

207+
def to_jwt_credentials(self):
208+
"""Creates a :cls:`google.auth.jwt.Credentials` instance from this
209+
instance.
210+
211+
The new instance will use the same private key as this instance and
212+
will use this instance's service account email as the issuer and
213+
subject.
214+
215+
This is the same as calling
216+
:meth:`jwt.Credentials.from_service_account_file` with the same
217+
file used to create these credentials::
218+
219+
svc_creds = service_account.Credentials.from_service_account_file(
220+
'service_account.json')
221+
jwt_from_svc = svc_credentials.to_jwt_credentials()
222+
# is the same as:
223+
jwt_creds = jwt.Credentials.from_service_account_file(
224+
'service_account.json')
225+
226+
Returns:
227+
google.auth.jwt.Credentials: A new Credentials instance.
228+
"""
229+
return jwt.Credentials(
230+
self._signer,
231+
issuer=self._service_account_email,
232+
subject=self._service_account_email)
233+
207234
@property
208235
def requires_scopes(self):
209236
"""Checks if the credentials requires scopes.

packages/google-auth/tests/oauth2/test_service_account.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,14 @@ def credentials_fixture(self, signer):
5858
signer, self.SERVICE_ACCOUNT_EMAIL, self.TOKEN_URI)
5959

6060
def test_from_service_account_info(self):
61-
with open(SERVICE_ACCOUNT_JSON_FILE, 'r') as fh:
62-
info = json.load(fh)
63-
6461
credentials = service_account.Credentials.from_service_account_info(
65-
info)
62+
SERVICE_ACCOUNT_INFO)
6663

67-
assert credentials._signer.key_id == info['private_key_id']
68-
assert credentials._service_account_email == info['client_email']
69-
assert credentials._token_uri == info['token_uri']
64+
assert (credentials._signer.key_id ==
65+
SERVICE_ACCOUNT_INFO['private_key_id'])
66+
assert (credentials._service_account_email ==
67+
SERVICE_ACCOUNT_INFO['client_email'])
68+
assert credentials._token_uri == SERVICE_ACCOUNT_INFO['token_uri']
7069

7170
def test_from_service_account_info_args(self):
7271
info = SERVICE_ACCOUNT_INFO.copy()
@@ -112,6 +111,17 @@ def test_from_service_account_file_args(self):
112111
assert credentials._subject == subject
113112
assert credentials._additional_claims == additional_claims
114113

114+
def test_to_jwt_credentials(self):
115+
jwt_from_svc = self.credentials.to_jwt_credentials()
116+
jwt_from_info = jwt.Credentials.from_service_account_info(
117+
SERVICE_ACCOUNT_INFO)
118+
119+
assert isinstance(jwt_from_svc, jwt.Credentials)
120+
assert jwt_from_svc._signer.key_id == jwt_from_info._signer.key_id
121+
assert jwt_from_svc._issuer == jwt_from_info._issuer
122+
assert jwt_from_svc._subject == jwt_from_info._subject
123+
assert jwt_from_svc._audience == jwt_from_info._audience
124+
115125
def test_default_state(self):
116126
assert not self.credentials.valid
117127
# Expiration hasn't been set yet

0 commit comments

Comments
 (0)