Skip to content

Allow access_type parameter to be overriden #16

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

Merged
merged 1 commit into from
Sep 19, 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
17 changes: 11 additions & 6 deletions google_auth_oauthlib/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ class Flow(object):
https://console.developers.google.com/apis/credentials
"""

def __init__(self, oauth2session, client_type, client_config):
def __init__(
self, oauth2session, client_type, client_config,
redirect_uri=None):
"""
Args:
oauth2session (requests_oauthlib.OAuth2Session):
Expand All @@ -96,6 +98,9 @@ def __init__(self, oauth2session, client_type, client_config):
``installed``.
client_config (Mapping[str, Any]): The client
configuration in the Google `client secrets`_ format.
redirect_uri (str): The OAuth 2.0 redirect URI if known at flow
creation time. Otherwise, it will need to be set using
:attr:`redirect_uri`.

.. _client secrets:
https://developers.google.com/api-client-library/python/guide
Expand All @@ -107,6 +112,7 @@ def __init__(self, oauth2session, client_type, client_config):
"""Mapping[str, Any]: The OAuth 2.0 client configuration."""
self.oauth2session = oauth2session
"""requests_oauthlib.OAuth2Session: The OAuth 2.0 session."""
self.redirect_uri = redirect_uri
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want a docstring for this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's already one for it below (it's a property)


@classmethod
def from_client_config(cls, client_config, scopes, **kwargs):
Expand Down Expand Up @@ -200,9 +206,9 @@ def authorization_url(self, **kwargs):
:class:`Flow` instance to obtain the token, you will need to
specify the ``state`` when constructing the :class:`Flow`.
"""
kwargs.setdefault('access_type', 'offline')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably worth updating the docstring to explain that access_type=offline will be added if not passed in?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's there.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

D'oh

url, state = self.oauth2session.authorization_url(
self.client_config['auth_uri'],
access_type='offline', **kwargs)
self.client_config['auth_uri'], **kwargs)

return url, state

Expand All @@ -229,10 +235,9 @@ def fetch_token(self, **kwargs):
:meth:`credentials` to obtain a
:class:`~google.auth.credentials.Credentials` instance.
"""
kwargs.setdefault('client_secret', self.client_config['client_secret'])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KeyError ouch?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's okay if that happens (for now at least)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return self.oauth2session.fetch_token(
self.client_config['token_uri'],
client_secret=self.client_config['client_secret'],
**kwargs)
self.client_config['token_uri'], **kwargs)

@property
def credentials(self):
Expand Down
16 changes: 16 additions & 0 deletions tests/test_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ def test_authorization_url(self, instance):
access_type='offline',
prompt='consent')

def test_authorization_url_access_type(self, instance):
scope = 'scope_one'
instance.oauth2session.scope = [scope]
authorization_url_patch = mock.patch.object(
instance.oauth2session, 'authorization_url',
wraps=instance.oauth2session.authorization_url)

with authorization_url_patch as authorization_url_spy:
url, _ = instance.authorization_url(access_type='meep')

assert CLIENT_SECRETS_INFO['web']['auth_uri'] in url
assert scope in url
authorization_url_spy.assert_called_with(
CLIENT_SECRETS_INFO['web']['auth_uri'],
access_type='meep')

def test_fetch_token(self, instance):
fetch_token_patch = mock.patch.object(
instance.oauth2session, 'fetch_token', autospec=True,
Expand Down