_service_account_async.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. """Service Accounts: JSON Web Token (JWT) Profile for OAuth 2.0
  15. NOTE: This file adds asynchronous refresh methods to both credentials
  16. classes, and therefore async/await syntax is required when calling this
  17. method when using service account credentials with asynchronous functionality.
  18. Otherwise, all other methods are inherited from the regular service account
  19. credentials file google.oauth2.service_account
  20. """
  21. from google.auth import _credentials_async as credentials_async
  22. from google.auth import _helpers
  23. from google.oauth2 import _client_async
  24. from google.oauth2 import service_account
  25. class Credentials(
  26. service_account.Credentials, credentials_async.Scoped, credentials_async.Credentials
  27. ):
  28. """Service account credentials
  29. Usually, you'll create these credentials with one of the helper
  30. constructors. To create credentials using a Google service account
  31. private key JSON file::
  32. credentials = _service_account_async.Credentials.from_service_account_file(
  33. 'service-account.json')
  34. Or if you already have the service account file loaded::
  35. service_account_info = json.load(open('service_account.json'))
  36. credentials = _service_account_async.Credentials.from_service_account_info(
  37. service_account_info)
  38. Both helper methods pass on arguments to the constructor, so you can
  39. specify additional scopes and a subject if necessary::
  40. credentials = _service_account_async.Credentials.from_service_account_file(
  41. 'service-account.json',
  42. scopes=['email'],
  43. subject='user@example.com')
  44. The credentials are considered immutable. If you want to modify the scopes
  45. or the subject used for delegation, use :meth:`with_scopes` or
  46. :meth:`with_subject`::
  47. scoped_credentials = credentials.with_scopes(['email'])
  48. delegated_credentials = credentials.with_subject(subject)
  49. To add a quota project, use :meth:`with_quota_project`::
  50. credentials = credentials.with_quota_project('myproject-123')
  51. """
  52. @_helpers.copy_docstring(credentials_async.Credentials)
  53. async def refresh(self, request):
  54. assertion = self._make_authorization_grant_assertion()
  55. access_token, expiry, _ = await _client_async.jwt_grant(
  56. request, self._token_uri, assertion
  57. )
  58. self.token = access_token
  59. self.expiry = expiry
  60. class IDTokenCredentials(
  61. service_account.IDTokenCredentials,
  62. credentials_async.Signing,
  63. credentials_async.Credentials,
  64. ):
  65. """Open ID Connect ID Token-based service account credentials.
  66. These credentials are largely similar to :class:`.Credentials`, but instead
  67. of using an OAuth 2.0 Access Token as the bearer token, they use an Open
  68. ID Connect ID Token as the bearer token. These credentials are useful when
  69. communicating to services that require ID Tokens and can not accept access
  70. tokens.
  71. Usually, you'll create these credentials with one of the helper
  72. constructors. To create credentials using a Google service account
  73. private key JSON file::
  74. credentials = (
  75. _service_account_async.IDTokenCredentials.from_service_account_file(
  76. 'service-account.json'))
  77. Or if you already have the service account file loaded::
  78. service_account_info = json.load(open('service_account.json'))
  79. credentials = (
  80. _service_account_async.IDTokenCredentials.from_service_account_info(
  81. service_account_info))
  82. Both helper methods pass on arguments to the constructor, so you can
  83. specify additional scopes and a subject if necessary::
  84. credentials = (
  85. _service_account_async.IDTokenCredentials.from_service_account_file(
  86. 'service-account.json',
  87. scopes=['email'],
  88. subject='user@example.com'))
  89. The credentials are considered immutable. If you want to modify the scopes
  90. or the subject used for delegation, use :meth:`with_scopes` or
  91. :meth:`with_subject`::
  92. scoped_credentials = credentials.with_scopes(['email'])
  93. delegated_credentials = credentials.with_subject(subject)
  94. """
  95. @_helpers.copy_docstring(credentials_async.Credentials)
  96. async def refresh(self, request):
  97. assertion = self._make_authorization_grant_assertion()
  98. access_token, expiry, _ = await _client_async.id_token_jwt_grant(
  99. request, self._token_uri, assertion
  100. )
  101. self.token = access_token
  102. self.expiry = expiry