Skip to content

Commit

Permalink
Merge pull request #269 from AzureAD/differentiate-oidc-discovery-errors
Browse files Browse the repository at this point in the history
Bubble http exceptions so apps could catch them
  • Loading branch information
rayluo authored Oct 30, 2020
2 parents ece1fe1 + 84cb2cf commit 1f56396
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions msal/authority.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def __init__(self, authority_url, http_client, validate_authority=True):
openid_config = tenant_discovery(
tenant_discovery_endpoint,
self.http_client)
except ValueError: # json.decoder.JSONDecodeError in Py3 subclasses this
except ValueError:
raise ValueError(
"Unable to get authority configuration for {}. "
"Authority would typically be in a format of "
Expand Down Expand Up @@ -140,8 +140,17 @@ def instance_discovery(url, http_client, **kwargs):
def tenant_discovery(tenant_discovery_endpoint, http_client, **kwargs):
# Returns Openid Configuration
resp = http_client.get(tenant_discovery_endpoint, **kwargs)
payload = json.loads(resp.text)
if 'authorization_endpoint' in payload and 'token_endpoint' in payload:
return payload
raise MsalServiceError(status_code=resp.status_code, **payload)
if resp.status_code == 200:
payload = json.loads(resp.text) # It could raise ValueError
if 'authorization_endpoint' in payload and 'token_endpoint' in payload:
return payload # Happy path
raise ValueError("OIDC Discovery does not provide enough information")
if 400 <= resp.status_code < 500:
# Nonexist tenant would hit this path
# e.g. https://login.microsoftonline.com/nonexist_tenant/v2.0/.well-known/openid-configuration
raise ValueError("OIDC Discovery endpoint rejects our request")
# Transient network error would hit this path
resp.raise_for_status()
raise RuntimeError( # A fallback here, in case resp.raise_for_status() is no-op
"Unable to complete OIDC Discovery: %d, %s" % (resp.status_code, resp.text))

0 comments on commit 1f56396

Please sign in to comment.