| 1 |
# 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 |
|
| 17 |
import httplib2 |
| 18 |
|
| 19 |
try: |
| 20 |
import google.auth |
| 21 |
import google.auth.credentials |
| 22 |
|
| 23 |
HAS_GOOGLE_AUTH = True |
| 24 |
except ImportError: # pragma: NO COVER |
| 25 |
HAS_GOOGLE_AUTH = False |
| 26 |
|
| 27 |
try: |
| 28 |
import google_auth_httplib2 |
| 29 |
except ImportError: # pragma: NO COVER |
| 30 |
google_auth_httplib2 = None |
| 31 |
|
| 32 |
try: |
| 33 |
import oauth2client |
| 34 |
import oauth2client.client |
| 35 |
|
| 36 |
HAS_OAUTH2CLIENT = True |
| 37 |
except ImportError: # pragma: NO COVER |
| 38 |
HAS_OAUTH2CLIENT = False |
| 39 |
|
| 40 |
|
| 41 |
def credentials_from_file(filename, scopes=None, quota_project_id=None): |
| 42 |
"""Returns credentials loaded from a file.""" |
| 43 |
if HAS_GOOGLE_AUTH: |
| 44 |
credentials, _ = google.auth.load_credentials_from_file( |
| 45 |
filename, scopes=scopes, quota_project_id=quota_project_id |
| 46 |
) |
| 47 |
return credentials |
| 48 |
else: |
| 49 |
raise EnvironmentError( |
| 50 |
"client_options.credentials_file is only supported in google-auth." |
| 51 |
) |
| 52 |
|
| 53 |
|
| 54 |
def default_credentials(scopes=None, quota_project_id=None): |
| 55 |
"""Returns Application Default Credentials.""" |
| 56 |
if HAS_GOOGLE_AUTH: |
| 57 |
credentials, _ = google.auth.default( |
| 58 |
scopes=scopes, quota_project_id=quota_project_id |
| 59 |
) |
| 60 |
return credentials |
| 61 |
elif HAS_OAUTH2CLIENT: |
| 62 |
if scopes is not None or quota_project_id is not None: |
| 63 |
raise EnvironmentError( |
| 64 |
"client_options.scopes and client_options.quota_project_id are not supported in oauth2client." |
| 65 |
"Please install google-auth." |
| 66 |
) |
| 67 |
return oauth2client.client.GoogleCredentials.get_application_default() |
| 68 |
else: |
| 69 |
raise EnvironmentError( |
| 70 |
"No authentication library is available. Please install either " |
| 71 |
"google-auth or oauth2client." |
| 72 |
) |
| 73 |
|
| 74 |
|
| 75 |
def with_scopes(credentials, scopes): |
| 76 |
"""Scopes the credentials if necessary. |
| 77 |
|
| 78 |
Args: |
| 79 |
credentials (Union[ |
| 80 |
google.auth.credentials.Credentials, |
| 81 |
oauth2client.client.Credentials]): The credentials to scope. |
| 82 |
scopes (Sequence[str]): The list of scopes. |
| 83 |
|
| 84 |
Returns: |
| 85 |
Union[google.auth.credentials.Credentials, |
| 86 |
oauth2client.client.Credentials]: The scoped credentials. |
| 87 |
""" |
| 88 |
if HAS_GOOGLE_AUTH and isinstance(credentials, google.auth.credentials.Credentials): |
| 89 |
return google.auth.credentials.with_scopes_if_required(credentials, scopes) |
| 90 |
else: |
| 91 |
try: |
| 92 |
if credentials.create_scoped_required(): |
| 93 |
return credentials.create_scoped(scopes) |
| 94 |
else: |
| 95 |
return credentials |
| 96 |
except AttributeError: |
| 97 |
return credentials |
| 98 |
|
| 99 |
|
| 100 |
def authorized_http(credentials): |
| 101 |
"""Returns an http client that is authorized with the given credentials. |
| 102 |
|
| 103 |
Args: |
| 104 |
credentials (Union[ |
| 105 |
google.auth.credentials.Credentials, |
| 106 |
oauth2client.client.Credentials]): The credentials to use. |
| 107 |
|
| 108 |
Returns: |
| 109 |
Union[httplib2.Http, google_auth_httplib2.AuthorizedHttp]: An |
| 110 |
authorized http client. |
| 111 |
""" |
| 112 |
from googleapiclient.http import build_http |
| 113 |
|
| 114 |
if HAS_GOOGLE_AUTH and isinstance(credentials, google.auth.credentials.Credentials): |
| 115 |
if google_auth_httplib2 is None: |
| 116 |
raise ValueError( |
| 117 |
"Credentials from google.auth specified, but " |
| 118 |
"google-api-python-client is unable to use these credentials " |
| 119 |
"unless google-auth-httplib2 is installed. Please install " |
| 120 |
"google-auth-httplib2." |
| 121 |
) |
| 122 |
return google_auth_httplib2.AuthorizedHttp(credentials, http=build_http()) |
| 123 |
else: |
| 124 |
return credentials.authorize(build_http()) |
| 125 |
|
| 126 |
|
| 127 |
def refresh_credentials(credentials): |
| 128 |
# Refresh must use a new http instance, as the one associated with the |
| 129 |
# credentials could be a AuthorizedHttp or an oauth2client-decorated |
| 130 |
# Http instance which would cause a weird recursive loop of refreshing |
| 131 |
# and likely tear a hole in spacetime. |
| 132 |
refresh_http = httplib2.Http() |
| 133 |
if HAS_GOOGLE_AUTH and isinstance(credentials, google.auth.credentials.Credentials): |
| 134 |
request = google_auth_httplib2.Request(refresh_http) |
| 135 |
return credentials.refresh(request) |
| 136 |
else: |
| 137 |
return credentials.refresh(refresh_http) |
| 138 |
|
| 139 |
|
| 140 |
def apply_credentials(credentials, headers): |
| 141 |
# oauth2client and google-auth have the same interface for this. |
| 142 |
if not is_valid(credentials): |
| 143 |
refresh_credentials(credentials) |
| 144 |
return credentials.apply(headers) |
| 145 |
|
| 146 |
|
| 147 |
def is_valid(credentials): |
| 148 |
if HAS_GOOGLE_AUTH and isinstance(credentials, google.auth.credentials.Credentials): |
| 149 |
return credentials.valid |
| 150 |
else: |
| 151 |
return ( |
| 152 |
credentials.access_token is not None |
| 153 |
and not credentials.access_token_expired |
| 154 |
) |
| 155 |
|
| 156 |
|
| 157 |
def get_credentials_from_http(http): |
| 158 |
if http is None: |
| 159 |
return None |
| 160 |
elif hasattr(http.request, "credentials"): |
| 161 |
return http.request.credentials |
| 162 |
elif hasattr(http, "credentials") and not isinstance( |
| 163 |
http.credentials, httplib2.Credentials |
| 164 |
): |
| 165 |
return http.credentials |
| 166 |
else: |
| 167 |
return None |