|
6 | 6 |
|
7 | 7 | import msal |
8 | 8 | from msal.authority import * |
9 | | -from msal.authority import _CIAM_DOMAIN_SUFFIX, TRUSTED_ISSUER_HOSTS # Explicitly import private/new constants |
| 9 | +from msal.authority import _CIAM_DOMAIN_SUFFIX |
10 | 10 | from tests import unittest |
11 | 11 | from tests.http_client import MinimalHttpClient |
12 | 12 |
|
@@ -37,10 +37,90 @@ def _test_authority_builder(self, host, tenant): |
37 | 37 | c.close() |
38 | 38 |
|
39 | 39 | def test_wellknown_host_and_tenant(self): |
40 | | - # Assert all well known authority hosts are using their own "common" tenant |
| 40 | + # This test makes real HTTP calls to authority endpoints. |
| 41 | + # It is intentionally network-based to validate reachable hosts end-to-end. |
| 42 | + excluded_hosts = { |
| 43 | + DEPRECATED_AZURE_CHINA, |
| 44 | + "login.microsoftonline.de", # deprecated |
| 45 | + "login.microsoft.com", # issuer-only in this test context |
| 46 | + "login.windows.net", # issuer-only in this test context |
| 47 | + "sts.windows.net", # issuer-only in this test context |
| 48 | + "login.partner.microsoftonline.cn", # issuer-only in this test context |
| 49 | + "login.usgovcloudapi.net", # issuer-only in this test context |
| 50 | + AZURE_GOV_FR, # currently unreachable in this environment |
| 51 | + AZURE_GOV_DE, # currently unreachable in this environment |
| 52 | + AZURE_GOV_SG, # currently unreachable in this environment |
| 53 | + } |
| 54 | + for host in WELL_KNOWN_AUTHORITY_HOSTS: |
| 55 | + if host in excluded_hosts: |
| 56 | + continue |
| 57 | + self._test_given_host_and_tenant(host, "common") |
| 58 | + |
| 59 | + @patch("msal.authority._instance_discovery") |
| 60 | + @patch("msal.authority.tenant_discovery") |
| 61 | + def test_new_sovereign_hosts_should_build_authority_endpoints( |
| 62 | + self, tenant_discovery_mock, instance_discovery_mock): |
| 63 | + for host in WELL_KNOWN_AUTHORITY_HOSTS: |
| 64 | + tenant_discovery_mock.return_value = { |
| 65 | + "authorization_endpoint": "https://{}/common/oauth2/v2.0/authorize".format(host), |
| 66 | + "token_endpoint": "https://{}/common/oauth2/v2.0/token".format(host), |
| 67 | + "issuer": "https://{}/common/v2.0".format(host), |
| 68 | + } |
| 69 | + instance_discovery_mock.return_value = { |
| 70 | + "tenant_discovery_endpoint": ( |
| 71 | + "https://{}/common/v2.0/.well-known/openid-configuration".format(host) |
| 72 | + ), |
| 73 | + } |
| 74 | + c = MinimalHttpClient() |
| 75 | + a = Authority(AuthorityBuilder(host, "common"), c) |
| 76 | + self.assertEqual( |
| 77 | + a.authorization_endpoint, |
| 78 | + "https://{}/common/oauth2/v2.0/authorize".format(host)) |
| 79 | + self.assertEqual( |
| 80 | + a.token_endpoint, |
| 81 | + "https://{}/common/oauth2/v2.0/token".format(host)) |
| 82 | + c.close() |
| 83 | + |
| 84 | + @patch("msal.authority._instance_discovery") |
| 85 | + @patch("msal.authority.tenant_discovery") |
| 86 | + def test_known_authority_should_use_same_host_and_skip_instance_discovery( |
| 87 | + self, tenant_discovery_mock, instance_discovery_mock): |
41 | 88 | for host in WELL_KNOWN_AUTHORITY_HOSTS: |
42 | | - if host != AZURE_CHINA: # It is prone to ConnectionError |
43 | | - self._test_given_host_and_tenant(host, "common") |
| 89 | + tenant_discovery_mock.return_value = { |
| 90 | + "authorization_endpoint": "https://{}/common/oauth2/v2.0/authorize".format(host), |
| 91 | + "token_endpoint": "https://{}/common/oauth2/v2.0/token".format(host), |
| 92 | + "issuer": "https://{}/common/v2.0".format(host), |
| 93 | + } |
| 94 | + c = MinimalHttpClient() |
| 95 | + Authority("https://{}/common".format(host), c) |
| 96 | + c.close() |
| 97 | + |
| 98 | + instance_discovery_mock.assert_not_called() |
| 99 | + tenant_discovery_endpoint = tenant_discovery_mock.call_args[0][0] |
| 100 | + self.assertTrue( |
| 101 | + tenant_discovery_endpoint.startswith( |
| 102 | + "https://{}/common/v2.0/.well-known/openid-configuration".format(host))) |
| 103 | + |
| 104 | + @patch("msal.authority._instance_discovery") |
| 105 | + @patch("msal.authority.tenant_discovery") |
| 106 | + def test_unknown_authority_should_use_world_wide_instance_discovery_endpoint( |
| 107 | + self, tenant_discovery_mock, instance_discovery_mock): |
| 108 | + tenant_discovery_mock.return_value = { |
| 109 | + "authorization_endpoint": "https://example.com/tenant/oauth2/v2.0/authorize", |
| 110 | + "token_endpoint": "https://example.com/tenant/oauth2/v2.0/token", |
| 111 | + "issuer": "https://example.com/tenant/v2.0", |
| 112 | + } |
| 113 | + instance_discovery_mock.return_value = { |
| 114 | + "tenant_discovery_endpoint": "https://example.com/tenant/v2.0/.well-known/openid-configuration", |
| 115 | + } |
| 116 | + |
| 117 | + c = MinimalHttpClient() |
| 118 | + Authority("https://example.com/tenant", c) |
| 119 | + c.close() |
| 120 | + |
| 121 | + self.assertEqual( |
| 122 | + "https://{}/common/discovery/instance".format(WORLD_WIDE), |
| 123 | + instance_discovery_mock.call_args[0][2]) |
44 | 124 |
|
45 | 125 | def test_wellknown_host_and_tenant_using_new_authority_builder(self): |
46 | 126 | self._test_authority_builder(AZURE_PUBLIC, "consumers") |
@@ -276,7 +356,24 @@ def test_by_default_a_known_to_microsoft_authority_should_skip_validation_but_st |
276 | 356 | app = msal.ClientApplication("id", authority="https://login.microsoftonline.com/common") |
277 | 357 | known_to_microsoft_validation.assert_not_called() |
278 | 358 | app.get_accounts() # This could make an instance metadata call for authority aliases |
279 | | - instance_metadata.assert_called_once_with() |
| 359 | + instance_metadata.assert_called_once_with("login.microsoftonline.com") |
| 360 | + |
| 361 | + def test_by_default_a_sovereign_known_authority_should_use_cloud_local_instance_metadata( |
| 362 | + self, instance_metadata, known_to_microsoft_validation, _): |
| 363 | + app = msal.ClientApplication("id", authority="https://login.microsoftonline.us/common") |
| 364 | + known_to_microsoft_validation.assert_not_called() |
| 365 | + app.get_accounts() # This could make an instance metadata call for authority aliases |
| 366 | + instance_metadata.assert_called_once_with("login.microsoftonline.us") |
| 367 | + |
| 368 | + def test_fr_known_authority_should_still_work_when_instance_metadata_has_no_alias_entry( |
| 369 | + self, instance_metadata, known_to_microsoft_validation, _): |
| 370 | + app = msal.ClientApplication("id", authority="https://{}/common".format(AZURE_GOV_FR)) |
| 371 | + known_to_microsoft_validation.assert_not_called() |
| 372 | + |
| 373 | + accounts = app.get_accounts() |
| 374 | + |
| 375 | + self.assertEqual([], accounts) |
| 376 | + instance_metadata.assert_called_once_with(AZURE_GOV_FR) |
280 | 377 |
|
281 | 378 | def test_validate_authority_boolean_should_skip_validation_and_instance_metadata( |
282 | 379 | self, instance_metadata, known_to_microsoft_validation, _): |
|
0 commit comments