Authentication
- Lesson 32 of 40
- 13 min
- Intermediate
- 12 steps
Authenticating means checking that someone is who they claim to be. Getting it wrong does not only open your system: it also exposes passwords your users repeat on other sites.
- If the database leaks
- nothing usable
- Logins with leaked passwords
- 0%
- Second factor
- delegated
- request
- response
- handshake
- failed request
Step 1 of 12
Problem
A table full of passwords
Ana creates her account and the sign-in service saves her password exactly as she typed it. Signing in is just comparing two strings.
One day a backup gets exposed: a misconfigured storage bucket or a lost laptop. Whoever gets it reads every password, and since many people use the same one everywhere, they can try it on her email or her bank too.
Step 2 of 12
Solution
Store a slow, salted hash
Do not store the password: store its hash, a fingerprint that cannot be reversed. At sign-in, hash what Ana typed and compare the fingerprints.
Use a function made for passwords, such as argon2id, bcrypt or scrypt. A random salt per user makes two equal passwords produce different hashes, and deliberate slowness (about 100 ms per guess, an example value) turns brute force into very expensive work.
What changed
- Passwords → argon2id hashes, each with its own salt
Step 3 of 12
Technique
After sign-in: a session
Nobody wants to type a password on every click. After a successful sign-in the service creates a session and hands the browser a random id in a cookie, which travels with every later request.
Mark the cookie HttpOnly (page JavaScript cannot read it), Secure (HTTPS only) and SameSite (not sent from other sites). The alternative is a short-lived signed token, as seen in «Stateless services».
What changed
- Session cookie after sign-in
Step 4 of 12
Technique
Case: «I forgot my password»
Password reset is another door into every account, and often the weakest. Typical bugs: tokens that can be guessed or never expire, and a form that answers «no account with that email», revealing which emails are registered.
A safe flow uses a long random token, single use and short-lived (15 minutes in this example); it gives the same answer whether or not the account exists, and it ends every session once the password changes.
What changed
- Email with a single-use token
- The token expires after 15 min
- Same answer for unknown emails
Step 5 of 12
Problem
Credential stuffing
A bot holds a list of a billion email + password pairs leaked from other sites and tries them all against your form, thousands per second.
Your hashing is flawless and still, say, 1% of attempts succeed: people who reused their password. At 2,000 attempts per second (example values) that is 20 accounts per second, 72,000 in an hour.
What changed
- A bot trying leaked passwords
- 1% of attempts succeed
Step 6 of 12
Solution
Make guessing expensive
Protect the form itself: limit attempts per account and per IP address (answer 429 Too Many Requests), add a challenge when traffic looks automated, and reject at sign-up passwords that appear in known breaches.
Flag sign-ins from a new device or country and ask for extra proof. In this example successes drop from 1% to 0.1%: 2 accounts per second instead of 20.
What changed
- Limits per account and per IP
- Breached passwords rejected
- Bot successes: 1% → 0.1%
Step 7 of 12
Solution
A second factor
Multi-factor authentication asks for something you have on top of something you know: a code from an authenticator app, an approval on your phone or a hardware key.
With it, a stolen password alone no longer opens the account: the bot does not have Ana's phone.
What changed
- Approval on Ana's phone
- A stolen password is no longer enough
Step 8 of 12
Problem
Case: a copied sign-in page
Ana gets an email about «unusual activity» linking to tlenda.example, an exact copy of your sign-in page with one letter swapped. She types her password and the 6-digit code from her app.
The fake site forwards both to the real one within seconds and keeps Ana's session. Codes and approvals stop stolen passwords, but not a middleman that passes them on in real time.
What changed
- Fake page forwarding in real time
- Password and code, captured
Step 9 of 12
Solution
Passkeys: nothing to type, nothing to steal
A passkey (the WebAuthn standard) replaces the password with a key pair kept on Ana's phone or laptop. To sign in, the device signs a challenge sent by the site, and that signature is bound to the real domain, tienda.example.
On the fake site the device has no passkey to offer: there is nothing to type and nothing to forward. The server stores only public keys, useless to whoever steals the database.
What changed
- Password + code → passkey
- The fake site gets nothing usable
- The server stores only public keys
Step 10 of 12
Technique
Or hand it to an identity provider
«Sign in with…» buttons use OpenID Connect, a layer on top of OAuth 2.0. Your app sends Ana to the identity provider; she proves who she is there, and the provider returns a signed ID token stating «this is Ana».
Your service verifies the signature and never sees, stores or leaks a password. Companies use the same idea for single sign-on (SSO) across their internal tools.
What changed
- Identity provider
- Passwords no longer stored
Step 11 of 12
Problem
Case: a stolen token
A bug logs request headers, and someone finds Ana's token in the logs. While it is valid, a token is as good as a password: if it never expires, the attacker is Ana forever.
Use short-lived access tokens (15 minutes in this example) and a carefully stored refresh token that can be revoked. Signing out, or «sign out of all devices», revokes it.
What changed
- Access tokens that expire after 15 min
- Revocable refresh tokens
- The stolen token is rejected once expired
Step 12 of 12
Summary
Recap
Store passwords as slow, salted hashes (argon2id, bcrypt).
Sessions in HttpOnly, Secure cookies, or short-lived tokens.
Stop credential stuffing with limits, breach lists and a second factor.
Guard password reset as closely as sign-in; passkeys defeat phishing.
Consider an identity provider; keep tokens short-lived and revocable.
← → step · R replay · Space pause
You will learn
- Store passwords as slow, salted hashes (covered)
- Build sessions and a safe password reset (covered)
- Stop credential stuffing with a second factor (covered)
- Understand phishing and why passkeys stop it (covered)
- Delegate to an identity provider and keep tokens short (covered)