_oauth2client.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # Copyright 2016 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. """Helpers for transitioning from oauth2client to google-auth.
  15. .. warning::
  16. This module is private as it is intended to assist first-party downstream
  17. clients with the transition from oauth2client to google-auth.
  18. """
  19. from __future__ import absolute_import
  20. from google.auth import _helpers
  21. import google.auth.app_engine
  22. import google.auth.compute_engine
  23. import google.oauth2.credentials
  24. import google.oauth2.service_account
  25. try:
  26. import oauth2client.client # type: ignore
  27. import oauth2client.contrib.gce # type: ignore
  28. import oauth2client.service_account # type: ignore
  29. except ImportError as caught_exc:
  30. raise ImportError("oauth2client is not installed.") from caught_exc
  31. try:
  32. import oauth2client.contrib.appengine # type: ignore
  33. _HAS_APPENGINE = True
  34. except ImportError:
  35. _HAS_APPENGINE = False
  36. _CONVERT_ERROR_TMPL = "Unable to convert {} to a google-auth credentials class."
  37. def _convert_oauth2_credentials(credentials):
  38. """Converts to :class:`google.oauth2.credentials.Credentials`.
  39. Args:
  40. credentials (Union[oauth2client.client.OAuth2Credentials,
  41. oauth2client.client.GoogleCredentials]): The credentials to
  42. convert.
  43. Returns:
  44. google.oauth2.credentials.Credentials: The converted credentials.
  45. """
  46. new_credentials = google.oauth2.credentials.Credentials(
  47. token=credentials.access_token,
  48. refresh_token=credentials.refresh_token,
  49. token_uri=credentials.token_uri,
  50. client_id=credentials.client_id,
  51. client_secret=credentials.client_secret,
  52. scopes=credentials.scopes,
  53. )
  54. new_credentials._expires = credentials.token_expiry
  55. return new_credentials
  56. def _convert_service_account_credentials(credentials):
  57. """Converts to :class:`google.oauth2.service_account.Credentials`.
  58. Args:
  59. credentials (Union[
  60. oauth2client.service_account.ServiceAccountCredentials,
  61. oauth2client.service_account._JWTAccessCredentials]): The
  62. credentials to convert.
  63. Returns:
  64. google.oauth2.service_account.Credentials: The converted credentials.
  65. """
  66. info = credentials.serialization_data.copy()
  67. info["token_uri"] = credentials.token_uri
  68. return google.oauth2.service_account.Credentials.from_service_account_info(info)
  69. def _convert_gce_app_assertion_credentials(credentials):
  70. """Converts to :class:`google.auth.compute_engine.Credentials`.
  71. Args:
  72. credentials (oauth2client.contrib.gce.AppAssertionCredentials): The
  73. credentials to convert.
  74. Returns:
  75. google.oauth2.service_account.Credentials: The converted credentials.
  76. """
  77. return google.auth.compute_engine.Credentials(
  78. service_account_email=credentials.service_account_email
  79. )
  80. def _convert_appengine_app_assertion_credentials(credentials):
  81. """Converts to :class:`google.auth.app_engine.Credentials`.
  82. Args:
  83. credentials (oauth2client.contrib.app_engine.AppAssertionCredentials):
  84. The credentials to convert.
  85. Returns:
  86. google.oauth2.service_account.Credentials: The converted credentials.
  87. """
  88. # pylint: disable=invalid-name
  89. return google.auth.app_engine.Credentials(
  90. scopes=_helpers.string_to_scopes(credentials.scope),
  91. service_account_id=credentials.service_account_id,
  92. )
  93. _CLASS_CONVERSION_MAP = {
  94. oauth2client.client.OAuth2Credentials: _convert_oauth2_credentials,
  95. oauth2client.client.GoogleCredentials: _convert_oauth2_credentials,
  96. oauth2client.service_account.ServiceAccountCredentials: _convert_service_account_credentials,
  97. oauth2client.service_account._JWTAccessCredentials: _convert_service_account_credentials,
  98. oauth2client.contrib.gce.AppAssertionCredentials: _convert_gce_app_assertion_credentials,
  99. }
  100. if _HAS_APPENGINE:
  101. _CLASS_CONVERSION_MAP[
  102. oauth2client.contrib.appengine.AppAssertionCredentials
  103. ] = _convert_appengine_app_assertion_credentials
  104. def convert(credentials):
  105. """Convert oauth2client credentials to google-auth credentials.
  106. This class converts:
  107. - :class:`oauth2client.client.OAuth2Credentials` to
  108. :class:`google.oauth2.credentials.Credentials`.
  109. - :class:`oauth2client.client.GoogleCredentials` to
  110. :class:`google.oauth2.credentials.Credentials`.
  111. - :class:`oauth2client.service_account.ServiceAccountCredentials` to
  112. :class:`google.oauth2.service_account.Credentials`.
  113. - :class:`oauth2client.service_account._JWTAccessCredentials` to
  114. :class:`google.oauth2.service_account.Credentials`.
  115. - :class:`oauth2client.contrib.gce.AppAssertionCredentials` to
  116. :class:`google.auth.compute_engine.Credentials`.
  117. - :class:`oauth2client.contrib.appengine.AppAssertionCredentials` to
  118. :class:`google.auth.app_engine.Credentials`.
  119. Returns:
  120. google.auth.credentials.Credentials: The converted credentials.
  121. Raises:
  122. ValueError: If the credentials could not be converted.
  123. """
  124. credentials_class = type(credentials)
  125. try:
  126. return _CLASS_CONVERSION_MAP[credentials_class](credentials)
  127. except KeyError as caught_exc:
  128. new_exc = ValueError(_CONVERT_ERROR_TMPL.format(credentials_class))
  129. raise new_exc from caught_exc