forked from googleapis/google-api-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for google-auth and remove Python 2.6 support (googleapis…
…#319) * `discovery.build` and `discovery.build_from_document` now accept both oauth2client credentials and google-auth credentials. * `discovery.build` and `discovery.build_from_document` now unambiguously use the http argument for *all* requests, including the request to get the discovery document. * The `http` and `credentials` arguments to `discovery.build` and `discovery.build_from_document` are now mutally exclusive. * If neither `http` or `credentials` is specified to `discovery.build` and `discovery.build_from_document`, then Application Default Credentials will be used. * oauth2client is still the "default" authentication library.
- Loading branch information
Jon Wayne Parrott
authored
Jan 5, 2017
1 parent
94a5394
commit 85c2c6d
Showing
7 changed files
with
308 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ sudo: false | |
cache: pip | ||
env: | ||
matrix: | ||
- TOX_ENV=py26 | ||
- TOX_ENV=py27 | ||
- TOX_ENV=py33 | ||
- TOX_ENV=py34 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Copyright 2016 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Helpers for authentication using oauth2client or google-auth.""" | ||
|
||
import httplib2 | ||
|
||
try: | ||
import google.auth | ||
import google_auth_httplib2 | ||
HAS_GOOGLE_AUTH = True | ||
except ImportError: # pragma: NO COVER | ||
HAS_GOOGLE_AUTH = False | ||
|
||
try: | ||
import oauth2client | ||
import oauth2client.client | ||
HAS_OAUTH2CLIENT = True | ||
except ImportError: # pragma: NO COVER | ||
HAS_OAUTH2CLIENT = False | ||
|
||
|
||
def default_credentials(): | ||
"""Returns Application Default Credentials.""" | ||
if HAS_GOOGLE_AUTH: | ||
credentials, _ = google.auth.default() | ||
return credentials | ||
elif HAS_OAUTH2CLIENT: | ||
return oauth2client.client.GoogleCredentials.get_application_default() | ||
else: | ||
raise EnvironmentError( | ||
'No authentication library is available. Please install either ' | ||
'google-auth or oauth2client.') | ||
|
||
|
||
def with_scopes(credentials, scopes): | ||
"""Scopes the credentials if necessary. | ||
Args: | ||
credentials (Union[ | ||
google.auth.credentials.Credentials, | ||
oauth2client.client.Credentials]): The credentials to scope. | ||
scopes (Sequence[str]): The list of scopes. | ||
Returns: | ||
Union[google.auth.credentials.Credentials, | ||
oauth2client.client.Credentials]: The scoped credentials. | ||
""" | ||
if HAS_GOOGLE_AUTH and isinstance( | ||
credentials, google.auth.credentials.Credentials): | ||
return google.auth.credentials.with_scopes_if_required( | ||
credentials, scopes) | ||
else: | ||
try: | ||
if credentials.create_scoped_required(): | ||
return credentials.create_scoped(scopes) | ||
else: | ||
return credentials | ||
except AttributeError: | ||
return credentials | ||
|
||
|
||
def authorized_http(credentials): | ||
"""Returns an http client that is authorized with the given credentials. | ||
Args: | ||
credentials (Union[ | ||
google.auth.credentials.Credentials, | ||
oauth2client.client.Credentials]): The credentials to use. | ||
Returns: | ||
Union[httplib2.Http, google_auth_httplib2.AuthorizedHttp]: An | ||
authorized http client. | ||
""" | ||
if HAS_GOOGLE_AUTH and isinstance( | ||
credentials, google.auth.credentials.Credentials): | ||
return google_auth_httplib2.AuthorizedHttp(credentials) | ||
else: | ||
return credentials.authorize(httplib2.Http()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
# Copyright 2016 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import mock | ||
|
||
import google.auth.credentials | ||
import google_auth_httplib2 | ||
import httplib2 | ||
import oauth2client.client | ||
import unittest2 | ||
|
||
from googleapiclient import _auth | ||
|
||
|
||
class TestAuthWithGoogleAuth(unittest2.TestCase): | ||
def setUp(self): | ||
_auth.HAS_GOOGLE_AUTH = True | ||
_auth.HAS_OAUTH2CLIENT = False | ||
|
||
def tearDown(self): | ||
_auth.HAS_GOOGLE_AUTH = True | ||
_auth.HAS_OAUTH2CLIENT = True | ||
|
||
def test_default_credentials(self): | ||
with mock.patch('google.auth.default', autospec=True) as default: | ||
default.return_value = ( | ||
mock.sentinel.credentials, mock.sentinel.project) | ||
|
||
credentials = _auth.default_credentials() | ||
|
||
self.assertEqual(credentials, mock.sentinel.credentials) | ||
|
||
def test_with_scopes_non_scoped(self): | ||
credentials = mock.Mock(spec=google.auth.credentials.Credentials) | ||
|
||
returned = _auth.with_scopes(credentials, mock.sentinel.scopes) | ||
|
||
self.assertEqual(credentials, returned) | ||
|
||
def test_with_scopes_scoped(self): | ||
class CredentialsWithScopes( | ||
google.auth.credentials.Credentials, | ||
google.auth.credentials.Scoped): | ||
pass | ||
|
||
credentials = mock.Mock(spec=CredentialsWithScopes) | ||
credentials.requires_scopes = True | ||
|
||
returned = _auth.with_scopes(credentials, mock.sentinel.scopes) | ||
|
||
self.assertNotEqual(credentials, returned) | ||
self.assertEqual(returned, credentials.with_scopes.return_value) | ||
credentials.with_scopes.assert_called_once_with(mock.sentinel.scopes) | ||
|
||
def test_authorized_http(self): | ||
credentials = mock.Mock(spec=google.auth.credentials.Credentials) | ||
|
||
http = _auth.authorized_http(credentials) | ||
|
||
self.assertIsInstance(http, google_auth_httplib2.AuthorizedHttp) | ||
self.assertEqual(http.credentials, credentials) | ||
|
||
|
||
class TestAuthWithOAuth2Client(unittest2.TestCase): | ||
def setUp(self): | ||
_auth.HAS_GOOGLE_AUTH = False | ||
_auth.HAS_OAUTH2CLIENT = True | ||
|
||
def tearDown(self): | ||
_auth.HAS_GOOGLE_AUTH = True | ||
_auth.HAS_OAUTH2CLIENT = True | ||
|
||
def test_default_credentials(self): | ||
default_patch = mock.patch( | ||
'oauth2client.client.GoogleCredentials.get_application_default') | ||
|
||
with default_patch as default: | ||
default.return_value = mock.sentinel.credentials | ||
|
||
credentials = _auth.default_credentials() | ||
|
||
self.assertEqual(credentials, mock.sentinel.credentials) | ||
|
||
def test_with_scopes_non_scoped(self): | ||
credentials = mock.Mock(spec=oauth2client.client.Credentials) | ||
|
||
returned = _auth.with_scopes(credentials, mock.sentinel.scopes) | ||
|
||
self.assertEqual(credentials, returned) | ||
|
||
def test_with_scopes_scoped(self): | ||
credentials = mock.Mock(spec=oauth2client.client.GoogleCredentials) | ||
credentials.create_scoped_required.return_value = True | ||
|
||
returned = _auth.with_scopes(credentials, mock.sentinel.scopes) | ||
|
||
self.assertNotEqual(credentials, returned) | ||
self.assertEqual(returned, credentials.create_scoped.return_value) | ||
credentials.create_scoped.assert_called_once_with(mock.sentinel.scopes) | ||
|
||
def test_authorized_http(self): | ||
credentials = mock.Mock(spec=oauth2client.client.Credentials) | ||
|
||
http = _auth.authorized_http(credentials) | ||
|
||
self.assertEqual(http, credentials.authorize.return_value) | ||
self.assertIsInstance( | ||
credentials.authorize.call_args[0][0], httplib2.Http) | ||
|
||
|
||
class TestAuthWithoutAuth(unittest2.TestCase): | ||
|
||
def setUp(self): | ||
_auth.HAS_GOOGLE_AUTH = False | ||
_auth.HAS_OAUTH2CLIENT = False | ||
|
||
def tearDown(self): | ||
_auth.HAS_GOOGLE_AUTH = True | ||
_auth.HAS_OAUTH2CLIENT = True | ||
|
||
def test_default_credentials(self): | ||
with self.assertRaises(EnvironmentError): | ||
print(_auth.default_credentials()) |
Oops, something went wrong.