Skip to content

Commit 9a01a84

Browse files
author
Alexander Schrijver
committed
Add a test for encrypted ids using the OneLogin_Saml2_Response class.
1 parent 5ebb85e commit 9a01a84

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

tests/src/OneLogin/saml2_tests/response_test.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Copyright (c) 2010-2021 OneLogin, Inc.
44
# MIT License
55

6-
from base64 import b64decode
6+
from base64 import b64decode, b64encode
77
from lxml import etree
88
from datetime import datetime
99
from datetime import timedelta
@@ -14,9 +14,11 @@
1414
from xml.dom.minidom import parseString
1515

1616
from onelogin.saml2 import compat
17+
from onelogin.saml2.constants import OneLogin_Saml2_Constants
1718
from onelogin.saml2.response import OneLogin_Saml2_Response
1819
from onelogin.saml2.settings import OneLogin_Saml2_Settings
1920
from onelogin.saml2.utils import OneLogin_Saml2_Utils
21+
from onelogin.saml2.xml_utils import OneLogin_Saml2_XML
2022

2123

2224
class OneLogin_Saml2_Response_Test(unittest.TestCase):
@@ -1861,3 +1863,49 @@ def testGetAssertionNotOnOrAfter(self):
18611863
response.is_valid(request_data)
18621864
self.assertIsNone(response.get_error())
18631865
self.assertEqual(response.get_assertion_not_on_or_after(), 2671081021)
1866+
1867+
def testEncryptedId(self):
1868+
"""
1869+
Test that decrypting EncryptedID elements works as expected.
1870+
"""
1871+
settings = OneLogin_Saml2_Settings(self.loadSettingsJSON())
1872+
1873+
base64_content = self.file_contents(join(self.data_path, 'responses', 'valid_unsigned_response.xml.base64'))
1874+
xml = b64decode(base64_content)
1875+
response_element = OneLogin_Saml2_XML.to_etree(xml)
1876+
1877+
# Add an EncryptedID element to the existing response.
1878+
encrypted_id = OneLogin_Saml2_Utils.generate_name_id(
1879+
"123456782",
1880+
sp_nq=None,
1881+
nq="urn:etoegang:1.9:EntityConcernedID:RSIN",
1882+
sp_format="urn:oasis:names:tc:SAML:2.0:nameid-format:persistent",
1883+
cert=settings.get_sp_cert(),
1884+
)
1885+
attribute = (
1886+
'<saml:Attribute xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" FriendlyName="ActingSubjectID" Name="urn:etoegang:core:LegalSubjectID">'
1887+
"<saml:AttributeValue>"
1888+
+ encrypted_id +
1889+
"</saml:AttributeValue></saml:Attribute>"
1890+
)
1891+
statement_element = OneLogin_Saml2_XML.query(response_element, '//saml:AttributeStatement')
1892+
encrypted_attribute_element = OneLogin_Saml2_XML.to_etree(attribute)
1893+
statement_element[0].append(encrypted_attribute_element)
1894+
1895+
# Try to parse the Response
1896+
response = OneLogin_Saml2_Response(
1897+
settings, b64encode(OneLogin_Saml2_XML.to_string(response_element))
1898+
)
1899+
response.is_valid(self.get_request_data())
1900+
attributes = response.get_attributes()
1901+
1902+
self.assertEqual(
1903+
attributes['urn:etoegang:core:LegalSubjectID'],
1904+
[
1905+
{'NameID': {
1906+
'Format': 'urn:oasis:names:tc:SAML:2.0:nameid-format:persistent',
1907+
'NameQualifier': 'urn:etoegang:1.9:EntityConcernedID:RSIN',
1908+
'value': '123456782'}
1909+
}
1910+
]
1911+
)

0 commit comments

Comments
 (0)