@@ -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):
140140def 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