Skip to content

Commit

Permalink
Add 'renew' flag to .check_token method
Browse files Browse the repository at this point in the history
This allows users of the warrant client to create different
workflows on expired tokens.

Function doc comments also updated.
  • Loading branch information
Ying-zong Huang committed Sep 24, 2017
1 parent 3d6aa8c commit b0bc1b8
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions warrant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,21 +241,25 @@ 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')
now = datetime.datetime.now()
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):
"""
Expand Down

0 comments on commit b0bc1b8

Please sign in to comment.