Skip to content

Commit

Permalink
add access expiration properties to token and implement should_refres…
Browse files Browse the repository at this point in the history
…h_token on fs backend
  • Loading branch information
riptusk331 committed Nov 10, 2019
1 parent e8f71cb commit 6c9acc6
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions O365/utils/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ def expiration_datetime(self):
expires_on = expires_on + dt.timedelta(days=90)
return expires_on

@property
def access_expiry(self):
"""
Returns the token's access expiration datetime
:return datetime: The datetime the token's access expires
"""
expires_at = self.get('expires_at')
if expires_at:
return dt.datetime.fromtimestamp(expires_at)
else:
return dt.datetime.now() - dt.timedelta(seconds=10)

@property
def is_access_expired(self):
"""
Returns whether or not the token's access is expired.
:return bool: True if the token's access is expired, False otherwise
"""
return dt.datetime.now() > self.access_expiry


class BaseTokenBackend(ABC):
""" A base token storage class """
Expand Down Expand Up @@ -146,6 +166,9 @@ def __init__(self, token_path=None, token_filename=None):
else:
token_filename = token_filename or 'o365_token.txt'
self.token_path = token_path / token_filename

# is this backend waiting on the filesystem
self.fs_wait = False

def __repr__(self):
return str(self.token_path)
Expand Down Expand Up @@ -199,6 +222,8 @@ def check_token(self):
"""
return self.token_path.exists()

def should_refresh_token(self):
return not self.fs_wait

class FirestoreBackend(BaseTokenBackend):
""" A Google Firestore database backend to store tokens """
Expand Down

0 comments on commit 6c9acc6

Please sign in to comment.