| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0d38ef8 commit 8ade867
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -32,7 +32,6 @@ | |||
| 32 | 32 | """ | |
| 33 | 33 | ||
| 34 | 34 | from datetime import datetime | |
| 35 | - import http.client as http_client | ||
| 36 | 35 | import io | |
| 37 | 36 | import json | |
| 38 | 37 | import logging | |
@@ -351,33 +350,6 @@ def with_universe_domain(self, universe_domain): | |||
| 351 | 350 | def _metric_header_for_usage(self): | |
| 352 | 351 | return metrics.CRED_TYPE_USER | |
| 353 | 352 | ||
| 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 | - | ||
| 381 | 353 | @_helpers.copy_docstring(credentials.Credentials) | |
| 382 | 354 | def refresh(self, request): | |
| 383 | 355 | if self._universe_domain != credentials.DEFAULT_UNIVERSE_DOMAIN: | |
@@ -414,7 +386,6 @@ def refresh(self, request): | |||
| 414 | 386 | ) | |
| 415 | 387 | self.token = token | |
| 416 | 388 | self.expiry = expiry | |
| 417 | - self._set_account_from_access_token(request) | ||
| 418 | 389 | return | |
| 419 | 390 | ||
| 420 | 391 | if ( | |
@@ -451,7 +422,6 @@ def refresh(self, request): | |||
| 451 | 422 | self._refresh_token = refresh_token | |
| 452 | 423 | self._id_token = grant_response.get("id_token") | |
| 453 | 424 | self._rapt_token = rapt_token | |
| 454 | - self._set_account_from_access_token(request) | ||
| 455 | 425 | ||
| 456 | 426 | if scopes and "scope" in grant_response: | |
| 457 | 427 | requested_scopes = frozenset(scopes) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -71,48 +71,6 @@ def test_default_state(self): | |||
| 71 | 71 | assert credentials.rapt_token == self.RAPT_TOKEN | |
| 72 | 72 | assert credentials.refresh_handler is None | |
| 73 | 73 | ||
| 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 | - | ||
| 116 | 74 | def test_get_cred_info(self): | |
| 117 | 75 | credentials = self.make_credentials() | |
| 118 | 76 | credentials._account = "fake-account" | |
@@ -205,15 +163,12 @@ def test_refresh_with_non_default_universe_domain(self): | |||
| 205 | 163 | "refresh is only supported in the default googleapis.com universe domain" | |
| 206 | 164 | ) | |
| 207 | 165 | ||
| 208 | - @mock.patch.object( | ||
| 209 | - credentials.Credentials, "_set_account_from_access_token", autospec=True | ||
| 210 | - ) | ||
| 211 | 166 | @mock.patch("google.oauth2.reauth.refresh_grant", autospec=True) | |
| 212 | 167 | @mock.patch( | |
| 213 | 168 | "google.auth._helpers.utcnow", | |
| 214 | 169 | return_value=datetime.datetime.min + _helpers.REFRESH_THRESHOLD, | |
| 215 | 170 | ) | |
| 216 | - def test_refresh_success(self, unused_utcnow, refresh_grant, set_account): | ||
| 171 | + def test_refresh_success(self, unused_utcnow, refresh_grant): | ||
| 217 | 172 | token = "token" | |
| 218 | 173 | new_rapt_token = "new_rapt_token" | |
| 219 | 174 | expiry = _helpers.utcnow() + datetime.timedelta(seconds=500) | |
@@ -259,8 +214,6 @@ def test_refresh_success(self, unused_utcnow, refresh_grant, set_account): | |||
| 259 | 214 | # expired) | |
| 260 | 215 | assert credentials.valid | |
| 261 | 216 | ||
| 262 | - set_account.assert_called_once() | ||
| 263 | - | ||
| 264 | 217 | def test_refresh_no_refresh_token(self): | |
| 265 | 218 | request = mock.create_autospec(transport.Request) | |
| 266 | 219 | credentials_ = credentials.Credentials(token=None, refresh_token=None) | |
@@ -270,16 +223,13 @@ def test_refresh_no_refresh_token(self): | |||
| 270 | 223 | ||
| 271 | 224 | request.assert_not_called() | |
| 272 | 225 | ||
| 273 | - @mock.patch.object( | ||
| 274 | - credentials.Credentials, "_set_account_from_access_token", autospec=True | ||
| 275 | - ) | ||
| 276 | 226 | @mock.patch("google.oauth2.reauth.refresh_grant", autospec=True) | |
| 277 | 227 | @mock.patch( | |
| 278 | 228 | "google.auth._helpers.utcnow", | |
| 279 | 229 | return_value=datetime.datetime.min + _helpers.REFRESH_THRESHOLD, | |
| 280 | 230 | ) | |
| 281 | 231 | def test_refresh_with_refresh_token_and_refresh_handler( | |
| 282 | - self, unused_utcnow, refresh_grant, set_account | ||
| 232 | + self, unused_utcnow, refresh_grant | ||
| 283 | 233 | ): | |
| 284 | 234 | token = "token" | |
| 285 | 235 | new_rapt_token = "new_rapt_token" | |
@@ -339,15 +289,8 @@ def test_refresh_with_refresh_token_and_refresh_handler( | |||
| 339 | 289 | # higher priority. | |
| 340 | 290 | refresh_handler.assert_not_called() | |
| 341 | 291 | ||
| 342 | - set_account.assert_called_once() | ||
| 343 | - | ||
| 344 | - @mock.patch.object( | ||
| 345 | - credentials.Credentials, "_set_account_from_access_token", autospec=True | ||
| 346 | - ) | ||
| 347 | 292 | @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): | ||
| 351 | 294 | expected_expiry = datetime.datetime.min + datetime.timedelta(seconds=2800) | |
| 352 | 295 | refresh_handler = mock.Mock(return_value=("ACCESS_TOKEN", expected_expiry)) | |
| 353 | 296 | scopes = ["email", "profile"] | |
@@ -371,17 +314,11 @@ def test_refresh_with_refresh_handler_success_scopes( | |||
| 371 | 314 | assert creds.expiry == expected_expiry | |
| 372 | 315 | assert creds.valid | |
| 373 | 316 | assert not creds.expired | |
| 374 | - set_account.assert_called_once() | ||
| 375 | 317 | # Confirm refresh handler called with the expected arguments. | |
| 376 | 318 | refresh_handler.assert_called_with(request, scopes=scopes) | |
| 377 | 319 | ||
| 378 | - @mock.patch.object( | ||
| 379 | - credentials.Credentials, "_set_account_from_access_token", autospec=True | ||
| 380 | - ) | ||
| 381 | 320 | @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): | ||
| 385 | 322 | expected_expiry = datetime.datetime.min + datetime.timedelta(seconds=2800) | |
| 386 | 323 | original_refresh_handler = mock.Mock( | |
| 387 | 324 | return_value=("UNUSED_TOKEN", expected_expiry) | |
@@ -409,7 +346,6 @@ def test_refresh_with_refresh_handler_success_default_scopes( | |||
| 409 | 346 | assert creds.expiry == expected_expiry | |
| 410 | 347 | assert creds.valid | |
| 411 | 348 | assert not creds.expired | |
| 412 | - set_account.assert_called_once() | ||
| 413 | 349 | # default_scopes should be used since no developer provided scopes | |
| 414 | 350 | # are provided. | |
| 415 | 351 | refresh_handler.assert_called_with(request, scopes=default_scopes) | |
@@ -503,16 +439,13 @@ def test_refresh_with_refresh_handler_expired_token(self, unused_utcnow): | |||
| 503 | 439 | # Confirm refresh handler called with the expected arguments. | |
| 504 | 440 | refresh_handler.assert_called_with(request, scopes=scopes) | |
| 505 | 441 | ||
| 506 | - @mock.patch.object( | ||
| 507 | - credentials.Credentials, "_set_account_from_access_token", autospec=True | ||
| 508 | - ) | ||
| 509 | 442 | @mock.patch("google.oauth2.reauth.refresh_grant", autospec=True) | |
| 510 | 443 | @mock.patch( | |
| 511 | 444 | "google.auth._helpers.utcnow", | |
| 512 | 445 | return_value=datetime.datetime.min + _helpers.REFRESH_THRESHOLD, | |
| 513 | 446 | ) | |
| 514 | 447 | def test_credentials_with_scopes_requested_refresh_success( | |
| 515 | - self, unused_utcnow, refresh_grant, set_account | ||
| 448 | + self, unused_utcnow, refresh_grant | ||
| 516 | 449 | ): | |
| 517 | 450 | scopes = ["email", "profile"] | |
| 518 | 451 | default_scopes = ["https://www.googleapis.com/auth/cloud-platform"] | |
@@ -568,22 +501,18 @@ def test_credentials_with_scopes_requested_refresh_success( | |||
| 568 | 501 | assert creds.has_scopes(scopes) | |
| 569 | 502 | assert creds.rapt_token == new_rapt_token | |
| 570 | 503 | assert creds.granted_scopes == scopes | |
| 571 | - set_account.assert_called_once() | ||
| 572 | 504 | ||
| 573 | 505 | # Check that the credentials are valid (have a token and are not | |
| 574 | 506 | # expired.) | |
| 575 | 507 | assert creds.valid | |
| 576 | 508 | ||
| 577 | - @mock.patch.object( | ||
| 578 | - credentials.Credentials, "_set_account_from_access_token", autospec=True | ||
| 579 | - ) | ||
| 580 | 509 | @mock.patch("google.oauth2.reauth.refresh_grant", autospec=True) | |
| 581 | 510 | @mock.patch( | |
| 582 | 511 | "google.auth._helpers.utcnow", | |
| 583 | 512 | return_value=datetime.datetime.min + _helpers.REFRESH_THRESHOLD, | |
| 584 | 513 | ) | |
| 585 | 514 | def test_credentials_with_only_default_scopes_requested( | |
| 586 | - self, unused_utcnow, refresh_grant, set_account | ||
| 515 | + self, unused_utcnow, refresh_grant | ||
| 587 | 516 | ): | |
| 588 | 517 | default_scopes = ["email", "profile"] | |
| 589 | 518 | token = "token" | |
@@ -637,22 +566,18 @@ def test_credentials_with_only_default_scopes_requested( | |||
| 637 | 566 | assert creds.has_scopes(default_scopes) | |
| 638 | 567 | assert creds.rapt_token == new_rapt_token | |
| 639 | 568 | assert creds.granted_scopes == default_scopes | |
| 640 | - set_account.assert_called_once() | ||
| 641 | 569 | ||
| 642 | 570 | # Check that the credentials are valid (have a token and are not | |
| 643 | 571 | # expired.) | |
| 644 | 572 | assert creds.valid | |
| 645 | 573 | ||
| 646 | - @mock.patch.object( | ||
| 647 | - credentials.Credentials, "_set_account_from_access_token", autospec=True | ||
| 648 | - ) | ||
| 649 | 574 | @mock.patch("google.oauth2.reauth.refresh_grant", autospec=True) | |
| 650 | 575 | @mock.patch( | |
| 651 | 576 | "google.auth._helpers.utcnow", | |
| 652 | 577 | return_value=datetime.datetime.min + _helpers.REFRESH_THRESHOLD, | |
| 653 | 578 | ) | |
| 654 | 579 | def test_credentials_with_scopes_returned_refresh_success( | |
| 655 | - self, unused_utcnow, refresh_grant, set_account | ||
| 580 | + self, unused_utcnow, refresh_grant | ||
| 656 | 581 | ): | |
| 657 | 582 | scopes = ["email", "profile"] | |
| 658 | 583 | token = "token" | |
@@ -706,22 +631,18 @@ def test_credentials_with_scopes_returned_refresh_success( | |||
| 706 | 631 | assert creds.has_scopes(scopes) | |
| 707 | 632 | assert creds.rapt_token == new_rapt_token | |
| 708 | 633 | assert creds.granted_scopes == scopes | |
| 709 | - set_account.assert_called_once() | ||
| 710 | 634 | ||
| 711 | 635 | # Check that the credentials are valid (have a token and are not | |
| 712 | 636 | # expired.) | |
| 713 | 637 | assert creds.valid | |
| 714 | 638 | ||
| 715 | - @mock.patch.object( | ||
| 716 | - credentials.Credentials, "_set_account_from_access_token", autospec=True | ||
| 717 | - ) | ||
| 718 | 639 | @mock.patch("google.oauth2.reauth.refresh_grant", autospec=True) | |
| 719 | 640 | @mock.patch( | |
| 720 | 641 | "google.auth._helpers.utcnow", | |
| 721 | 642 | return_value=datetime.datetime.min + _helpers.REFRESH_THRESHOLD, | |
| 722 | 643 | ) | |
| 723 | 644 | 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 | ||
| 725 | 646 | ): | |
| 726 | 647 | default_scopes = ["email", "profile"] | |
| 727 | 648 | token = "token" | |
@@ -775,22 +696,18 @@ def test_credentials_with_only_default_scopes_requested_different_granted_scopes | |||
| 775 | 696 | assert creds.has_scopes(default_scopes) | |
| 776 | 697 | assert creds.rapt_token == new_rapt_token | |
| 777 | 698 | assert creds.granted_scopes == ["email"] | |
| 778 | - set_account.assert_called_once() | ||
| 779 | 699 | ||
| 780 | 700 | # Check that the credentials are valid (have a token and are not | |
| 781 | 701 | # expired.) | |
| 782 | 702 | assert creds.valid | |
| 783 | 703 | ||
| 784 | - @mock.patch.object( | ||
| 785 | - credentials.Credentials, "_set_account_from_access_token", autospec=True | ||
| 786 | - ) | ||
| 787 | 704 | @mock.patch("google.oauth2.reauth.refresh_grant", autospec=True) | |
| 788 | 705 | @mock.patch( | |
| 789 | 706 | "google.auth._helpers.utcnow", | |
| 790 | 707 | return_value=datetime.datetime.min + _helpers.REFRESH_THRESHOLD, | |
| 791 | 708 | ) | |
| 792 | 709 | def test_credentials_with_scopes_refresh_different_granted_scopes( | |
| 793 | - self, unused_utcnow, refresh_grant, set_account | ||
| 710 | + self, unused_utcnow, refresh_grant | ||
| 794 | 711 | ): | |
| 795 | 712 | scopes = ["email", "profile"] | |
| 796 | 713 | scopes_returned = ["email"] | |
@@ -848,7 +765,6 @@ def test_credentials_with_scopes_refresh_different_granted_scopes( | |||
| 848 | 765 | assert creds.has_scopes(scopes) | |
| 849 | 766 | assert creds.rapt_token == new_rapt_token | |
| 850 | 767 | assert creds.granted_scopes == scopes_returned | |
| 851 | - set_account.assert_called_once() | ||
| 852 | 768 | ||
| 853 | 769 | # Check that the credentials are valid (have a token and are not | |
| 854 | 770 | # expired.) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments