|
| 1 | +import json |
| 2 | + |
| 3 | +from tests import unittest |
| 4 | +from tests.http_client import RecordingHttpClient, MinimalResponse |
| 5 | +from msal.application import ConfidentialClientApplication |
| 6 | + |
| 7 | + |
| 8 | +class TestSovereignAuthorityForClientCredentialWithRecordingHttpClient(unittest.TestCase): |
| 9 | + def test_acquire_token_for_client_on_gov_fr_should_keep_calls_on_same_host(self): |
| 10 | + host = "login.sovcloud-identity.fr" |
| 11 | + expected_instance_discovery_url = "https://{}/common/discovery/instance".format(host) |
| 12 | + expected_instance_discovery_params = { |
| 13 | + "api-version": "1.1", |
| 14 | + "authorization_endpoint": ( |
| 15 | + "https://{}/common/oauth2/authorize".format(host) |
| 16 | + ), |
| 17 | + } |
| 18 | + |
| 19 | + http_client = RecordingHttpClient() |
| 20 | + |
| 21 | + def is_oidc_discovery(call): |
| 22 | + return call["url"].startswith( |
| 23 | + "https://{}/common/v2.0/.well-known/openid-configuration".format(host)) |
| 24 | + |
| 25 | + def oidc_discovery_response(_call): |
| 26 | + return MinimalResponse(status_code=200, text=json.dumps({ |
| 27 | + "authorization_endpoint": "https://{}/common/oauth2/v2.0/authorize".format(host), |
| 28 | + "token_endpoint": "https://{}/common/oauth2/v2.0/token".format(host), |
| 29 | + "issuer": "https://{}/common/v2.0".format(host), |
| 30 | + })) |
| 31 | + |
| 32 | + def is_instance_discovery(call): |
| 33 | + return ( |
| 34 | + call["url"] == expected_instance_discovery_url |
| 35 | + and call["params"] == expected_instance_discovery_params |
| 36 | + ) |
| 37 | + |
| 38 | + def instance_discovery_response(_call): |
| 39 | + return MinimalResponse(status_code=200, text=json.dumps({ |
| 40 | + "tenant_discovery_endpoint": ( |
| 41 | + "https://login.microsoftonline.us/" |
| 42 | + "cab8a31a-1906-4287-a0d8-4eef66b95f6e/" |
| 43 | + "v2.0/.well-known/openid-configuration" |
| 44 | + ), |
| 45 | + "api-version": "1.1", |
| 46 | + "metadata": [ |
| 47 | + { |
| 48 | + "preferred_network": "login.microsoftonline.com", |
| 49 | + "preferred_cache": "login.windows.net", |
| 50 | + "aliases": [ |
| 51 | + "login.microsoftonline.com", |
| 52 | + "login.windows.net", |
| 53 | + "login.microsoft.com", |
| 54 | + "sts.windows.net", |
| 55 | + ], |
| 56 | + }, |
| 57 | + { |
| 58 | + "preferred_network": "login.partner.microsoftonline.cn", |
| 59 | + "preferred_cache": "login.partner.microsoftonline.cn", |
| 60 | + "aliases": [ |
| 61 | + "login.partner.microsoftonline.cn", |
| 62 | + "login.chinacloudapi.cn", |
| 63 | + ], |
| 64 | + }, |
| 65 | + { |
| 66 | + "preferred_network": "login.microsoftonline.de", |
| 67 | + "preferred_cache": "login.microsoftonline.de", |
| 68 | + "aliases": ["login.microsoftonline.de"], |
| 69 | + }, |
| 70 | + { |
| 71 | + "preferred_network": "login.microsoftonline.us", |
| 72 | + "preferred_cache": "login.microsoftonline.us", |
| 73 | + "aliases": [ |
| 74 | + "login.microsoftonline.us", |
| 75 | + "login.usgovcloudapi.net", |
| 76 | + ], |
| 77 | + }, |
| 78 | + { |
| 79 | + "preferred_network": "login-us.microsoftonline.com", |
| 80 | + "preferred_cache": "login-us.microsoftonline.com", |
| 81 | + "aliases": ["login-us.microsoftonline.com"], |
| 82 | + }, |
| 83 | + ], |
| 84 | + })) |
| 85 | + |
| 86 | + token_counter = {"value": 0} |
| 87 | + |
| 88 | + def is_token_call(call): |
| 89 | + return call["url"].startswith("https://{}/common/oauth2/v2.0/token".format(host)) |
| 90 | + |
| 91 | + def token_response(_call): |
| 92 | + token_counter["value"] += 1 |
| 93 | + return MinimalResponse(status_code=200, text=json.dumps({ |
| 94 | + "access_token": "AT_{}".format(token_counter["value"]), |
| 95 | + "expires_in": 3600, |
| 96 | + })) |
| 97 | + |
| 98 | + http_client.add_get_route(is_oidc_discovery, oidc_discovery_response) |
| 99 | + http_client.add_get_route(is_instance_discovery, instance_discovery_response) |
| 100 | + http_client.add_post_route(is_token_call, token_response) |
| 101 | + |
| 102 | + app = ConfidentialClientApplication( |
| 103 | + "client_id", |
| 104 | + client_credential="secret", |
| 105 | + authority="https://{}/common".format(host), |
| 106 | + http_client=http_client, |
| 107 | + ) |
| 108 | + |
| 109 | + result1 = app.acquire_token_for_client(["scope1"]) |
| 110 | + self.assertEqual("AT_1", result1.get("access_token")) |
| 111 | + |
| 112 | + get_calls_after_first = list(http_client.get_calls) |
| 113 | + post_calls_after_first = list(http_client.post_calls) |
| 114 | + |
| 115 | + result2 = app.acquire_token_for_client(["scope2"]) |
| 116 | + self.assertEqual("AT_2", result2.get("access_token")) |
| 117 | + |
| 118 | + post_count_after_scope2 = len(http_client.post_calls) |
| 119 | + get_count_after_scope2 = len(http_client.get_calls) |
| 120 | + |
| 121 | + cached_result1 = app.acquire_token_for_client(["scope1"]) |
| 122 | + self.assertEqual("AT_1", cached_result1.get("access_token")) |
| 123 | + |
| 124 | + cached_result2 = app.acquire_token_for_client(["scope2"]) |
| 125 | + self.assertEqual("AT_2", cached_result2.get("access_token")) |
| 126 | + |
| 127 | + cached_result3 = app.acquire_token_for_client(["scope1"]) |
| 128 | + self.assertEqual("AT_1", cached_result3.get("access_token")) |
| 129 | + |
| 130 | + self.assertEqual( |
| 131 | + post_count_after_scope2, |
| 132 | + len(http_client.post_calls), |
| 133 | + "Subsequent same-scope calls should be served from cache without token POST") |
| 134 | + self.assertEqual( |
| 135 | + get_count_after_scope2, |
| 136 | + len(http_client.get_calls), |
| 137 | + "Subsequent same-authority calls should not trigger additional discovery GET") |
| 138 | + |
| 139 | + self.assertEqual(1, len(get_calls_after_first), "First acquire should trigger one discovery GET") |
| 140 | + self.assertTrue( |
| 141 | + get_calls_after_first[0]["url"].startswith( |
| 142 | + "https://{}/common/v2.0/.well-known/openid-configuration".format(host))) |
| 143 | + |
| 144 | + self.assertEqual(1, len(post_calls_after_first), "First acquire should trigger one token POST") |
| 145 | + self.assertTrue( |
| 146 | + post_calls_after_first[0]["url"].startswith("https://{}/common/oauth2/v2.0/token".format(host))) |
| 147 | + |
| 148 | + self.assertEqual(1, len(http_client.get_calls), "Second acquire on same authority should not re-discover") |
| 149 | + self.assertEqual(2, len(http_client.post_calls), "Second acquire with a different scope should request another token") |
| 150 | + self.assertTrue( |
| 151 | + http_client.post_calls[1]["url"].startswith("https://{}/common/oauth2/v2.0/token".format(host))) |
| 152 | + |
| 153 | + all_urls = [c["url"] for c in http_client.get_calls + http_client.post_calls] |
| 154 | + self.assertTrue(all("login.microsoftonline.com" not in url for url in all_urls)) |
| 155 | + self.assertTrue(all("https://{}/".format(host) in url for url in all_urls)) |
0 commit comments