| 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".08wHYeuh1rJXmcBcMrz6NxmbxAnCQp2rGTKfRNIkxiw=" |
| 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"\xd3\xcc\x07a\xeb\xa1\xd6\xb2W\x99\xc0\\2\xbc\xfa7" |
| 37 |
b"\x19\x9b\xc4\t\xc2B\x9d\xab\x192\x9fD\xd2$\xc6," |
| 38 |
), |
| 39 |
} |