FOSUserBundle icon indicating copy to clipboard operation
FOSUserBundle copied to clipboard

How to create Salt after commit a9a08c2d

Open SDPrio opened this issue 9 years ago • 7 comments

I have just updated to the latest version and recognized, that new user accounts are created with an empty/null salt.

Some research showed that this is because of the changes in commit #a9a08c2d. What is the "correct" way of setting the salt from this commit onwards?

The "problem" is, that I am using the BCryptPasswordEncoder. I cannot switch to another encoder, this the salt/password are not only used within my Symfony webapp to authenticate the users but also in some remote-apps that are connected via a REST API (salt send to client, encrypts password with Bcrypt+Salt, sends the hash to the server...). Thus changing the encoder in the webApp would break the connection to the clients.

SDPrio avatar Dec 05 '16 17:12 SDPrio

you should not create the salt yourselves when using BCryptPasswordEncoder. PHP generates a salt with a better implementation (and as of PHP 7, it does not allow to provide the salt externally at all). And the salt does not need to be stored separately, because it is included in the hash generated by BCryptPasswordEncoder. The salt field of the User is totally ignored by BCryptPasswordEncoder when verifying a password against the hash.

stof avatar Dec 05 '16 17:12 stof

Thanks for the information, I did not know that. However I still need the salt to send it to the clients. They need the salt to locally hash the password and send the result back to the server for authentication. Maybe this it not necessary but this is the way the clients are implemented right now. Removing the salt (setting it null) will break the communication with thousands of clients.

Is there any way to extract the salt from the hash value? I does not have to be used by the bundle at all, I just need it to communicate with the clients...

SDPrio avatar Dec 05 '16 18:12 SDPrio

Isn't that stored on database? fos_user table did not had the salt field? does it fit for you?

rudirocha avatar Dec 07 '16 11:12 rudirocha

No it is not, not anymore. Before the recent update, the FOSUserBundle did create the salt manually, stored it in the DB and passed it to PHPs password_hash method to generate the bcyrpt hash. This was changed, since password_hash deprecated the option to set the salt manually. PHP does not trust the users to generate "good" salts and thus only uses salts it creates automatically. Since FOS does not generate the salt anymore, the salt is not stored in the DB anymore as well...

This is no problem in general, but in my case it is. I need to know the salt to be able to pass it to the REST clients.

It seems, that one can simply copy the chars 7 - 29 from the bcrypt result string (the hashed password) because they represent the used salt. However I am not sure if this really is a 100% valid/reliable.

SDPrio avatar Dec 07 '16 12:12 SDPrio

However I am not sure if this really is a 100% valid/reliable.

I suppose you could test it with several test passwords?

XWB avatar Dec 07 '16 15:12 XWB

@SDPrio or just generate a salt client-side at the time you hash the password. The salt being in the field is never used by the BCryptPasswordEncoder when checking a password anyway. It uses the salt being inside the hash. So extracting the salt from the bcrypt hash to store it again in the separate DB field is totally useless.

stof avatar Dec 07 '16 15:12 stof

Closing this. Feel free to open a new issue if there are further problems.

XWB avatar Jan 31 '17 12:01 XWB