@@ -83,7 +83,7 @@ def __init__(self, authority_url, http_client, validate_authority=True):
83
83
openid_config = tenant_discovery (
84
84
tenant_discovery_endpoint ,
85
85
self .http_client )
86
- except ValueError : # json.decoder.JSONDecodeError in Py3 subclasses this
86
+ except ValueError :
87
87
raise ValueError (
88
88
"Unable to get authority configuration for {}. "
89
89
"Authority would typically be in a format of "
@@ -140,8 +140,17 @@ def instance_discovery(url, http_client, **kwargs):
140
140
def tenant_discovery (tenant_discovery_endpoint , http_client , ** kwargs ):
141
141
# Returns Openid Configuration
142
142
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 ))
147
156
0 commit comments