Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scopes argument to default #75

Merged
merged 1 commit into from
Nov 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion google/auth/_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from google.auth import environment_vars
from google.auth import exceptions
from google.auth.compute_engine import _metadata
import google.auth.credentials
import google.auth.transport._http_client
from google.oauth2 import service_account
import google.oauth2.credentials
Expand Down Expand Up @@ -185,7 +186,7 @@ def _get_gce_credentials(request=None):
return None, None


def default(request=None):
def default(scopes=None, request=None):
"""Gets the default credentials for the current environment.

`Application Default Credentials`_ provides an easy way to obtain
Expand Down Expand Up @@ -238,6 +239,9 @@ def default(request=None):
credentials, project_id = google.auth.default()

Args:
scopes (Sequence[str]): The list of scopes for the credentials. If
specified, the credentials will automatically be scoped if
necessary.
request (google.auth.transport.Request): An object used to make
HTTP requests. This is used to detect whether the application
is running on Compute Engine. If not specified, then it will
Expand Down Expand Up @@ -265,6 +269,8 @@ def default(request=None):
for checker in checkers:
credentials, project_id = checker()
if credentials is not None:
credentials = google.auth.credentials.with_scopes_if_required(
credentials, scopes)
return credentials, explicit_project_id or project_id

raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
16 changes: 16 additions & 0 deletions tests/test__default.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,19 @@ def test_default_explict_project_id(get_mock, monkeypatch):
def test_default_fail(unused_gce, unused_gae, unused_sdk, unused_explicit):
with pytest.raises(exceptions.DefaultCredentialsError):
assert _default.default()


@mock.patch(
'google.auth._default._get_explicit_environ_credentials',
return_value=(mock.sentinel.credentials, mock.sentinel.project_id))
@mock.patch(
'google.auth.credentials.with_scopes_if_required')
def test_default_scoped(with_scopes_mock, get_mock):
scopes = ['one', 'two']

credentials, project_id = _default.default(scopes=scopes)

assert credentials == with_scopes_mock.return_value
assert project_id == mock.sentinel.project_id
with_scopes_mock.assert_called_once_with(
mock.sentinel.credentials, scopes)