1717
1818from onelogin .saml2 .constants import OneLogin_Saml2_Constants
1919from onelogin .saml2 .utils import OneLogin_Saml2_Utils
20+ from onelogin .saml2 .errors import OneLogin_Saml2_Error , OneLogin_Saml2_ValidationError
2021
2122
2223class OneLogin_Saml2_Logout_Response (object ):
@@ -91,20 +92,29 @@ def is_valid(self, request_data, request_id=None, raise_exceptions=False):
9192 if self .__settings .is_strict ():
9293 res = OneLogin_Saml2_Utils .validate_xml (self .document , 'saml-schema-protocol-2.0.xsd' , self .__settings .is_debug_active ())
9394 if not isinstance (res , Document ):
94- raise Exception ('Invalid SAML Logout Response. Not match the saml-schema-protocol-2.0.xsd' )
95+ raise OneLogin_Saml2_ValidationError (
96+ 'Invalid SAML Logout Response. Not match the saml-schema-protocol-2.0.xsd' ,
97+ OneLogin_Saml2_ValidationError .INVALID_XML_FORMAT
98+ )
9599
96100 security = self .__settings .get_security_data ()
97101
98102 # Check if the InResponseTo of the Logout Response matches the ID of the Logout Request (requestId) if provided
99103 if request_id is not None and self .document .documentElement .hasAttribute ('InResponseTo' ):
100104 in_response_to = self .document .documentElement .getAttribute ('InResponseTo' )
101105 if request_id != in_response_to :
102- raise Exception ('The InResponseTo of the Logout Response: %s, does not match the ID of the Logout request sent by the SP: %s' % (in_response_to , request_id ))
106+ raise OneLogin_Saml2_ValidationError (
107+ 'The InResponseTo of the Logout Response: %s, does not match the ID of the Logout request sent by the SP: %s' % (in_response_to , request_id ),
108+ OneLogin_Saml2_ValidationError .WRONG_INRESPONSETO
109+ )
103110
104111 # Check issuer
105112 issuer = self .get_issuer ()
106113 if issuer is not None and issuer != idp_entity_id :
107- raise Exception ('Invalid issuer in the Logout Request' )
114+ raise OneLogin_Saml2_ValidationError (
115+ 'Invalid issuer in the Logout Request' ,
116+ OneLogin_Saml2_ValidationError .WRONG_ISSUER
117+ )
108118
109119 current_url = OneLogin_Saml2_Utils .get_self_url_no_query (request_data )
110120
@@ -113,11 +123,17 @@ def is_valid(self, request_data, request_id=None, raise_exceptions=False):
113123 destination = self .document .documentElement .getAttribute ('Destination' )
114124 if destination != '' :
115125 if current_url not in destination :
116- raise Exception ('The LogoutRequest was received at $currentURL instead of $destination' )
126+ raise OneLogin_Saml2_ValidationError (
127+ 'The LogoutResponse was received at %s instead of %s' % (current_url , destination ),
128+ OneLogin_Saml2_ValidationError .WRONG_DESTINATION
129+ )
117130
118131 if security ['wantMessagesSigned' ]:
119132 if 'Signature' not in get_data :
120- raise Exception ('The Message of the Logout Response is not signed and the SP require it' )
133+ raise OneLogin_Saml2_ValidationError (
134+ 'The Message of the Logout Response is not signed and the SP require it' ,
135+ OneLogin_Saml2_ValidationError .NO_SIGNED_RESPONSE
136+ )
121137
122138 if 'Signature' in get_data :
123139 if 'SigAlg' not in get_data :
@@ -131,11 +147,17 @@ def is_valid(self, request_data, request_id=None, raise_exceptions=False):
131147 signed_query = '%s&SigAlg=%s' % (signed_query , OneLogin_Saml2_Utils .get_encoded_parameter (get_data , 'SigAlg' , OneLogin_Saml2_Constants .RSA_SHA1 , lowercase_urlencoding = lowercase_urlencoding ))
132148
133149 if 'x509cert' not in idp_data or not idp_data ['x509cert' ]:
134- raise Exception ('In order to validate the sign on the Logout Response, the x509cert of the IdP is required' )
150+ raise OneLogin_Saml2_Error (
151+ 'In order to validate the sign on the Logout Response, the x509cert of the IdP is required' ,
152+ OneLogin_Saml2_Error .CERT_NOT_FOUND
153+ )
135154 cert = idp_data ['x509cert' ]
136155
137156 if not OneLogin_Saml2_Utils .validate_binary_sign (signed_query , b64decode (get_data ['Signature' ]), cert , sign_alg ):
138- raise Exception ('Signature validation failed. Logout Response rejected' )
157+ raise OneLogin_Saml2_ValidationError (
158+ 'Signature validation failed. Logout Response rejected' ,
159+ OneLogin_Saml2_ValidationError .INVALID_SIGNATURE
160+ )
139161
140162 return True
141163 # pylint: disable=R0801
0 commit comments