Identity, Credentials and Permission Models (RBAC, ABAC, ReBAC)
Distinguish authentication from authorization, analyse the architectural trade-offs between RBAC, ABAC, and ReBAC, and select the right permission paradigm for your domain.
Contentsand lesson path
On this page
- Why it matters
- Core terminology: principals, resources and actions
- Authentication flows: login, logout and password reset
- Role-Based Access Control (RBAC)
- Attribute-Based Access Control (ABAC)
- Relationship-Based Access Control (ReBAC)
- Check your understanding
- Apply it
- Review prompt
Lessons of Authentication & Authorization
- Identity, Credentials and Permission Models (RBAC, ABAC, ReBAC) Concept · 20 min
- Storing Passwords So a Breach Is Not a Disaster Worked example · 20 min
- Sessions, Tokens or API Keys? Trade-offs and Pitfalls Concept · 30 min
- Delegated Login with OAuth 2.0 and OpenID Connect, and Stopping Token Replay Concept · 30 min
- Lab: Roles and Ownership Checks in FastAPI Lab · 30 min
- Check: Spot the Authorization Bug Knowledge check · 10 questions
What you will learn
- Store and verify passwords with an adaptive hash (Argon2id or bcrypt) and implement login, logout and password reset flows that resist account enumeration and brute force.
- Enforce role-based and object-level authorization in one central place, and prove with tests that one user cannot read or change another user's resources.
Prerequisites: HTTP & APIs
01. Why it matters
Security incidents frequently make headlines when attackers breach systems, but a significant portion of real-world vulnerabilities stem not from cracked cryptographic hashes, but from broken access control. When an API verifies that a user is logged in, but fails to verify whether that user has legitimate authority to view, modify, or delete a specific resource, the application suffers from Broken Object Level Authorization (BOLA), formerly known as Insecure Direct Object References (IDOR).
Every backend engineer must keep two core concepts apart:
- Authentication (AuthN): Answers the question "Who are you?" AuthN is the verification of identity through credentials—such as passwords, cryptographic public keys, session cookies, or biometric assertions.
- Authorization (AuthZ): Answers the question "What are you permitted to do?" AuthZ is the evaluation of policy to decide whether an authenticated identity has permission to execute an action on a target resource.
Conflating authentication with authorization is dangerous. A user presenting a perfectly valid, signed JWT access token is fully authenticated; however, that token grants zero inherent right to delete another tenant's invoices. A clear permission model, chosen early and checked on every request, makes leaks of one customer's data to another much less likely and avoids costly rewrites later.
02. Core terminology: principals, resources and actions
All modern access control frameworks rely on four formal entities:
- Principal (or subject): The entity performing the request. This can be a human user (
user_102), an automated service account (billing-sync-worker), or an external partner API client (partner_acme). - Resource (or object): The entity being accessed or mutated. Examples include database records, files, payment charges, and document workspaces (
invoice_9082). - Action (or verb): The operation the principal wishes to execute on the resource (
read,update,approve,delete,export). - Environment or context: Transient metadata surrounding the execution, such as client IP address, time of day, authentication factor strength (e.g., whether MFA was completed), and tenant context.
The authorization engine evaluates the tuple (Subject, Action, Resource, Context) and returns a binary authorization decision: ALLOW or DENY. Under deny by default (also called fail-safe defaults), the decision is DENY unless an explicit rule permits access, and a check that fails or meets a state it does not handle also returns DENY. This is a different rule from least privilege, which says that the rules you do write should grant each principal only the access its job needs.
03. Authentication flows: login, logout and password reset
Authorization can only be as good as the identity it starts from. For password login, three flows decide that identity, and each has a trap that leaks information or invites guessing:
- Login. Store only a slow, salted password hash (Argon2id or bcrypt, next lesson), never the password. Give the same answer and do roughly the same work for "no such user" and "wrong password", so the response does not tell an attacker which email addresses have accounts (account enumeration). Limit failed attempts per account and per source address, and slow down or challenge rather than lock accounts for good, which would let anyone lock out a victim.
- Logout. Deleting the cookie in the browser is not enough: end the session on the server, so a copied session token stops working too.
- Password reset. Answer "if that address has an account, we sent a link" for every address. The link carries a random, single-use token that expires within minutes and is stored only as a hash. After a reset, end the user's other sessions.
The next lesson builds these flows with Argon2id and tests each trap.
04. Role-Based Access Control (RBAC)
Role-Based Access Control (RBAC) assigns permissions to abstract roles rather than assigning permissions directly to individual users. Users are subsequently granted one or more roles. This simplifies identity management across standard business organizations.
In classical RBAC architectures:
- A permission is a discrete capability, such as
invoices:createorreports:view. - A role is a collection of permissions, such as
accountant(havinginvoices:createandinvoices:read) orbilling_admin. - A user is assigned roles. If Alice has role
accountant, she inherits all permissions bundled into that role.
While RBAC is intuitive and simple to query in relational databases with a handful of tables (users, roles and permissions, plus the join tables user_roles, which records who holds which role, and role_permissions), it suffers from a well-known architectural trap: role explosion.
When business requirements become nuanced—for example, "An accountant can only view invoices belonging to their regional branch", or "A manager can only edit contracts in draft status"—pure RBAC forces teams to invent dozens of hyper-specific roles: accountant_apac, accountant_emea, contract_editor_draft_only. With hundreds of static roles, nobody can say any more who may do what, and every new region or status needs new roles.
05. Attribute-Based Access Control (ABAC)
Attribute-Based Access Control (ABAC) avoids role explosion by evaluating policies against dynamic attributes of the subject, resource, and environment. Instead of asking "Does Alice have the role editor?", ABAC asks: "Do Alice's attributes satisfy the policy for this specific document at this moment in time?"
Here is an example of an ABAC policy expressed in declarative Python logic:
from dataclasses import dataclass
from datetime import datetime, timezone
@dataclass
class Subject:
user_id: str
tenant_id: str
department: str
clearance_level: int
@dataclass
class DocumentResource:
document_id: str
tenant_id: str
owner_id: str
classification: int
is_archived: bool
def can_edit_document(subject: Subject, resource: DocumentResource) -> bool:
# 1. Multi-tenant isolation boundary
if subject.tenant_id != resource.tenant_id:
return False
# 2. Immutable state restriction
if resource.is_archived:
return False
# 3. Ownership attribute rule
if subject.user_id == resource.owner_id:
return True
# 4. Departmental clearance attribute rule
if (
subject.department == "compliance"
and subject.clearance_level >= resource.classification
):
return True
return FalseNotice how one function combines tenant boundaries, resource state (is_archived), ownership and clearance levels. ABAC can express detailed compliance rules without new roles. However, ABAC policies can become computationally expensive to evaluate across large database listing queries because filtering requires joining multiple attribute tables or evaluating policies on individual records.
06. Relationship-Based Access Control (ReBAC)
In modern collaborative SaaS applications (such as Google Drive, Notion, or Figma), permissions are primarily defined by graphs of relationships between entities. A user has access to a document not because of a global role or security badge, but because they belong to a team that owns a folder that contains the document.
Relationship-Based Access Control (ReBAC)—pioneered by Google's Zanzibar system and implemented by open-source engines like OpenFGA and Ory Keto—expresses access as relationship tuples:
- Tuple schema:
(resource_type:resource_id, relation, subject) - Example 1:
(folder:engineering, member, user:alice) - Example 2:
(doc:roadmap_2027, parent, folder:engineering) - Rule definition: A user can read a document if they are a viewer of the document OR if they are a member of the document's parent folder.
ReBAC models access as a directed graph traversal. It provides granular sharing and folder inheritance, and revoking access means deleting one relationship tuple instead of updating permission records on millions of child entities. Such engines answer checks from replicated, cached data; Zanzibar issues consistency tokens (zookies) so that a check made after a change is evaluated on data that includes it.
07. Check your understanding
08. Apply it
Examine your current system's authorization logic. Are route handlers simply checking user.role == "admin" or are they validating resource-level ownership? If you find roles multiplying to accommodate regional or status constraints, sketch an ABAC attribute policy or ReBAC relation schema for your domain entities.
Keeping authentication and authorization separate makes the missing ownership check, the cause of BOLA, easy to spot in review and to test for. In the next lesson, you will learn how to safely hash and store user passwords using Argon2id so that a leaked database does not hand attackers your users' passwords.
09. Review prompt
Compare and contrast RBAC, ABAC, and ReBAC, detailing the primary strengths and weaknesses of each. Explain what causes role explosion in RBAC and how ABAC solves it. Finally, describe how a BOLA (IDOR) vulnerability occurs when authorization is omitted after successful authentication.
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
10 questions · about 8 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.