24
24
25
25
from pathutils import full_path
26
26
27
+
27
28
SIGNED = full_path ("saml_signed.xml" )
28
29
UNSIGNED = full_path ("saml_unsigned.xml" )
29
30
SIMPLE_SAML_PHP_RESPONSE = full_path ("simplesamlphp_authnresponse.xml" )
36
37
ENC_PUB_KEY = full_path ("pki/test_1.crt" )
37
38
ENC_PRIV_KEY = full_path ("pki/test.key" )
38
39
40
+ INVALID_KEY = full_path ("non-existent.key" )
41
+
42
+ IDP_EXAMPLE = full_path ("idp_example.xml" )
43
+ METADATA_CERT = full_path ("metadata_cert.xml" )
44
+
45
+
39
46
def _eq (l1 , l2 ):
40
47
return set (l1 ) == set (l2 )
41
48
@@ -721,7 +728,7 @@ def setup_class(self):
721
728
conf = config .SPConfig ()
722
729
conf .load_file ("server_conf" )
723
730
md = MetadataStore ([saml , samlp ], None , conf )
724
- md .load ("local" , full_path ( "metadata_cert.xml" ) )
731
+ md .load ("local" , METADATA_CERT )
725
732
726
733
conf .metadata = md
727
734
conf .only_use_keys_in_metadata = False
@@ -742,7 +749,7 @@ def setup_class(self):
742
749
conf = config .SPConfig ()
743
750
conf .load_file ("server_conf" )
744
751
md = MetadataStore ([saml , samlp ], None , conf )
745
- md .load ("local" , full_path ( "metadata_cert.xml" ) )
752
+ md .load ("local" , METADATA_CERT )
746
753
747
754
conf .metadata = md
748
755
conf .only_use_keys_in_metadata = False
@@ -762,7 +769,7 @@ def test_xbox():
762
769
conf = config .SPConfig ()
763
770
conf .load_file ("server_conf" )
764
771
md = MetadataStore ([saml , samlp ], None , conf )
765
- md .load ("local" , full_path ( "idp_example.xml" ) )
772
+ md .load ("local" , IDP_EXAMPLE )
766
773
767
774
conf .metadata = md
768
775
conf .only_use_keys_in_metadata = False
@@ -773,49 +780,58 @@ def test_xbox():
773
780
issue_instant = "2009-10-30T13:20:28Z" ,
774
781
signature = sigver .pre_signature_part ("11111" , sec .my_cert , 1 ),
775
782
attribute_statement = do_attribute_statement (
776
- {("" , "" , "surName" ): ("Foo" , "" ),
777
- ("" , "" , "givenName" ): ("Bar" , "" ), })
783
+ {
784
+ ("" , "" , "surName" ): ("Foo" , "" ),
785
+ ("" , "" , "givenName" ): ("Bar" , "" ),
786
+ }
787
+ )
778
788
)
779
789
780
- sigass = sec .sign_statement (assertion , class_name (assertion ),
781
- key_file = full_path ("test.key" ),
782
- node_id = assertion .id )
790
+ sigass = sec .sign_statement (
791
+ assertion ,
792
+ class_name (assertion ),
793
+ key_file = PRIV_KEY ,
794
+ node_id = assertion .id ,
795
+ )
783
796
784
797
_ass0 = saml .assertion_from_string (sigass )
785
-
786
798
encrypted_assertion = EncryptedAssertion ()
787
799
encrypted_assertion .add_extension_element (_ass0 )
788
800
789
- _ , pre = make_temp (str (pre_encryption_part ()).encode ('utf-8' ), decode = False )
801
+ _ , pre = make_temp (
802
+ str (pre_encryption_part ()).encode ('utf-8' ), decode = False
803
+ )
790
804
enctext = sec .crypto .encrypt (
791
- str (encrypted_assertion ), conf .cert_file , pre , "des-192" ,
792
- '/*[local-name()="EncryptedAssertion"]/*[local-name()="Assertion"]' )
805
+ str (encrypted_assertion ),
806
+ conf .cert_file ,
807
+ pre ,
808
+ "des-192" ,
809
+ '/*[local-name()="EncryptedAssertion"]/*[local-name()="Assertion"]' ,
810
+ )
793
811
794
812
decr_text = sec .decrypt (enctext , key_file = PRIV_KEY )
795
813
_seass = saml .encrypted_assertion_from_string (decr_text )
796
814
assertions = []
797
- assers = extension_elements_to_elements (_seass .extension_elements ,
798
- [saml , samlp ])
799
-
800
- sign_cert_file = full_path ("test.pem" )
815
+ assers = extension_elements_to_elements (
816
+ _seass .extension_elements , [saml , samlp ]
817
+ )
801
818
802
819
for ass in assers :
803
- _ass = "%s" % ass
804
- #_ass = _ass.replace('xsi:nil="true" ', '')
805
- #assert sigass == _ass
806
- _txt = sec .verify_signature (_ass , sign_cert_file ,
807
- node_name = class_name (assertion ))
820
+ _txt = sec .verify_signature (
821
+ str (ass ), PUB_KEY , node_name = class_name (assertion )
822
+ )
808
823
if _txt :
809
824
assertions .append (ass )
810
825
826
+ assert assertions
811
827
print (assertions )
812
828
813
829
814
830
def test_xbox_non_ascii_ava ():
815
831
conf = config .SPConfig ()
816
832
conf .load_file ("server_conf" )
817
833
md = MetadataStore ([saml , samlp ], None , conf )
818
- md .load ("local" , full_path ( "idp_example.xml" ) )
834
+ md .load ("local" , IDP_EXAMPLE )
819
835
820
836
conf .metadata = md
821
837
conf .only_use_keys_in_metadata = False
@@ -826,41 +842,50 @@ def test_xbox_non_ascii_ava():
826
842
issue_instant = "2009-10-30T13:20:28Z" ,
827
843
signature = sigver .pre_signature_part ("11111" , sec .my_cert , 1 ),
828
844
attribute_statement = do_attribute_statement (
829
- {("" , "" , "surName" ): ("Föö" , "" ),
830
- ("" , "" , "givenName" ): ("Bär" , "" ), })
845
+ {
846
+ ("" , "" , "surName" ): ("Föö" , "" ),
847
+ ("" , "" , "givenName" ): ("Bär" , "" ),
848
+ }
849
+ )
831
850
)
832
851
833
- sigass = sec .sign_statement (assertion , class_name (assertion ),
834
- key_file = full_path ("test.key" ),
835
- node_id = assertion .id )
852
+ sigass = sec .sign_statement (
853
+ assertion ,
854
+ class_name (assertion ),
855
+ key_file = PRIV_KEY ,
856
+ node_id = assertion .id ,
857
+ )
836
858
837
859
_ass0 = saml .assertion_from_string (sigass )
838
-
839
860
encrypted_assertion = EncryptedAssertion ()
840
861
encrypted_assertion .add_extension_element (_ass0 )
841
862
842
- _ , pre = make_temp (str (pre_encryption_part ()).encode ('utf-8' ), decode = False )
863
+ _ , pre = make_temp (
864
+ str (pre_encryption_part ()).encode ('utf-8' ), decode = False
865
+ )
843
866
enctext = sec .crypto .encrypt (
844
- str (encrypted_assertion ), conf .cert_file , pre , "des-192" ,
845
- '/*[local-name()="EncryptedAssertion"]/*[local-name()="Assertion"]' )
867
+ str (encrypted_assertion ),
868
+ conf .cert_file ,
869
+ pre ,
870
+ "des-192" ,
871
+ '/*[local-name()="EncryptedAssertion"]/*[local-name()="Assertion"]' ,
872
+ )
846
873
847
874
decr_text = sec .decrypt (enctext , key_file = PRIV_KEY )
848
875
_seass = saml .encrypted_assertion_from_string (decr_text )
849
876
assertions = []
850
- assers = extension_elements_to_elements (_seass .extension_elements ,
851
- [saml , samlp ])
852
-
853
- sign_cert_file = full_path ("test.pem" )
877
+ assers = extension_elements_to_elements (
878
+ _seass .extension_elements , [saml , samlp ]
879
+ )
854
880
855
881
for ass in assers :
856
- _ass = "%s" % ass
857
- #_ass = _ass.replace('xsi:nil="true" ', '')
858
- #assert sigass == _ass
859
- _txt = sec .verify_signature (_ass , sign_cert_file ,
860
- node_name = class_name (assertion ))
882
+ _txt = sec .verify_signature (
883
+ str (ass ), PUB_KEY , node_name = class_name (assertion )
884
+ )
861
885
if _txt :
862
886
assertions .append (ass )
863
887
888
+ assert assertions
864
889
print (assertions )
865
890
866
891
@@ -869,7 +894,7 @@ def test_okta():
869
894
conf .load_file ("server_conf" )
870
895
conf .id_attr_name = 'Id'
871
896
md = MetadataStore ([saml , samlp ], None , conf )
872
- md .load ("local" , full_path ( "idp_example.xml" ) )
897
+ md .load ("local" , IDP_EXAMPLE )
873
898
874
899
conf .metadata = md
875
900
conf .only_use_keys_in_metadata = False
@@ -892,7 +917,7 @@ def test_xmlsec_err():
892
917
conf = config .SPConfig ()
893
918
conf .load_file ("server_conf" )
894
919
md = MetadataStore ([saml , samlp ], None , conf )
895
- md .load ("local" , full_path ( "idp_example.xml" ) )
920
+ md .load ("local" , IDP_EXAMPLE )
896
921
897
922
conf .metadata = md
898
923
conf .only_use_keys_in_metadata = False
@@ -909,7 +934,7 @@ def test_xmlsec_err():
909
934
910
935
try :
911
936
sec .sign_statement (assertion , class_name (assertion ),
912
- key_file = full_path ( "tes.key" ) ,
937
+ key_file = INVALID_KEY ,
913
938
node_id = assertion .id )
914
939
except (XmlsecError , SigverError ) as err : # should throw an exception
915
940
pass
@@ -921,7 +946,7 @@ def test_xmlsec_err_non_ascii_ava():
921
946
conf = config .SPConfig ()
922
947
conf .load_file ("server_conf" )
923
948
md = MetadataStore ([saml , samlp ], None , conf )
924
- md .load ("local" , full_path ( "idp_example.xml" ) )
949
+ md .load ("local" , IDP_EXAMPLE )
925
950
926
951
conf .metadata = md
927
952
conf .only_use_keys_in_metadata = False
@@ -938,7 +963,7 @@ def test_xmlsec_err_non_ascii_ava():
938
963
939
964
try :
940
965
sec .sign_statement (assertion , class_name (assertion ),
941
- key_file = full_path ( "tes.key" ) ,
966
+ key_file = INVALID_KEY ,
942
967
node_id = assertion .id )
943
968
except (XmlsecError , SigverError ) as err : # should throw an exception
944
969
pass
@@ -950,7 +975,7 @@ def test_sha256_signing():
950
975
conf = config .SPConfig ()
951
976
conf .load_file ("server_conf" )
952
977
md = MetadataStore ([saml , samlp ], None , conf )
953
- md .load ("local" , full_path ( "idp_example.xml" ) )
978
+ md .load ("local" , IDP_EXAMPLE )
954
979
955
980
conf .metadata = md
956
981
conf .only_use_keys_in_metadata = False
@@ -967,7 +992,7 @@ def test_sha256_signing():
967
992
)
968
993
969
994
s = sec .sign_statement (assertion , class_name (assertion ),
970
- key_file = full_path ( "test.key" ) ,
995
+ key_file = PRIV_KEY ,
971
996
node_id = assertion .id )
972
997
assert s
973
998
@@ -976,7 +1001,7 @@ def test_sha256_signing_non_ascii_ava():
976
1001
conf = config .SPConfig ()
977
1002
conf .load_file ("server_conf" )
978
1003
md = MetadataStore ([saml , samlp ], None , conf )
979
- md .load ("local" , full_path ( "idp_example.xml" ) )
1004
+ md .load ("local" , IDP_EXAMPLE )
980
1005
981
1006
conf .metadata = md
982
1007
conf .only_use_keys_in_metadata = False
@@ -993,7 +1018,7 @@ def test_sha256_signing_non_ascii_ava():
993
1018
)
994
1019
995
1020
s = sec .sign_statement (assertion , class_name (assertion ),
996
- key_file = full_path ( "test.key" ) ,
1021
+ key_file = PRIV_KEY ,
997
1022
node_id = assertion .id )
998
1023
assert s
999
1024
0 commit comments