Skip to content

Commit 2b697fc

Browse files
committed
Remove check on known hosts and adjust error message towards regular authority API
1 parent 7af3828 commit 2b697fc

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

msal/authority.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ def __init__(
108108
"The issuer '{iss}' does not match the authority '{auth}' or a known pattern. "
109109
"When using the 'oidc_authority' parameter in ClientApplication, the authority "
110110
"will be validated against the issuer from {auth}/.well-known/openid-configuration ."
111+
"If using a known Entra authority (e.g. login.microsoftonline.com) the "
112+
"'authority' parameter should be used instead of 'oidc_authority'. "
113+
""
111114
).format(iss=self._issuer, auth=oidc_authority_url))
112115
def _initialize_oidc_authority(self, oidc_authority_url):
113116
authority, self.instance, tenant = canonicalize(oidc_authority_url)
@@ -189,7 +192,6 @@ def has_valid_issuer(self) -> bool:
189192
190193
An issuer is valid if one of the following is true:
191194
- It exactly matches the authority URL
192-
- It has a known Microsoft host (e.g., login.microsoftonline.com)
193195
- It has the same scheme and host as the authority (path can be different)
194196
- For CIAM, the issuer follows the pattern of {tenant}.ciamlogin.com (tenant comes from the authority)
195197
"""
@@ -201,10 +203,6 @@ def has_valid_issuer(self) -> bool:
201203
if not issuer:
202204
return False
203205

204-
# Check if issuer has a known Microsoft host
205-
if issuer.hostname in WELL_KNOWN_AUTHORITY_HOSTS:
206-
return True
207-
208206
# Check if issuer has the same scheme and host as the authority
209207
authority = urlparse(self._oidc_authority_url)
210208
if authority.scheme == issuer.scheme and authority.netloc == issuer.netloc:

tests/test_authority.py

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -328,14 +328,6 @@ def test_no_issuer(self, tenant_discovery_mock):
328328
Authority(None, self.http_client, oidc_authority_url=authority_url)
329329
self.assertIn("issuer", str(context.exception).lower())
330330

331-
@patch("msal.authority.tenant_discovery")
332-
def test_microsoft_host_issuer(self, tenant_discovery_mock):
333-
"""Test when issuer has a known Microsoft host"""
334-
authority_url = "https://custom-domain.com/tenant"
335-
issuer = f"https://{WORLD_WIDE}/tenant"
336-
authority = self._create_authority_with_issuer(authority_url, issuer, tenant_discovery_mock)
337-
self.assertTrue(authority.has_valid_issuer(), "Issuer should be valid when it has a known Microsoft host")
338-
339331
@patch("msal.authority.tenant_discovery")
340332
def test_same_scheme_and_host_different_path(self, tenant_discovery_mock):
341333
"""Test when issuer has same scheme and host but different path"""
@@ -377,3 +369,46 @@ def test_invalid_issuer(self, tenant_discovery_mock):
377369
self.assertIn("issuer", str(context.exception).lower())
378370
self.assertIn(issuer, str(context.exception))
379371
self.assertIn(authority_url, str(context.exception))
372+
373+
@patch("msal.authority.tenant_discovery")
374+
def test_custom_authority_with_microsoft_issuer(self, tenant_discovery_mock):
375+
"""Test when custom authority is used with a known Microsoft issuer (should fail)"""
376+
authority_url = "https://custom-domain.com/tenant"
377+
issuer = f"https://{WORLD_WIDE}/tenant"
378+
379+
tenant_discovery_mock.return_value = {
380+
"authorization_endpoint": "https://example.com/oauth2/authorize",
381+
"token_endpoint": "https://example.com/oauth2/token",
382+
"issuer": issuer,
383+
}
384+
385+
# Since initialization now checks for valid issuer and we removed the check for known hosts,
386+
# we expect it to raise ValueError because the hosts don't match
387+
with self.assertRaises(ValueError) as context:
388+
Authority(None, self.http_client, oidc_authority_url=authority_url)
389+
390+
self.assertIn("issuer", str(context.exception).lower())
391+
self.assertIn(issuer, str(context.exception))
392+
self.assertIn(authority_url, str(context.exception))
393+
394+
@patch("msal.authority.tenant_discovery")
395+
def test_known_authority_with_non_matching_issuer(self, tenant_discovery_mock):
396+
"""Test when known authority is used with an issuer that doesn't match (should fail)"""
397+
# Known Microsoft authority URLs
398+
authority_url = f"https://{WORLD_WIDE}/tenant"
399+
issuer = "https://custom-domain.com/tenant"
400+
401+
tenant_discovery_mock.return_value = {
402+
"authorization_endpoint": "https://example.com/oauth2/authorize",
403+
"token_endpoint": "https://example.com/oauth2/token",
404+
"issuer": issuer,
405+
}
406+
407+
# We expect it to raise ValueError because the paths don't match
408+
# and we're now checking for exact matches
409+
with self.assertRaises(ValueError) as context:
410+
Authority(None, self.http_client, oidc_authority_url=authority_url)
411+
412+
self.assertIn("issuer", str(context.exception).lower())
413+
self.assertIn(issuer, str(context.exception))
414+
self.assertIn(authority_url, str(context.exception))

0 commit comments

Comments
 (0)