Skip to content

Commit 30cbe7c

Browse files
committed
Allow empty nameid if setting wantNameId is false. Only raise Exceptions when strict mode is enabled
1 parent df3db49 commit 30cbe7c

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/onelogin/saml2/response.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,16 +421,19 @@ def get_nameid_data(self):
421421
nameid_nodes = self.__query_assertion('/saml:Subject/saml:NameID')
422422
if nameid_nodes:
423423
nameid = nameid_nodes[0]
424+
425+
is_strict = self.__settings.is_strict()
426+
want_nameid = self.__settings.get_security_data().get('wantNameId', True)
424427
if nameid is None:
425428
security = self.__settings.get_security_data()
426429

427-
if security.get('wantNameId', True):
430+
if is_strict and want_nameid:
428431
raise OneLogin_Saml2_ValidationError(
429432
'NameID not found in the assertion of the Response',
430433
OneLogin_Saml2_ValidationError.NO_NAMEID
431434
)
432435
else:
433-
if self.__settings.is_strict() and not nameid.text:
436+
if is_strict and want_nameid and not nameid.text:
434437
raise OneLogin_Saml2_ValidationError(
435438
'An empty NameID value found',
436439
OneLogin_Saml2_ValidationError.EMPTY_NAMEID

tests/src/OneLogin/saml2_tests/response_test.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def testReturnNameId(self):
8282
Tests the get_nameid method of the OneLogin_Saml2_Response
8383
"""
8484
json_settings = self.loadSettingsJSON()
85+
json_settings['strict'] = True
8586

8687
settings = OneLogin_Saml2_Settings(json_settings)
8788
xml = self.file_contents(join(self.data_path, 'responses', 'response1.xml.base64'))
@@ -135,11 +136,18 @@ def testReturnNameId(self):
135136
with self.assertRaisesRegexp(OneLogin_Saml2_ValidationError, 'An empty NameID value found'):
136137
response_9.get_nameid()
137138

139+
json_settings['security']['wantNameId'] = False
140+
settings = OneLogin_Saml2_Settings(json_settings)
141+
142+
nameid_9 = response_9.get_nameid()
143+
self.assertEqual(None, nameid_9)
144+
138145
def testReturnNameIdFormat(self):
139146
"""
140147
Tests the get_nameid_format method of the OneLogin_Saml2_Response
141148
"""
142149
json_settings = self.loadSettingsJSON()
150+
json_settings['strict'] = True
143151

144152
settings = OneLogin_Saml2_Settings(json_settings)
145153
xml = self.file_contents(join(self.data_path, 'responses', 'response1.xml.base64'))
@@ -193,11 +201,18 @@ def testReturnNameIdFormat(self):
193201
with self.assertRaisesRegexp(OneLogin_Saml2_ValidationError, 'An empty NameID value found'):
194202
response_9.get_nameid_format()
195203

204+
json_settings['security']['wantNameId'] = False
205+
settings = OneLogin_Saml2_Settings(json_settings)
206+
207+
nameid_9 = response_9.get_nameid_format()
208+
self.assertEqual('urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress', nameid_9)
209+
196210
def testGetNameIdData(self):
197211
"""
198212
Tests the get_nameid_data method of the OneLogin_Saml2_Response
199213
"""
200214
json_settings = self.loadSettingsJSON()
215+
json_settings['strict'] = True
201216

202217
settings = OneLogin_Saml2_Settings(self.loadSettingsJSON())
203218
xml = self.file_contents(join(self.data_path, 'responses', 'response1.xml.base64'))
@@ -231,8 +246,9 @@ def testGetNameIdData(self):
231246

232247
xml_4 = self.file_contents(join(self.data_path, 'responses', 'invalids', 'no_nameid.xml.base64'))
233248
response_4 = OneLogin_Saml2_Response(settings, xml_4)
234-
with self.assertRaisesRegexp(OneLogin_Saml2_ValidationError, 'NameID not found in the assertion of the Response'):
235-
response_4.get_nameid_data()
249+
250+
nameid_data_4 = response_4.get_nameid_data()
251+
self.assertEqual({}, nameid_data_4)
236252

237253
json_settings['security']['wantNameId'] = True
238254
settings = OneLogin_Saml2_Settings(json_settings)
@@ -262,13 +278,23 @@ def testGetNameIdData(self):
262278
response_8 = OneLogin_Saml2_Response(settings, xml_5)
263279
with self.assertRaisesRegexp(OneLogin_Saml2_ValidationError, 'The SPNameQualifier value mistmatch the SP entityID value.'):
264280
response_8.get_nameid_data()
265-
self.assertTrue(False)
266281

267282
xml_6 = self.file_contents(join(self.data_path, 'responses', 'invalids', 'empty_nameid.xml.base64'))
268283
response_9 = OneLogin_Saml2_Response(settings, xml_6)
269284
with self.assertRaisesRegexp(OneLogin_Saml2_ValidationError, 'An empty NameID value found'):
270285
response_9.get_nameid_data()
271286

287+
json_settings['security']['wantNameId'] = False
288+
settings = OneLogin_Saml2_Settings(json_settings)
289+
290+
nameid_data_9 = response_9.get_nameid_data()
291+
292+
expected_nameid_data_4 = {
293+
'Value': None,
294+
'Format': 'urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress',
295+
}
296+
self.assertEqual(expected_nameid_data_4, nameid_data_9)
297+
272298
def testCheckStatus(self):
273299
"""
274300
Tests the check_status method of the OneLogin_Saml2_Response

0 commit comments

Comments
 (0)