|
| 1 | +import typing as t |
| 2 | +from unittest import TestCase |
| 3 | +from joserfc import jwt |
| 4 | +from joserfc.jwk import OKPKey |
| 5 | +from joserfc.errors import InvalidKeyTypeError, BadSignatureError |
| 6 | +from tests.base import load_key |
| 7 | + |
| 8 | + |
| 9 | +class TestEdDSA(TestCase): |
| 10 | + x25519_key = t.cast(OKPKey, load_key("okp-x25519-alice.json")) |
| 11 | + ed25519_key = t.cast(OKPKey, load_key("okp-ed25519-private.json")) |
| 12 | + ed448_key = t.cast(OKPKey, load_key("okp-ed448-private.pem")) |
| 13 | + |
| 14 | + def test_EdDSA(self): |
| 15 | + algorithms = ["EdDSA"] |
| 16 | + encoded_jwt = jwt.encode({"alg": "EdDSA"}, {}, self.ed25519_key, algorithms=algorithms) |
| 17 | + jwt.decode(encoded_jwt, self.ed25519_key, algorithms=algorithms) |
| 18 | + self.assertRaises(InvalidKeyTypeError, jwt.decode, encoded_jwt, self.x25519_key, algorithms=algorithms) |
| 19 | + self.assertRaises(InvalidKeyTypeError, jwt.encode, {"alg": "EdDSA"}, {}, self.x25519_key, algorithms=algorithms) |
| 20 | + |
| 21 | + def test_Ed25519(self): |
| 22 | + algorithms = ["Ed25519"] |
| 23 | + encoded_jwt = jwt.encode({"alg": "Ed25519"}, {}, self.ed25519_key, algorithms=algorithms) |
| 24 | + jwt.decode(encoded_jwt, self.ed25519_key, algorithms=algorithms) |
| 25 | + self.assertRaises( |
| 26 | + InvalidKeyTypeError, jwt.encode, {"alg": "Ed25519"}, {}, self.ed448_key, algorithms=algorithms |
| 27 | + ) |
| 28 | + self.assertRaises(InvalidKeyTypeError, jwt.decode, encoded_jwt, self.ed448_key, algorithms=algorithms) |
| 29 | + wrong_key = OKPKey.generate_key("Ed25519", private=False) |
| 30 | + self.assertRaises(BadSignatureError, jwt.decode, encoded_jwt, wrong_key, algorithms=algorithms) |
| 31 | + |
| 32 | + def test_Ed448(self): |
| 33 | + algorithms = ["Ed448"] |
| 34 | + encoded_jwt = jwt.encode({"alg": "Ed448"}, {}, self.ed448_key, algorithms=algorithms) |
| 35 | + jwt.decode(encoded_jwt, self.ed448_key, algorithms=algorithms) |
| 36 | + self.assertRaises( |
| 37 | + InvalidKeyTypeError, jwt.encode, {"alg": "Ed448"}, {}, self.ed25519_key, algorithms=algorithms |
| 38 | + ) |
| 39 | + self.assertRaises(InvalidKeyTypeError, jwt.decode, encoded_jwt, self.ed25519_key, algorithms=algorithms) |
| 40 | + wrong_key = OKPKey.generate_key("Ed448", private=False) |
| 41 | + self.assertRaises(BadSignatureError, jwt.decode, encoded_jwt, wrong_key, algorithms=algorithms) |
0 commit comments