Authentication vs authorization
Two words that sound alike, and get mixed up constantly, are the foundation of all application security:
- Authentication (often written authn) answers "Who are you?" Logging in with a password, a passkey or "Sign in with Google" proves your identity.
- Authorization (authz) answers "What are you allowed to do?" Can this user see that invoice, delete this post or open the admin panel?
Authentication always comes first: you can't decide what someone may do until you know who they are. But passing authentication grants nothing on its own. Many of the worst security bugs happen when an app checks that someone is logged in, but not whether this person may access this thing.
An everyday analogy
At an airport, showing your passport at security is authentication: it proves you are Asha Rao. Your boarding pass is authorization: it lets you board flight 6E-204 only, and only seat 14C. A valid passport doesn't let you walk onto any plane.
Ways to authenticate
| Factor | Examples |
|---|---|
| Something you know | Password, PIN, security question |
| Something you have | Phone with an authenticator app, a security key, a passkey on your device |
| Something you are | Fingerprint, face (usually used to unlock a passkey on the device) |
Using two different factors is multi-factor authentication (MFA). After a successful login, the server gives the client something to prove it on later requests (a session cookie or a token), so the user doesn't type their password on every click. The next lessons cover each of these.
401 or 403?
HTTP has a status code for each concept (the names are historical and confusing):
| Status | Really means | Example |
|---|---|---|
| 401 Unauthorized | Not authenticated: we don't know who you are | No token, expired token, wrong password |
| 403 Forbidden | Not authorized: we know who you are, and the answer is no | A customer calling an admin endpoint |
| 404 Not Found | Sometimes used instead of 403 | To avoid revealing that a private resource exists |
Authorization models
Role-based access control (RBAC)
Users get roles; roles get permissions. Checking a permission, not a role, keeps code flexible when roles change:
Ownership checks
Roles aren't enough. A customer may read orders, but only their own. Forgetting this check is the #1 vulnerability on the OWASP Top 10, broken access control (also called IDOR, insecure direct object reference):
(Never trust headers like X-User-Id in a real app: the identity must come from a verified session or token. Here they only simulate one.)
Attribute-based access control (ABAC)
For richer rules ("editors may publish articles in their own department, during business hours, if the article has been reviewed"), policies look at attributes of the user, the resource and the context. Large systems use policy engines such as Open Policy Agent, Cedar or OpenFGA (relationship-based access, as in Google Docs sharing).
Principles that never change
- Deny by default. Every route requires authentication and an explicit permission unless deliberately made public.
- Check on the server, on every request. Hiding a button in the UI is not security; anyone can call the API directly.
- Least privilege. Give users, services and database accounts only the permissions they need.
- Centralise the checks in middleware or a policy module, so they can't be forgotten in one route.
- Test the "no" cases: write tests that prove user A cannot read user B's data.
Practice
- Add a
DELETE /v2/orders/:idroute that only admins may use (403 for everyone else). - Add a
refunds:create:limitedpermission for support staff that only allows refunds under 100. - Write the list of tests you'd need to prove the order API's authorization is correct.
1.A user's password is correct. Which step has succeeded?
2.A logged-in user tries to open another user's invoice. Which response is appropriate?
3.Why isn't hiding the "Delete" button enough to protect a delete endpoint?