1+ import re
2+
13from contextlib import closing
4+
25from saml2 .authn_context import INTERNETPROTOCOLPASSWORD
36from saml2 .server import Server
47from saml2 .sigver import pre_encryption_part , ASSERT_XPATH , EncryptError
912
1013__author__ = 'roland'
1114
12- TMPL_NO_HEADER = """<ns0:EncryptedData xmlns:ns0="http://www.w3.org/2001/04/xmlenc#" xmlns:ns1="http://www.w3.org/2000/09/xmldsig#" Id="ED " Type="http://www.w3.org/2001/04/xmlenc#Element"><ns0:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" /><ns1:KeyInfo><ns0:EncryptedKey Id="EK "><ns0:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" /><ns1:KeyInfo><ns1:KeyName>my-rsa-key</ns1:KeyName></ns1:KeyInfo><ns0:CipherData><ns0:CipherValue /></ns0:CipherData></ns0:EncryptedKey></ns1:KeyInfo><ns0:CipherData><ns0:CipherValue /></ns0:CipherData></ns0:EncryptedData>"""
15+ TMPL_NO_HEADER = """<ns0:EncryptedData xmlns:ns0="http://www.w3.org/2001/04/xmlenc#" xmlns:ns1="http://www.w3.org/2000/09/xmldsig#" Id="{ed_id} " Type="http://www.w3.org/2001/04/xmlenc#Element"><ns0:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" /><ns1:KeyInfo><ns0:EncryptedKey Id="{ek_id} "><ns0:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" /><ns1:KeyInfo><ns1:KeyName>my-rsa-key</ns1:KeyName></ns1:KeyInfo><ns0:CipherData><ns0:CipherValue /></ns0:CipherData></ns0:EncryptedKey></ns1:KeyInfo><ns0:CipherData><ns0:CipherValue /></ns0:CipherData></ns0:EncryptedData>"""
1316TMPL = "<?xml version='1.0' encoding='UTF-8'?>\n %s" % TMPL_NO_HEADER
1417
1518IDENTITY = {"eduPersonAffiliation" : ["staff" , "member" ],
2427}
2528
2629
27- def test_pre_enc ():
30+ def test_pre_enc_key_format ():
31+ def the_xsd_ID_value_must_start_with_either_a_letter_or_underscore (id ):
32+ result = re .match (r"^[a-zA-Z_]" , id [0 ])
33+ return result
34+
35+ def the_xsd_ID_value_may_contain_only_letters_digits_underscores_hyphens_periods (id ):
36+ result = re .match (r"^[a-zA-Z0-9._-]*$" , id [1 :])
37+ return result
38+
39+ tmpl = pre_encryption_part ()
40+ for id in (tmpl .id , tmpl .key_info .encrypted_key .id ):
41+ assert the_xsd_ID_value_must_start_with_either_a_letter_or_underscore (id )
42+ assert the_xsd_ID_value_may_contain_only_letters_digits_underscores_hyphens_periods (id )
43+
44+
45+ def test_pre_enc_with_pregenerated_key ():
2846 tmpl = pre_encryption_part (encrypted_key_id = "EK" , encrypted_data_id = "ED" )
29- print (tmpl )
30- assert "%s" % tmpl in (TMPL_NO_HEADER , TMPL )
47+ expected = TMPL_NO_HEADER .format (
48+ ed_id = tmpl .id ,
49+ ek_id = tmpl .key_info .encrypted_key .id ,
50+ )
51+ assert str (tmpl ) == expected
52+
53+
54+ def test_pre_enc_with_generated_key ():
55+ tmpl = pre_encryption_part ()
56+ expected = TMPL_NO_HEADER .format (
57+ ed_id = tmpl .id ,
58+ ek_id = tmpl .key_info .encrypted_key .id ,
59+ )
60+ assert str (tmpl ) == expected
3161
3262
3363def test_reshuffle_response ():
@@ -41,7 +71,6 @@ def test_reshuffle_response():
4171
4272 resp2 = pre_encrypt_assertion (resp_ )
4373
44- print (resp2 )
4574 assert resp2 .encrypted_assertion .extension_elements
4675
4776
@@ -74,7 +103,6 @@ def test_enc1():
74103 crypto = CryptoBackendXmlSec1 (xmlsec_path )
75104 (_stdout , _stderr , output ) = crypto ._run_xmlsec (com_list , [tmpl ])
76105
77- print (output )
78106 assert _stderr == ""
79107 assert _stdout == ""
80108
@@ -93,7 +121,6 @@ def test_enc2():
93121 enc_resp = crypto .encrypt_assertion (resp_ , full_path ("pubkey.pem" ),
94122 pre_encryption_part ())
95123
96- print (enc_resp )
97124 assert enc_resp
98125
99126if __name__ == "__main__" :
0 commit comments