Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base64url_decode TypeError: must be str, not bytes #74

Closed
rvernica opened this issue Oct 16, 2017 · 3 comments · Fixed by #187
Closed

base64url_decode TypeError: must be str, not bytes #74

rvernica opened this issue Oct 16, 2017 · 3 comments · Fixed by #187

Comments

@rvernica
Copy link

Example copied from docs:

>>> from jose.utils import base64url_decode
>>> token = "eyJhbGciOiJIUzI1NiIsImtpZCI6IjAxOGMwYWU1LTRkOWItNDcxYi1iZmQ2LWVlZjMxNGJjNzAzNyJ9.SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ29pbmcgb3V0IHlvdXIgZG9vci4gWW91IHN0ZXAgb250byB0aGUgcm9hZCwgYW5kIGlmIHlvdSBkb24ndCBrZWVwIHlvdXIgZmVldCwgdGhlcmXigJlzIG5vIGtub3dpbmcgd2hlcmUgeW91IG1pZ2h0IGJlIHN3ZXB0IG9mZiB0by4.s0h6KThzkfBBBkLspW1h84VsJZFTsPPqMDA7g1Md7p0"
>>> message, encoded_sig = token.rsplit('.', 1)
>>> decoded_sig = base64url_decode(encoded_sig)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/vernica/.local/lib/python3.6/site-packages/jose/utils.py", line 88, in base64url_decode
    input += b'=' * (4 - rem)
TypeError: must be str, not bytes

Environment:

  • Python 3.6.2
  • jose 1.4.0
@f1yingbanana
Copy link

f1yingbanana commented Apr 17, 2018

In python 3, you have to encode your input to bytes first, because implicit conversion between str and byte is no longer possible. Try:
base64url_decode(encoded_sig.encode('utf-8'))

@karlmutch
Copy link

The append operation within the base64 decode functions is appending bytes not strings resulting in byte arrays being sent to the python base64 functions and failing.

using the encode function on the input strings for padding does not help the root issue.

An example can be seen here:

input += b'=' * (4 - rem)

@karlmutch
Copy link

In python 3, you have to encode your input to bytes first, because implicit conversion between str and byte is no longer possible. Try:
base64url_decode(encoded_sig.encode('utf-8'))

The internal functions used for padding will void this fix. See comment below.

@blag blag mentioned this issue Sep 7, 2020
@blag blag closed this as completed in #187 Sep 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants