Authorization: Roles and Permissions

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

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