Skip to content

Commit 1f56396

Browse files
authored
Merge pull request #269 from AzureAD/differentiate-oidc-discovery-errors
Bubble http exceptions so apps could catch them
2 parents ece1fe1 + 84cb2cf commit 1f56396

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

msal/authority.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def __init__(self, authority_url, http_client, validate_authority=True):
8383
openid_config = tenant_discovery(
8484
tenant_discovery_endpoint,
8585
self.http_client)
86-
except ValueError: # json.decoder.JSONDecodeError in Py3 subclasses this
86+
except ValueError:
8787
raise ValueError(
8888
"Unable to get authority configuration for {}. "
8989
"Authority would typically be in a format of "
@@ -140,8 +140,17 @@ def instance_discovery(url, http_client, **kwargs):
140140
def tenant_discovery(tenant_discovery_endpoint, http_client, **kwargs):
141141
# Returns Openid Configuration
142142
resp = http_client.get(tenant_discovery_endpoint, **kwargs)
143-
payload = json.loads(resp.text)
144-
if 'authorization_endpoint' in payload and 'token_endpoint' in payload:
145-
return payload
146-
raise MsalServiceError(status_code=resp.status_code, **payload)
143+
if resp.status_code == 200:
144+
payload = json.loads(resp.text) # It could raise ValueError
145+
if 'authorization_endpoint' in payload and 'token_endpoint' in payload:
146+
return payload # Happy path
147+
raise ValueError("OIDC Discovery does not provide enough information")
148+
if 400 <= resp.status_code < 500:
149+
# Nonexist tenant would hit this path
150+
# e.g. https://login.microsoftonline.com/nonexist_tenant/v2.0/.well-known/openid-configuration
151+
raise ValueError("OIDC Discovery endpoint rejects our request")
152+
# Transient network error would hit this path
153+
resp.raise_for_status()
154+
raise RuntimeError( # A fallback here, in case resp.raise_for_status() is no-op
155+
"Unable to complete OIDC Discovery: %d, %s" % (resp.status_code, resp.text))
147156

0 commit comments

Comments
 (0)