Authorization
Authorization
Authorization decides what you are allowed to do. It always comes after authentication, which decides who you are.
Think of an office building:
- Showing your ID card at the entrance → authentication
- Your card opening only the 3rd floor, not the server room → authorization
| Authentication | Authorization | |
|---|---|---|
| Question | Who are you? | What can you do? |
| Happens | First | After authentication |
| Based on | Password, token, OTP | Roles, permissions, rules |
| Failure code | 401 Unauthorized | 403 Forbidden |
401 vs 403: 401 means "I don't know who you are, log in". 403 means "I know who you are, but you're not allowed".
Core Terms
| Term | Meaning | Example |
|---|---|---|
| Subject | Who is acting | User Ravi |
| Resource | What is being accessed | Order #501 |
| Action | What they want to do | read, update, delete |
| Permission | Allowed action on a resource | orders:read |
| Policy | Rule that decides allow/deny | "Owners can edit their own orders" |
Every authorization check answers: Can this subject do this action on this resource?
Authorization Models
1. ACL (Access Control List)
Each resource keeps a list of who can do what.
1report.pdf → Ravi: read, write | Priya: readSimple, but hard to manage with millions of users and resources. Used in file systems and Google Drive-style sharing.
2. RBAC (Role-Based Access Control)
Permissions are given to roles, and users get roles.
1User → Role → Permissions2 3Ravi → CUSTOMER → orders:read, orders:create4Priya → ADMIN → orders:read, orders:delete, users:manageThe most common model. Easy to understand and audit. Problem: role explosion when you need fine rules (e.g., "ADMIN_INDIA_READONLY").
3. ABAC (Attribute-Based Access Control)
Decisions use attributes of the user, resource, and environment.
1Allow if user.department == resource.department2 AND user.level >= 33 AND time is between 9 AM and 6 PMVery flexible, but rules are harder to write, test, and debug.
4. ReBAC (Relationship-Based Access Control)
Access depends on relationships between users and resources.
1Ravi is OWNER of folder "Trips"2"Trips" contains photo.jpg3→ Ravi can edit photo.jpgGreat for sharing-heavy apps (Google Drive, GitHub, social networks). Google's Zanzibar system uses this model.
| Model | Decides using | Best for |
|---|---|---|
| ACL | List on each resource | Files, simple sharing |
| RBAC | User's role | Most business apps |
| ABAC | Attributes and conditions | Complex enterprise rules |
| ReBAC | Relationships | Docs, social apps, nested sharing |
Real systems often combine them, e.g., RBAC for broad access + ownership checks for individual records.
Where Authorization Happens in a System
1Client → API Gateway → Order Service → Database2 │ │3 coarse check: fine check:4 "valid token? "is Ravi the owner5 has orders scope?" of order #501?"| Layer | What it checks |
|---|---|
| API Gateway | Coarse rules: valid token, required scope or role |
| Service | Fine rules: ownership, business logic |
| Database | Last defense: row-level security, filtered queries |
Centralized vs Decentralized
| Decentralized (in each service) | Centralized (policy service) | |
|---|---|---|
| How | Each service has its own rules in code | Services ask a policy engine (e.g., OPA) |
| Pros | Fast, no extra network call | One place to manage and audit rules |
| Cons | Rules duplicated and inconsistent | Extra latency, another critical component |
A common middle ground: keep policies in a central place, but evaluate them locally in each service (sidecar or library).
Authorization With Tokens
Roles and permissions are often put inside the access token:
1{ "sub": "101", "role": "CUSTOMER", "scope": "orders:read orders:create" }- Scopes (from OAuth 2.0) limit what an app can do on the user's behalf.
- Roles/permissions limit what the user can do.
Trade-off: if Ravi's role changes, old tokens still carry the old role until they expire. Keep tokens short-lived, or check critical permissions from the database.
Common Mistakes
- IDOR (Insecure Direct Object Reference): checking the user is logged in, but not that they own the resource.
1Ravi calls GET /orders/502 (Priya's order)2Server only checks "is logged in?" → returns Priya's order ✘3Fix: check order.userId == Ravi's id- Checking permissions only in the UI (hiding buttons) but not on the server.
- Allow by default instead of deny by default.
- Trusting role or userId sent in the request body instead of the verified token.
Best Practices
- Deny by default: allow only what is explicitly permitted.
- Least privilege: give the minimum access needed.
- Check authorization on every request, on the server.
- Always verify ownership for user-specific resources.
- Log access decisions, especially denials, for auditing.
- Keep rules in one well-tested place, not scattered across code.
Summary
1Authentication = who you are → Authorization = what you can do2→ 401 = not logged in, 403 = not allowed3→ models: ACL, RBAC (most common), ABAC, ReBAC4→ gateway does coarse checks, services do fine checks5→ watch for IDOR, deny by default, least privilegeNext, we'll look at API Security, where authentication and authorization combine with other protections to keep APIs safe.