1
+ import re
2
+
1
3
from contextlib import closing
4
+
2
5
from saml2 .authn_context import INTERNETPROTOCOLPASSWORD
3
6
from saml2 .server import Server
4
7
from saml2 .sigver import pre_encryption_part , ASSERT_XPATH , EncryptError
9
12
10
13
__author__ = 'roland'
11
14
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>"""
13
16
TMPL = "<?xml version='1.0' encoding='UTF-8'?>\n %s" % TMPL_NO_HEADER
14
17
15
18
IDENTITY = {"eduPersonAffiliation" : ["staff" , "member" ],
24
27
}
25
28
26
29
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 ():
28
46
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
31
61
32
62
33
63
def test_reshuffle_response ():
@@ -41,7 +71,6 @@ def test_reshuffle_response():
41
71
42
72
resp2 = pre_encrypt_assertion (resp_ )
43
73
44
- print (resp2 )
45
74
assert resp2 .encrypted_assertion .extension_elements
46
75
47
76
@@ -74,7 +103,6 @@ def test_enc1():
74
103
crypto = CryptoBackendXmlSec1 (xmlsec_path )
75
104
(_stdout , _stderr , output ) = crypto ._run_xmlsec (com_list , [tmpl ])
76
105
77
- print (output )
78
106
assert _stderr == ""
79
107
assert _stdout == ""
80
108
@@ -93,7 +121,6 @@ def test_enc2():
93
121
enc_resp = crypto .encrypt_assertion (resp_ , full_path ("pubkey.pem" ),
94
122
pre_encryption_part ())
95
123
96
- print (enc_resp )
97
124
assert enc_resp
98
125
99
126
if __name__ == "__main__" :
0 commit comments