Skip to content

Commit 7f4e3e5

Browse files
committed
Reformat code for test_40_sigver
Signed-off-by: Ivan Kanakarakis <[email protected]>
1 parent 342e376 commit 7f4e3e5

File tree

1 file changed

+74
-49
lines changed

1 file changed

+74
-49
lines changed

tests/test_40_sigver.py

Lines changed: 74 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
from pathutils import full_path
2626

27+
2728
SIGNED = full_path("saml_signed.xml")
2829
UNSIGNED = full_path("saml_unsigned.xml")
2930
SIMPLE_SAML_PHP_RESPONSE = full_path("simplesamlphp_authnresponse.xml")
@@ -36,6 +37,12 @@
3637
ENC_PUB_KEY = full_path("pki/test_1.crt")
3738
ENC_PRIV_KEY = full_path("pki/test.key")
3839

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+
3946
def _eq(l1, l2):
4047
return set(l1) == set(l2)
4148

@@ -721,7 +728,7 @@ def setup_class(self):
721728
conf = config.SPConfig()
722729
conf.load_file("server_conf")
723730
md = MetadataStore([saml, samlp], None, conf)
724-
md.load("local", full_path("metadata_cert.xml"))
731+
md.load("local", METADATA_CERT)
725732

726733
conf.metadata = md
727734
conf.only_use_keys_in_metadata = False
@@ -742,7 +749,7 @@ def setup_class(self):
742749
conf = config.SPConfig()
743750
conf.load_file("server_conf")
744751
md = MetadataStore([saml, samlp], None, conf)
745-
md.load("local", full_path("metadata_cert.xml"))
752+
md.load("local", METADATA_CERT)
746753

747754
conf.metadata = md
748755
conf.only_use_keys_in_metadata = False
@@ -762,7 +769,7 @@ def test_xbox():
762769
conf = config.SPConfig()
763770
conf.load_file("server_conf")
764771
md = MetadataStore([saml, samlp], None, conf)
765-
md.load("local", full_path("idp_example.xml"))
772+
md.load("local", IDP_EXAMPLE)
766773

767774
conf.metadata = md
768775
conf.only_use_keys_in_metadata = False
@@ -773,49 +780,58 @@ def test_xbox():
773780
issue_instant="2009-10-30T13:20:28Z",
774781
signature=sigver.pre_signature_part("11111", sec.my_cert, 1),
775782
attribute_statement=do_attribute_statement(
776-
{("", "", "surName"): ("Foo", ""),
777-
("", "", "givenName"): ("Bar", ""), })
783+
{
784+
("", "", "surName"): ("Foo", ""),
785+
("", "", "givenName"): ("Bar", ""),
786+
}
787+
)
778788
)
779789

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+
)
783796

784797
_ass0 = saml.assertion_from_string(sigass)
785-
786798
encrypted_assertion = EncryptedAssertion()
787799
encrypted_assertion.add_extension_element(_ass0)
788800

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+
)
790804
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+
)
793811

794812
decr_text = sec.decrypt(enctext, key_file=PRIV_KEY)
795813
_seass = saml.encrypted_assertion_from_string(decr_text)
796814
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+
)
801818

802819
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+
)
808823
if _txt:
809824
assertions.append(ass)
810825

826+
assert assertions
811827
print(assertions)
812828

813829

814830
def test_xbox_non_ascii_ava():
815831
conf = config.SPConfig()
816832
conf.load_file("server_conf")
817833
md = MetadataStore([saml, samlp], None, conf)
818-
md.load("local", full_path("idp_example.xml"))
834+
md.load("local", IDP_EXAMPLE)
819835

820836
conf.metadata = md
821837
conf.only_use_keys_in_metadata = False
@@ -826,41 +842,50 @@ def test_xbox_non_ascii_ava():
826842
issue_instant="2009-10-30T13:20:28Z",
827843
signature=sigver.pre_signature_part("11111", sec.my_cert, 1),
828844
attribute_statement=do_attribute_statement(
829-
{("", "", "surName"): ("Föö", ""),
830-
("", "", "givenName"): ("Bär", ""), })
845+
{
846+
("", "", "surName"): ("Föö", ""),
847+
("", "", "givenName"): ("Bär", ""),
848+
}
849+
)
831850
)
832851

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+
)
836858

