Skip to content

Commit

Permalink
Merge pull request googleapis#2 from O365/master
Browse files Browse the repository at this point in the history
update janscas changes
  • Loading branch information
riptusk331 authored Nov 12, 2019
2 parents 8f0d99a + efeedcc commit 1e0da3b
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions O365/utils/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
log = logging.getLogger(__name__)


EXPIRES_ON_THRESHOLD = 2 * 60 # 2 minutes
EXPIRES_ON_THRESHOLD = 1 * 60 # 1 minute


class Token(dict):
Expand Down Expand Up @@ -35,15 +35,33 @@ def expiration_datetime(self):
Returns the expiration datetime
:return datetime: The datetime this token expires
"""
expires_at = self.get('expires_at')
if expires_at is None:
# consider it is expired
return dt.datetime.now() - dt.timedelta(seconds=10)
expires_on = dt.datetime.fromtimestamp(expires_at) - dt.timedelta(seconds=EXPIRES_ON_THRESHOLD)
access_expires_at = self.access_expiration_datetime
expires_on = access_expires_at - dt.timedelta(seconds=EXPIRES_ON_THRESHOLD)
if self.is_long_lived:
expires_on = expires_on + dt.timedelta(days=90)
return expires_on

@property
def access_expiration_datetime(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:
# consider the token expired, add 10 second buffer to current dt
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_expiration_datetime


class BaseTokenBackend(ABC):
""" A base token storage class """
Expand Down Expand Up @@ -142,8 +160,8 @@ class FileSystemTokenBackend(BaseTokenBackend):
def __init__(self, token_path=None, token_filename=None):
"""
Init Backend
:param token_path str or Path: the path where to store the token
:param token_filename str: the name of the token file
:param str or Path token_path: the path where to store the token
:param str token_filename: the name of the token file
"""
super().__init__()
if not isinstance(token_path, Path):
Expand All @@ -154,6 +172,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 @@ -207,17 +228,20 @@ def check_token(self):
"""
return self.token_path.exists()

def should_refresh_token(self, con=None):
return not self.fs_wait


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

def __init__(self, client, collection, doc_id, field_name='token'):
"""
Init Backend
:param client firestore.Client: the firestore Client instance
:param collection str: the firestore collection where to store tokens (can be a field_path)
:param doc_id str: # the key of the token document. Must be unique per-case.
:param field_name: the name of the field that stores the token in the document
:param firestore.Client client: the firestore Client instance
:param str collection: the firestore collection where to store tokens (can be a field_path)
:param str doc_id: # the key of the token document. Must be unique per-case.
:param str field_name: the name of the field that stores the token in the document
"""
super().__init__()
self.client = client
Expand Down

0 comments on commit 1e0da3b

Please sign in to comment.