Skip to content

Commit a255f89

Browse files
committed
Refactor reading authority aliases
1 parent 5a22736 commit a255f89

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

msal/application.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,7 @@ def __init__(
104104
# Here the self.authority is not the same type as authority in input
105105
self.token_cache = token_cache or TokenCache()
106106
self.client = self._build_client(client_credential, self.authority)
107-
self.authority_groups = self._get_authority_aliases()
108-
109-
def _get_authority_aliases(self):
110-
resp = requests.get(
111-
"https://login.microsoftonline.com/common/discovery/instance?api-version=1.1&authorization_endpoint=https://login.microsoftonline.com/common/oauth2/authorize",
112-
headers={'Accept': 'application/json'})
113-
resp.raise_for_status()
114-
return [set(group['aliases']) for group in resp.json()['metadata']]
107+
self.authority_groups = None
115108

116109
def _build_client(self, client_credential, authority):
117110
client_assertion = None
@@ -249,13 +242,10 @@ def get_accounts(self, username=None):
249242
"""
250243
accounts = self._find_msal_accounts(environment=self.authority.instance)
251244
if not accounts: # Now try other aliases of this authority instance
252-
for group in self.authority_groups:
253-
if self.authority.instance in group:
254-
for alias in group:
255-
if alias != self.authority.instance:
256-
accounts = self._find_msal_accounts(environment=alias)
257-
if accounts:
258-
break
245+
for alias in self._get_authority_aliases(self.authority.instance):
246+
accounts = self._find_msal_accounts(environment=alias)
247+
if accounts:
248+
break
259249
if username:
260250
# Federated account["username"] from AAD could contain mixed case
261251
lowercase_username = username.lower()
@@ -274,6 +264,19 @@ def _find_msal_accounts(self, environment):
274264
if a["authority_type"] in (
275265
TokenCache.AuthorityType.ADFS, TokenCache.AuthorityType.MSSTS)]
276266

267+
def _get_authority_aliases(self, instance):
268+
if not self.authority_groups:
269+
resp = requests.get(
270+
"https://login.microsoftonline.com/common/discovery/instance?api-version=1.1&authorization_endpoint=https://login.microsoftonline.com/common/oauth2/authorize",
271+
headers={'Accept': 'application/json'})
272+
resp.raise_for_status()
273+
self.authority_groups = [
274+
set(group['aliases']) for group in resp.json()['metadata']]
275+
for group in self.authority_groups:
276+
if instance in group:
277+
return [alias for alias in group if alias != instance]
278+
return []
279+
277280
def acquire_token_silent(
278281
self,
279282
scopes, # type: List[str]
@@ -309,19 +312,15 @@ def acquire_token_silent(
309312
result = self._acquire_token_silent(scopes, account, self.authority, **kwargs)
310313
if result:
311314
return result
312-
for group in self.authority_groups:
313-
if self.authority.instance in group:
314-
for alias in group:
315-
if alias != self.authority.instance:
316-
the_authority = Authority(
317-
"https://" + alias + "/" + self.authority.tenant,
318-
validate_authority=False,
319-
verify=self.verify, proxies=self.proxies,
320-
timeout=self.timeout,)
321-
result = self._acquire_token_silent(
322-
scopes, account, the_authority, **kwargs)
323-
if result:
324-
return result
315+
for alias in self._get_authority_aliases(self.authority.instance):
316+
the_authority = Authority(
317+
"https://" + alias + "/" + self.authority.tenant,
318+
validate_authority=False,
319+
verify=self.verify, proxies=self.proxies, timeout=self.timeout)
320+
result = self._acquire_token_silent(
321+
scopes, account, the_authority, **kwargs)
322+
if result:
323+
return result
325324

326325
def _acquire_token_silent(
327326
self,

0 commit comments

Comments
 (0)