lingthio / Flask-User

Customizable User Authorization & User Management: Register, Confirm, Login, Change username/password, Forgot password and more.
http://flask-user.readthedocs.io/
MIT License
1.06k stars 292 forks source link

Fixed AttributeError: 'bytes' object has no attribute 'encode' #229

Open yuhr123 opened 6 years ago

yuhr123 commented 6 years ago

The key field already be decode in Python 3. So I add a condition statement to check version of python to fix this error.

carlosvega commented 5 years ago

Please consider this

and-semakin commented 4 years ago

Hi @yuhr123! Thank you for your contribution!

Could you please explain how to reproduce the error you are trying to fix? What version of Python does it affect -- 2 or 3?

I don't think that this solution is appropriate as it breaks a valid use case on Python 3 when flask_secret_key is str with following error:

>           key = key + b' '*32    # Make sure the key is at least 32 bytes long
E           TypeError: can only concatenate str (not "bytes") to str
and-semakin commented 4 years ago

Maybe you wanted to do the opposite?

if sys.version_info[0] < 3:
    key = flask_secret_key
else:
    key = flask_secret_key.encode()

So don't encode on python2 as str is already a byte string, but do encode on python3 to get bytes.

webcoderz commented 4 years ago

can we push this please having difficulty with docker images that rely on this this change enables it to work on py3

blaushild commented 3 years ago

I use urandom() for a secret key generation. The key is always in bytes. And it doesn't matter which version of python is in use.

I guess something like this:

if isinstance(key, bytes):
    key = flask_secret_key
else:
    key = flask_secret_key.encode()

PS Sorry, I'm new here. I don't know how to work correctly with github system.