Skip to content

Commit 0e26409

Browse files
authored
feat: service account is able to use a private token endpoint (#784)
In [Private Service Connect](https://cloud.google.com/vpc/docs/private-service-connect), users can use an endpoint which is private to their VPC network. The request is eventually routed to the oauth2.googleapis.com/token so the "aud" in the assertion still should be oauth2.googleapis.com/token. After this change, service account can send requests to the private endpoint (if configured) and still use the oauth2.googleapis.com/token in the assertion.
1 parent 2f5c3a6 commit 0e26409

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

google/oauth2/service_account.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
from google.oauth2 import _client
8181

8282
_DEFAULT_TOKEN_LIFETIME_SECS = 3600 # 1 hour in seconds
83+
_GOOGLE_OAUTH2_TOKEN_ENDPOINT = "https://oauth2.googleapis.com/token"
8384

8485

8586
class Credentials(
@@ -382,7 +383,7 @@ def _make_authorization_grant_assertion(self):
382383
# The issuer must be the service account email.
383384
"iss": self._service_account_email,
384385
# The audience must be the auth token endpoint's URI
385-
"aud": self._token_uri,
386+
"aud": _GOOGLE_OAUTH2_TOKEN_ENDPOINT,
386387
"scope": _helpers.scopes_to_string(self._scopes or ()),
387388
}
388389

@@ -643,7 +644,7 @@ def _make_authorization_grant_assertion(self):
643644
# The issuer must be the service account email.
644645
"iss": self.service_account_email,
645646
# The audience must be the auth token endpoint's URI
646-
"aud": self._token_uri,
647+
"aud": _GOOGLE_OAUTH2_TOKEN_ENDPOINT,
647648
# The target audience specifies which service the ID token is
648649
# intended for.
649650
"target_audience": self._target_audience,

tests/oauth2/test_service_account.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def test__make_authorization_grant_assertion(self):
167167
token = credentials._make_authorization_grant_assertion()
168168
payload = jwt.decode(token, PUBLIC_CERT_BYTES)
169169
assert payload["iss"] == self.SERVICE_ACCOUNT_EMAIL
170-
assert payload["aud"] == self.TOKEN_URI
170+
assert payload["aud"] == service_account._GOOGLE_OAUTH2_TOKEN_ENDPOINT
171171

172172
def test__make_authorization_grant_assertion_scoped(self):
173173
credentials = self.make_credentials()
@@ -440,7 +440,7 @@ def test__make_authorization_grant_assertion(self):
440440
token = credentials._make_authorization_grant_assertion()
441441
payload = jwt.decode(token, PUBLIC_CERT_BYTES)
442442
assert payload["iss"] == self.SERVICE_ACCOUNT_EMAIL
443-
assert payload["aud"] == self.TOKEN_URI
443+
assert payload["aud"] == service_account._GOOGLE_OAUTH2_TOKEN_ENDPOINT
444444
assert payload["target_audience"] == self.TARGET_AUDIENCE
445445

446446
@mock.patch("google.oauth2._client.id_token_jwt_grant", autospec=True)

tests_async/oauth2/test_service_account_async.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ def test__make_authorization_grant_assertion(self):
152152
token = credentials._make_authorization_grant_assertion()
153153
payload = jwt.decode(token, test_service_account.PUBLIC_CERT_BYTES)
154154
assert payload["iss"] == self.SERVICE_ACCOUNT_EMAIL
155-
assert payload["aud"] == self.TOKEN_URI
155+
assert (
156+
payload["aud"]
157+
== service_account.service_account._GOOGLE_OAUTH2_TOKEN_ENDPOINT
158+
)
156159

157160
def test__make_authorization_grant_assertion_scoped(self):
158161
credentials = self.make_credentials()
@@ -311,7 +314,10 @@ def test__make_authorization_grant_assertion(self):
311314
token = credentials._make_authorization_grant_assertion()
312315
payload = jwt.decode(token, test_service_account.PUBLIC_CERT_BYTES)
313316
assert payload["iss"] == self.SERVICE_ACCOUNT_EMAIL
314-
assert payload["aud"] == self.TOKEN_URI
317+
assert (
318+
payload["aud"]
319+
== service_account.service_account._GOOGLE_OAUTH2_TOKEN_ENDPOINT
320+
)
315321
assert payload["target_audience"] == self.TARGET_AUDIENCE
316322

317323
@mock.patch("google.oauth2._client_async.id_token_jwt_grant", autospec=True)

0 commit comments

Comments
 (0)