# Authorization: Roles and Permissions

> Decide what an authenticated user may do with RBAC, ownership checks and attribute-based rules. Free notes, key concepts, resources and a mini project.

Canonical: https://www.internhack.in/roadmaps/tracks/backend/authorization-models
Last updated: 2026-11-28

Decide what an authenticated user may do with RBAC, ownership checks and attribute-based rules.

Intermediate · about 5 hours · difficulty 3/5

Learn first: [Sessions and Cookies](https://www.internhack.in/roadmaps/tracks/backend/sessions-cookies)

Authentication tells you **who** someone is. **Authorization** decides **what they can do**. Broken access control is the number one risk in the OWASP Top 10, usually because a check was forgotten, not because the model was wrong.

### Common models

- **Ownership checks**: a user can edit only their own resources. Always filter by owner in the query (`WHERE id = $1 AND user_id = $2`) instead of loading by id alone.
- **RBAC (role-based)**: users get roles (admin, editor, viewer) and roles grant permissions (`post:publish`). Simple and widely understood.
- **ABAC (attribute-based)**: rules evaluate attributes of the user, resource and context ("managers can approve expenses under 1000 in their own department").
- **ReBAC (relationship-based)**: permissions follow relationships, like Google Drive sharing. Tools include OpenFGA and SpiceDB, based on Google's Zanzibar paper.
- **Multi-tenancy**: every query scoped to the current tenant, ideally enforced centrally (or with PostgreSQL row-level security).

### Implementation habits

- Centralize checks in middleware or a policy layer, not scattered `if` statements.
- Deny by default.
- Check permissions on the server for every request, even if the UI hides the button.
- Write tests that try to access other users' data.

### Pitfalls

- **IDOR** (insecure direct object reference): `GET /invoices/1043` returns someone else's invoice because only authentication was checked.

You are ready to move on when your app has roles, ownership checks and tests proving a user cannot read or modify another user's data.

## Key concepts

- **Authentication vs authorization**
- **Ownership checks and IDOR**
- **RBAC**
- **ABAC and policy engines**
- **ReBAC (OpenFGA, SpiceDB)**
- **Multi-tenant isolation**
- **PostgreSQL row-level security**

## Mini project

Add RBAC to a project management API (owner, member, viewer per project) enforced in a policy module, plus tests that attempt cross-tenant access and must fail.

## Check yourself

Your UI hides the delete button from viewers. Why is that not authorization, and where must the real check live?

## Free resources

- [OWASP Authorization Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Authorization_Cheat_Sheet.html) (OWASP)
- [OpenFGA documentation](https://openfga.dev/docs) (OpenFGA)

Previous: [SAML and Enterprise Single Sign-On](https://www.internhack.in/roadmaps/tracks/backend/saml-enterprise-sso) · Next: [Storing Passwords and Hashing](https://www.internhack.in/roadmaps/tracks/backend/password-hashing)

## More in Who Is Calling and What Can They Do

- [Sessions and Cookies](https://www.internhack.in/roadmaps/tracks/backend/sessions-cookies)
- [Token-Based Auth and JWT](https://www.internhack.in/roadmaps/tracks/backend/token-auth-jwt)
- [OAuth 2.0 and OpenID Connect](https://www.internhack.in/roadmaps/tracks/backend/oauth2-oidc)
- [SAML and Enterprise Single Sign-On](https://www.internhack.in/roadmaps/tracks/backend/saml-enterprise-sso)
- [Authorization: Roles and Permissions](https://www.internhack.in/roadmaps/tracks/backend/authorization-models)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Backend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/backend)
