Skip to content

Commit 7c238bd

Browse files
committed
first part of adapting request_token
Need to figure out how to build a valid Token object from the msal auth results
1 parent b861394 commit 7c238bd

File tree

1 file changed

+16
-68
lines changed

1 file changed

+16
-68
lines changed

O365/connection.py

Lines changed: 16 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -549,91 +549,39 @@ def get_authorization_url(self, requested_scopes=None,
549549
return flow.get('auth_uri'), flow
550550

551551
def request_token(self, authorization_url, *,
552-
state=None,
553-
redirect_uri=None,
554-
requested_scopes=None,
552+
flow=None,
555553
store_token=True,
556554
**kwargs):
557555
""" Authenticates for the specified url and gets the token, save the
558556
token for future based if requested
559557
560558
:param str or None authorization_url: url given by the authorization flow
561-
:param str state: session-state identifier for web-flows
562-
:param str redirect_uri: callback url for web-flows
563-
:param lst requested_scopes: a list of scopes to be requested.
564-
Only used when auth_flow_type is 'credentials'
565-
:param bool store_token: whether or not to store the token,
559+
:param str flow: dict object holding the data used in get_authorization_url
560+
:param bool store_token: True to store the token in the token backend
566561
so you don't have to keep opening the auth link and
567562
authenticating every time
568563
:param kwargs: allow to pass unused params in conjunction with Connection
569564
:return: Success/Failure
570565
:rtype: bool
571566
"""
572567

573-
redirect_uri = redirect_uri or self.oauth_redirect_url
574-
575-
# Allow token scope to not match requested scope.
576-
# (Other auth libraries allow this, but Requests-OAuthlib
577-
# raises exception on scope mismatch by default.)
578-
os.environ['OAUTHLIB_RELAX_TOKEN_SCOPE'] = '1'
579-
os.environ['OAUTHLIB_IGNORE_SCOPE_CHANGE'] = '1'
568+
# parse the authorization url to obtain the query string params
569+
parsed = urlparse(authorization_url)
570+
query_params_dict = {k: v[0] for k, v in parse_qs(parsed.query).items()}
580571

581-
scopes = requested_scopes or self.scopes
582-
583-
if self.session is None:
584-
if self.auth_flow_type in ('authorization', 'public'):
585-
self.session = self.get_session(state=state,
586-
redirect_uri=redirect_uri)
587-
elif self.auth_flow_type in ('credentials', 'certificate', 'password'):
588-
self.session = self.get_session(scopes=scopes)
589-
else:
590-
raise ValueError('"auth_flow_type" must be "authorization", "public", "credentials", "password",'
591-
' or "certificate"')
572+
result = self._msal_client.acquire_token_by_auth_code_flow(flow, auth_response=query_params_dict)
592573

593-
try:
594-
if self.auth_flow_type == 'authorization':
595-
self.token_backend.token = Token(self.session.fetch_token(
596-
token_url=self._oauth2_token_url,
597-
authorization_response=authorization_url,
598-
include_client_id=True,
599-
client_secret=self.auth[1],
600-
verify=self.verify_ssl))
601-
elif self.auth_flow_type == 'public':
602-
self.token_backend.token = Token(self.session.fetch_token(
603-
token_url=self._oauth2_token_url,
604-
authorization_response=authorization_url,
605-
include_client_id=True,
606-
verify=self.verify_ssl))
607-
elif self.auth_flow_type == 'credentials':
608-
self.token_backend.token = Token(self.session.fetch_token(
609-
token_url=self._oauth2_token_url,
610-
include_client_id=True,
611-
client_secret=self.auth[1],
612-
scope=scopes,
613-
verify=self.verify_ssl))
614-
elif self.auth_flow_type == 'password':
615-
self.token_backend.token = Token(self.session.fetch_token(
616-
token_url=self._oauth2_token_url,
617-
include_client_id=True,
618-
username=self.username,
619-
password=self.password,
620-
scope=scopes,
621-
verify=self.verify_ssl))
622-
elif self.auth_flow_type == 'certificate':
623-
self.token_backend.token = Token(self.session.fetch_token(
624-
token_url=self._oauth2_token_url,
625-
include_client_id=True,
626-
client_assertion=self.auth[1],
627-
client_assertion_type="urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
628-
scope=scopes,
629-
verify=self.verify_ssl))
630-
except Exception as e:
631-
log.error('Unable to fetch auth token. Error: {}'.format(str(e)))
574+
if "access_token" not in result:
575+
log.error('Unable to fetch auth token. Error: {}'.format(result.get("error")))
632576
return False
577+
else:
578+
access_token = result["access_token"]
579+
# TODO: retrieve token data from results and create a Token object with it
580+
# How to pass this Token object into msal again?
581+
if store_token:
582+
self.token_backend.save_token()
633583

634-
if store_token:
635-
self.token_backend.save_token()
636-
return True
584+
return True
637585

638586
def get_session(self, *, state=None,
639587
redirect_uri=None,

0 commit comments

Comments
 (0)