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 'AnonymousCredentials' class. #206

Merged
merged 3 commits into from
Oct 30, 2017
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
37 changes: 37 additions & 0 deletions google/auth/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,43 @@ def before_request(self, request, method, url, headers):
self.apply(headers)


class AnonymousCredentials(Credentials):
"""Credentials that do not provide any authentication information.

These are useful in the case of services that support anonymous access or
local service emulators that do not use credentials.
"""

@property
def expired(self):
"""Returns `False`, anonymous credentials never expire."""
return False

@property
def valid(self):
"""Returns `True`, anonymous credentials are always valid."""
return True

def refresh(self, request):
"""Raises :class:`ValueError``, anonymous credentials cannot be
refreshed."""
raise ValueError("Anonymous credentials cannot be refreshed.")

def apply(self, headers, token=None):
"""Anonymous credentials do nothing to the request.

The optional ``token`` argument is not supported.

Raises:
ValueError: If a token was specified.
"""
if token is not None:
raise ValueError("Anonymous credentials don't support tokens.")

def before_request(self, request, method, url, headers):
"""Anonymous credentials do nothing to the request."""


@six.add_metaclass(abc.ABCMeta)
class ReadOnlyScoped(object):
"""Interface for credentials whose scopes can be queried.
Expand Down
36 changes: 36 additions & 0 deletions tests/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import datetime

import pytest

from google.auth import _helpers
from google.auth import credentials

Expand Down Expand Up @@ -77,6 +79,40 @@ def test_before_request():
assert headers['authorization'] == 'Bearer token'


def test_anonymous_credentials_ctor():
anon = credentials.AnonymousCredentials()
assert anon.token is None
assert anon.expiry is None
assert not anon.expired
assert anon.valid


def test_anonymous_credentials_refresh():
anon = credentials.AnonymousCredentials()
request = object()
with pytest.raises(ValueError):
anon.refresh(request)


def test_anonymous_credentials_apply_default():
anon = credentials.AnonymousCredentials()
headers = {}
anon.apply(headers)
assert headers == {}
with pytest.raises(ValueError):
anon.apply(headers, token='TOKEN')


def test_anonymous_credentials_before_request():
anon = credentials.AnonymousCredentials()
request = object()
method = 'GET'
url = 'https://example.com/api/endpoint'
headers = {}
anon.before_request(request, method, url, headers)
assert headers == {}


class ReadOnlyScopedCredentialsImpl(
credentials.ReadOnlyScoped, CredentialsImpl):
@property
Expand Down