Skip to content

Commit e433f95

Browse files
committed
Decouple internal logic & always trigger callbacks
1 parent 1659f67 commit e433f95

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

oauth2cli/oauth2.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010
import warnings
1111
import time
1212
import base64
13+
import sys
1314

1415
import requests
1516

1617

18+
string_types = (str,) if sys.version_info[0] >= 3 else (basestring, )
19+
1720

1821
class BaseClient(object):
1922
# This low-level interface works. Yet you'll find its sub-class
@@ -163,13 +166,15 @@ def _obtain_token( # The verb "obtain" is influenced by OAUTH2 RFC 6749
163166
raise
164167

165168
def obtain_token_by_refresh_token(self, refresh_token, scope=None, **kwargs):
169+
# type: (str, Union[str, list, set, tuple]) -> dict
166170
"""Obtain an access token via a refresh token.
167171
168172
:param refresh_token: The refresh token issued to the client
169173
:param scope: If omitted, is treated as equal to the scope originally
170174
granted by the resource ownser,
171175
according to https://tools.ietf.org/html/rfc6749#section-6
172176
"""
177+
assert isinstance(refresh_token, string_types)
173178
data = kwargs.pop('data', {})
174179
data.update(refresh_token=refresh_token, scope=scope)
175180
return self._obtain_token("refresh_token", data=data, **kwargs)
@@ -380,14 +385,10 @@ def __init__(self,
380385
self.on_removing_rt = on_removing_rt
381386
self.on_updating_rt = on_updating_rt
382387

383-
def _obtain_token(self, grant_type, params=None, data=None,
384-
rt_getter=lambda token_item: token_item["refresh_token"],
385-
*args, **kwargs):
388+
def _obtain_token(self, grant_type, params=None, data=None, *args, **kwargs):
386389
RT = "refresh_token"
387390
_data = data.copy() # to prevent side effect
388391
refresh_token = _data.get(RT)
389-
if grant_type == RT and isinstance(refresh_token, dict):
390-
_data[RT] = rt_getter(refresh_token) # Put raw RT in _data
391392
resp = super(Client, self)._obtain_token(
392393
grant_type, params, _data, *args, **kwargs)
393394
if "error" not in resp:
@@ -416,31 +417,31 @@ def obtain_token_by_refresh_token(self, token_item, scope=None,
416417
on_removing_rt=None,
417418
**kwargs):
418419
# type: (Union[str, dict], Union[str, list, set, tuple], Callable) -> dict
419-
"""This is an "overload" which accepts a refresh token item as a dict,
420-
therefore this method can relay refresh_token item to event listeners.
420+
"""This is an overload which will trigger token storage callbacks.
421421
422422
:param token_item:
423-
A refresh token item as a dict, came from the cache managed by this lib.
423+
A refresh token (RT) item, in flexible format. It can be a string,
424+
or a whatever data structure containing RT string and its metadata,
425+
in such case the `rt_getter` callable must be able to
426+
extract the RT string out from the token item data structure.
427+
428+
Either way, this token_item will be passed into other callbacks as-is.
424429
425-
Alternatively, you can still use a refresh token (RT) as a string,
426-
supposedly came from a token cache managed by a different library,
427-
then this library will store the new RT (if Authority Server issued one)
428-
into this lib's cache. This is a way to migrate from other lib to us.
429430
:param scope: If omitted, is treated as equal to the scope originally
430431
granted by the resource ownser,
431432
according to https://tools.ietf.org/html/rfc6749#section-6
432-
:param rt_getter: A callable used to extract the RT from token_item
433+
:param rt_getter: A callable to translate the token_item to a raw RT string
433434
:param on_removing_rt: If absent, fall back to the one defined in initialization
434435
"""
435436
resp = super(Client, self).obtain_token_by_refresh_token(
436-
token_item, scope=scope,
437-
rt_getter=rt_getter, # Wire up this for _obtain_token()
437+
rt_getter(token_item)
438+
if not isinstance(token_item, string_types) else token_item,
439+
scope=scope,
438440
**kwargs)
439-
if isinstance(token_item, dict):
440-
if resp.get('error') == 'invalid_grant':
441-
(on_removing_rt or self.on_removing_rt)(token_item) # Discard old RT
442-
if 'refresh_token' in resp:
443-
self.on_updating_rt(token_item, resp['refresh_token'])
441+
if resp.get('error') == 'invalid_grant':
442+
(on_removing_rt or self.on_removing_rt)(token_item) # Discard old RT
443+
if 'refresh_token' in resp:
444+
self.on_updating_rt(token_item, resp['refresh_token'])
444445
return resp
445446

446447
def obtain_token_by_assertion(

0 commit comments

Comments
 (0)