Best practices for user authentication
User authentication is a functionality every web app shares. It should have been perfected a long time ago, considering the number of times it has been implemented. However, there are so many mistakes made and vulnerabilities that haven’t been patched yet.
Below is a list of best practices regarding user authentication. This list will cover as many of the related pitfalls as possible. So here is the list of things that will make your backend authentication mechanisms much more secure:
- Always use TLS. Every web API should use TLS (Transport Layer Security). TLS protects the information your API sends (and the information that users send to your API) by encrypting your messages while they’re in transit. In case you don’t use TLS on your website, a third party could intercept and read sensitive data that is in the process of transfer.
- Use bcrypt/scrypt/PBKDF2 for password storing. Don’t use MD5 or SHA, as they are not the best for password storage. Long salt (per user) is mandatory (the aforementioned algorithms have it built-in).
- Use API keys to give existing users programmatic access. While your REST endpoints can serve your own website, a big advantage of REST is that it provides a standard way for other programs to interact with your service. To keep things simple, don’t make your users do OAuth2 locally or make them provide a username/password combo — that would defeat the point of having used OAuth2 for authentication in the first place. Instead, keep things simple for yourself and your users, and issue API keys.
- Use CSRF protection (e.g. CSRF one-time tokens that are verified with each request). Frameworks have such functionality built-in.
- Set session expirations to avoid having forever-lasting sessions. Upon closing the website tab — the user’s session should expire.
- Limit login attempts.
- Don’t allow attackers to figure out if an email is registered or not by leaking information through error messages.
- Forgotten password flow. Send one-time (or expiring) links to users when trying to reset a password.
- Use the secure option for cookies. It will tell the browser to send cookies over SSL/TLS connections.
- Don’t leak information through error messages — you shouldn’t allow attackers to figure out if an email is registered or not. If an email is not found, upon login, just report “Incorrect credentials.” On password resets, it may be something like “If your email is registered, you should have received a password reset email.” This is often at odds with usability — people don’t often remember the email they used to register, and the ability to check a number of them before getting in might be important. So this rule is not absolute, though it’s desirable, especially for more critical systems.
References
- https://www.enzoic.com/hashing-what-you-need-to-know-about-storing-passwords/
- https://www.cloudflare.com/en-gb/learning/ssl/why-is-http-not-secure/
- https://blog.dareboost.com/en/2019/03/secure-cookies-secure-httponly-flags/
- https://www.veracode.com/security/error-handling-flaws-information-and-how-fix-tutorial
- https://blog.teamtreehouse.com/how-to-create-totally-secure-cookies