From 517ed27bb1d0dcd0c9ad7ea58f7cb6df43cc37b2 Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Wed, 4 Jan 2023 18:28:24 +0100 Subject: [PATCH 1/3] Improve error messages when cryptography isn't installed --- jwt/api_jwk.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/jwt/api_jwk.py b/jwt/api_jwk.py index ef8cce8b..fdcde21a 100644 --- a/jwt/api_jwk.py +++ b/jwt/api_jwk.py @@ -4,7 +4,7 @@ import time from typing import Any, Optional -from .algorithms import get_default_algorithms +from .algorithms import get_default_algorithms, has_crypto, requires_cryptography from .exceptions import InvalidKeyError, PyJWKError, PyJWKSetError from .types import JWKDict @@ -49,10 +49,13 @@ def __init__(self, jwk_data: JWKDict, algorithm: Optional[str] = None) -> None: else: raise InvalidKeyError(f"Unsupported kty: {kty}") + if not has_crypto and algorithm in requires_cryptography: + raise PyJWKError(f"{algorithm} requires 'cryptography' to be installed.") + self.Algorithm = self._algorithms.get(algorithm) if not self.Algorithm: - raise PyJWKError(f"Unable to find a algorithm for key: {self._jwk_data}") + raise PyJWKError(f"Unable to find an algorithm for key: {self._jwk_data}") self.key = self.Algorithm.from_jwk(self._jwk_data) @@ -66,11 +69,11 @@ def from_json(data: str, algorithm: None = None) -> "PyJWK": return PyJWK.from_dict(obj, algorithm) @property - def key_type(self) -> str: + def key_type(self) -> Optional[str]: return self._jwk_data.get("kty", None) @property - def key_id(self) -> str: + def key_id(self) -> Optional[str]: return self._jwk_data.get("kid", None) @property @@ -96,7 +99,9 @@ def __init__(self, keys: list[JWKDict]) -> None: continue if len(self.keys) == 0: - raise PyJWKSetError("The JWK Set did not contain any usable keys") + raise PyJWKSetError( + "The JWK Set did not contain any usable keys. Perhaps 'cryptography' is not installed?" + ) @staticmethod def from_dict(obj: dict[str, Any]) -> "PyJWKSet": From 95f8bb68eedeedf6f1777adfb676b23b50bebe2c Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Wed, 4 Jan 2023 18:43:53 +0100 Subject: [PATCH 2/3] Add test --- tests/test_api_jwk.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/test_api_jwk.py b/tests/test_api_jwk.py index 3f40fc5b..08314401 100644 --- a/tests/test_api_jwk.py +++ b/tests/test_api_jwk.py @@ -6,7 +6,7 @@ from jwt.api_jwk import PyJWK, PyJWKSet from jwt.exceptions import InvalidKeyError, PyJWKError, PyJWKSetError -from .utils import crypto_required, key_path +from .utils import crypto_required, key_path, no_crypto_required if has_crypto: from jwt.algorithms import ECAlgorithm, HMACAlgorithm, OKPAlgorithm, RSAAlgorithm @@ -207,6 +207,11 @@ def test_from_dict_should_throw_exception_if_arg_is_invalid(self): with pytest.raises(InvalidKeyError): PyJWK.from_dict(v) + @no_crypto_required + def test_missing_crypto_library_good_error_message(self): + with pytest.raises(PyJWKError) as exc: + PyJWK({"kty": "dummy"}, algorithm="RS256") + assert "cryptography" in str(exc.value) @crypto_required class TestPyJWKSet: From 2c71527bb189f353aac0c3e48a77a25fefd3c783 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 4 Jan 2023 17:44:59 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_api_jwk.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_api_jwk.py b/tests/test_api_jwk.py index 08314401..f04f5757 100644 --- a/tests/test_api_jwk.py +++ b/tests/test_api_jwk.py @@ -213,6 +213,7 @@ def test_missing_crypto_library_good_error_message(self): PyJWK({"kty": "dummy"}, algorithm="RS256") assert "cryptography" in str(exc.value) + @crypto_required class TestPyJWKSet: def test_should_load_keys_from_jwk_data_dict(self):