Skip to content

Commit c022fc4

Browse files
committed
fix: improvements on errors
1 parent 6ee717f commit c022fc4

File tree

2 files changed

+52
-19
lines changed

2 files changed

+52
-19
lines changed

src/joserfc/_rfc7518/oct_key.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def import_from_dict(cls, value: DictKey) -> bytes:
3737
def import_from_bytes(cls, value: bytes, password: Any | None = None) -> bytes:
3838
# security check
3939
if value.startswith(POSSIBLE_UNSAFE_KEYS):
40-
warnings.warn("This key may not be safe to import", SecurityWarning)
40+
warnings.warn("This key should not be used as an oct key", SecurityWarning)
4141
return value
4242

4343

src/joserfc/errors.py

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,7 @@ def __init__(self, description: str | None = None):
2323
super(JoseError, self).__init__(message)
2424

2525

26-
class DecodeError(JoseError):
27-
"""This error is designed for JWS/JWE. It is raised when deserialization
28-
and decryption fails.
29-
"""
30-
31-
error = "decode_error"
26+
# --- Key related errors --- #
3227

3328

3429
class MissingKeyError(JoseError):
@@ -47,10 +42,6 @@ class UnsupportedKeyOperationError(JoseError):
4742
error = "unsupported_key_operation"
4843

4944

50-
class InvalidKeyLengthError(JoseError):
51-
error = "invalid_key_length"
52-
53-
5445
class MissingKeyTypeError(JoseError):
5546
error = "missing_key_type"
5647

@@ -59,25 +50,35 @@ class InvalidKeyTypeError(JoseError):
5950
error = "invalid_key_type"
6051

6152

62-
class InvalidKeyCurveError(JoseError):
63-
error = "invalid_key_curve"
64-
65-
6653
class InvalidKeyIdError(JoseError):
54+
"""This error is designed for Key Set. It is raised when a key
55+
can not be found with the given key ID."""
56+
6757
error = "invalid_key_id"
6858

6959

7060
class InvalidExchangeKeyError(JoseError):
61+
"""This error is designed for EC and OKP keys. It is raised when
62+
exchanging derive key failed."""
63+
7164
error = "invalid_exchange_key"
7265
description = "Invalid key for exchanging shared key"
7366

7467

75-
class InvalidEncryptedKeyError(JoseError):
76-
error = "invalid_encrypted_key"
77-
description = "JWE Encrypted Key value SHOULD be an empty octet sequence"
68+
# --- JWS & JWE related errors --- #
69+
70+
71+
class DecodeError(JoseError):
72+
"""This error is designed for both JWS and JWE. It is raised when deserialization
73+
and decryption fails.
74+
"""
75+
76+
error = "decode_error"
7877

7978

8079
class MissingAlgorithmError(JoseError):
80+
"""Raised when an algorithm ("alg") is missing."""
81+
8182
error = "missing_algorithm"
8283
description = "Missing 'alg' value in header"
8384

@@ -87,14 +88,22 @@ class ConflictAlgorithmError(JoseError):
8788

8889

8990
class UnsupportedAlgorithmError(JoseError):
91+
"""This error is designed for both JWS and JWE. It is raised when the
92+
given algorithm is not supported in the registry.
93+
"""
94+
9095
error = "unsupported_algorithm"
9196

9297

9398
class InvalidHeaderValueError(JoseError):
99+
"""Raised when the given header's value is invalid."""
100+
94101
error = "invalid_header_value"
95102

96103

97104
class UnsupportedHeaderError(JoseError):
105+
"""Raised when an unsupported header is encountered."""
106+
98107
error = "unsupported_header"
99108

100109

@@ -126,8 +135,24 @@ class MissingEncryptionError(JoseError):
126135
description = "Missing 'enc' value in header"
127136

128137

138+
class InvalidKeyCurveError(JoseError):
139+
"""This error is designed for JWS. It is raised when key's
140+
curve name does not match with the given algorithm.
141+
"""
142+
143+
error = "invalid_key_curve"
144+
145+
146+
class InvalidKeyLengthError(JoseError):
147+
"""This error is designed for JWE. It is raised when key's
148+
length does not align with the given algorithm.
149+
"""
150+
151+
error = "invalid_key_length"
152+
153+
129154
class BadSignatureError(JoseError):
130-
"""This error is designed for JWS/JWT. It is raised when signature
155+
"""This error is designed for JWS. It is raised when signature
131156
does not match.
132157
"""
133158

@@ -149,6 +174,11 @@ class InvalidEncryptionAlgorithmError(JoseError):
149174
error = "invalid_encryption_algorithm"
150175

151176

177+
class InvalidEncryptedKeyError(JoseError):
178+
error = "invalid_encrypted_key"
179+
description = "JWE Encrypted Key value SHOULD be an empty octet sequence"
180+
181+
152182
class InvalidCEKLengthError(JoseError):
153183
error = "invalid_cek_length"
154184
description = "Invalid 'cek' length"
@@ -158,6 +188,9 @@ def __init__(self, cek_size: int): # pragma: no cover
158188
super(InvalidCEKLengthError, self).__init__(description=description)
159189

160190

191+
# --- JWT related errors --- #
192+
193+
161194
class InvalidClaimError(JoseError):
162195
"""This error is designed for JWT. It raised when the claim contains
163196
invalid values or types."""

0 commit comments

Comments
 (0)