Authorization — RBAC, ABAC, ReBAC & Policies
Where authorization checks belong, object-level authorization and IDOR/BOLA, role-based, attribute-based and relationship-based access control (Google Zanzibar), scopes vs permissions, policy engines (OPA, Cedar), multi-tenant isolation, and filtering lists by permission.
Authorization is checked on every request
Authentication tells you who; authorization decides whether this subject may perform this action on this resource, now.
Text
decision = authorize(subject, action, resource, context)
e.g. (user:42, "refund", order:9, {ip, time, mfa_age})
Python
# ❌ vulnerable: any logged-in user can read any invoice by changing the ID
@app.get("/invoices/{invoice_id}")
def get_invoice(invoice_id: str, user=Depends(current_user)):
return db.invoices.get(invoice_id)
# ✅ scope every lookup by the caller's tenant (and check object-level rules)
@app.get("/invoices/{invoice_id}")
def get_invoice(invoice_id: str, user=Depends(current_user)):
invoice = db.invoices.find_one(id=invoice_id, tenant_id=user.tenant_id)
if invoice is None or not policy.can(user, "invoice:read", invoice):
raise HTTPException(404) # don't reveal existence
return invoice
Models
| RBAC | ABAC | ReBAC | |
|---|---|---|---|
| Decision based on | role membership | attributes + policies | relationships in a graph |
| Expressiveness | low–medium | very high | high (hierarchies, sharing) |
| "Who can access X?" | easy | hard | possible via reverse lookups |
| Typical | internal admin tools, SaaS tenant roles | compliance, government, cloud IAM conditions | Drive/Docs sharing, GitHub, Notion, multi-level orgs |
| Tools | built-in tables, Casbin | OPA/Rego, Cedar, XACML | SpiceDB, OpenFGA, Ory Keto, Permify |
RBAC schema
SQL
CREATE TABLE role (id SERIAL PRIMARY KEY, tenant_id BIGINT, name TEXT, UNIQUE (tenant_id, name));
CREATE TABLE permission (id SERIAL PRIMARY KEY, name TEXT UNIQUE); -- 'invoice:approve'
CREATE TABLE role_permission (role_id INT REFERENCES role, permission_id INT REFERENCES permission,
PRIMARY KEY (role_id, permission_id));
CREATE TABLE user_role (user_id BIGINT, role_id INT REFERENCES role, PRIMARY KEY (user_id, role_id));
-- does user 42 have invoice:approve (in their tenant)?
SELECT EXISTS (
SELECT 1 FROM user_role ur
JOIN role_permission rp ON rp.role_id = ur.role_id
JOIN permission p ON p.id = rp.permission_id
WHERE ur.user_id = 42 AND p.name = 'invoice:approve'
);
ReBAC (Zanzibar-style) model
Text
// OpenFGA / SpiceDB-style schema
type user
type team
relations
define member: [user]
type folder
relations
define owner: [user]
define editor: [user, team#member] or owner
define viewer: [user, team#member] or editor
type document
relations
define parent: [folder]
define editor: [user] or editor from parent
define viewer: [user] or viewer from parent
// tuples (facts)
team:eng#member@user:asha
folder:q4#editor@team:eng#member
document:plan#parent@folder:q4
// check: document:plan#editor@user:asha → ALLOWED (via team → folder → document)
ABAC with a policy engine
Text
# OPA / Rego
package orders.authz
default allow := false
allow if {
input.action == "refund"
input.subject.roles[_] == "support_agent"
input.resource.tenant_id == input.subject.tenant_id
input.resource.amount_cents <= 50000 # agents may refund up to 500.00
time.now_ns() - input.subject.mfa_at_ns < 15 * 60 * 1e9
}
Text
// AWS Cedar
permit (principal, action == Action::"refund", resource)
when { principal in Role::"support_agent"
&& resource.tenant == principal.tenant
&& resource.amount_cents <= 50000 };
Architecture: PEP and PDP
Multi-tenant isolation
- Resolve the tenant from the token (never from a client-supplied header alone).
- Scope every query by
tenant_id; enforce with Postgres Row-Level Security as a backstop. - Separate encryption keys per tenant for regulated customers.
- Cross-tenant admin access only through audited, break-glass paths.
- Test: automated "tenant A cannot see tenant B" suites.
Design rules
- Deny by default; explicit allows.
- Least privilege: narrow roles, short-lived elevated access (just-in-time).
- Check at the object level, on the server, every time.
- Keep authorization logic centralised (library/PDP), not copy-pasted.
- Re-check sensitive actions with step-up authentication (recent MFA).
- Audit every decision for sensitive resources.
- Separate scopes (what the app may do) from permissions (what the user may do).
Interview questions
Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.