837859
_ass0 = saml.assertion_from_string(sigass)
838-
839860
encrypted_assertion = EncryptedAssertion()
840861
encrypted_assertion.add_extension_element(_ass0)
841862

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+
)
843866
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+
)
846873

847874
decr_text = sec.decrypt(enctext, key_file=PRIV_KEY)
848875
_seass = saml.encrypted_assertion_from_string(decr_text)
849876
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+
)
854880

855881
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+
)
861885
if _txt:
862886
assertions.append(ass)
863887

888+
assert assertions
864889
print(assertions)
865890

866891

@@ -869,7 +894,7 @@ def test_okta():
869894
conf.load_file("server_conf")
870895
conf.id_attr_name = 'Id'
871896
md = MetadataStore([saml, samlp], None, conf)
872-
md.load("local", full_path("idp_example.xml"))
897+
md.load("local", IDP_EXAMPLE)
873898

874899
conf.metadata = md
875900
conf.only_use_keys_in_metadata = False
@@ -892,7 +917,7 @@ def test_xmlsec_err():
892917
conf = config.SPConfig()
893918
conf.load_file("server_conf")
894919
md = MetadataStore([saml, samlp], None, conf)
895-
md.load("local", full_path("idp_example.xml"))
920+
md.load("local", IDP_EXAMPLE)
896921

897922
conf.metadata = md
898923
conf.only_use_keys_in_metadata = False
@@ -909,7 +934,7 @@ def test_xmlsec_err():
909934

910935
try:
911936
sec.sign_statement(assertion, class_name(assertion),
912-
key_file=full_path("tes.key"),
937+
key_file=INVALID_KEY,
913938
node_id=assertion.id)
914939
except (XmlsecError, SigverError) as err: # should throw an exception
915940
pass
@@ -921,7 +946,7 @@ def test_xmlsec_err_non_ascii_ava():
921946
conf = config.SPConfig()
922947
conf.load_file("server_conf")
923948
md = MetadataStore([saml, samlp], None, conf)
924-
md.load("local", full_path("idp_example.xml"))
949+
md.load("local", IDP_EXAMPLE)
925950

926951
conf.metadata = md
927952
conf.only_use_keys_in_metadata = False
@@ -938,7 +963,7 @@ def test_xmlsec_err_non_ascii_ava():
938963

939964
try:
940965
sec.sign_statement(assertion, class_name(assertion),
941-
key_file=full_path("tes.key"),
966+
key_file=INVALID_KEY,
942967
node_id=assertion.id)
943968
except (XmlsecError, SigverError) as err: # should throw an exception
944969
pass
@@ -950,7 +975,7 @@ def test_sha256_signing():
950975
conf = config.SPConfig()
951976
conf.load_file("server_conf")
952977
md = MetadataStore([saml, samlp], None, conf)
953-
md.load("local", full_path("idp_example.xml"))
978+
md.load("local", IDP_EXAMPLE)
954979

955980
conf.metadata = md
956981
conf.only_use_keys_in_metadata = False
@@ -967,7 +992,7 @@ def test_sha256_signing():
967992
)
968993

969994
s = sec.sign_statement(assertion, class_name(assertion),
970-
key_file=full_path("test.key"),
995+
key_file=PRIV_KEY,
971996
node_id=assertion.id)
972997
assert s
973998

@@ -976,7 +1001,7 @@ def test_sha256_signing_non_ascii_ava():
9761001
conf = config.SPConfig()
9771002
conf.load_file("server_conf")
9781003
md = MetadataStore([saml, samlp], None, conf)
979-
md.load("local", full_path("idp_example.xml"))
1004+
md.load("local", IDP_EXAMPLE)
9801005

9811006
conf.metadata = md
9821007
conf.only_use_keys_in_metadata = False
@@ -993,7 +1018,7 @@ def test_sha256_signing_non_ascii_ava():
9931018
)
9941019

9951020
s = sec.sign_statement(assertion, class_name(assertion),
996-
key_file=full_path("test.key"),
1021+
key_file=PRIV_KEY,
9971022
node_id=assertion.id)
9981023
assert s
9991024

0 commit comments

Comments
 (0)