From 9dca8eaa0ec43bcf54ac645872078ecdb0ca8d96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Padilla?= Date: Mon, 11 Jan 2021 22:14:36 -0500 Subject: [PATCH] Fix `from_jwk()` for all algorithms (#598) * Fix `from_jwk()` for all algorithms * Update CHANGELOG.rst --- CHANGELOG.rst | 1 + jwt/algorithms.py | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c32928ba..21074382 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -11,6 +11,7 @@ Changed ~~~~~~~ - Rename CHANGELOG.md to CHANGELOG.rst and include in docs `#597 `__ +- Fix `from_jwk()` for all algorithms `#598 `__ Fixed ~~~~~ diff --git a/jwt/algorithms.py b/jwt/algorithms.py index 9f167018..0d543825 100644 --- a/jwt/algorithms.py +++ b/jwt/algorithms.py @@ -199,7 +199,15 @@ def to_jwk(key_obj): @staticmethod def from_jwk(jwk): - obj = json.loads(jwk) + try: + if isinstance(jwk, str): + obj = json.loads(jwk) + elif isinstance(jwk, dict): + obj = jwk + else: + raise ValueError + except ValueError: + raise InvalidKeyError("Key is not valid JSON") if obj.get("kty") != "oct": raise InvalidKeyError("Not an HMAC key") @@ -424,9 +432,13 @@ def verify(self, msg, key, sig): @staticmethod def from_jwk(jwk): - try: - obj = json.loads(jwk) + if isinstance(jwk, str): + obj = json.loads(jwk) + elif isinstance(jwk, dict): + obj = jwk + else: + raise ValueError except ValueError: raise InvalidKeyError("Key is not valid JSON")