On this page

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
AuthenticationAuthorization
QuestionWho are you?What can you do?
HappensFirstAfter authentication
Based onPassword, token, OTPRoles, permissions, rules
Failure code401 Unauthorized403 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

TermMeaningExample
SubjectWho is actingUser Ravi
ResourceWhat is being accessedOrder #501
ActionWhat they want to doread, update, delete
PermissionAllowed action on a resourceorders:read
PolicyRule 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: read

Simple, 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 → Permissions
2
3Ravi → CUSTOMER → orders:read, orders:create
4Priya → ADMIN → orders:read, orders:delete, users:manage

The 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.department
2 AND user.level >= 3
3 AND time is between 9 AM and 6 PM

Very 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.jpg
3→ Ravi can edit photo.jpg

Great for sharing-heavy apps (Google Drive, GitHub, social networks). Google's Zanzibar system uses this model.

ModelDecides usingBest for
ACLList on each resourceFiles, simple sharing
RBACUser's roleMost business apps
ABACAttributes and conditionsComplex enterprise rules
ReBACRelationshipsDocs, 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 → Database
2 │ │
3 coarse check: fine check:
4 "valid token? "is Ravi the owner
5 has orders scope?" of order #501?"
LayerWhat it checks
API GatewayCoarse rules: valid token, required scope or role
ServiceFine rules: ownership, business logic
DatabaseLast defense: row-level security, filtered queries

Centralized vs Decentralized

Decentralized (in each service)Centralized (policy service)
HowEach service has its own rules in codeServices ask a policy engine (e.g., OPA)
ProsFast, no extra network callOne place to manage and audit rules
ConsRules duplicated and inconsistentExtra 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 do
2→ 401 = not logged in, 403 = not allowed
3→ models: ACL, RBAC (most common), ABAC, ReBAC
4→ gateway does coarse checks, services do fine checks
5→ watch for IDOR, deny by default, least privilege

Next, we'll look at API Security, where authentication and authorization combine with other protections to keep APIs safe.

Next TopicAPI Security