Skip to content

Commit

Permalink
Allow empty nameid if setting wantNameId is false. Only raise Excepti…
Browse files Browse the repository at this point in the history
…ons when strict mode is enabled
  • Loading branch information
pitbulk committed Sep 14, 2017
1 parent df3db49 commit 30cbe7c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/onelogin/saml2/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,16 +421,19 @@ def get_nameid_data(self):
nameid_nodes = self.__query_assertion('/saml:Subject/saml:NameID')
if nameid_nodes:
nameid = nameid_nodes[0]

is_strict = self.__settings.is_strict()
want_nameid = self.__settings.get_security_data().get('wantNameId', True)
if nameid is None:
security = self.__settings.get_security_data()

if security.get('wantNameId', True):
if is_strict and want_nameid:
raise OneLogin_Saml2_ValidationError(
'NameID not found in the assertion of the Response',
OneLogin_Saml2_ValidationError.NO_NAMEID
)
else:
if self.__settings.is_strict() and not nameid.text:
if is_strict and want_nameid and not nameid.text:
raise OneLogin_Saml2_ValidationError(
'An empty NameID value found',
OneLogin_Saml2_ValidationError.EMPTY_NAMEID
Expand Down
32 changes: 29 additions & 3 deletions tests/src/OneLogin/saml2_tests/response_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def testReturnNameId(self):
Tests the get_nameid method of the OneLogin_Saml2_Response
"""
json_settings = self.loadSettingsJSON()
json_settings['strict'] = True

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

json_settings['security']['wantNameId'] = False
settings = OneLogin_Saml2_Settings(json_settings)

nameid_9 = response_9.get_nameid()
self.assertEqual(None, nameid_9)

def testReturnNameIdFormat(self):
"""
Tests the get_nameid_format method of the OneLogin_Saml2_Response
"""
json_settings = self.loadSettingsJSON()
json_settings['strict'] = True

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

json_settings['security']['wantNameId'] = False
settings = OneLogin_Saml2_Settings(json_settings)

nameid_9 = response_9.get_nameid_format()
self.assertEqual('urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress', nameid_9)

def testGetNameIdData(self):
"""
Tests the get_nameid_data method of the OneLogin_Saml2_Response
"""
json_settings = self.loadSettingsJSON()
json_settings['strict'] = True

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

xml_4 = self.file_contents(join(self.data_path, 'responses', 'invalids', 'no_nameid.xml.base64'))
response_4 = OneLogin_Saml2_Response(settings, xml_4)
with self.assertRaisesRegexp(OneLogin_Saml2_ValidationError, 'NameID not found in the assertion of the Response'):
response_4.get_nameid_data()

nameid_data_4 = response_4.get_nameid_data()
self.assertEqual({}, nameid_data_4)

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

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

json_settings['security']['wantNameId'] = False
settings = OneLogin_Saml2_Settings(json_settings)

nameid_data_9 = response_9.get_nameid_data()

expected_nameid_data_4 = {
'Value': None,
'Format': 'urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress',
}
self.assertEqual(expected_nameid_data_4, nameid_data_9)

def testCheckStatus(self):
"""
Tests the check_status method of the OneLogin_Saml2_Response
Expand Down

0 comments on commit 30cbe7c

Please sign in to comment.