Skip to content

Commit dac051a

Browse files
committed
Add method-level on_removing_rt trigger
1 parent 699b2dc commit dac051a

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

oauth2cli/oauth2.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,16 +396,18 @@ def _obtain_token(self, grant_type, params=None, data=None, *args, **kwargs):
396396

397397
def obtain_token_by_refresh_token(self, token_item, scope=None,
398398
rt_getter=lambda token_item: token_item["refresh_token"],
399+
on_removing_rt=None,
399400
**kwargs):
400401
# type: (Union[str, dict], Union[str, list, set, tuple], Callable) -> dict
401402
"""This is an "overload" which accepts a refresh token item as a dict,
402403
therefore this method can relay refresh_token item to event listeners.
403404
404-
:param refresh_token_item: A refresh token item came from storage
405+
:param token_item: A refresh token item came from storage
405406
:param scope: If omitted, is treated as equal to the scope originally
406407
granted by the resource ownser,
407408
according to https://tools.ietf.org/html/rfc6749#section-6
408409
:param rt_getter: A callable used to extract the RT from token_item
410+
:param on_removing_rt: If absent, fall back to the one defined in initialization
409411
"""
410412
if isinstance(token_item, str):
411413
# Satisfy the L of SOLID, although we expect caller uses a dict
@@ -415,7 +417,7 @@ def obtain_token_by_refresh_token(self, token_item, scope=None,
415417
resp = super(Client, self).obtain_token_by_refresh_token(
416418
rt_getter(token_item), scope=scope, **kwargs)
417419
if resp.get('error') == 'invalid_grant':
418-
self.on_removing_rt(token_item) # Discard old RT
420+
(on_removing_rt or self.on_removing_rt)(token_item) # Discard old RT
419421
if 'refresh_token' in resp:
420422
self.on_updating_rt(token_item, resp['refresh_token'])
421423
return resp

0 commit comments

Comments
 (0)