Skip to content

Commit 9347506

Browse files
committed
Make SPNameQualifier optional on the generateNameId method. Avoid the use of SPNameQualifier when generating the NameID on the LogoutRequest builder.
1 parent 3b124a3 commit 9347506

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

src/onelogin/saml2/logout_request.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,15 @@ def __init__(self, settings, request=None, name_id=None, session_index=None):
6565

6666
if name_id is not None:
6767
nameIdFormat = sp_data['NameIDFormat']
68+
spNameQualifier = None
6869
else:
6970
name_id = idp_data['entityId']
7071
nameIdFormat = OneLogin_Saml2_Constants.NAMEID_ENTITY
72+
spNameQualifier = sp_data['entityId']
7173

7274
name_id_obj = OneLogin_Saml2_Utils.generate_name_id(
7375
name_id,
74-
sp_data['entityId'],
76+
spNameQualifier,
7577
nameIdFormat,
7678
cert
7779
)

src/onelogin/saml2/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,8 @@ def generate_name_id(value, sp_nq, sp_format, cert=None, debug=False):
611611
name_id_container.setAttribute("xmlns:saml", OneLogin_Saml2_Constants.NS_SAML)
612612

613613
name_id = doc.createElement('saml:NameID')
614-
name_id.setAttribute('SPNameQualifier', sp_nq)
614+
if sp_nq is not None:
615+
name_id.setAttribute('SPNameQualifier', sp_nq)
615616
name_id.setAttribute('Format', sp_format)
616617
name_id.appendChild(doc.createTextNode(value))
617618
name_id_container.appendChild(name_id)

tests/src/OneLogin/saml2_tests/utils_test.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,9 +557,10 @@ def testQuery(self):
557557
signature_nodes_5 = OneLogin_Saml2_Utils.query(dom, './/ds:SignatureValue', assertion)
558558
self.assertEqual(1, len(signature_nodes_5))
559559

560-
def testGenerateNameId(self):
560+
def testGenerateNameIdWithSPNameQualifier(self):
561561
"""
562562
Tests the generateNameId method of the OneLogin_Saml2_Utils
563+
Adding a SPNameQualifier
563564
"""
564565
name_id_value = 'ONELOGIN_ce998811003f4e60f8b07a311dc641621379cfde'
565566
entity_id = 'http://stuff.com/endpoints/metadata.php'
@@ -577,6 +578,25 @@ def testGenerateNameId(self):
577578
expected_name_id_enc = '<saml:EncryptedID><xenc:EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"><xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/><dsig:KeyInfo xmlns:dsig="http://www.w3.org/2000/09/xmldsig#"><xenc:EncryptedKey><xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p"/><xenc:CipherData><xenc:CipherValue>'
578579
self.assertIn(expected_name_id_enc, name_id_enc)
579580

581+
def testGenerateNameIdWithoutSPNameQualifier(self):
582+
"""
583+
Tests the generateNameId method of the OneLogin_Saml2_Utils
584+
"""
585+
name_id_value = 'ONELOGIN_ce998811003f4e60f8b07a311dc641621379cfde'
586+
name_id_format = 'urn:oasis:names:tc:SAML:2.0:nameid-format:unspecified'
587+
588+
name_id = OneLogin_Saml2_Utils.generate_name_id(name_id_value, None, name_id_format)
589+
expected_name_id = '<saml:NameID Format="urn:oasis:names:tc:SAML:2.0:nameid-format:unspecified">ONELOGIN_ce998811003f4e60f8b07a311dc641621379cfde</saml:NameID>'
590+
self.assertEqual(name_id, expected_name_id)
591+
592+
settings_info = self.loadSettingsJSON()
593+
x509cert = settings_info['idp']['x509cert']
594+
key = OneLogin_Saml2_Utils.format_cert(x509cert)
595+
596+
name_id_enc = OneLogin_Saml2_Utils.generate_name_id(name_id_value, None, name_id_format, key)
597+
expected_name_id_enc = '<saml:EncryptedID><xenc:EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"><xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/><dsig:KeyInfo xmlns:dsig="http://www.w3.org/2000/09/xmldsig#"><xenc:EncryptedKey><xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p"/><xenc:CipherData><xenc:CipherValue>'
598+
self.assertIn(expected_name_id_enc, name_id_enc)
599+
580600
def testCalculateX509Fingerprint(self):
581601
"""
582602
Tests the calculateX509Fingerprint method of the OneLogin_Saml2_Utils

0 commit comments

Comments
 (0)