Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit 6a3f0ec

Browse files
hiranya911Jon Wayne Parrott
authored andcommitted
Expose Project ID from service account credentials (#187)
1 parent 2f44c49 commit 6a3f0ec

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

google/oauth2/service_account.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class Credentials(credentials.Signing,
117117
"""
118118

119119
def __init__(self, signer, service_account_email, token_uri, scopes=None,
120-
subject=None, additional_claims=None):
120+
subject=None, project_id=None, additional_claims=None):
121121
"""
122122
Args:
123123
signer (google.auth.crypt.Signer): The signer used to sign JWTs.
@@ -127,6 +127,8 @@ def __init__(self, signer, service_account_email, token_uri, scopes=None,
127127
token_uri (str): The OAuth 2.0 Token URI.
128128
subject (str): For domain-wide delegation, the email address of the
129129
user to for which to request delegated access.
130+
project_id (str): Project ID associated with the service account
131+
credential.
130132
additional_claims (Mapping[str, str]): Any additional claims for
131133
the JWT assertion used in the authorization grant.
132134
@@ -141,6 +143,7 @@ def __init__(self, signer, service_account_email, token_uri, scopes=None,
141143
self._signer = signer
142144
self._service_account_email = service_account_email
143145
self._subject = subject
146+
self._project_id = project_id
144147
self._token_uri = token_uri
145148

146149
if additional_claims is not None:
@@ -167,7 +170,8 @@ def _from_signer_and_info(cls, signer, info, **kwargs):
167170
return cls(
168171
signer,
169172
service_account_email=info['client_email'],
170-
token_uri=info['token_uri'], **kwargs)
173+
token_uri=info['token_uri'],
174+
project_id=info.get('project_id'), **kwargs)
171175

172176
@classmethod
173177
def from_service_account_info(cls, info, **kwargs):
@@ -210,6 +214,11 @@ def service_account_email(self):
210214
"""The service account email."""
211215
return self._service_account_email
212216

217+
@property
218+
def project_id(self):
219+
"""Project ID associated with this credential."""
220+
return self._project_id
221+
213222
@property
214223
def requires_scopes(self):
215224
"""Checks if the credentials requires scopes.
@@ -227,6 +236,7 @@ def with_scopes(self, scopes):
227236
scopes=scopes,
228237
token_uri=self._token_uri,
229238
subject=self._subject,
239+
project_id=self._project_id,
230240
additional_claims=self._additional_claims.copy())
231241

232242
def with_subject(self, subject):
@@ -245,6 +255,7 @@ def with_subject(self, subject):
245255
scopes=self._scopes,
246256
token_uri=self._token_uri,
247257
subject=subject,
258+
project_id=self._project_id,
248259
additional_claims=self._additional_claims.copy())
249260

250261
def with_claims(self, additional_claims):
@@ -268,6 +279,7 @@ def with_claims(self, additional_claims):
268279
scopes=self._scopes,
269280
token_uri=self._token_uri,
270281
subject=self._subject,
282+
project_id=self._project_id,
271283
additional_claims=new_additional_claims)
272284

273285
def _make_authorization_grant_assertion(self):

tests/oauth2/test_service_account.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def test_from_service_account_info_args(self):
7474
additional_claims=additional_claims)
7575

7676
assert credentials.service_account_email == info['client_email']
77+
assert credentials.project_id == info['project_id']
7778
assert credentials._signer.key_id == info['private_key_id']
7879
assert credentials._token_uri == info['token_uri']
7980
assert credentials._scopes == scopes
@@ -87,6 +88,7 @@ def test_from_service_account_file(self):
8788
SERVICE_ACCOUNT_JSON_FILE)
8889

8990
assert credentials.service_account_email == info['client_email']
91+
assert credentials.project_id == info['project_id']
9092
assert credentials._signer.key_id == info['private_key_id']
9193
assert credentials._token_uri == info['token_uri']
9294

@@ -101,6 +103,7 @@ def test_from_service_account_file_args(self):
101103
scopes=scopes, additional_claims=additional_claims)
102104

103105
assert credentials.service_account_email == info['client_email']
106+
assert credentials.project_id == info['project_id']
104107
assert credentials._signer.key_id == info['private_key_id']
105108
assert credentials._token_uri == info['token_uri']
106109
assert credentials._scopes == scopes

0 commit comments

Comments
 (0)