blob: 87d37095126758c759c0427d6151ce4998dcce8b [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 Parrott85c2c6d2017-01-05 12:34:49 -080017try:
18 import google.auth
Wilson Lian09527302017-01-11 14:38:18 -080019 import google.auth.credentials
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -080020 import google_auth_httplib2
21 HAS_GOOGLE_AUTH = True
22except ImportError: # pragma: NO COVER
23 HAS_GOOGLE_AUTH = False
24
25try:
26 import oauth2client
27 import oauth2client.client
28 HAS_OAUTH2CLIENT = True
29except ImportError: # pragma: NO COVER
30 HAS_OAUTH2CLIENT = False
31
Igor Maravić22435292017-01-19 22:28:22 +010032from googleapiclient.http import build_http
33
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -080034
35def default_credentials():
36 """Returns Application Default Credentials."""
37 if HAS_GOOGLE_AUTH:
38 credentials, _ = google.auth.default()
39 return credentials
40 elif HAS_OAUTH2CLIENT:
41 return oauth2client.client.GoogleCredentials.get_application_default()
42 else:
43 raise EnvironmentError(
44 'No authentication library is available. Please install either '
45 'google-auth or oauth2client.')
46
47
48def with_scopes(credentials, scopes):
49 """Scopes the credentials if necessary.
50
51 Args:
52 credentials (Union[
53 google.auth.credentials.Credentials,
54 oauth2client.client.Credentials]): The credentials to scope.
55 scopes (Sequence[str]): The list of scopes.
56
57 Returns:
58 Union[google.auth.credentials.Credentials,
59 oauth2client.client.Credentials]: The scoped credentials.
60 """
61 if HAS_GOOGLE_AUTH and isinstance(
62 credentials, google.auth.credentials.Credentials):
63 return google.auth.credentials.with_scopes_if_required(
64 credentials, scopes)
65 else:
66 try:
67 if credentials.create_scoped_required():
68 return credentials.create_scoped(scopes)
69 else:
70 return credentials
71 except AttributeError:
72 return credentials
73
74
75def authorized_http(credentials):
76 """Returns an http client that is authorized with the given credentials.
77
78 Args:
79 credentials (Union[
80 google.auth.credentials.Credentials,
81 oauth2client.client.Credentials]): The credentials to use.
82
83 Returns:
84 Union[httplib2.Http, google_auth_httplib2.AuthorizedHttp]: An
85 authorized http client.
86 """
87 if HAS_GOOGLE_AUTH and isinstance(
88 credentials, google.auth.credentials.Credentials):
Igor Maravić22435292017-01-19 22:28:22 +010089 return google_auth_httplib2.AuthorizedHttp(credentials,
90 http=build_http())
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -080091 else:
Igor Maravić22435292017-01-19 22:28:22 +010092 return credentials.authorize(build_http())