#Why this matters
Broken authentication and broken access control sit at the top of every major web risk list. The pattern is the same in most breaches: the application worked as coded, but nobody decided clearly who may do what, or the decision was made only in the user interface and not on the server. A well designed identity layer is cheap to build early and very expensive to retrofit after launch.
This lesson covers three connected design areas: how users prove identity, how that proof is carried from request to request, and how the server decides on every request whether an action is allowed.
#Authentication: proving identity
Passwords. Never store passwords in a reversible form or with a fast hash such as plain SHA-256. Use a memory-hard password hashing function such as Argon2id with a unique random salt per user. The OWASP Password Storage Cheat Sheet suggests, as a starting point, at least 19 MiB of memory, two iterations and one lane; tune the cost to your hardware so that a login takes a noticeable fraction of a second.
Login flow. Return the same message and similar response time whether the username or the password was wrong, otherwise attackers can enumerate valid accounts. Throttle attempts per account and per source address, and add progressive delays rather than permanent lockouts, which let anyone lock out a victim.
Second factor. Prefer phishing-resistant factors such as passkeys (WebAuthn) or hardware keys; time-based one-time codes are better than nothing; SMS is the weakest option and should be a last resort.
Recovery. Password reset is an authentication path too. Use single-use, short-lived, random tokens sent to a verified address, invalidate them after use, and do not reveal whether the address exists.
#Sessions: carrying identity safely
After login the server issues a session identifier that stands in for the password. Treat it as a secret.
| Control | Reason |
|---|---|
| Random identifier with at least 128 bits of entropy | Cannot be guessed |
Secure cookie attribute |
Never sent over plain HTTP |
HttpOnly cookie attribute |
Scripts cannot read it, limiting theft through cross-site scripting |
SameSite=Lax or Strict |
Reduces cross-site request forgery |
| New identifier at login and at privilege change | Defeats session fixation |
| Idle and absolute timeouts, server-side logout | Limits the window of a stolen session |
| Invalidate all sessions after a password change | Evicts an attacker who already has a session |
$ curl -si https://app.example.test/login -d 'user=alice&password=correct-horse' | grep -i set-cookie
Set-Cookie: sid=9f2c1d7a64be40a98e3b5c0d1f7a8e22; Path=/; Secure; HttpOnly; SameSite=Lax; Max-Age=1800Signed tokens such as JWT are fine for short-lived API access, but they cannot be revoked easily. Keep access tokens short and re-check the current role of the user on the server for sensitive actions.
#Authorization: deciding what is allowed
Authentication answers who you are; authorization answers what you may do. Apply three rules everywhere: deny by default, enforce on the server for every request, and check the specific object rather than only the type of action.
def get_invoice(request, invoice_id):
user = request.user # taken from the server-side session
invoice = db.invoices.get(invoice_id)
if invoice is None or invoice.owner_id != user.id:
raise NotFound() # same answer for missing and forbidden
return invoiceWithout the owner check this is BOLA, the first item of the OWASP API Security Top 10 (2023): changing the number in the URL returns somebody else's data.
| Model | Policy is expressed as | Best for |
|---|---|---|
| RBAC | permissions grouped into roles such as student, teacher, admin |
Stable organisational structures |
| ABAC | rules over attributes of user, resource and context | Fine-grained or contextual rules, such as "a doctor may read records of patients assigned to them during their shift" |
Many systems combine both: roles for coarse access and attributes for ownership and context. Keep the policy in one place, not scattered through controllers.
#Defending / Common mistakes
- Relying on client-side checks, hidden fields or disabled buttons.
- Keeping the same session identifier before and after login.
- Putting sensitive data or the only copy of the role inside a long-lived token.
- Returning different errors for "unknown user" and "wrong password".
- Writing authorization logic differently in every controller instead of one policy layer.
- Skipping tests for negative cases: most bugs are found by trying what should fail.
#Summary
Hash passwords with Argon2id, make login and recovery uniform and throttled, and add a strong second factor. Carry identity in a random, short-lived session with Secure, HttpOnly and SameSite set, rotated at login. Decide access on the server for every request and every object, deny by default, and choose RBAC, ABAC or a mix to match the policy you can state in one sentence.