Skip to content

Commit 9dca8ea

Browse files
authored
Fix from_jwk() for all algorithms (#598)
* Fix `from_jwk()` for all algorithms * Update CHANGELOG.rst
1 parent fdfd687 commit 9dca8ea

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Changed
1111
~~~~~~~
1212

1313
- Rename CHANGELOG.md to CHANGELOG.rst and include in docs `#597 <https://github.com/jpadilla/pyjwt/pull/597>`__
14+
- Fix `from_jwk()` for all algorithms `#598 <https://github.com/jpadilla/pyjwt/pull/598>`__
1415

1516
Fixed
1617
~~~~~

jwt/algorithms.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,15 @@ def to_jwk(key_obj):
199199

200200
@staticmethod
201201
def from_jwk(jwk):
202-
obj = json.loads(jwk)
202+
try:
203+
if isinstance(jwk, str):
204+
obj = json.loads(jwk)
205+
elif isinstance(jwk, dict):
206+
obj = jwk
207+
else:
208+
raise ValueError
209+
except ValueError:
210+
raise InvalidKeyError("Key is not valid JSON")
203211

204212
if obj.get("kty") != "oct":
205213
raise InvalidKeyError("Not an HMAC key")
@@ -424,9 +432,13 @@ def verify(self, msg, key, sig):
424432

425433
@staticmethod
426434
def from_jwk(jwk):
427-
428435
try:
429-
obj = json.loads(jwk)
436+
if isinstance(jwk, str):
437+
obj = json.loads(jwk)
438+
elif isinstance(jwk, dict):
439+
obj = jwk
440+
else:
441+
raise ValueError
430442
except ValueError:
431443
raise InvalidKeyError("Key is not valid JSON")
432444

0 commit comments

Comments
 (0)