Skip to content

Commit e613b25

Browse files
committed
Refactor: instance_discovery() emits actionable msg
It will guide app developer to use validate_authority=False when needed.
1 parent 4550793 commit e613b25

File tree

2 files changed

+17
-28
lines changed

2 files changed

+17
-28
lines changed

msal/authority.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,18 @@ def __init__(self, authority_url, validate_authority=True,
4646
))
4747
if (tenant != "adfs" and validate_authority
4848
and self.instance not in WELL_KNOWN_AUTHORITY_HOSTS):
49-
tenant_discovery_endpoint = instance_discovery(
49+
payload = instance_discovery(
5050
canonicalized + "/oauth2/v2.0/authorize",
5151
verify=verify, proxies=proxies, timeout=timeout)
52+
if payload.get("error") == "invalid_instance":
53+
raise ValueError(
54+
"invalid_instance: "
55+
"The authority you provided, %s, is not whitelisted. "
56+
"If it is indeed your legit customized domain name, "
57+
"you can turn off this check by passing in "
58+
"validate_authority=False"
59+
% authority_url)
60+
tenant_discovery_endpoint = payload['tenant_discovery_endpoint']
5261
openid_config = tenant_discovery(
5362
tenant_discovery_endpoint,
5463
verify=verify, proxies=proxies, timeout=timeout)
@@ -80,20 +89,15 @@ def canonicalize(url):
8089
"https://login.microsoftonline.com/<tenant_name>" % url)
8190
return match_object.group(0), match_object.group(1), match_object.group(2)
8291

83-
def instance_discovery(url, response=None, **kwargs):
84-
# Returns tenant discovery endpoint
85-
resp = requests.get( # Note: This URL seemingly returns V1 endpoint only
92+
def instance_discovery(url, **kwargs):
93+
return requests.get( # Note: This URL seemingly returns V1 endpoint only
8694
'https://{}/common/discovery/instance'.format(
8795
WORLD_WIDE # Historically using WORLD_WIDE. Could use self.instance too
8896
# See https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/blob/4.0.0/src/Microsoft.Identity.Client/Instance/AadInstanceDiscovery.cs#L101-L103
8997
# and https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/blob/4.0.0/src/Microsoft.Identity.Client/Instance/AadAuthority.cs#L19-L33
9098
),
9199
params={'authorization_endpoint': url, 'api-version': '1.0'},
92-
**kwargs)
93-
payload = response or resp.json()
94-
if 'tenant_discovery_endpoint' not in payload:
95-
raise MsalServiceError(status_code=resp.status_code, **payload)
96-
return payload['tenant_discovery_endpoint']
100+
**kwargs).json()
97101

98102
def tenant_discovery(tenant_discovery_endpoint, **kwargs):
99103
# Returns Openid Configuration

tests/test_authority.py

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import os
2+
13
from msal.authority import *
24
from msal.exceptions import MsalServiceError
35
from tests import unittest
46

57

8+
@unittest.skipIf(os.getenv("TRAVIS_TAG"), "Skip network io during tagged release")
69
class TestAuthority(unittest.TestCase):
710

811
def test_wellknown_host_and_tenant(self):
@@ -26,7 +29,7 @@ def test_lessknown_host_will_return_a_set_of_v1_endpoints(self):
2629
self.assertNotIn('v2.0', a.token_endpoint)
2730

2831
def test_unknown_host_wont_pass_instance_discovery(self):
29-
with self.assertRaisesRegexp(MsalServiceError, "invalid_instance"):
32+
with self.assertRaisesRegexp(ValueError, "invalid_instance"):
3033
Authority('https://unknown.host/tenant_doesnt_matter_in_this_case')
3134

3235
def test_invalid_host_skipping_validation_meets_connection_error_down_the_road(self):
@@ -63,21 +66,3 @@ def test_canonicalize_rejects_tenantless_host_with_trailing_slash(self):
6366
with self.assertRaises(ValueError):
6467
canonicalize("https://no.tenant.example.com/")
6568

66-
67-
class TestAuthorityInternalHelperInstanceDiscovery(unittest.TestCase):
68-
69-
def test_instance_discovery_happy_case(self):
70-
self.assertEqual(
71-
instance_discovery("https://login.windows.net/tenant"),
72-
"https://login.windows.net/tenant/.well-known/openid-configuration")
73-
74-
def test_instance_discovery_with_unknown_instance(self):
75-
with self.assertRaisesRegexp(MsalServiceError, "invalid_instance"):
76-
instance_discovery('https://unknown.host/tenant_doesnt_matter_here')
77-
78-
def test_instance_discovery_with_mocked_response(self):
79-
mock_response = {'tenant_discovery_endpoint': 'http://a.com/t/openid'}
80-
endpoint = instance_discovery(
81-
"https://login.microsoftonline.in/tenant.com", response=mock_response)
82-
self.assertEqual(endpoint, mock_response['tenant_discovery_endpoint'])
83-

0 commit comments

Comments
 (0)