mpdavis / python-jose

A JOSE implementation in Python
MIT License
1.54k stars 236 forks source link

Can't convert 'bytes' object to str implicitly #51

Open ssonow opened 7 years ago

ssonow commented 7 years ago

??? decoded_sig = base64url_decode(encoded_sig) Traceback (most recent call last): File "", line 1, in File "/home/as/.local/lib/python3.5/site-packages/jose/utils.py", line 39, in base64url_decode input += b'=' * (4 - rem) TypeError: Can't convert 'bytes' object to str implicitly

mpdavis commented 7 years ago

It appears you are passing a bytes object to base64url_decode instead of a string.

Try this:

decoded_sig = base64url_decode(encoded_sig.decode('utf-8'))
ssonow commented 7 years ago

Thx, it seems still not work. This is what I got from the example code:

from jose import jwk from jose.utils import base64url_decode token = "eyJhbGciOiJIUzI1NiIsImtpZCI6IjAxOGMwYWU1LTRkOWItNDcxYi1iZmQ2LWVlZjMxNGJjNzAzNyJ9.SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ29pbmcgb3V0IHlvdXIgZG9vci4gWW91IHN0ZXAgb250byB0aGUgcm9hZCwgYW5kIGlmIHlvdSBkb24ndCBrZWVwIHlvdXIgZmVldCwgdGhlcmXigJlzIG5vIGtub3dpbmcgd2hlcmUgeW91IG1pZ2h0IGJlIHN3ZXB0IG9mZiB0by4.s0h6KThzkfBBBkLspW1h84VsJZFTsPPqMDA7g1Md7p0" hmac_key = { ... "kty": "oct", ... "kid": "018c0ae5-4d9b-471b-bfd6-eef314bc7037", ... "use": "sig", ... "alg": "HS256", ... "k": "hJtXIZ2uSN5kbQfbtTNWbpdmhkV8FJG-Onbc6mxCcYg" ... } key = jwk.construct(hmac_key) message, encoded_sig = token.rsplit('.', 1) decoded_sig = base64url_decode(encoded_sig.decode("utf-8")) Traceback (most recent call last): File "", line 1, in AttributeError: 'str' object has no attribute 'decode'

Originally I got:

decoded_sig = base64url_decode(encoded_sig) Traceback (most recent call last): File "", line 1, in File "/home/as/.local/lib/python3.5/site-packages/jose/utils.py", line 39, in base64url_decode input += b'=' * (4 - rem) TypeError: Can't convert 'bytes' object to str implicitly

zopyx commented 7 years ago

I can confirm this issue with Python 3.6 and the example from the documentation http://python-jose.readthedocs.io/en/latest/jwk/index.html

Traceback (most recent call last):
  File "o.py", line 13, in <module>
    key = jwk.construct(hmac_key)
  File "/home/ajung/src/coa-pim-service/lib/python3.6/site-packages/jose/utils.py", line 37, in base64url_decode
    rem = len(input) % 4
TypeError: must be str, not bytes
vvvhung commented 7 years ago

It'll work if adding encode() (default code is "ascii"): decoded_sig = base64url_decode(encoded_sig.encode()) key.verify(message.encode(), decoded_sig)

But does the code or the document need a fix?