Skip to content

Commit

Permalink
Fix bug on auto refresh on client credentials flow googleapis#294
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Casanovas committed Sep 4, 2019
1 parent f3900e1 commit a93d32a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
3 changes: 2 additions & 1 deletion O365/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ def authenticate(self, *, scopes=None, **kwargs):
:return: Success / Failure
:rtype: bool
"""
kwargs.setdefault('token_backend', self.con.token_backend)

if self.con.auth_flow_type == 'web':
scopes = scopes or self.con.scopes
# TODO: set connection defaults.
kwargs.setdefault('token_backend', self.con.token_backend)
return oauth_authentication_flow(*self.con.auth, scopes=scopes,
protocol=self.protocol, **kwargs)
elif self.con.auth_flow_type == 'backend':
Expand Down
11 changes: 7 additions & 4 deletions O365/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,12 @@ def get_session(self, *, state=None,
raise RuntimeError('No auth token found. Authentication Flow needed')

oauth_client.token = token
if self.auth_flow_type == 'web':
requested_scopes = None # the scopes are already in the token (Not if type is backend)
session = OAuth2Session(client_id=client_id,
client=oauth_client,
token=token)
token=token,
scope=requested_scopes)
else:
session = OAuth2Session(client_id=client_id,
client=oauth_client,
Expand Down Expand Up @@ -607,8 +610,7 @@ def _internal_request(self, request_obj, url, method, **kwargs):
log.info('Requesting ({}) URL: {}'.format(method.upper(), url))
log.info('Request parameters: {}'.format(kwargs))
# auto_retry will occur inside this function call if enabled
response = request_obj.request(method, url,
**kwargs)
response = request_obj.request(method, url, **kwargs)
response.raise_for_status() # raise 4XX and 5XX error codes.
log.info('Received response ({}) from URL {}'.format(
response.status_code, response.url))
Expand All @@ -622,7 +624,8 @@ def _internal_request(self, request_obj, url, method, **kwargs):
# Refresh token done but still TokenExpiredError raise
raise RuntimeError('Token Refresh Operation not working')
log.info('Oauth Token is expired, fetching a new token')
self.refresh_token()
if self.refresh_token() is False:
raise RuntimeError('Token Refresh Operation not working')
log.info('New oauth token fetched')
token_refreshed = True
except (ConnectionError, ProxyError, SSLError, Timeout) as e:
Expand Down

0 comments on commit a93d32a

Please sign in to comment.