blob: 71fd7bf41d126ae560c59a9f1bf06785048ec0cf [file] [log] [blame]
Jon Wayne Parrotta896d2a2016-11-02 23:42:51 -07001# Copyright 2016 Google Inc.
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
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
Danny Hermesae5d3a42017-11-09 12:04:14 -080024import six
25
Jon Wayne Parrotta896d2a2016-11-02 23:42:51 -070026from google.auth import _helpers
27import google.auth.app_engine
28import google.oauth2.credentials
29import google.oauth2.service_account
30
31try:
32 import oauth2client.client
33 import oauth2client.contrib.gce
34 import oauth2client.service_account
Danny Hermes895e3692017-11-09 11:35:57 -080035except ImportError as caught_exc:
Danny Hermesae5d3a42017-11-09 12:04:14 -080036 six.raise_from(
37 ImportError('oauth2client is not installed.'), caught_exc)
Jon Wayne Parrotta896d2a2016-11-02 23:42:51 -070038
39try:
40 import oauth2client.contrib.appengine
41 _HAS_APPENGINE = True
42except ImportError:
43 _HAS_APPENGINE = False
44
45
46_CONVERT_ERROR_TMPL = (
47 'Unable to convert {} to a google-auth credentials class.')
48
49
50def _convert_oauth2_credentials(credentials):
51 """Converts to :class:`google.oauth2.credentials.Credentials`.
52
53 Args:
54 credentials (Union[oauth2client.client.OAuth2Credentials,
55 oauth2client.client.GoogleCredentials]): The credentials to
56 convert.
57
58 Returns:
59 google.oauth2.credentials.Credentials: The converted credentials.
60 """
61 new_credentials = google.oauth2.credentials.Credentials(
62 token=credentials.access_token,
63 refresh_token=credentials.refresh_token,
64 token_uri=credentials.token_uri,
65 client_id=credentials.client_id,
66 client_secret=credentials.client_secret,
67 scopes=credentials.scopes)
68
69 new_credentials._expires = credentials.token_expiry
70
71 return new_credentials
72
73
74def _convert_service_account_credentials(credentials):
75 """Converts to :class:`google.oauth2.service_account.Credentials`.
76
77 Args:
78 credentials (Union[
79 oauth2client.service_account.ServiceAccountCredentials,
80 oauth2client.service_account._JWTAccessCredentials]): The
81 credentials to convert.
82
83 Returns:
84 google.oauth2.service_account.Credentials: The converted credentials.
85 """
86 info = credentials.serialization_data.copy()
87 info['token_uri'] = credentials.token_uri
88 return google.oauth2.service_account.Credentials.from_service_account_info(
89 info)
90
91
92def _convert_gce_app_assertion_credentials(credentials):
93 """Converts to :class:`google.auth.compute_engine.Credentials`.
94
95 Args:
96 credentials (oauth2client.contrib.gce.AppAssertionCredentials): The
97 credentials to convert.
98
99 Returns:
100 google.oauth2.service_account.Credentials: The converted credentials.
101 """
102 return google.auth.compute_engine.Credentials(
103 service_account_email=credentials.service_account_email)
104
105
106def _convert_appengine_app_assertion_credentials(credentials):
107 """Converts to :class:`google.auth.app_engine.Credentials`.
108
109 Args:
110 credentials (oauth2client.contrib.app_engine.AppAssertionCredentials):
111 The credentials to convert.
112
113 Returns:
114 google.oauth2.service_account.Credentials: The converted credentials.
115 """
116 # pylint: disable=invalid-name
117 return google.auth.app_engine.Credentials(
118 scopes=_helpers.string_to_scopes(credentials.scope),
119 service_account_id=credentials.service_account_id)
120
121
122_CLASS_CONVERSION_MAP = {
123 oauth2client.client.OAuth2Credentials: _convert_oauth2_credentials,
124 oauth2client.client.GoogleCredentials: _convert_oauth2_credentials,
125 oauth2client.service_account.ServiceAccountCredentials:
126 _convert_service_account_credentials,
127 oauth2client.service_account._JWTAccessCredentials:
128 _convert_service_account_credentials,
129 oauth2client.contrib.gce.AppAssertionCredentials:
130 _convert_gce_app_assertion_credentials,
131}
132
133if _HAS_APPENGINE:
134 _CLASS_CONVERSION_MAP[
135 oauth2client.contrib.appengine.AppAssertionCredentials] = (
136 _convert_appengine_app_assertion_credentials)
137
138
139def convert(credentials):
140 """Convert oauth2client credentials to google-auth credentials.
141
142 This class converts:
143
144 - :class:`oauth2client.client.OAuth2Credentials` to
145 :class:`google.oauth2.credentials.Credentials`.
146 - :class:`oauth2client.client.GoogleCredentials` to
147 :class:`google.oauth2.credentials.Credentials`.
148 - :class:`oauth2client.service_account.ServiceAccountCredentials` to
149 :class:`google.oauth2.service_account.Credentials`.
150 - :class:`oauth2client.service_account._JWTAccessCredentials` to
151 :class:`google.oauth2.service_account.Credentials`.
152 - :class:`oauth2client.contrib.gce.AppAssertionCredentials` to
153 :class:`google.auth.compute_engine.Credentials`.
154 - :class:`oauth2client.contrib.appengine.AppAssertionCredentials` to
155 :class:`google.auth.app_engine.Credentials`.
156
157 Returns:
158 google.auth.credentials.Credentials: The converted credentials.
159
160 Raises:
161 ValueError: If the credentials could not be converted.
162 """
163
164 credentials_class = type(credentials)
165
166 try:
167 return _CLASS_CONVERSION_MAP[credentials_class](credentials)
Danny Hermes895e3692017-11-09 11:35:57 -0800168 except KeyError as caught_exc:
169 new_exc = ValueError(_CONVERT_ERROR_TMPL.format(credentials_class))
170 six.raise_from(new_exc, caught_exc)