Skip to content

Commit b5ff714

Browse files
committed
Fix #204. On a LogoutRequest if the NameIdFormat is entity, NameQualifier and SPNameQualifier will be ommited. If the NameIdFormat is not entity and a NameQualifier is provided, then the SPNameQualifier will be also added. Update info related to LogoutRequest on the README
1 parent 3bda379 commit b5ff714

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,11 +801,17 @@ target_url = 'https://example.com'
801801
auth.logout(return_to=target_url)
802802
```
803803

804-
Also there are 2 optional parameters that can be set:
804+
Also there are 4 optional parameters that can be set:
805805

806806
* name_id. That will be used to build the LogoutRequest. If not name_id parameter is set and the auth object processed a
807807
SAML Response with a NameId, then this NameId will be used.
808808
* session_index. SessionIndex that identifies the session of the user.
809+
* nq. IDP Name Qualifier
810+
* name_id_format. The NameID Format that will be set in the LogoutRequest
811+
812+
If no name_id is provided, the LogoutRequest will contain a NameID with the entity Format.
813+
If name_id is provided and no name_id_format is provided, the NameIDFormat of the settings will be used.
814+
If nq is provided, the SPNameQualifier will be also attached to the NameId.
809815

810816
If a match on the LogoutResponse ID and the LogoutRequest ID to be sent is required, that LogoutRequest ID must to be extracted and stored for future validation, we can get that ID by
811817

src/onelogin/saml2/logout_request.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,15 @@ def __init__(self, settings, request=None, name_id=None, session_index=None, nq=
8080
nameIdFormat = name_id_format
8181
else:
8282
nameIdFormat = sp_data['NameIDFormat']
83-
spNameQualifier = None
8483
else:
85-
name_id = idp_data['entityId']
8684
nameIdFormat = OneLogin_Saml2_Constants.NAMEID_ENTITY
85+
86+
spNameQualifier = None
87+
if nameIdFormat == OneLogin_Saml2_Constants.NAMEID_ENTITY:
88+
name_id = idp_data['entityId']
89+
nq = None
90+
elif nq is not None:
91+
# We only gonna include SPNameQualifier if NameQualifier is provided
8792
spNameQualifier = sp_data['entityId']
8893

8994
name_id_obj = OneLogin_Saml2_Utils.generate_name_id(

tests/src/OneLogin/saml2_tests/auth_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ def testGetLastLogoutRequest(self):
10471047
expectedFragment = (
10481048
' Destination="http://idp.example.com/SingleLogoutService.php">\n'
10491049
' <saml:Issuer>http://stuff.com/endpoints/metadata.php</saml:Issuer>\n'
1050-
' <saml:NameID Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity" SPNameQualifier="http://stuff.com/endpoints/metadata.php">http://idp.example.com/</saml:NameID>\n'
1050+
' <saml:NameID Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity">http://idp.example.com/</saml:NameID>\n'
10511051
' \n </samlp:LogoutRequest>'
10521052
)
10531053
self.assertIn(expectedFragment, auth.get_last_request_xml())

tests/src/OneLogin/saml2_tests/logout_request_test.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,11 @@ def testGetNameIdData(self):
154154
OneLogin_Saml2_Logout_Request.get_nameid_data(dom_2.toxml(), key)
155155

156156
idp_data = settings.get_idp_data()
157+
sp_data = settings.get_sp_data()
157158
expected_name_id_data = {
158159
'Format': 'urn:oasis:names:tc:SAML:2.0:nameid-format:emailAddress',
159160
'NameQualifier': idp_data['entityId'],
161+
'SPNameQualifier': sp_data['entityId'],
160162
'Value': 'ONELOGIN_9c86c4542ab9d6fce07f2f7fd335287b9b3cdf69'
161163
}
162164

@@ -169,6 +171,24 @@ def testGetNameIdData(self):
169171
name_id_data_3 = OneLogin_Saml2_Logout_Request.get_nameid_data(dom)
170172
self.assertEqual(expected_name_id_data, name_id_data_3)
171173

174+
expected_name_id_data = {
175+
'Format': 'urn:oasis:names:tc:SAML:2.0:nameid-format:emailAddress',
176+
'Value': 'ONELOGIN_9c86c4542ab9d6fce07f2f7fd335287b9b3cdf69'
177+
}
178+
logout_request = OneLogin_Saml2_Logout_Request(settings, None, expected_name_id_data['Value'], None, None, expected_name_id_data['Format'])
179+
dom = parseString(logout_request.get_xml())
180+
name_id_data_4 = OneLogin_Saml2_Logout_Request.get_nameid_data(dom)
181+
self.assertEqual(expected_name_id_data, name_id_data_4)
182+
183+
expected_name_id_data = {
184+
'Format': 'urn:oasis:names:tc:SAML:2.0:nameid-format:entity',
185+
'Value': 'http://idp.example.com/'
186+
}
187+
logout_request = OneLogin_Saml2_Logout_Request(settings)
188+
dom = parseString(logout_request.get_xml())
189+
name_id_data_5 = OneLogin_Saml2_Logout_Request.get_nameid_data(dom)
190+
self.assertEqual(expected_name_id_data, name_id_data_5)
191+
172192
def testGetNameId(self):
173193
"""
174194
Tests the get_nameid of the OneLogin_Saml2_LogoutRequest
@@ -478,7 +498,7 @@ def testGetXML(self):
478498
expectedFragment = (
479499
'Destination="http://idp.example.com/SingleLogoutService.php">\n'
480500
' <saml:Issuer>http://stuff.com/endpoints/metadata.php</saml:Issuer>\n'
481-
' <saml:NameID Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity" SPNameQualifier="http://stuff.com/endpoints/metadata.php">http://idp.example.com/</saml:NameID>\n'
501+
' <saml:NameID Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity">http://idp.example.com/</saml:NameID>\n'
482502
' \n </samlp:LogoutRequest>'
483503
)
484504
self.assertIn(expectedFragment, logout_request_generated.get_xml())

0 commit comments

Comments
 (0)