Skip to content

Commit afb9e5a

Browse files
fix: remove token_info call from token refresh path (#1595)
* fix: remove token_info call from token refresh path * update secret * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 2910b6b commit afb9e5a

File tree

3 files changed

+9
-123
lines changed

3 files changed

+9
-123
lines changed

google/oauth2/credentials.py

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
"""
3333

3434
from datetime import datetime
35-
import http.client as http_client
3635
import io
3736
import json
3837
import logging
@@ -351,33 +350,6 @@ def with_universe_domain(self, universe_domain):
351350
def _metric_header_for_usage(self):
352351
return metrics.CRED_TYPE_USER
353352

354-
def _set_account_from_access_token(self, request):
355-
"""Obtain the account from token info endpoint and set the account field.
356-
357-
Args:
358-
request (google.auth.transport.Request): A callable used to make
359-
HTTP requests.
360-
"""
361-
# We only set the account if it's not yet set.
362-
if self._account:
363-
return
364-
365-
if not self.token:
366-
return
367-
368-
# Make request to token info endpoint with the access token.
369-
# If the token is invalid, it returns 400 error code.
370-
# If the token is valid, it returns 200 status with a JSON. The account
371-
# is the "email" field of the JSON.
372-
token_info_url = "{}?access_token={}".format(
373-
_GOOGLE_OAUTH2_TOKEN_INFO_ENDPOINT, self.token
374-
)
375-
response = request(method="GET", url=token_info_url)
376-
377-
if response.status == http_client.OK:
378-
response_data = json.loads(response.data.decode("utf-8"))
379-
self._account = response_data.get("email")
380-
381353
@_helpers.copy_docstring(credentials.Credentials)
382354
def refresh(self, request):
383355
if self._universe_domain != credentials.DEFAULT_UNIVERSE_DOMAIN:
@@ -414,7 +386,6 @@ def refresh(self, request):
414386
)
415387
self.token = token
416388
self.expiry = expiry
417-
self._set_account_from_access_token(request)
418389
return
419390

420391
if (
@@ -451,7 +422,6 @@ def refresh(self, request):
451422
self._refresh_token = refresh_token
452423
self._id_token = grant_response.get("id_token")
453424
self._rapt_token = rapt_token
454-
self._set_account_from_access_token(request)
455425

456426
if scopes and "scope" in grant_response:
457427
requested_scopes = frozenset(scopes)

system_tests/secrets.tar.enc

0 Bytes
Binary file not shown.

tests/oauth2/test_credentials.py

Lines changed: 9 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -71,48 +71,6 @@ def test_default_state(self):
7171
assert credentials.rapt_token == self.RAPT_TOKEN
7272
assert credentials.refresh_handler is None
7373

74-
def test__set_account_from_access_token_no_token(self):
75-
credentials = self.make_credentials()
76-
assert not credentials.token
77-
assert not credentials.account
78-
79-
credentials._set_account_from_access_token(mock.Mock())
80-
assert not credentials.account
81-
82-
def test__set_account_from_access_token_account_already_set(self):
83-
credentials = self.make_credentials()
84-
credentials.token = "fake-token"
85-
credentials._account = "fake-account"
86-
87-
credentials._set_account_from_access_token(mock.Mock())
88-
assert credentials.account == "fake-account"
89-
90-
def test__set_account_from_access_token_error_response(self):
91-
credentials = self.make_credentials()
92-
credentials.token = "fake-token"
93-
assert not credentials.account
94-
95-
mock_response = mock.Mock()
96-
mock_response.status = 400
97-
mock_request = mock.Mock(return_value=mock_response)
98-
credentials._set_account_from_access_token(mock_request)
99-
assert not credentials.account
100-
101-
def test__set_account_from_access_token_success(self):
102-
credentials = self.make_credentials()
103-
credentials.token = "fake-token"
104-
assert not credentials.account
105-
106-
mock_response = mock.Mock()
107-
mock_response.status = 200
108-
mock_response.data = (
109-
b'{"aud": "aud", "sub": "sub", "scope": "scope", "email": "fake-account"}'
110-
)
111-
112-
mock_request = mock.Mock(return_value=mock_response)
113-
credentials._set_account_from_access_token(mock_request)
114-
assert credentials.account == "fake-account"
115-
11674
def test_get_cred_info(self):
11775
credentials = self.make_credentials()
11876
credentials._account = "fake-account"
@@ -205,15 +163,12 @@ def test_refresh_with_non_default_universe_domain(self):
205163
"refresh is only supported in the default googleapis.com universe domain"
206164
)
207165

208-
@mock.patch.object(
209-
credentials.Credentials, "_set_account_from_access_token", autospec=True
210-
)
211166
@mock.patch("google.oauth2.reauth.refresh_grant", autospec=True)
212167
@mock.patch(
213168
"google.auth._helpers.utcnow",
214169
return_value=datetime.datetime.min + _helpers.REFRESH_THRESHOLD,
215170
)
216-
def test_refresh_success(self, unused_utcnow, refresh_grant, set_account):
171+
def test_refresh_success(self, unused_utcnow, refresh_grant):
217172
token = "token"
218173
new_rapt_token = "new_rapt_token"
219174
expiry = _helpers.utcnow() + datetime.timedelta(seconds=500)
@@ -259,8 +214,6 @@ def test_refresh_success(self, unused_utcnow, refresh_grant, set_account):
259214
# expired)
260215
assert credentials.valid
261216

262-
set_account.assert_called_once()
263-
264217
def test_refresh_no_refresh_token(self):
265218
request = mock.create_autospec(transport.Request)
266219
credentials_ = credentials.Credentials(token=None, refresh_token=None)
@@ -270,16 +223,13 @@ def test_refresh_no_refresh_token(self):
270223

271224
request.assert_not_called()
272225

273-
@mock.patch.object(
274-
credentials.Credentials, "_set_account_from_access_token", autospec=True
275-
)
276226
@mock.patch("google.oauth2.reauth.refresh_grant", autospec=True)
277227
@mock.patch(
278228
"google.auth._helpers.utcnow",
279229
return_value=datetime.datetime.min + _helpers.REFRESH_THRESHOLD,
280230
)
281231
def test_refresh_with_refresh_token_and_refresh_handler(
282-
self, unused_utcnow, refresh_grant, set_account
232+
self, unused_utcnow, refresh_grant
283233
):
284234
token = "token"
285235
new_rapt_token = "new_rapt_token"
@@ -339,15 +289,8 @@ def test_refresh_with_refresh_token_and_refresh_handler(
339289
# higher priority.
340290
refresh_handler.assert_not_called()
341291

342-
set_account.assert_called_once()
343-
344-
@mock.patch.object(
345-
credentials.Credentials, "_set_account_from_access_token", autospec=True
346-
)
347292
@mock.patch("google.auth._helpers.utcnow", return_value=datetime.datetime.min)
348-
def test_refresh_with_refresh_handler_success_scopes(
349-
self, unused_utcnow, set_account
350-
):
293+
def test_refresh_with_refresh_handler_success_scopes(self, unused_utcnow):
351294
expected_expiry = datetime.datetime.min + datetime.timedelta(seconds=2800)
352295
refresh_handler = mock.Mock(return_value=("ACCESS_TOKEN", expected_expiry))
353296
scopes = ["email", "profile"]
@@ -371,17 +314,11 @@ def test_refresh_with_refresh_handler_success_scopes(
371314
assert creds.expiry == expected_expiry
372315
assert creds.valid
373316
assert not creds.expired
374-
set_account.assert_called_once()
375317
# Confirm refresh handler called with the expected arguments.
376318
refresh_handler.assert_called_with(request, scopes=scopes)
377319

378-
@mock.patch.object(
379-
credentials.Credentials, "_set_account_from_access_token", autospec=True
380-
)
381320
@mock.patch("google.auth._helpers.utcnow", return_value=datetime.datetime.min)
382-
def test_refresh_with_refresh_handler_success_default_scopes(
383-
self, unused_utcnow, set_account
384-
):
321+
def test_refresh_with_refresh_handler_success_default_scopes(self, unused_utcnow):
385322
expected_expiry = datetime.datetime.min + datetime.timedelta(seconds=2800)
386323
original_refresh_handler = mock.Mock(
387324
return_value=("UNUSED_TOKEN", expected_expiry)
@@ -409,7 +346,6 @@ def test_refresh_with_refresh_handler_success_default_scopes(
409346
assert creds.expiry == expected_expiry
410347
assert creds.valid
411348
assert not creds.expired
412-
set_account.assert_called_once()
413349
# default_scopes should be used since no developer provided scopes
414350
# are provided.
415351
refresh_handler.assert_called_with(request, scopes=default_scopes)
@@ -503,16 +439,13 @@ def test_refresh_with_refresh_handler_expired_token(self, unused_utcnow):
503439
# Confirm refresh handler called with the expected arguments.
504440
refresh_handler.assert_called_with(request, scopes=scopes)
505441

506-
@mock.patch.object(
507-
credentials.Credentials, "_set_account_from_access_token", autospec=True
508-
)
509442
@mock.patch("google.oauth2.reauth.refresh_grant", autospec=True)
510443
@mock.patch(
511444
"google.auth._helpers.utcnow",
512445
return_value=datetime.datetime.min + _helpers.REFRESH_THRESHOLD,
513446
)
514447
def test_credentials_with_scopes_requested_refresh_success(
515-
self, unused_utcnow, refresh_grant, set_account
448+
self, unused_utcnow, refresh_grant
516449
):
517450
scopes = ["email", "profile"]
518451
default_scopes = ["https://www.googleapis.com/auth/cloud-platform"]
@@ -568,22 +501,18 @@ def test_credentials_with_scopes_requested_refresh_success(
568501
assert creds.has_scopes(scopes)
569502
assert creds.rapt_token == new_rapt_token
570503
assert creds.granted_scopes == scopes
571-
set_account.assert_called_once()
572504

573505
# Check that the credentials are valid (have a token and are not
574506
# expired.)
575507
assert creds.valid
576508

577-
@mock.patch.object(
578-
credentials.Credentials, "_set_account_from_access_token", autospec=True
579-
)
580509
@mock.patch("google.oauth2.reauth.refresh_grant", autospec=True)
581510
@mock.patch(
582511
"google.auth._helpers.utcnow",
583512
return_value=datetime.datetime.min + _helpers.REFRESH_THRESHOLD,
584513
)
585514
def test_credentials_with_only_default_scopes_requested(
586-
self, unused_utcnow, refresh_grant, set_account
515+
self, unused_utcnow, refresh_grant
587516
):
588517
default_scopes = ["email", "profile"]
589518
token = "token"
@@ -637,22 +566,18 @@ def test_credentials_with_only_default_scopes_requested(
637566
assert creds.has_scopes(default_scopes)
638567
assert creds.rapt_token == new_rapt_token
639568
assert creds.granted_scopes == default_scopes
640-
set_account.assert_called_once()
641569

642570
# Check that the credentials are valid (have a token and are not
643571
# expired.)
644572
assert creds.valid
645573

646-
@mock.patch.object(
647-
credentials.Credentials, "_set_account_from_access_token", autospec=True
648-
)
649574
@mock.patch("google.oauth2.reauth.refresh_grant", autospec=True)
650575
@mock.patch(
651576
"google.auth._helpers.utcnow",
652577
return_value=datetime.datetime.min + _helpers.REFRESH_THRESHOLD,
653578
)
654579
def test_credentials_with_scopes_returned_refresh_success(
655-
self, unused_utcnow, refresh_grant, set_account
580+
self, unused_utcnow, refresh_grant
656581
):
657582
scopes = ["email", "profile"]
658583
token = "token"
@@ -706,22 +631,18 @@ def test_credentials_with_scopes_returned_refresh_success(
706631
assert creds.has_scopes(scopes)
707632
assert creds.rapt_token == new_rapt_token
708633
assert creds.granted_scopes == scopes
709-
set_account.assert_called_once()
710634

711635
# Check that the credentials are valid (have a token and are not
712636
# expired.)
713637
assert creds.valid
714638

715-
@mock.patch.object(
716-
credentials.Credentials, "_set_account_from_access_token", autospec=True
717-
)
718639
@mock.patch("google.oauth2.reauth.refresh_grant", autospec=True)
719640
@mock.patch(
720641
"google.auth._helpers.utcnow",
721642
return_value=datetime.datetime.min + _helpers.REFRESH_THRESHOLD,
722643
)
723644
def test_credentials_with_only_default_scopes_requested_different_granted_scopes(
724-
self, unused_utcnow, refresh_grant, set_account
645+
self, unused_utcnow, refresh_grant
725646
):
726647
default_scopes = ["email", "profile"]
727648
token = "token"
@@ -775,22 +696,18 @@ def test_credentials_with_only_default_scopes_requested_different_granted_scopes
775696
assert creds.has_scopes(default_scopes)
776697
assert creds.rapt_token == new_rapt_token
777698
assert creds.granted_scopes == ["email"]
778-
set_account.assert_called_once()
779699

780700
# Check that the credentials are valid (have a token and are not
781701
# expired.)
782702
assert creds.valid
783703

784-
@mock.patch.object(
785-
credentials.Credentials, "_set_account_from_access_token", autospec=True
786-
)
787704
@mock.patch("google.oauth2.reauth.refresh_grant", autospec=True)
788705
@mock.patch(
789706
"google.auth._helpers.utcnow",
790707
return_value=datetime.datetime.min + _helpers.REFRESH_THRESHOLD,
791708
)
792709
def test_credentials_with_scopes_refresh_different_granted_scopes(
793-
self, unused_utcnow, refresh_grant, set_account
710+
self, unused_utcnow, refresh_grant
794711
):
795712
scopes = ["email", "profile"]
796713
scopes_returned = ["email"]
@@ -848,7 +765,6 @@ def test_credentials_with_scopes_refresh_different_granted_scopes(
848765
assert creds.has_scopes(scopes)
849766
assert creds.rapt_token == new_rapt_token
850767
assert creds.granted_scopes == scopes_returned
851-
set_account.assert_called_once()
852768

853769
# Check that the credentials are valid (have a token and are not
854770
# expired.)

0 commit comments

Comments
 (0)