@@ -125,6 +125,13 @@ def get_next_path(self, request: HttpRequest) -> str:
125125 next_path = validate_referral_url (request , next_path )
126126 return next_path
127127
128+ def unknown_idp (self , request , idp ):
129+ msg = (f'Error: IdP EntityID { idp } was not found in metadata' )
130+ logger .error (msg )
131+ return HttpResponse (
132+ msg .format ('Please contact technical support.' ), status = 403
133+ )
134+
128135 def get (self , request , * args , ** kwargs ):
129136 logger .debug ('Login process started' )
130137 next_path = self .get_next_path (request )
@@ -149,10 +156,10 @@ def get(self, request, *args, **kwargs):
149156
150157 try :
151158 conf = self .get_sp_config (request )
152- except SourceNotFound as excp :
153- msg = ( 'Error, IdP EntityID was not found in metadata: {}' )
154- logger . exception ( msg . format ( excp ) )
155- return HttpResponse ( msg . format ( 'Please contact technical support.' ), status = 500 )
159+ except SourceNotFound as excp : # pragma: no cover
160+ # this is deprecated and it's here only for the doubts that something
161+ # would happen the day after I'll remove it! : )
162+ return self . unknown_idp ( request , idp = 'unknown' )
156163
157164 # is a embedded wayf or DiscoveryService needed?
158165 configured_idps = available_idps (conf )
@@ -186,9 +193,9 @@ def get(self, request, *args, **kwargs):
186193 })
187194
188195 # is the first one, otherwise next logger message will print None
189- if not configured_idps :
196+ if not configured_idps : # pragma: no cover
190197 raise IdPConfigurationMissing (
191- ('IdP configuration is missing or its metadata is expired.' ))
198+ ('IdP is missing or its metadata is expired.' ))
192199 if selected_idp is None :
193200 selected_idp = list (configured_idps .keys ())[0 ]
194201
@@ -202,15 +209,17 @@ def get(self, request, *args, **kwargs):
202209 )
203210 sso_kwargs ['scoping' ] = idp_scoping
204211
205-
206212 # choose a binding to try first
207213 binding = getattr (settings , 'SAML_DEFAULT_BINDING' ,
208214 saml2 .BINDING_HTTP_POST )
209215 logger .debug (f'Trying binding { binding } for IDP { selected_idp } ' )
210216
211217 # ensure our selected binding is supported by the IDP
212- supported_bindings = get_idp_sso_supported_bindings (
213- selected_idp , config = conf )
218+ try :
219+ supported_bindings = get_idp_sso_supported_bindings (
220+ selected_idp , config = conf )
221+ except saml2 .s_utils .UnknownSystemEntity :
222+ return self .unknown_idp (request , selected_idp )
214223
215224 if binding not in supported_bindings :
216225 logger .debug (
@@ -223,17 +232,17 @@ def get(self, request, *args, **kwargs):
223232 f'trying { saml2 .BINDING_HTTP_REDIRECT } ' ,
224233 )
225234 binding = saml2 .BINDING_HTTP_REDIRECT
226- else :
235+ else : # pragma: no cover
227236 logger .warning (
228237 f'IDP { selected_idp } does not support { binding } '
229238 f'trying { saml2 .BINDING_HTTP_POST } ' ,
230239 )
231240 binding = saml2 .BINDING_HTTP_POST
232241 # if switched binding still not supported, give up
233- if binding not in supported_bindings :
242+ if binding not in supported_bindings : # pragma: no cover
234243 raise UnsupportedBinding (
235244 f'IDP { selected_idp } does not support '
236- f'{ saml2 .BINDING_HTTP_POST } and { saml2 .BINDING_HTTP_REDIRECT } '
245+ f'{ saml2 .BINDING_HTTP_POST } or { saml2 .BINDING_HTTP_REDIRECT } '
237246 )
238247
239248 client = Saml2Client (conf )
0 commit comments