07. Security / Lesson 2 of 3
Authorization
After we know who you are, decide what you are allowed to do, on every request.
- 8 min
- Beginner
- 9 steps
- Decision?
- Entries to manage—
- Invoices Ann can read—
- Request
- Response
- Abuse
- Failed request
- Health check
Step 1 of 9: Knowing who you are is not enough
Read every step as text
Step 1: Knowing who you are is not enough
Authentication answers "who are you?": Ann logged in and her token is valid. Authorization answers "what may you do?": may Ann delete invoice 7? Read someone else's invoice? Refund a payment?
Without the second question, every logged-in user is a superuser. Most serious data leaks are not broken passwords but missing permission checks.
Authentication: who. Authorization: what. They are two different checks and both are needed.
Step 2: Check on the server, on every request
A hidden or greyed-out button is not security. Anyone can copy the request and send it with a script, skipping the page completely. The server must decide each time, using the token of the caller.
Each check asks three things: who (from the token), which action (delete) and which resource (invoice 7). If the answer is no, return
403 Forbidden. (401means we do not know who you are.)Deny by default: if no rule says yes, the answer is no. Forgetting a rule should lock a door, not open it.
Step 3: Roles: give permissions to roles, roles to people
If every one of 5,000 users has their own list of 20 permissions, that is 100,000 entries to keep right. With role-based access control (RBAC) you define a few roles, viewer, editor and admin, with permissions each, and give people roles.
That is at most 3 × 20 = 60 role entries plus 5,000 user-to-role entries: 5,060. When a person is promoted, one entry changes.
RBAC is easy to explain and audit. Its weakness is role explosion when rules get specific, such as 'editor, but only for Europe'.
Step 4: Attributes: decide from facts about the request
Attribute-based access control (ABAC) writes rules over attributes of the user, the resource and the situation: "a user may edit a document if
user.department == doc.departmentanddoc.status != 'locked'".A few rules like this can cover thousands of cases without a new role for each. Here 4 policies cover all 5,000 users. The cost: rules are harder to read and test, and a decision can depend on data that must be loaded first.
Many systems mix both: roles for the broad rights, attributes for the details.
Step 5: Roles say what you may do; ownership says to what
Ann is an editor, so she may read invoices. But which ones? If the code only checks the role, then
GET /invoices/1002works for any number, and a script can walk through all 10,000 invoices, including other customers'. This very common bug is called broken object-level authorization.The fix is to check the owner every time:
SELECT * FROM invoices WHERE id = 1002 AND owner_id = 31. Now Ann can read only her 3, not all 10,000.Random-looking ids do not protect you. The check must be in the code, never in the hope that nobody guesses.
Step 6: Check at the gateway and again in the service
An API gateway is a good place for coarse checks: is the token valid, does the role allow this route, is the caller within limits. It protects every service in one place.
But only the service that owns the data knows who owns invoice 1002. So fine-grained checks (ownership, status) must happen inside the service. Never trust a request just because it came from "inside" the network: check again.
Defence in depth: the gateway stops the obvious, the service has the last word.
Step 7: Give the least access that does the job
Least privilege: every person, service and token gets only the rights it needs, for only as long as it needs them. A reports job only reads, so it connects to the database with a read-only user.
Then a bug or a stolen key in that job cannot delete data. Use short-lived tokens, narrow scopes (
invoices:read) and review old rights regularly.Ask 'what is the worst thing this credential can do?' and make that answer small.
Step 8: Write down every decision
Keep an audit log: who did what to which resource, when, and whether it was allowed. The record is append-only and kept apart from the app's own data, so an intruder cannot erase it.
Denied requests matter most: a user who triggers many denials in a minute may be probing. Never put passwords, tokens or full card numbers in the log.
Logs help only if someone looks. Alert on bursts of denials and on use of admin rights.
Step 9: Authorization in one picture
Authentication says who; authorization says what, and it is checked on the server for every request. Roles keep the rules short (5,060 entries instead of 100,000), attributes handle the details, and an ownership check keeps Ann to her 3 invoices out of 10,000.
Check at the gateway and in the service, give each credential the least it needs, and log every decision.