diff options
author | Jon Dufresne <jon.dufresne@gmail.com> | 2020-12-16 14:24:40 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-16 17:24:40 -0500 |
commit | 696f65d654d13c1581f396c00f09dd48dfef59b4 (patch) | |
tree | 6e23b0311ebd366dc79df2843ea6a6d7cabfb947 | |
parent | 529647ab8cfc4e05776ebd5d146db9a1f9af6ebf (diff) | |
download | pyjwt-696f65d654d13c1581f396c00f09dd48dfef59b4.tar.gz |
Remove unnecessary Unicode decoding before json.loads() (#542)
Since Python 3.6, json.loads() accepts both Unicode and byte strings.
https://docs.python.org/3/library/json.html#json.loads
> Changed in version 3.6: s can now be of type bytes or bytearray. The
> input encoding should be UTF-8, UTF-16 or UTF-32.
-rw-r--r-- | jwt/api_jws.py | 2 | ||||
-rw-r--r-- | jwt/api_jwt.py | 4 | ||||
-rw-r--r-- | tests/test_api_jws.py | 6 |
3 files changed, 6 insertions, 6 deletions
diff --git a/jwt/api_jws.py b/jwt/api_jws.py index e9f8974..f01f53e 100644 --- a/jwt/api_jws.py +++ b/jwt/api_jws.py @@ -188,7 +188,7 @@ class PyJWS: raise DecodeError("Invalid header padding") from err try: - header = json.loads(header_data.decode("utf-8")) + header = json.loads(header_data) except ValueError as e: raise DecodeError("Invalid header string: %s" % e) from e diff --git a/jwt/api_jwt.py b/jwt/api_jwt.py index 4862580..d5bcb15 100644 --- a/jwt/api_jwt.py +++ b/jwt/api_jwt.py @@ -95,9 +95,9 @@ class PyJWT(PyJWS): try: if complete: - payload = json.loads(decoded["payload"].decode("utf-8")) + payload = json.loads(decoded["payload"]) else: - payload = json.loads(decoded.decode("utf-8")) + payload = json.loads(decoded) except ValueError as e: raise DecodeError("Invalid payload string: %s" % e) if not isinstance(payload, dict): diff --git a/tests/test_api_jws.py b/tests/test_api_jws.py index b824e80..7da3b0c 100644 --- a/tests/test_api_jws.py +++ b/tests/test_api_jws.py @@ -232,7 +232,7 @@ class TestJWS: decoded_payload = jws.decode( example_jws, example_pubkey, algorithms=["ES256"] ) - json_payload = json.loads(force_unicode(decoded_payload)) + json_payload = json.loads(decoded_payload) assert json_payload == example_payload @@ -262,7 +262,7 @@ class TestJWS: decoded_payload = jws.decode( example_jws, example_pubkey, algorithms=["RS384"] ) - json_payload = json.loads(force_unicode(decoded_payload)) + json_payload = json.loads(decoded_payload) assert json_payload == example_payload @@ -699,7 +699,7 @@ class TestJWS: ) header = force_bytes(force_unicode(token).split(".")[0]) - header = json.loads(force_unicode(base64url_decode(header))) + header = json.loads(base64url_decode(header)) assert "some_decimal" in header assert header["some_decimal"] == "it worked" |