python-jose
python-jose copied to clipboard
Documentation error in python-jose tutorial (encoding)
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)
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! 😃
Still exists
I would be happy to review and merge a pull request fixing this issue.