Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bubble http exceptions so apps could catch them #269

Merged
merged 1 commit into from
Oct 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
rayluo marked this conversation as resolved.
Show resolved Hide resolved
# 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
rayluo marked this conversation as resolved.
Show resolved Hide resolved
"Unable to complete OIDC Discovery: %d, %s" % (resp.status_code, resp.text))