|
15 | 15 | from cryptography.x509 import load_pem_x509_certificate
|
16 | 16 |
|
17 | 17 | from ..constants import ALGORITHMS
|
18 |
| -from ..exceptions import JWEError, JWKError |
| 18 | +from ..exceptions import JWEError, JWKError, JWKAlgMismatchError |
19 | 19 | from ..utils import base64_to_long, base64url_decode, base64url_encode, ensure_binary, long_to_base64
|
20 | 20 | from .base import Key
|
21 | 21 |
|
@@ -52,7 +52,7 @@ class CryptographyECKey(Key):
|
52 | 52 |
|
53 | 53 | def __init__(self, key, algorithm, cryptography_backend=default_backend):
|
54 | 54 | if algorithm not in ALGORITHMS.EC:
|
55 |
| - raise JWKError("hash_alg: %s is not a valid hash algorithm" % algorithm) |
| 55 | + raise JWKAlgMismatchError("%s is not a valid EC algorithm" % algorithm) |
56 | 56 |
|
57 | 57 | self.hash_alg = {
|
58 | 58 | ALGORITHMS.ES256: self.SHA256,
|
@@ -97,7 +97,7 @@ def __init__(self, key, algorithm, cryptography_backend=default_backend):
|
97 | 97 |
|
98 | 98 | def _process_jwk(self, jwk_dict):
|
99 | 99 | if not jwk_dict.get("kty") == "EC":
|
100 |
| - raise JWKError("Incorrect key type. Expected: 'EC', Received: %s" % jwk_dict.get("kty")) |
| 100 | + raise JWKAlgMismatchError("Incorrect key type. Expected: 'EC', Received: %s" % jwk_dict.get("kty")) |
101 | 101 |
|
102 | 102 | if not all(k in jwk_dict for k in ["x", "y", "crv"]):
|
103 | 103 | raise JWKError("Mandatory parameters are missing")
|
@@ -226,7 +226,7 @@ class CryptographyRSAKey(Key):
|
226 | 226 |
|
227 | 227 | def __init__(self, key, algorithm, cryptography_backend=default_backend):
|
228 | 228 | if algorithm not in ALGORITHMS.RSA:
|
229 |
| - raise JWKError("hash_alg: %s is not a valid hash algorithm" % algorithm) |
| 229 | + raise JWKAlgMismatchError("%s is not a valid RSA algorithm" % algorithm) |
230 | 230 |
|
231 | 231 | self.hash_alg = {
|
232 | 232 | ALGORITHMS.RS256: self.SHA256,
|
@@ -273,7 +273,7 @@ def __init__(self, key, algorithm, cryptography_backend=default_backend):
|
273 | 273 |
|
274 | 274 | def _process_jwk(self, jwk_dict):
|
275 | 275 | if not jwk_dict.get("kty") == "RSA":
|
276 |
| - raise JWKError("Incorrect key type. Expected: 'RSA', Received: %s" % jwk_dict.get("kty")) |
| 276 | + raise JWKAlgMismatchError("Incorrect key type. Expected: 'RSA', Received: %s" % jwk_dict.get("kty")) |
277 | 277 |
|
278 | 278 | e = base64_to_long(jwk_dict.get("e", 256))
|
279 | 279 | n = base64_to_long(jwk_dict.get("n"))
|
@@ -441,9 +441,9 @@ class CryptographyAESKey(Key):
|
441 | 441 |
|
442 | 442 | def __init__(self, key, algorithm):
|
443 | 443 | if algorithm not in ALGORITHMS.AES:
|
444 |
| - raise JWKError("%s is not a valid AES algorithm" % algorithm) |
| 444 | + raise JWKAlgMismatchError("%s is not a valid AES algorithm" % algorithm) |
445 | 445 | if algorithm not in ALGORITHMS.SUPPORTED.union(ALGORITHMS.AES_PSEUDO):
|
446 |
| - raise JWKError("%s is not a supported algorithm" % algorithm) |
| 446 | + raise JWKAlgMismatchError("%s is not a supported algorithm" % algorithm) |
447 | 447 |
|
448 | 448 | self._algorithm = algorithm
|
449 | 449 | self._mode = self.MODES.get(self._algorithm)
|
@@ -538,7 +538,7 @@ class CryptographyHMACKey(Key):
|
538 | 538 |
|
539 | 539 | def __init__(self, key, algorithm):
|
540 | 540 | if algorithm not in ALGORITHMS.HMAC:
|
541 |
| - raise JWKError("hash_alg: %s is not a valid hash algorithm" % algorithm) |
| 541 | + raise JWKAlgMismatchError("hash_alg: %s is not a valid hash algorithm" % algorithm) |
542 | 542 | self._algorithm = algorithm
|
543 | 543 | self._hash_alg = self.ALG_MAP.get(algorithm)
|
544 | 544 |
|
@@ -569,7 +569,7 @@ def __init__(self, key, algorithm):
|
569 | 569 |
|
570 | 570 | def _process_jwk(self, jwk_dict):
|
571 | 571 | if not jwk_dict.get("kty") == "oct":
|
572 |
| - raise JWKError("Incorrect key type. Expected: 'oct', Received: %s" % jwk_dict.get("kty")) |
| 572 | + raise JWKAlgMismatchError("Incorrect key type. Expected: 'oct', Received: %s" % jwk_dict.get("kty")) |
573 | 573 |
|
574 | 574 | k = jwk_dict.get("k")
|
575 | 575 | k = k.encode("utf-8")
|
|
0 commit comments