Skip to content

Commit a40b55b

Browse files
committed
feat: new unit test
1 parent 1d95397 commit a40b55b

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

pyeudiw/tests/jwt/test_helper.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
from cryptography.hazmat.primitives.asymmetric import ec
2+
from cryptojwt.jwk.ec import ECKey
3+
4+
from pyeudiw.jwk import JWK
15
from pyeudiw.jwt.helper import validate_jwt_timestamps_claims
6+
from pyeudiw.jwt.jws_helper import _validate_key_with_jws_header
27
from pyeudiw.tools.utils import iat_now
8+
import pyeudiw.tests.x509.test_x509 as test_x509
9+
from pyeudiw.x509.verify import DER_cert_to_B64DER_cert
310

411

512
def test_validate_jwt_timestamps_claims_ok():
@@ -71,3 +78,63 @@ def test_test_validate_jwt_timestamps_claims_tolerance_window():
7178
assert (
7279
False
7380
), f"encountered unexpeted error when validating the lifetime of a token payload with a tolerance window (for exp): {e}"
81+
82+
83+
def test_validate_key_with_jws_header_x5c_ok():
84+
private_ec_key = ec.generate_private_key(ec.SECP256R1())
85+
x509_der_chain = test_x509.gen_chain(leaf_private_key=private_ec_key)
86+
x5c = [DER_cert_to_B64DER_cert(der) for der in x509_der_chain]
87+
88+
ec_jwk = ECKey()
89+
ec_jwk.load_key(private_ec_key)
90+
key = ec_jwk.serialize(private=True)
91+
92+
try:
93+
_validate_key_with_jws_header(key, {"x5c": x5c}, {})
94+
assert True
95+
except Exception as e:
96+
assert False, f"unexpected exception when validating header for correct key: {e}"
97+
98+
99+
def test_validate_key_with_jws_header_kid_ok():
100+
key = JWK().as_dict()
101+
kid = "1234567890"
102+
key["kid"] = kid
103+
104+
try:
105+
_validate_key_with_jws_header(key, {"kid": kid}, {})
106+
assert True
107+
except Exception as e:
108+
assert False, f"unexpected exception when validating header for correct key: {e}"
109+
110+
111+
def test_validate_key_with_jws_header_expect_x5c_fail():
112+
private_ec_key = ec.generate_private_key(ec.SECP256R1())
113+
x509_der_chain = test_x509.gen_chain(leaf_private_key=private_ec_key)
114+
x5c = [DER_cert_to_B64DER_cert(der) for der in x509_der_chain]
115+
116+
wrong_ec_key = ec.generate_private_key(ec.SECP256R1())
117+
wrong_ec_jwk = ECKey()
118+
wrong_ec_jwk.load_key(wrong_ec_key)
119+
wrong_key = wrong_ec_jwk.serialize(private=True)
120+
121+
try:
122+
_validate_key_with_jws_header(wrong_key, {"x5c": x5c}, {})
123+
assert False, f"should have encountered exception when validating header 'x5c' for wrong key"
124+
except Exception as _:
125+
assert True
126+
127+
def test_validate_key_with_jws_header_expect_kid_fail():
128+
wrong_key = JWK().as_dict()
129+
wrong_kid = "1234567890"
130+
wrong_key["kid"] = wrong_kid
131+
132+
key = JWK().as_dict()
133+
kid = "qwertyuiop"
134+
key["kid"] = kid
135+
136+
try:
137+
_validate_key_with_jws_header(key, {"kid": "1234567890"}, {})
138+
assert False, f"should have encountered exception when validating header 'kid' for wrong key"
139+
except Exception as _:
140+
assert True

0 commit comments

Comments
 (0)