From b0bc1b8b69a25197fb007db35be98e0907217ccd Mon Sep 17 00:00:00 2001 From: Ying-zong Huang Date: Sat, 23 Sep 2017 23:40:40 -0400 Subject: [PATCH] Add 'renew' flag to .check_token method This allows users of the warrant client to create different workflows on expired tokens. Function doc comments also updated. --- warrant/__init__.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/warrant/__init__.py b/warrant/__init__.py index 8d1b1424..0852cee4 100644 --- a/warrant/__init__.py +++ b/warrant/__init__.py @@ -241,11 +241,12 @@ def switch_session(self,session): """ self.client = session.client('cognito-idp') - def check_token(self): + def check_token(self, renew=True): """ Checks the exp attribute of the access_token and either refreshes the tokens by calling the renew_access_tokens method or does nothing - :return: None + :param renew: bool indicating whether to refresh on expiration + :return: bool indicating whether access_token has expired """ if not self.access_token: raise AttributeError('Access Token Required to Check Token') @@ -253,9 +254,12 @@ def check_token(self): dec_access_token = jwt.get_unverified_claims(self.access_token) if now > datetime.datetime.fromtimestamp(dec_access_token['exp']): - self.renew_access_token() - return True - return False + expired = True + if renew: + self.renew_access_token() + else: + expired = False + return expired def register(self, username, password, attr_map=None, **kwargs): """