Skip to content

Commit a4f896d

Browse files
authored
Merge pull request SAML-Toolkits#288 from mateuszmandera/responsecodes
Support building a LogoutResponse with non-success status
2 parents b4199c5 + 6f973d1 commit a4f896d

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

src/onelogin/saml2/logout_response.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"""
1111

1212
from onelogin.saml2 import compat
13+
from onelogin.saml2.constants import OneLogin_Saml2_Constants
1314
from onelogin.saml2.utils import OneLogin_Saml2_Utils, OneLogin_Saml2_ValidationError
1415
from onelogin.saml2.xml_templates import OneLogin_Saml2_Templates
1516
from onelogin.saml2.xml_utils import OneLogin_Saml2_XML
@@ -152,27 +153,28 @@ def _query(self, query):
152153
"""
153154
return OneLogin_Saml2_XML.query(self.document, query)
154155

155-
def build(self, in_response_to):
156+
def build(self, in_response_to, status=OneLogin_Saml2_Constants.STATUS_SUCCESS):
156157
"""
157158
Creates a Logout Response object.
158159
:param in_response_to: InResponseTo value for the Logout Response.
159160
:type in_response_to: string
161+
:param: status: The status of the response
162+
:type: status: string
160163
"""
161164
sp_data = self._settings.get_sp_data()
162165

163166
self.id = self._generate_request_id()
164167

165168
issue_instant = OneLogin_Saml2_Utils.parse_time_to_SAML(OneLogin_Saml2_Utils.now())
166169

167-
logout_response = OneLogin_Saml2_Templates.LOGOUT_RESPONSE % \
168-
{
169-
'id': self.id,
170-
'issue_instant': issue_instant,
171-
'destination': self._settings.get_idp_slo_response_url(),
172-
'in_response_to': in_response_to,
173-
'entity_id': sp_data['entityId'],
174-
'status': "urn:oasis:names:tc:SAML:2.0:status:Success"
175-
}
170+
logout_response = OneLogin_Saml2_Templates.LOGOUT_RESPONSE % {
171+
"id": self.id,
172+
"issue_instant": issue_instant,
173+
"destination": self._settings.get_idp_slo_response_url(),
174+
"in_response_to": in_response_to,
175+
"entity_id": sp_data["entityId"],
176+
"status": status,
177+
}
176178

177179
self._logout_response = logout_response
178180

tests/src/OneLogin/saml2_tests/logout_response_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,23 @@ def testGetXML(self):
401401

402402
logout_response_processed = OneLogin_Saml2_Logout_Response(settings, OneLogin_Saml2_Utils.deflate_and_base64_encode(response))
403403
self.assertEqual(response, logout_response_processed.get_xml())
404+
405+
def testBuildWithStatus(self):
406+
"""
407+
Tests the build method when called specifying a non-default status for the LogoutResponse.
408+
"""
409+
settings = OneLogin_Saml2_Settings(self.loadSettingsJSON())
410+
411+
response_builder = OneLogin_Saml2_Logout_Response(settings)
412+
response_builder.build("InResponseValue", status=OneLogin_Saml2_Constants.STATUS_REQUESTER)
413+
generated_encoded_response = response_builder.get_response()
414+
415+
# Parse and verify the status of the response, as the receiver will do:
416+
parsed_response = OneLogin_Saml2_Logout_Response(settings, generated_encoded_response)
417+
expectedFragment = (
418+
' <samlp:Status>\n'
419+
' <samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Requester" />\n'
420+
' </samlp:Status>\n'
421+
)
422+
self.assertIn(expectedFragment, parsed_response.get_xml())
423+
self.assertEqual(parsed_response.get_status(), OneLogin_Saml2_Constants.STATUS_REQUESTER)

0 commit comments

Comments
 (0)