_credentials_async.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # Copyright 2020 Google LLC
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """OAuth 2.0 Async Credentials.
  15. This module provides credentials based on OAuth 2.0 access and refresh tokens.
  16. These credentials usually access resources on behalf of a user (resource
  17. owner).
  18. Specifically, this is intended to use access tokens acquired using the
  19. `Authorization Code grant`_ and can refresh those tokens using a
  20. optional `refresh token`_.
  21. Obtaining the initial access and refresh token is outside of the scope of this
  22. module. Consult `rfc6749 section 4.1`_ for complete details on the
  23. Authorization Code grant flow.
  24. .. _Authorization Code grant: https://tools.ietf.org/html/rfc6749#section-1.3.1
  25. .. _refresh token: https://tools.ietf.org/html/rfc6749#section-6
  26. .. _rfc6749 section 4.1: https://tools.ietf.org/html/rfc6749#section-4.1
  27. """
  28. from google.auth import _credentials_async as credentials
  29. from google.auth import _helpers
  30. from google.auth import exceptions
  31. from google.oauth2 import _reauth_async as reauth
  32. from google.oauth2 import credentials as oauth2_credentials
  33. class Credentials(oauth2_credentials.Credentials):
  34. """Credentials using OAuth 2.0 access and refresh tokens.
  35. The credentials are considered immutable. If you want to modify the
  36. quota project, use :meth:`with_quota_project` or ::
  37. credentials = credentials.with_quota_project('myproject-123)
  38. """
  39. @_helpers.copy_docstring(credentials.Credentials)
  40. async def refresh(self, request):
  41. if (
  42. self._refresh_token is None
  43. or self._token_uri is None
  44. or self._client_id is None
  45. or self._client_secret is None
  46. ):
  47. raise exceptions.RefreshError(
  48. "The credentials do not contain the necessary fields need to "
  49. "refresh the access token. You must specify refresh_token, "
  50. "token_uri, client_id, and client_secret."
  51. )
  52. (
  53. access_token,
  54. refresh_token,
  55. expiry,
  56. grant_response,
  57. rapt_token,
  58. ) = await reauth.refresh_grant(
  59. request,
  60. self._token_uri,
  61. self._refresh_token,
  62. self._client_id,
  63. self._client_secret,
  64. scopes=self._scopes,
  65. rapt_token=self._rapt_token,
  66. enable_reauth_refresh=self._enable_reauth_refresh,
  67. )
  68. self.token = access_token
  69. self.expiry = expiry
  70. self._refresh_token = refresh_token
  71. self._id_token = grant_response.get("id_token")
  72. self._rapt_token = rapt_token
  73. if self._scopes and "scope" in grant_response:
  74. requested_scopes = frozenset(self._scopes)
  75. granted_scopes = frozenset(grant_response["scope"].split())
  76. scopes_requested_but_not_granted = requested_scopes - granted_scopes
  77. if scopes_requested_but_not_granted:
  78. raise exceptions.RefreshError(
  79. "Not all requested scopes were granted by the "
  80. "authorization server, missing scopes {}.".format(
  81. ", ".join(scopes_requested_but_not_granted)
  82. )
  83. )
  84. @_helpers.copy_docstring(credentials.Credentials)
  85. async def before_request(self, request, method, url, headers):
  86. if not self.valid:
  87. await self.refresh(request)
  88. self.apply(headers)
  89. class UserAccessTokenCredentials(oauth2_credentials.UserAccessTokenCredentials):
  90. """Access token credentials for user account.
  91. Obtain the access token for a given user account or the current active
  92. user account with the ``gcloud auth print-access-token`` command.
  93. Args:
  94. account (Optional[str]): Account to get the access token for. If not
  95. specified, the current active account will be used.
  96. quota_project_id (Optional[str]): The project ID used for quota
  97. and billing.
  98. """