Skip to content

Commit 5545e21

Browse files
committed
Make response function is_valid support raising exceptions
1 parent c3d92fb commit 5545e21

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/onelogin/saml2/response.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def __init__(self, settings, response):
4747
self.encrypted = True
4848
self.decrypted_document = self.__decrypt_assertion(decrypted_document)
4949

50-
def is_valid(self, request_data, request_id=None):
50+
def is_valid(self, request_data, request_id=None, raises=False):
5151
"""
5252
Validates the response object.
5353
@@ -57,6 +57,9 @@ def is_valid(self, request_data, request_id=None):
5757
:param request_id: Optional argument. The ID of the AuthNRequest sent by this SP to the IdP
5858
:type request_id: string
5959
60+
:param raises: Optional argument. If true, the function will raise an exception as soon as first validation test fails
61+
:type raises: bool
62+
6063
:returns: True if the SAML Response is valid, False if not
6164
:rtype: bool
6265
"""
@@ -226,6 +229,8 @@ def is_valid(self, request_data, request_id=None):
226229
debug = self.__settings.is_debug_active()
227230
if debug:
228231
print(err)
232+
if raises:
233+
raise
229234
return False
230235

231236
def check_status(self):

tests/src/OneLogin/saml2_tests/response_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,3 +1349,18 @@ def testIsValidWithoutInResponseTo(self):
13491349
'http_host': 'pitbulk.no-ip.org',
13501350
'script_name': 'newonelogin/demo1/index.php?acs'
13511351
}))
1352+
1353+
def testIsValidRaisesExceptionWhenRaisesArgumentIsTrue(self):
1354+
"""
1355+
Tests that the internal exception gets raised if the raise parameter
1356+
is True.
1357+
"""
1358+
settings = OneLogin_Saml2_Settings(self.loadSettingsJSON())
1359+
settings.set_strict(True)
1360+
xml = self.file_contents(join(self.data_path, 'responses', 'invalids', 'no_conditions.xml.base64'))
1361+
response = OneLogin_Saml2_Response(settings, xml)
1362+
1363+
self.assertFalse(response.is_valid(self.get_request_data()))
1364+
1365+
with self.assertRaises(Exception):
1366+
response.is_valid(self.get_request_data(), raises=True)

0 commit comments

Comments
 (0)