Skip to content

Commit

Permalink
Remove unnecessary Unicode decoding before json.loads() (#542)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jdufresne committed Dec 16, 2020
1 parent 529647a commit 696f65d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion jwt/api_jws.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def _load(self, jwt):
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

Expand Down
4 changes: 2 additions & 2 deletions jwt/api_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ def decode(

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):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_api_jws.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def test_decodes_valid_es384_jws(self, jws):
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

Expand Down Expand Up @@ -262,7 +262,7 @@ def test_decodes_valid_rs384_jws(self, jws):
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

Expand Down Expand Up @@ -699,7 +699,7 @@ def default(self, o):
)

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"
Expand Down

0 comments on commit 696f65d

Please sign in to comment.