Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Possibility to change password encryption #143

Closed
dinoshauer opened this issue Jul 31, 2013 · 16 comments
Closed

Possibility to change password encryption #143

dinoshauer opened this issue Jul 31, 2013 · 16 comments

Comments

@dinoshauer
Copy link

Hello!

I've made the horrible mistake of not setting SECURITY_PASSWORD_HASH to something other than plain text and was hoping there was a way to either change all the password to a hashed format on the fly?

It would be pretty cool in the future if there was a method for this kind of stuff :)

@mattupstate
Copy link
Collaborator

You should not have to do anything beyond changing the setting. The next time the user successfully logs in, their password will be updated to the new hash type. This is done in the verify_and_update_password function.

@KasperJacobsen
Copy link

It is not working though, I've tried changing app.config['SECURITY_PASSWORD_HASH'] from 'plaintext' to 'pbkdf2_sha512' with a salt, but when I try to log in I get the Invalid password message.

@KangOl
Copy link
Contributor

KangOl commented Nov 16, 2013

The problem is that flask-security does not store encrypted passwords into the database.
It store then encrypted HMAC of the password... unless you choose to not encrypt the password.
In this case, you're stuck in plaintext mode. Or you force all your users to reset their password.

pull-request coming in the next minutes.

KangOl added a commit to KangOl/flask-security that referenced this issue Nov 16, 2013
… a better one and (optionnaly) automatically re-hash passwords as users log in.

Follow up of pallets-eco#65
Fix pallets-eco#143
@robin-wittler
Copy link

Tested flask-security today and i was a little bit confused when i take a look at the database and saw a passwords not encrypted. After applying your patch everything works as suggested. Thank you! ;)

@mattupstate
Copy link
Collaborator

It all depends on how you add users to your database. If you have set SECURITY_REGISTERABLE = True, and are only adding users via the built-in registration form, then you'll be fine. If you're adding users by any other means you must encrypt the password using flask_security.utils.encrypt_password before saving the user record in your database.

@KangOl
Copy link
Contributor

KangOl commented Nov 17, 2013

The problem is if you change your SECURITY_PASSWORD_HASH config from plaintext to anything else, existing users cannot log in.

@robin-wittler
Copy link

On 11/17/2013 08:59 PM, Matt Wright wrote:

It all depends on how you add users to your database. If you have set
|SECURITY_REGISTERABLE = True|, and are only adding users via the
built-in registration form, then you'll be fine. If you're adding users
by any other means you must encrypt the password using
|flask_security.utils.encrypt_password| before saving the user record in
your database.

Yes - looks like i stumbled over this behavior. I've just followed the
tutorial and created a user with the @app.before_first_request and
thought that the "user_datastore.create_user" call would do everything
which is necessary to have a secure (and encrypted) password in the
database.

Later i looked at the code and saw that this call will do only things
with Roles. Then i used encrypt_password in the
"user_datastore.create_user" call and the password was stored encrypted
in the database.

From my point of view this behavior is unexpected. I think the
datastore.create_user call (or better the self._prepare_create_user_args
call) should also do the encryption stuff.

At least (IMHO) you should think about pointing out this behavior
clearly in your tutorial - or extend self._prepare_create_user_args to
do encryption.

Anyway - thank you for flask-security and keep up your good work. ;)


Reply to this email directly or view it on GitHub
#143 (comment).

@dinoshauer
Copy link
Author

@KangOl the patch is working on 1.7.0
Thank you very much

@mattupstate
Copy link
Collaborator

@KangOl revisiting this, are you still concerned? Or have you moved on?

@bdemirtas
Copy link

Hi Guys, i was wondering if there a way to use SHA1 encryption. I know its now good but one of my client existant user password is based on sha1 and i am redeveloping the application but they want full compatibility.

Thanks

@mattupstate
Copy link
Collaborator

FWIW, I've fixed and tested this in the pytest branch

@mattupstate
Copy link
Collaborator

@bdemirtas SHA1 is not supported, sorry.

@citizen-stig
Copy link

@mattupstate that's really sad. Django has a PasswordHasher class, that can be overwritten by the developer, so it brings more flexibility.
Why didn't you want to allow that?

@citizen-stig
Copy link

Also pbkdf2_sha1 is a default in werkzeug(https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/security.py#L204), so a lot project, that has been built not from scratch, can meet this problem

@adityagupta679
Copy link

adityagupta679 commented Jan 7, 2017

#143 (comment)

robin-wittler commented on Nov 18, 2013
On 11/17/2013 08:59 PM, Matt Wright wrote:

It all depends on how you add users to your database. If you have set
|SECURITY_REGISTERABLE = True|, and are only adding users via the
built-in registration form, then you'll be fine. If you're adding users
by any other means you must encrypt the password using
|flask_security.utils.encrypt_password| before saving the user record in
your database.
Yes - looks like i stumbled over this behavior. I've just followed the
tutorial and created a user with the @app.before_first_request and
thought that the "user_datastore.create_user" call would do everything
which is necessary to have a secure (and encrypted) password in the
database.

Later i looked at the code and saw that this call will do only things
with Roles. Then i used encrypt_password in the
"user_datastore.create_user" call and the password was stored encrypted
in the database.

From my point of view this behavior is unexpected. I think the
datastore.create_user call (or better the self._prepare_create_user_args
call) should also do the encryption stuff.

At least (IMHO) you should think about pointing out this behavior
clearly in your tutorial - or extend self._prepare_create_user_args to
do encryption.

Anyway - thank you for flask-security and keep up your good work. ;)

@mattupstate I am wondering why encryption is not built in with create_user.

@adityagupta679
Copy link

Explanation in #136 satisfies my curiosity. Though it would be nice to include this fact in the documentation. @mattupstate

galeo pushed a commit to galeo/flask-security-outdated that referenced this issue Mar 4, 2020
Remove script.py
Remove old sqlalchemy workaround.

closes: pallets-eco#143
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging a pull request may close this issue.

8 participants