Skip to content

Commit 02013cd

Browse files
committed
implemented flow into account.
TokenBackend now inherits from msal TokenCache removed TokenBackend get_token method and token property
1 parent 6d74e0c commit 02013cd

File tree

4 files changed

+195
-165
lines changed

4 files changed

+195
-165
lines changed

O365/account.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ def authenticate(self, *, scopes: Optional[list] = None,
103103
if self.con.scopes is None:
104104
raise ValueError('The scopes are not set. Define the scopes requested.')
105105

106-
consent_url, _ = self.con.get_authorization_url(**kwargs)
106+
consent_url, flow = self.con.get_authorization_url(**kwargs)
107107

108108
token_url = handle_consent(consent_url)
109109

110110
if token_url:
111-
result = self.con.request_token(token_url, **kwargs) # no need to pass state as the session is the same
111+
result = self.con.request_token(token_url, flow=flow, **kwargs)
112112
if result:
113113
print('Authentication Flow Completed. Oauth Access Token Stored. You can now use the API.')
114114
else:

O365/connection.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -487,15 +487,16 @@ def set_proxy(self, proxy_server, proxy_port, proxy_username,
487487
@property
488488
def msal_client(self):
489489
""" Returns the msal client or creates it if it's not already done """
490-
if self.auth_flow_type == 'public':
491-
client = PublicClientApplication(client_id=self.auth[0], authority=self._msal_authority)
492-
elif self.auth_flow_type in ('authorization', 'credentials'):
493-
client = ConfidentialClientApplication(client_id=self.auth[0], client_credential=self.auth[1],
494-
authority=self._msal_authority)
495-
else:
496-
raise ValueError('"auth_flow_type" must be "authorization", "public" or "credentials"')
497-
self._msal_client = client
498-
return client
490+
if self._msal_client is None:
491+
if self.auth_flow_type == 'public':
492+
client = PublicClientApplication(client_id=self.auth[0], authority=self._msal_authority)
493+
elif self.auth_flow_type in ('authorization', 'credentials'):
494+
client = ConfidentialClientApplication(client_id=self.auth[0], client_credential=self.auth[1],
495+
authority=self._msal_authority)
496+
else:
497+
raise ValueError('"auth_flow_type" must be "authorization", "public" or "credentials"')
498+
self._msal_client = client
499+
return self._msal_client
499500

500501
def get_token_with_msal_simple(self, requested_scopes=None):
501502
""" Gets the token using"""
@@ -544,7 +545,7 @@ def get_authorization_url(self, requested_scopes=None,
544545
if not scopes:
545546
raise ValueError('Must provide at least one scope')
546547

547-
flow = self._msal_client.initiate_auth_code_flow(scopes=scopes, redirect_uri=redirect_uri)
548+
flow = self.msal_client.initiate_auth_code_flow(scopes=scopes, redirect_uri=redirect_uri)
548549

549550
return flow.get('auth_uri'), flow
550551

@@ -569,17 +570,17 @@ def request_token(self, authorization_url, *,
569570
parsed = urlparse(authorization_url)
570571
query_params_dict = {k: v[0] for k, v in parse_qs(parsed.query).items()}
571572

572-
result = self._msal_client.acquire_token_by_auth_code_flow(flow, auth_response=query_params_dict)
573-
573+
result = self.msal_client.acquire_token_by_auth_code_flow(flow, auth_response=query_params_dict)
574+
print(result)
574575
if "access_token" not in result:
575576
log.error('Unable to fetch auth token. Error: {}'.format(result.get("error")))
576577
return False
577578
else:
578579
access_token = result["access_token"]
579580
# TODO: retrieve token data from results and create a Token object with it
580581
# How to pass this Token object into msal again?
581-
if store_token:
582-
self.token_backend.save_token()
582+
# if store_token:
583+
# self.token_backend.save_token()
583584

584585
return True
585586

0 commit comments

Comments
 (0)