diff --git a/CHANGELOG.rst b/CHANGELOG.rst index d2cfb771..eca4c59f 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -12,6 +12,7 @@ Fixed - Annotate PyJWKSet.keys for pyright by @tamird in `#1134 `__ - Close ``HTTPError`` response to prevent ``ResourceWarning`` on Python 3.14 by @veeceey in `#1133 `__ +- Do not keep ``algorithms`` dict in PyJWK instances by @akx in `#1143 `__ Added ~~~~~ diff --git a/jwt/api_jwk.py b/jwt/api_jwk.py index 960de75c..4ae7cc3f 100644 --- a/jwt/api_jwk.py +++ b/jwt/api_jwk.py @@ -28,7 +28,6 @@ def __init__(self, jwk_data: JWKDict, algorithm: str | None = None) -> None: :raises MissingCryptographyError: If the algorithm requires ``cryptography`` to be installed and it is not available. :raises PyJWKError: If unable to find an algorithm for the key. """ - self._algorithms = get_default_algorithms() self._jwk_data = jwk_data kty = self._jwk_data.get("kty", None) @@ -73,10 +72,12 @@ def __init__(self, jwk_data: JWKDict, algorithm: str | None = None) -> None: self.algorithm_name = algorithm - if algorithm in self._algorithms: - self.Algorithm = self._algorithms[algorithm] - else: - raise PyJWKError(f"Unable to find an algorithm for key: {self._jwk_data}") + try: + self.Algorithm = get_default_algorithms()[algorithm] + except KeyError: + raise PyJWKError( + f"Unable to find an algorithm for key: {self._jwk_data}", + ) from None self.key = self.Algorithm.from_jwk(self._jwk_data)