32
How to store user's access tokens/API keys without hashing them?
(lemmy.pe1uca.dev)
Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!
Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.
Hope you enjoy the instance!
Rules
Follow the wormhole through a path of communities !webdev@programming.dev
As for client side token generation...
Never trust the client.
Say you hash the password client side. At this point, you have to have static salt (which can be extracted from clients), and the hashed result becomes the password.
All of this greatly weakens the security.
If the client sends a username, and the server returns a salt, then it's a bit more secure. At least this way the salt can be randomly generated for each user.
But, it's an extra API call.
You could use the username as the salt. This makes things a bit better, but you open yourself to being rainbow-tabled for usernames like "admin". Also, the salt doesn't change when a password is updated.
Here's a SE post that kinda pertains to what you want:
https://security.stackexchange.com/questions/93395/how-to-do-client-side-hashing-of-password-using-bcrypt
This one has a section on client side hashing:
https://security.stackexchange.com/questions/211/how-to-securely-hash-passwords/31846#31846
Edit:
Client side key generation isn't worth it. There aren't any good implementations.
I think one of the answers linked above alludes to the following solution:
Do a double salt:
Username & password get bcrypted (or similar) together in the client. The username-as-salt reduces the parallel of brute force attacks to a single user (ie if an attacker has a bunch of hashes of different passwords for the same user, they can brute force them all at the same time - they know the salt)
Username and hash is sent along with the request (the hash essentially becomes the new password without leaking it in plaintext in server logs, gateways, proxies etc.).
The server then retrieves the users salt from the database, hashes it again using the appropriate salt.
This way, at least the data at rest is fairly well protected. But I'm not a crypto guy. I have learned to follow the herd when it comes to authentication and security.
And I don't think there is actually a decent way to do this that actually provides the kind of security required these days.