summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosé Padilla <jpadilla@webapplicate.com>2021-01-11 22:14:36 -0500
committerGitHub <noreply@github.com>2021-01-11 22:14:36 -0500
commit9dca8eaa0ec43bcf54ac645872078ecdb0ca8d96 (patch)
tree8f27c3752f1ac215332fa95b6589ed6b03da1be7
parentfdfd6871536690e46b6128512825897e52cc9761 (diff)
downloadpyjwt-9dca8eaa0ec43bcf54ac645872078ecdb0ca8d96.tar.gz
Fix `from_jwk()` for all algorithms (#598)
* Fix `from_jwk()` for all algorithms * Update CHANGELOG.rst
-rw-r--r--CHANGELOG.rst1
-rw-r--r--jwt/algorithms.py18
2 files changed, 16 insertions, 3 deletions
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index c32928b..2107438 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -11,6 +11,7 @@ Changed
~~~~~~~
- Rename CHANGELOG.md to CHANGELOG.rst and include in docs `#597 <https://github.com/jpadilla/pyjwt/pull/597>`__
+- Fix `from_jwk()` for all algorithms `#598 <https://github.com/jpadilla/pyjwt/pull/598>`__
Fixed
~~~~~
diff --git a/jwt/algorithms.py b/jwt/algorithms.py
index 9f16701..0d54382 100644
--- a/jwt/algorithms.py
+++ b/jwt/algorithms.py
@@ -199,7 +199,15 @@ class HMACAlgorithm(Algorithm):
@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 @@ if has_crypto:
@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")