_oauth2client.py 5.7 KB

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