1010"""
1111
1212from onelogin .saml2 .constants import OneLogin_Saml2_Constants
13- from onelogin .saml2 .utils import OneLogin_Saml2_Utils , return_false_on_exception
13+ from onelogin .saml2 .utils import OneLogin_Saml2_Utils , OneLogin_Saml2_Error , OneLogin_Saml2_ValidationError
1414from onelogin .saml2 .xml_templates import OneLogin_Saml2_Templates
1515from onelogin .saml2 .xml_utils import OneLogin_Saml2_XML
1616
@@ -141,7 +141,10 @@ def get_nameid_data(request, key=None):
141141
142142 if len (encrypted_entries ) == 1 :
143143 if key is None :
144- raise Exception ('Key is required in order to decrypt the NameID' )
144+ raise OneLogin_Saml2_Error (
145+ 'Private Key is required in order to decrypt the NameID, check settings' ,
146+ OneLogin_Saml2_Error .PRIVATE_KEY_NOT_FOUND
147+ )
145148
146149 encrypted_data_nodes = OneLogin_Saml2_XML .query (elem , '/samlp:LogoutRequest/saml:EncryptedID/xenc:EncryptedData' )
147150 if len (encrypted_data_nodes ) == 1 :
@@ -153,7 +156,10 @@ def get_nameid_data(request, key=None):
153156 name_id = entries [0 ]
154157
155158 if name_id is None :
156- raise Exception ('Not NameID found in the Logout Request' )
159+ raise OneLogin_Saml2_ValidationError (
160+ 'Not NameID found in the Logout Request' ,
161+ OneLogin_Saml2_ValidationError .NO_NAMEID
162+ )
157163
158164 name_id_data = {
159165 'Value' : name_id .text
@@ -236,7 +242,10 @@ def is_valid(self, request_data, raise_exceptions=False):
236242 if self .__settings .is_strict ():
237243 res = OneLogin_Saml2_XML .validate_xml (root , 'saml-schema-protocol-2.0.xsd' , self .__settings .is_debug_active ())
238244 if isinstance (res , str ):
239- raise Exception ('Invalid SAML Logout Request. Not match the saml-schema-protocol-2.0.xsd' )
245+ raise OneLogin_Saml2_ValidationError (
246+ 'Invalid SAML Logout Request. Not match the saml-schema-protocol-2.0.xsd' ,
247+ OneLogin_Saml2_ValidationError .INVALID_XML_FORMAT
248+ )
240249
241250 security = self .__settings .get_security_data ()
242251
@@ -246,30 +255,41 @@ def is_valid(self, request_data, raise_exceptions=False):
246255 if root .get ('NotOnOrAfter' , None ):
247256 na = OneLogin_Saml2_Utils .parse_SAML_to_time (root .get ('NotOnOrAfter' ))
248257 if na <= OneLogin_Saml2_Utils .now ():
249- raise Exception ('Could not validate timestamp: expired. Check system clock.)' )
258+ raise OneLogin_Saml2_ValidationError (
259+ 'Could not validate timestamp: expired. Check system clock.)' ,
260+ OneLogin_Saml2_ValidationError .RESPONSE_EXPIRED
261+ )
250262
251263 # Check destination
252264 if root .get ('Destination' , None ):
253265 destination = root .get ('Destination' )
254266 if destination != '' :
255267 if current_url not in destination :
256- raise Exception (
268+ raise OneLogin_Saml2_ValidationError (
257269 'The LogoutRequest was received at '
258270 '%(currentURL)s instead of %(destination)s' %
259271 {
260272 'currentURL' : current_url ,
261273 'destination' : destination ,
262- }
274+ },
275+ OneLogin_Saml2_ValidationError .WRONG_DESTINATION
263276 )
264277
265278 # Check issuer
266279 issuer = OneLogin_Saml2_Logout_Request .get_issuer (root )
267280 if issuer is not None and issuer != idp_entity_id :
268- raise Exception ('Invalid issuer in the Logout Request' )
281+ raise OneLogin_Saml2_ValidationError (
282+ 'Invalid issuer in the Logout Request' ,
283+ OneLogin_Saml2_ValidationError .WRONG_ISSUER
284+ )
269285
270286 if security ['wantMessagesSigned' ]:
271287 if 'Signature' not in get_data :
272- raise Exception ('The Message of the Logout Request is not signed and the SP require it' )
288+ raise OneLogin_Saml2_ValidationError (
289+ 'The Message of the Logout Request is not signed and the SP require it' ,
290+ OneLogin_Saml2_ValidationError .NO_SIGNED_RESPONSE
291+ )
292+
273293 return True
274294 except Exception as err :
275295 # pylint: disable=R0801
0 commit comments