Authentication design
Authentication is where an attacker meets your front door, so the failures
are expensive: a leaked password database, a brute-forced account, a
hijacked session. The safe path is well-known and boring, and the danger is
in the shortcuts: home-rolled hashing, no throttling, a session ID that
survives login.
Method
- Hash passwords with a slow, salted algorithm. Use bcrypt, scrypt, or
Argon2id with a per-user salt (the library adds it) and a work factor
tuned to roughly 250 ms per hash. Never use MD5, SHA-1, or a bare
SHA-256: they are built for speed, which is exactly what an offline
cracker wants.
- Rate limit and lock out credential guessing. Cap login attempts per
account and per IP: exponential backoff after 5 failures, a temporary
lock after 10. Apply the same throttle to password-reset and MFA
endpoints. Without it, weak passwords fall to a script overnight.
- Rotate the session ID on login. Issue a fresh session identifier the
moment authentication succeeds and invalidate the pre-login one. Reusing
the anonymous session's ID lets an attacker who planted that ID ride into
the authenticated session (session fixation).
- Return identical responses for unknown user and wrong password. "No
such user" versus "wrong password" hands an attacker a roster of valid
accounts. Use one generic message and a constant-time comparison so
timing does not leak the difference either.
- Enforce strength by length and a breach check. Require 12+
characters, allow long passphrases and pasting, and screen new passwords
against a known-breach list (the Have I Been Pwned k-anonymity API). Drop
forced rotation and composition rules that only push users toward
Password1!.
- Treat reset tokens like credentials. Make password-reset tokens long,
random, single-use, and short-lived (under an hour), and store only their
hash. A reset link that never expires is a permanent backdoor sitting in
an old inbox.
Litmus tests
- If the database leaked today, how long would offline cracking take against
your hashes?
- Does login rotate the session ID, confirmed by watching the cookie change
before and after?
- Do wrong-password and unknown-user attempts return the same message and
similar timing?
Boundaries
This skill covers password-based sign-in. Multi-factor authentication,
OAuth/OIDC federation, and passkeys are adjacent designs with their own
trade-offs; add them, but get the password path right first. Authorization
(what an authenticated user may do) is handled in authz-design, and ongoing
session handling lives in session-management.
1---2name: authn-design3description: Design sign-in so credentials are hashed with a slow algorithm, guessing is rate limited, and sessions rotate on login to defeat fixation. Use when building or reviewing registration, login, or password-reset flows.4---56# Authentication design78Authentication is where an attacker meets your front door, so the failures9are expensive: a leaked password database, a brute-forced account, a10hijacked session. The safe path is well-known and boring, and the danger is11in the shortcuts: home-rolled hashing, no throttling, a session ID that12survives login.1314## Method15161. **Hash passwords with a slow, salted algorithm.** Use bcrypt, scrypt, or17 Argon2id with a per-user salt (the library adds it) and a work factor18 tuned to roughly 250 ms per hash. Never use MD5, SHA-1, or a bare19 SHA-256: they are built for speed, which is exactly what an offline20 cracker wants.212. **Rate limit and lock out credential guessing.** Cap login attempts per22 account and per IP: exponential backoff after 5 failures, a temporary23 lock after 10. Apply the same throttle to password-reset and MFA24 endpoints. Without it, weak passwords fall to a script overnight.253. **Rotate the session ID on login.** Issue a fresh session identifier the26 moment authentication succeeds and invalidate the pre-login one. Reusing27 the anonymous session's ID lets an attacker who planted that ID ride into28 the authenticated session (session fixation).294. **Return identical responses for unknown user and wrong password.** "No30 such user" versus "wrong password" hands an attacker a roster of valid31 accounts. Use one generic message and a constant-time comparison so32 timing does not leak the difference either.335. **Enforce strength by length and a breach check.** Require 12+34 characters, allow long passphrases and pasting, and screen new passwords35 against a known-breach list (the Have I Been Pwned k-anonymity API). Drop36 forced rotation and composition rules that only push users toward37 `Password1!`.386. **Treat reset tokens like credentials.** Make password-reset tokens long,39 random, single-use, and short-lived (under an hour), and store only their40 hash. A reset link that never expires is a permanent backdoor sitting in41 an old inbox.4243## Litmus tests4445- If the database leaked today, how long would offline cracking take against46 your hashes?47- Does login rotate the session ID, confirmed by watching the cookie change48 before and after?49- Do wrong-password and unknown-user attempts return the same message and50 similar timing?5152## Boundaries5354This skill covers password-based sign-in. Multi-factor authentication,55OAuth/OIDC federation, and passkeys are adjacent designs with their own56trade-offs; add them, but get the password path right first. Authorization57(what an authenticated user may do) is handled in authz-design, and ongoing58session handling lives in session-management.