3
3
# Copyright (c) 2010-2021 OneLogin, Inc.
4
4
# MIT License
5
5
6
- from base64 import b64decode
6
+ from base64 import b64decode , b64encode
7
7
from lxml import etree
8
8
from datetime import datetime
9
9
from datetime import timedelta
14
14
from xml .dom .minidom import parseString
15
15
16
16
from onelogin .saml2 import compat
17
+ from onelogin .saml2 .constants import OneLogin_Saml2_Constants
17
18
from onelogin .saml2 .response import OneLogin_Saml2_Response
18
19
from onelogin .saml2 .settings import OneLogin_Saml2_Settings
19
20
from onelogin .saml2 .utils import OneLogin_Saml2_Utils
21
+ from onelogin .saml2 .xml_utils import OneLogin_Saml2_XML
20
22
21
23
22
24
class OneLogin_Saml2_Response_Test (unittest .TestCase ):
@@ -1861,3 +1863,49 @@ def testGetAssertionNotOnOrAfter(self):
1861
1863
response .is_valid (request_data )
1862
1864
self .assertIsNone (response .get_error ())
1863
1865
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