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 with_scopes_if_required helper #65

Merged
merged 1 commit into from
Oct 31, 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
24 changes: 24 additions & 0 deletions google/auth/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,30 @@ def has_scopes(self, scopes):
return set(scopes).issubset(set(self._scopes or []))


def with_scopes_if_required(credentials, scopes):
"""Creates a copy of the credentials with scopes if scoping is required.

This helper function is useful when you do not know (or care to know) the
specific type of credentials you are using (such as when you use
:func:`google.auth.default`). This function will call
:meth:`Scoped.with_scopes` if the credentials are scoped credentials and if
the credentials require scoping. Otherwise, it will return the credentials
as-is.

Args:
credentials (Credentials): The credentials to scope if necessary.
scopes (Sequence[str]): The list of scopes to use.

Returns:
Credentials: Either a new set of scoped credentials, or the passed in
credentials instance if no scoping was required.
"""
if isinstance(credentials, Scoped) and credentials.requires_scopes:
return credentials.with_scopes(scopes)
else:
return credentials


@six.add_metaclass(abc.ABCMeta)
class Signing(object):
"""Interface for credentials that can cryptographically sign messages."""
Expand Down
31 changes: 31 additions & 0 deletions tests/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,34 @@ def test_scoped_credentials_scopes():
def test_scoped_credentials_requires_scopes():
credentials = ScopedCredentialsImpl()
assert not credentials.requires_scopes


class RequiresScopedCredentialsImpl(credentials.Scoped, CredentialsImpl):
def __init__(self, scopes=None):
super(RequiresScopedCredentialsImpl, self).__init__()
self._scopes = scopes

@property
def requires_scopes(self):
return not self.scopes

def with_scopes(self, scopes):
return RequiresScopedCredentialsImpl(scopes=scopes)


def test_create_scoped_if_required_scoped():
unscoped_credentials = RequiresScopedCredentialsImpl()
scoped_credentials = credentials.with_scopes_if_required(
unscoped_credentials, ['one', 'two'])

assert scoped_credentials is not unscoped_credentials
assert not scoped_credentials.requires_scopes
assert scoped_credentials.has_scopes(['one', 'two'])

This comment was marked as spam.



def test_create_scoped_if_required_not_scopes():
unscoped_credentials = CredentialsImpl()
scoped_credentials = credentials.with_scopes_if_required(
unscoped_credentials, ['one', 'two'])

assert scoped_credentials is unscoped_credentials