blob: 8a2f673ddcc74af3bb2e7985972fedf6489d1fcb [file] [log] [blame]
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -08001# Copyright 2016 Google Inc. All Rights Reserved.
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 authentication using oauth2client or google-auth."""
16
Jon Wayne Parrottd3a5cf42017-06-19 17:55:04 -070017import httplib2
18
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -080019try:
20 import google.auth
Wilson Lian09527302017-01-11 14:38:18 -080021 import google.auth.credentials
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070022
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -080023 HAS_GOOGLE_AUTH = True
24except ImportError: # pragma: NO COVER
25 HAS_GOOGLE_AUTH = False
26
27try:
Jon Wayne Parrott401e8862017-09-19 18:36:35 -070028 import google_auth_httplib2
29except ImportError: # pragma: NO COVER
30 google_auth_httplib2 = None
31
32try:
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -080033 import oauth2client
34 import oauth2client.client
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070035
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -080036 HAS_OAUTH2CLIENT = True
37except ImportError: # pragma: NO COVER
38 HAS_OAUTH2CLIENT = False
39
40
41def default_credentials():
42 """Returns Application Default Credentials."""
43 if HAS_GOOGLE_AUTH:
44 credentials, _ = google.auth.default()
45 return credentials
46 elif HAS_OAUTH2CLIENT:
47 return oauth2client.client.GoogleCredentials.get_application_default()
48 else:
49 raise EnvironmentError(
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070050 "No authentication library is available. Please install either "
51 "google-auth or oauth2client."
52 )
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -080053
54
55def with_scopes(credentials, scopes):
56 """Scopes the credentials if necessary.
57
58 Args:
59 credentials (Union[
60 google.auth.credentials.Credentials,
61 oauth2client.client.Credentials]): The credentials to scope.
62 scopes (Sequence[str]): The list of scopes.
63
64 Returns:
65 Union[google.auth.credentials.Credentials,
66 oauth2client.client.Credentials]: The scoped credentials.
67 """
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070068 if HAS_GOOGLE_AUTH and isinstance(credentials, google.auth.credentials.Credentials):
69 return google.auth.credentials.with_scopes_if_required(credentials, scopes)
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -080070 else:
71 try:
72 if credentials.create_scoped_required():
73 return credentials.create_scoped(scopes)
74 else:
75 return credentials
76 except AttributeError:
77 return credentials
78
79
80def authorized_http(credentials):
81 """Returns an http client that is authorized with the given credentials.
82
83 Args:
84 credentials (Union[
85 google.auth.credentials.Credentials,
86 oauth2client.client.Credentials]): The credentials to use.
87
88 Returns:
89 Union[httplib2.Http, google_auth_httplib2.AuthorizedHttp]: An
90 authorized http client.
91 """
Jon Wayne Parrottd3a5cf42017-06-19 17:55:04 -070092 from googleapiclient.http import build_http
93
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070094 if HAS_GOOGLE_AUTH and isinstance(credentials, google.auth.credentials.Credentials):
Jon Wayne Parrott401e8862017-09-19 18:36:35 -070095 if google_auth_httplib2 is None:
96 raise ValueError(
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070097 "Credentials from google.auth specified, but "
98 "google-api-python-client is unable to use these credentials "
99 "unless google-auth-httplib2 is installed. Please install "
100 "google-auth-httplib2."
101 )
102 return google_auth_httplib2.AuthorizedHttp(credentials, http=build_http())
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -0800103 else:
Igor Maravić22435292017-01-19 22:28:22 +0100104 return credentials.authorize(build_http())
Jon Wayne Parrottd3a5cf42017-06-19 17:55:04 -0700105
106
107def refresh_credentials(credentials):
108 # Refresh must use a new http instance, as the one associated with the
109 # credentials could be a AuthorizedHttp or an oauth2client-decorated
110 # Http instance which would cause a weird recursive loop of refreshing
111 # and likely tear a hole in spacetime.
112 refresh_http = httplib2.Http()
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700113 if HAS_GOOGLE_AUTH and isinstance(credentials, google.auth.credentials.Credentials):
Jon Wayne Parrottd3a5cf42017-06-19 17:55:04 -0700114 request = google_auth_httplib2.Request(refresh_http)
115 return credentials.refresh(request)
116 else:
117 return credentials.refresh(refresh_http)
118
119
120def apply_credentials(credentials, headers):
121 # oauth2client and google-auth have the same interface for this.
Jon Wayne Parrott20e61352018-01-18 09:16:37 -0800122 if not is_valid(credentials):
123 refresh_credentials(credentials)
Jon Wayne Parrottd3a5cf42017-06-19 17:55:04 -0700124 return credentials.apply(headers)
125
126
127def is_valid(credentials):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700128 if HAS_GOOGLE_AUTH and isinstance(credentials, google.auth.credentials.Credentials):
Jon Wayne Parrottd3a5cf42017-06-19 17:55:04 -0700129 return credentials.valid
130 else:
Jon Wayne Parrott20e61352018-01-18 09:16:37 -0800131 return (
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700132 credentials.access_token is not None
133 and not credentials.access_token_expired
134 )
Jon Wayne Parrottd3a5cf42017-06-19 17:55:04 -0700135
136
137def get_credentials_from_http(http):
138 if http is None:
139 return None
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700140 elif hasattr(http.request, "credentials"):
Jon Wayne Parrottd3a5cf42017-06-19 17:55:04 -0700141 return http.request.credentials
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700142 elif hasattr(http, "credentials") and not isinstance(
143 http.credentials, httplib2.Credentials
144 ):
Jon Wayne Parrottd3a5cf42017-06-19 17:55:04 -0700145 return http.credentials
146 else:
147 return None