-
-
Notifications
You must be signed in to change notification settings - Fork 426
Modify token auto refresh handling. #265
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
Changes from all commits
cf3ff0a
f22426b
e5e888a
5e91016
fd7eaa5
eee296a
6960d8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,33 @@ | ||
| from __future__ import unicode_literals | ||
|
|
||
| import logging | ||
| import warnings | ||
|
|
||
| from oauthlib.common import generate_token, urldecode | ||
| from oauthlib.oauth2 import WebApplicationClient, InsecureTransportError | ||
| from oauthlib.oauth2 import TokenExpiredError, is_secure_transport | ||
|
|
||
| import requests | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| # Preserve so we can call it. | ||
| old_showwarning = warnings.showwarning | ||
|
|
||
| def logwarning(message, category, filename, lineno, file=None): | ||
| """ | ||
| Log warnings as well as print them. | ||
| """ | ||
| log.warning( | ||
| '%s:%s: %s:%s' % | ||
| (filename, lineno, category.__name__, message)) | ||
| old_showwarning(message, category, filename, lineno, file=file) | ||
|
|
||
| # Install our new handler. | ||
| warnings.showwarning = logwarning | ||
|
|
||
|
|
||
| class TokenUpdated(Warning): | ||
| def __init__(self, token): | ||
| super(TokenUpdated, self).__init__() | ||
|
|
@@ -279,9 +297,6 @@ def refresh_token(self, token_url, refresh_token=None, body='', auth=None, | |
|
|
||
| refresh_token = refresh_token or self.token.get('refresh_token') | ||
|
|
||
| log.debug('Adding auto refresh key word arguments %s.', | ||
| self.auto_refresh_kwargs) | ||
| kwargs.update(self.auto_refresh_kwargs) | ||
| body = self._client.prepare_refresh_body(body=body, | ||
| refresh_token=refresh_token, scope=self.scope, **kwargs) | ||
| log.debug('Prepared refresh token request body %s', body) | ||
|
|
@@ -334,13 +349,42 @@ def request(self, method, url, data=None, headers=None, withhold_token=False, | |
| log.debug('Auto refresh is set, attempting to refresh at %s.', | ||
| self.auto_refresh_url) | ||
|
|
||
| # We mustn't pass auth twice. | ||
| auth = kwargs.pop('auth', None) | ||
| if client_id and client_secret and (auth is None): | ||
| # Someday this code can be eliminated, we should be able to | ||
| # simply pass **self.auto_refresh_kwargs to | ||
| # `refresh_token()` what follows is to maintain | ||
| # compatibility | ||
|
|
||
| # Start with kwargs explicitly requested for refresh. | ||
| refresh_kwargs = self.auto_refresh_kwargs.copy() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder, isn't it going to change the semantic of |
||
|
|
||
| if kwargs.get('auth', None): | ||
| # If user supplied `auth` to `request()` warn them to | ||
| # instead use `auto_refresh_kwargs`. But honor their | ||
| # intent for now. | ||
| warnings.warn('auth argument supplied. Please specify ' | ||
| 'token refresh authentication in ' | ||
| 'auto_refresh_kwargs.') | ||
| refresh_kwargs['auth'] = kwargs.pop('auth') | ||
| elif client_id: | ||
| # If no auth was provided, but `client_id` and | ||
| # `client_secret` were, warn the user and create an | ||
| # HTTP Basic auth header. It is preferred if the user | ||
| # instead place an auth param into `auto_refresh_kwargs` | ||
| warnings.warn('client_id argument supplied, Please ' | ||
| 'specify token refresh authentication in' | ||
| 'auto_refresh_kwargs') | ||
| log.debug('Encoding client_id "%s" with client_secret as Basic auth credentials.', client_id) | ||
| auth = requests.auth.HTTPBasicAuth(client_id, client_secret) | ||
| if not client_secret: | ||
| warnings.warn('client_id provided, but ' | ||
| 'client_secret missing') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this warning? I'm not certain how likely this is to be an error.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added it because the code replaces the missing argument with an empty string and continues with authentication. I thought that seemed like an omission on the callers part, something they might need warned about. This could be an incorrect assumption.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than do that, let's just require both the client ID and secret. |
||
| client_secret = '' | ||
| refresh_kwargs['auth'] = \ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A style note: we'd rather use implicit newlines inside parentheses than explicit ones.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The newline does not show up in the resulting string, it is strictly for formatting, the two strings end up concatenated into one (single line) string.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Example: >>> print('this is a multi-'
... 'line string in the code but'
... ' a single line when printed.')
this is a multi-line string in the code but a single line when printed.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know. I'm saying don't use |
||
| requests.auth.HTTPBasicAuth(client_id, client_secret) | ||
|
|
||
| # End of compat. code. | ||
|
|
||
| token = self.refresh_token( | ||
| self.auto_refresh_url, auth=auth, **kwargs | ||
| self.auto_refresh_url, **refresh_kwargs | ||
| ) | ||
| if self.token_updater: | ||
| log.debug('Updating token to %s using %s.', | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am very nervous about monkeypatching the warnings module. I'd rather not do it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://docs.python.org/2/library/warnings.html#warnings.showwarning
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but it is still unnecessary for us to do this. We can just use a regular function, rather than change global state.