Skip to content

Commit

Permalink
made sure new session is used when oauth is refreshed by another inst…
Browse files Browse the repository at this point in the history
…ance
  • Loading branch information
riptusk331 committed Nov 11, 2019
1 parent 6c9acc6 commit 07d2449
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions O365/connection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
import inspect
import os
import time

Expand Down Expand Up @@ -534,7 +535,7 @@ def get_session(self, *, state=None,

if load_token:
# gets a fresh token from the store
token = self.token_backend.get_token()
token = self.token_backend.load_token()
if token is None:
raise RuntimeError('No auth token found. Authentication Flow needed')

Expand Down Expand Up @@ -676,10 +677,13 @@ def _fs_token_lock(self):
f'retrying {_ - 1} more times.')
time.sleep(2)
log.debug('Waking up and rechecking token file for update'
'from other instance...')
self.token_backend.load_token()
' from other instance...')
self.token_backend.token = self.token_backend.load_token()
else:
self.token_backend.fs_wait = False
# don't change token state back from wait if we found the token
# was refreshed by another instance. this will be our signal
# to the calling method that we need to use a new session
self.session = self.get_session(load_token=True)
log.info('Token was refreshed by another instance...')
return True
return False
Expand Down Expand Up @@ -727,6 +731,7 @@ def _internal_request(self, request_obj, url, method, **kwargs):
request_done = True
return response
except TokenExpiredError as e:
log.info(inspect.stack())
# Token has expired, try to refresh the token and try again on the next loop
if self.token_backend.token.is_long_lived is False and self.auth_flow_type == 'authorization':
raise e
Expand All @@ -739,6 +744,12 @@ def _internal_request(self, request_obj, url, method, **kwargs):
# if token is on filesystem, ensure atomic operations in the event of concurrent token access
if isinstance(self.token_backend, FileSystemTokenBackend):
token_refreshed = self._fs_token_lock()
# if token was updated by another instance, we need to reload the session object
# that was passed to this method with the new token. we test for this by checking
# if there is still a wait state on the token_backend and token_refreshed is True
if self.token_backend.fs_wait and token_refreshed:
request_obj = self.session
self.token_backend.fs_wait = False
else:
if self.refresh_token() is False:
raise RuntimeError('Token Refresh Operation not working')
Expand Down Expand Up @@ -813,7 +824,7 @@ def oauth_request(self, url, method, **kwargs):
# oauth authentication
if self.session is None:
self.session = self.get_session(load_token=True)

return self._internal_request(self.session, url, method, **kwargs)

def get(self, url, params=None, **kwargs):
Expand Down

0 comments on commit 07d2449

Please sign in to comment.