| 1 |
import json |
| 2 |
import zlib |
| 3 |
|
| 4 |
from jwt import PyJWT |
| 5 |
|
| 6 |
|
| 7 |
class CompressedPyJWT(PyJWT): |
| 8 |
def _decode_payload(self, decoded: dict[str, bytes]) -> dict[str, object]: |
| 9 |
payload = json.loads( |
| 10 |
# wbits=-15 has zlib not worry about headers of crc's |
| 11 |
zlib.decompress(decoded["payload"], wbits=-15).decode("utf-8") |
| 12 |
) |
| 13 |
assert isinstance(payload, dict) |
| 14 |
return payload |
| 15 |
|
| 16 |
|
| 17 |
def test_decodes_complete_valid_jwt_with_compressed_payload() -> None: |
| 18 |
# Test case from https://github.com/jpadilla/pyjwt/pull/753/files |
| 19 |
example_payload = {"hello": "world"} |
| 20 |
example_secret = "secret" |
| 21 |
# payload made with the pako (https://nodeca.github.io/pako/) library in Javascript: |
| 22 |
# Buffer.from(pako.deflateRaw('{"hello": "world"}')).toString('base64') |
| 23 |
example_jwt = ( |
| 24 |
b"eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9" |
| 25 |
b".q1bKSM3JyVeyUlAqzy_KSVGqBQA" |
| 26 |
b".AAn1elCJC5MQCYFwTwa2tjjtyLgqLUVU-Y1vFBsU8jo" |
| 27 |
) |
| 28 |
decoded = CompressedPyJWT().decode_complete( |
| 29 |
example_jwt, example_secret, algorithms=["HS256"] |
| 30 |
) |
| 31 |
|
| 32 |
assert decoded == { |
| 33 |
"header": {"alg": "HS256", "typ": "JWT"}, |
| 34 |
"payload": example_payload, |
| 35 |
"signature": ( |
| 36 |
b"\x00\t\xf5zP\x89\x0b\x93\x10\t\x81pO\x06\xb6\xb68" |
| 37 |
b"\xed\xc8\xb8*-ET\xf9\x8do\x14\x1b\x14\xf2:" |
| 38 |
), |
| 39 |
} |