python-jose icon indicating copy to clipboard operation
python-jose copied to clipboard

Documentation error in python-jose tutorial (encoding)

Open raphet opened this issue 4 years ago • 3 comments

Env python-jose (3.2.0) python3.6

error: python3.6/site-packages/jose/utils.py", line 87, in base64url_decode input += b'=' * (4 - rem) TypeError: must be str, not bytes

trigger: When one follows your basic tutorial line-by-line... https://python-jose.readthedocs.io/en/latest/jwk/index.html#verifying-token-signatures

error line: >>> decoded_sig = base64url_decode(encoded_sig)

Update 1

I tried using base64url_decode(encoded_sig.encode('utf-8')) instead, but then the last line fails: python3.6/site-packages/cryptography/utils.py", line 38, in _check_byteslike raise TypeError("{} must be bytes-like".format(name))

Update 2

The message also needed to be encoded with utf-8.

So now it works and here is my suggested change to your tutorial page:

message, signature = list(map(lambda x: x.encode('utf-8'), token.rsplit('.',1)))
decoded_signature = base64url_decode(signature)
key.verify(message, decoded_signature)

raphet avatar Aug 17 '20 12:08 raphet

Good catch. This must have been originally coded under Python 2, where bytes and strings are the same thing. Now with Python 3, that's not the case.

I would be happy to review a pull request fixing this issue. Thanks! 😃

blag avatar Aug 18 '20 20:08 blag

Still exists

david-shiko avatar Jan 27 '21 02:01 david-shiko

I would be happy to review and merge a pull request fixing this issue.

blag avatar Jan 27 '21 03:01 blag