blob: 3512e1d116f17677ee3e8fa59ecf30f21e259158 [file] [log] [blame]
C.J. Collier37141e42020-02-13 13:49:49 -08001# Copyright 2016 Google LLC
Jon Wayne Parrotta896d2a2016-11-02 23:42:51 -07002#
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
15"""Helpers for transitioning from oauth2client to google-auth.
16
17.. warning::
18 This module is private as it is intended to assist first-party downstream
19 clients with the transition from oauth2client to google-auth.
20"""
21
22from __future__ import absolute_import
23
24from google.auth import _helpers
25import google.auth.app_engine
Teddy Sudola10b15e2018-10-05 10:20:33 -070026import google.auth.compute_engine
Jon Wayne Parrotta896d2a2016-11-02 23:42:51 -070027import google.oauth2.credentials
28import google.oauth2.service_account
29
30try:
31 import oauth2client.client
32 import oauth2client.contrib.gce
33 import oauth2client.service_account
Danny Hermes895e3692017-11-09 11:35:57 -080034except ImportError as caught_exc:
Tres Seaver560cf1e2021-08-03 16:35:54 -040035 raise ImportError("oauth2client is not installed.") from caught_exc
Jon Wayne Parrotta896d2a2016-11-02 23:42:51 -070036
37try:
Teddy Sudola10b15e2018-10-05 10:20:33 -070038 import oauth2client.contrib.appengine # pytype: disable=import-error
Bu Sun Kim9eec0912019-10-21 17:04:21 -070039
Jon Wayne Parrotta896d2a2016-11-02 23:42:51 -070040 _HAS_APPENGINE = True
41except ImportError:
42 _HAS_APPENGINE = False
43
44
Bu Sun Kim9eec0912019-10-21 17:04:21 -070045_CONVERT_ERROR_TMPL = "Unable to convert {} to a google-auth credentials class."
Jon Wayne Parrotta896d2a2016-11-02 23:42:51 -070046
47
48def _convert_oauth2_credentials(credentials):
49 """Converts to :class:`google.oauth2.credentials.Credentials`.
50
51 Args:
52 credentials (Union[oauth2client.client.OAuth2Credentials,
53 oauth2client.client.GoogleCredentials]): The credentials to
54 convert.
55
56 Returns:
57 google.oauth2.credentials.Credentials: The converted credentials.
58 """
59 new_credentials = google.oauth2.credentials.Credentials(
60 token=credentials.access_token,
61 refresh_token=credentials.refresh_token,
62 token_uri=credentials.token_uri,
63 client_id=credentials.client_id,
64 client_secret=credentials.client_secret,
Bu Sun Kim9eec0912019-10-21 17:04:21 -070065 scopes=credentials.scopes,
66 )
Jon Wayne Parrotta896d2a2016-11-02 23:42:51 -070067
68 new_credentials._expires = credentials.token_expiry
69
70 return new_credentials
71
72
73def _convert_service_account_credentials(credentials):
74 """Converts to :class:`google.oauth2.service_account.Credentials`.
75
76 Args:
77 credentials (Union[
78 oauth2client.service_account.ServiceAccountCredentials,
79 oauth2client.service_account._JWTAccessCredentials]): The
80 credentials to convert.
81
82 Returns:
83 google.oauth2.service_account.Credentials: The converted credentials.
84 """
85 info = credentials.serialization_data.copy()
Bu Sun Kim9eec0912019-10-21 17:04:21 -070086 info["token_uri"] = credentials.token_uri
87 return google.oauth2.service_account.Credentials.from_service_account_info(info)
Jon Wayne Parrotta896d2a2016-11-02 23:42:51 -070088
89
90def _convert_gce_app_assertion_credentials(credentials):
91 """Converts to :class:`google.auth.compute_engine.Credentials`.
92
93 Args:
94 credentials (oauth2client.contrib.gce.AppAssertionCredentials): The
95 credentials to convert.
96
97 Returns:
98 google.oauth2.service_account.Credentials: The converted credentials.
99 """
100 return google.auth.compute_engine.Credentials(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700101 service_account_email=credentials.service_account_email
102 )
Jon Wayne Parrotta896d2a2016-11-02 23:42:51 -0700103
104
105def _convert_appengine_app_assertion_credentials(credentials):
106 """Converts to :class:`google.auth.app_engine.Credentials`.
107
108 Args:
109 credentials (oauth2client.contrib.app_engine.AppAssertionCredentials):
110 The credentials to convert.
111
112 Returns:
113 google.oauth2.service_account.Credentials: The converted credentials.
114 """
115 # pylint: disable=invalid-name
116 return google.auth.app_engine.Credentials(
117 scopes=_helpers.string_to_scopes(credentials.scope),
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700118 service_account_id=credentials.service_account_id,
119 )
Jon Wayne Parrotta896d2a2016-11-02 23:42:51 -0700120
121
122_CLASS_CONVERSION_MAP = {
123 oauth2client.client.OAuth2Credentials: _convert_oauth2_credentials,
124 oauth2client.client.GoogleCredentials: _convert_oauth2_credentials,
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700125 oauth2client.service_account.ServiceAccountCredentials: _convert_service_account_credentials,
126 oauth2client.service_account._JWTAccessCredentials: _convert_service_account_credentials,
127 oauth2client.contrib.gce.AppAssertionCredentials: _convert_gce_app_assertion_credentials,
Jon Wayne Parrotta896d2a2016-11-02 23:42:51 -0700128}
129
130if _HAS_APPENGINE:
131 _CLASS_CONVERSION_MAP[
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700132 oauth2client.contrib.appengine.AppAssertionCredentials
133 ] = _convert_appengine_app_assertion_credentials
Jon Wayne Parrotta896d2a2016-11-02 23:42:51 -0700134
135
136def convert(credentials):
137 """Convert oauth2client credentials to google-auth credentials.
138
139 This class converts:
140
141 - :class:`oauth2client.client.OAuth2Credentials` to
142 :class:`google.oauth2.credentials.Credentials`.
143 - :class:`oauth2client.client.GoogleCredentials` to
144 :class:`google.oauth2.credentials.Credentials`.
145 - :class:`oauth2client.service_account.ServiceAccountCredentials` to
146 :class:`google.oauth2.service_account.Credentials`.
147 - :class:`oauth2client.service_account._JWTAccessCredentials` to
148 :class:`google.oauth2.service_account.Credentials`.
149 - :class:`oauth2client.contrib.gce.AppAssertionCredentials` to
150 :class:`google.auth.compute_engine.Credentials`.
151 - :class:`oauth2client.contrib.appengine.AppAssertionCredentials` to
152 :class:`google.auth.app_engine.Credentials`.
153
154 Returns:
155 google.auth.credentials.Credentials: The converted credentials.
156
157 Raises:
158 ValueError: If the credentials could not be converted.
159 """
160
161 credentials_class = type(credentials)
162
163 try:
164 return _CLASS_CONVERSION_MAP[credentials_class](credentials)
Danny Hermes895e3692017-11-09 11:35:57 -0800165 except KeyError as caught_exc:
166 new_exc = ValueError(_CONVERT_ERROR_TMPL.format(credentials_class))
Tres Seaver560cf1e2021-08-03 16:35:54 -0400167 raise new_exc from caught_exc