Skip to content

Commit

Permalink
Merge pull request capless#54 from yingzong/renew_optional
Browse files Browse the repository at this point in the history
Add 'renew' flag to .check_token method
  • Loading branch information
bjinwright authored Oct 8, 2017
2 parents a60424d + b0bc1b8 commit a93392b
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 @@ -240,21 +240,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 a93392b

Please sign in to comment.