Skip to content

Commit 3e2b56d

Browse files
authored
Merge pull request #394 from AzureAD/region-endpoint-specs-changes
Changing region endpoint, and remove usage of REGION_NAME env var
2 parents c687d5b + 5fdae2d commit 3e2b56d

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

msal/application.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ def _get_regional_authority(self, central_authority):
371371
self._region_configured if is_region_specified else self._region_detected)
372372
if region_to_use:
373373
logger.info('Region to be used: {}'.format(repr(region_to_use)))
374-
regional_host = ("{}.login.microsoft.com".format(region_to_use)
374+
regional_host = ("{}.r.login.microsoftonline.com".format(region_to_use)
375375
if central_authority.instance in (
376376
# The list came from https://github.com/AzureAD/microsoft-authentication-library-for-python/pull/358/files#r629400328
377377
"login.microsoftonline.com",

msal/region.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@
55

66

77
def _detect_region(http_client=None):
8-
region = _detect_region_of_azure_function() # It is cheap, so we do it always
9-
if http_client and not region:
8+
if http_client:
109
return _detect_region_of_azure_vm(http_client) # It could hang for minutes
11-
return region
12-
13-
14-
def _detect_region_of_azure_function():
15-
return os.environ.get("REGION_NAME")
10+
return None
1611

1712

1813
def _detect_region_of_azure_vm(http_client):

tests/test_e2e.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,15 @@ def assertCacheWorksForApp(self, result_from_wire, scope):
137137
def _test_username_password(self,
138138
authority=None, client_id=None, username=None, password=None, scope=None,
139139
client_secret=None, # Since MSAL 1.11, confidential client has ROPC too
140+
azure_region=None,
141+
http_client=None,
140142
**ignored):
141143
assert authority and client_id and username and password and scope
142144
self.app = msal.ClientApplication(
143-
client_id, authority=authority, http_client=MinimalHttpClient(),
145+
client_id, authority=authority,
146+
http_client=http_client or MinimalHttpClient(),
147+
azure_region=azure_region, # Regional endpoint does not support ROPC.
148+
# Here we just use it to test a regional app won't break ROPC.
144149
client_credential=client_secret)
145150
result = self.app.acquire_token_by_username_password(
146151
username, password, scopes=scope)
@@ -541,11 +546,16 @@ def _test_acquire_token_by_auth_code_flow(
541546
error_description=result.get("error_description")))
542547
self.assertCacheWorksForUser(result, scope, username=None)
543548

544-
def _test_acquire_token_obo(self, config_pca, config_cca):
549+
def _test_acquire_token_obo(self, config_pca, config_cca,
550+
azure_region=None, # Regional endpoint does not really support OBO.
551+
# Here we just test regional apps won't adversely break OBO
552+
http_client=None,
553+
):
545554
# 1. An app obtains a token representing a user, for our mid-tier service
546555
pca = msal.PublicClientApplication(
547556
config_pca["client_id"], authority=config_pca["authority"],
548-
http_client=MinimalHttpClient())
557+
azure_region=azure_region,
558+
http_client=http_client or MinimalHttpClient())
549559
pca_result = pca.acquire_token_by_username_password(
550560
config_pca["username"],
551561
config_pca["password"],
@@ -560,7 +570,8 @@ def _test_acquire_token_obo(self, config_pca, config_cca):
560570
config_cca["client_id"],
561571
client_credential=config_cca["client_secret"],
562572
authority=config_cca["authority"],
563-
http_client=MinimalHttpClient(),
573+
azure_region=azure_region,
574+
http_client=http_client or MinimalHttpClient(),
564575
# token_cache= ..., # Default token cache is all-tokens-store-in-memory.
565576
# That's fine if OBO app uses short-lived msal instance per session.
566577
# Otherwise, the OBO app need to implement a one-cache-per-user setup.
@@ -778,6 +789,7 @@ def test_b2c_acquire_token_by_ropc(self):
778789

779790
class WorldWideRegionalEndpointTestCase(LabBasedTestCase):
780791
region = "westus"
792+
timeout = 2 # Short timeout makes this test case responsive on non-VM
781793

782794
def test_acquire_token_for_client_should_hit_regional_endpoint(self):
783795
"""This is the only grant supported by regional endpoint, for now"""
@@ -798,7 +810,7 @@ def test_acquire_token_for_client_should_hit_regional_endpoint(self):
798810
status_code=400, text='{"error": "mock"}')) as mocked_method:
799811
self.app.acquire_token_for_client(scopes)
800812
mocked_method.assert_called_with(
801-
'https://westus.login.microsoft.com/{}/oauth2/v2.0/token'.format(
813+
'https://westus.r.login.microsoftonline.com/{}/oauth2/v2.0/token'.format(
802814
self.app.authority.tenant),
803815
params=ANY, data=ANY, headers=ANY)
804816
result = self.app.acquire_token_for_client(
@@ -808,15 +820,6 @@ def test_acquire_token_for_client_should_hit_regional_endpoint(self):
808820
self.assertIn('access_token', result)
809821
self.assertCacheWorksForApp(result, scopes)
810822

811-
812-
class RegionalEndpointViaEnvVarTestCase(WorldWideRegionalEndpointTestCase):
813-
814-
def setUp(self):
815-
os.environ["REGION_NAME"] = "eastus"
816-
817-
def tearDown(self):
818-
del os.environ["REGION_NAME"]
819-
820823
@unittest.skipUnless(
821824
os.getenv("LAB_OBO_CLIENT_SECRET"),
822825
"Need LAB_OBO_CLIENT_SECRET from https://aka.ms/GetLabSecret?Secret=TodoListServiceV2-OBO")
@@ -842,7 +845,11 @@ def test_cca_obo_should_bypass_regional_endpoint_therefore_still_work(self):
842845
config_pca["password"] = self.get_lab_user_secret(config_pca["lab_name"])
843846
config_pca["scope"] = ["api://%s/read" % config_cca["client_id"]]
844847

845-
self._test_acquire_token_obo(config_pca, config_cca)
848+
self._test_acquire_token_obo(
849+
config_pca, config_cca,
850+
azure_region=self.region,
851+
http_client=MinimalHttpClient(timeout=self.timeout),
852+
)
846853

847854
@unittest.skipUnless(
848855
os.getenv("LAB_OBO_CLIENT_SECRET"),
@@ -859,7 +866,10 @@ def test_cca_ropc_should_bypass_regional_endpoint_therefore_still_work(self):
859866
config["client_id"] = os.getenv("LAB_OBO_CONFIDENTIAL_CLIENT_ID")
860867
config["scope"] = ["https://graph.microsoft.com/.default"]
861868
config["client_secret"] = os.getenv("LAB_OBO_CLIENT_SECRET")
862-
self._test_username_password(**config)
869+
self._test_username_password(
870+
azure_region=self.region,
871+
http_client=MinimalHttpClient(timeout=self.timeout),
872+
**config)
863873

864874

865875
class ArlingtonCloudTestCase(LabBasedTestCase):

0 commit comments

Comments
 (0)