Skip to content

Commit 730f5d5

Browse files
committed
fix(jwk): group key related errors with KeyParameterError
1 parent 26ada47 commit 730f5d5

File tree

6 files changed

+25
-20
lines changed

6 files changed

+25
-20
lines changed

src/joserfc/_rfc7517/models.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
)
1414
from ..util import to_bytes
1515
from ..errors import (
16+
KeyParameterError,
1617
UnsupportedKeyUseError,
1718
UnsupportedKeyAlgorithmError,
1819
UnsupportedKeyOperationError,
@@ -58,13 +59,13 @@ def as_bytes(
5859
def validate_dict_key_registry(cls, dict_key: DictKey, registry: KeyParameterRegistryDict) -> None:
5960
for k in registry:
6061
if registry[k].required and k not in dict_key:
61-
raise ValueError(f"'{k}' is required")
62+
raise KeyParameterError(f"'{k}' is required")
6263

6364
if k in dict_key:
6465
try:
6566
registry[k].validate(dict_key[k])
6667
except ValueError as error:
67-
raise ValueError(f"'{k}' {error}")
68+
raise KeyParameterError(f"'{k}' {error}")
6869

6970
@classmethod
7071
def validate_dict_key_use_operations(cls, dict_key: DictKey) -> None:
@@ -73,7 +74,7 @@ def validate_dict_key_use_operations(cls, dict_key: DictKey) -> None:
7374
operations = cls.use_key_ops_registry[_use]
7475
for op in dict_key["key_ops"]:
7576
if op not in operations:
76-
raise ValueError("'use' and 'key_ops' does not match")
77+
raise KeyParameterError("'use' and 'key_ops' does not match")
7778

7879

7980
class BaseKey(t.Generic[NativePrivateKey, NativePublicKey], metaclass=ABCMeta):

src/joserfc/_rfc7517/pem.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def dump_pem_key(
7171
elif encoding == "DER":
7272
encoding_enum = Encoding.DER
7373
else: # pragma: no cover
74-
raise ValueError(f"Invalid encoding: {encoding!r}")
74+
raise ValueError(f"Invalid encoding: {encoding}")
7575

7676
if private:
7777
encryption_algorithm: KeySerializationEncryption

src/joserfc/_rfc7518/rsa_key.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
)
1616
from cryptography.hazmat.backends import default_backend
1717
from ..registry import KeyParameter
18-
from ..errors import SecurityWarning
18+
from ..errors import SecurityWarning, KeyParameterError
1919
from .._rfc7517.models import AsymmetricKey
2020
from .._rfc7517.pem import CryptographyBinding
2121
from .._rfc7517.types import KeyParameters, AnyKey
@@ -194,6 +194,6 @@ def has_all_prime_factors(obj: RSADictKey) -> bool:
194194
return True
195195

196196
if any(props_found):
197-
raise ValueError("RSA key must include all parameters if any are present besides d")
197+
raise KeyParameterError("RSA key must include all parameters if any are present besides d")
198198

199199
return False

src/joserfc/errors.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,31 @@ def __init__(self, description: str | None = None):
2626
# --- Key related errors --- #
2727

2828

29+
class KeyParameterError(JoseError):
30+
error = "key_parameter"
31+
32+
2933
class MissingKeyError(JoseError):
3034
error = "missing_key"
3135

3236

33-
class UnsupportedKeyUseError(JoseError):
37+
class UnsupportedKeyUseError(KeyParameterError):
3438
error = "unsupported_key_use"
3539

3640

37-
class UnsupportedKeyAlgorithmError(JoseError):
41+
class UnsupportedKeyAlgorithmError(KeyParameterError):
3842
error = "unsupported_key_alg"
3943

4044

41-
class UnsupportedKeyOperationError(JoseError):
45+
class UnsupportedKeyOperationError(KeyParameterError):
4246
error = "unsupported_key_operation"
4347

4448

45-
class MissingKeyTypeError(JoseError):
49+
class MissingKeyTypeError(KeyParameterError):
4650
error = "missing_key_type"
4751

4852

49-
class InvalidKeyTypeError(JoseError):
53+
class InvalidKeyTypeError(KeyParameterError):
5054
error = "invalid_key_type"
5155

5256

tests/jwk/test_oct_key.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from unittest import TestCase
22
from joserfc.jwk import OctKey
3-
from joserfc.errors import SecurityWarning
3+
from joserfc.errors import SecurityWarning, KeyParameterError
44
from tests.keys import read_key
55

66

@@ -55,35 +55,35 @@ def test_import_missing_k(self):
5555
"kty": "oct",
5656
"alg": "A128KW",
5757
}
58-
self.assertRaises(ValueError, OctKey.import_key, data)
58+
self.assertRaises(KeyParameterError, OctKey.import_key, data)
5959

6060
def test_invalid_typeof_k(self):
6161
data = {
6262
"kty": "oct",
6363
"alg": "A128KW",
6464
"k": 123,
6565
}
66-
self.assertRaises(ValueError, OctKey.import_key, data)
66+
self.assertRaises(KeyParameterError, OctKey.import_key, data)
6767

6868
def test_mismatch_use_key_ops(self):
6969
data = {"kty": "oct", "alg": "A128KW", "k": "GawgguFyGrWKav7AX4VKUg", "use": "sig", "key_ops": ["wrapKey"]}
70-
self.assertRaises(ValueError, OctKey.import_key, data)
70+
self.assertRaises(KeyParameterError, OctKey.import_key, data)
7171

7272
def test_invalid_use(self):
7373
data = {
7474
"kty": "oct",
7575
"k": "GawgguFyGrWKav7AX4VKUg",
7676
"use": "invalid",
7777
}
78-
self.assertRaises(ValueError, OctKey.import_key, data)
78+
self.assertRaises(KeyParameterError, OctKey.import_key, data)
7979

8080
def test_invalid_key_ops(self):
8181
data = {
8282
"kty": "oct",
8383
"k": "GawgguFyGrWKav7AX4VKUg",
8484
"key_ops": ["invalid"],
8585
}
86-
self.assertRaises(ValueError, OctKey.import_key, data)
86+
self.assertRaises(KeyParameterError, OctKey.import_key, data)
8787

8888
def test_import_pem_key(self):
8989
public_pem = read_key("ec-p256-public.pem")

tests/jwk/test_rsa_key.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from unittest import TestCase
22
from joserfc.jwk import RSAKey
3-
from joserfc.errors import SecurityWarning, InvalidKeyTypeError
3+
from joserfc.errors import SecurityWarning, KeyParameterError, InvalidKeyTypeError
44
from tests.keys import read_key
55

66

@@ -42,7 +42,7 @@ def test_with_oth(self):
4242
"e": "AQAB",
4343
"oth": "invalid information",
4444
}
45-
self.assertRaises(ValueError, RSAKey.import_key, data)
45+
self.assertRaises(KeyParameterError, RSAKey.import_key, data)
4646

4747
def test_import_only_from_d(self):
4848
data = {
@@ -72,7 +72,7 @@ def test_import_only_from_d(self):
7272
"9vDQSccA3xXHOAFOPJ8R9EeIAbTi1VwBYnbTp87X-xcPWlEPkrdoUKW60tgs1aNd_Nnc"
7373
"9LEVVPMS390zbFxt8TN_biaBgelNgbC95sM"
7474
)
75-
self.assertRaises(ValueError, RSAKey.import_key, data)
75+
self.assertRaises(KeyParameterError, RSAKey.import_key, data)
7676

7777
def test_import_key_from_ssh(self):
7878
ssh_public_pem = read_key("ssh-rsa-public.pem")

0 commit comments

Comments
 (0)