Authorization
Rules that decide every request the same way
The Rolebook engine: your resources and their actions, roles given per workspace or project, conditions, field rules, answers that explain themselves, and checks for your rules before they ship.
01 · Authorization
Policies, resources and actions
- What it does
- A policy lists the kinds of things in your product (organizations, workspaces, projects), what can be done to each, and the roles that allow or block those actions.
- Why it matters
- Access lives in one place instead of in checks scattered through your code, so every page, button and request follows the same rules.
- How it works
- You write the policy as TypeScript. Rolebook validates it once, rejects anything inconsistent, and returns a frozen copy that every check reuses.
import { definePolicy } from "rolebook";
export const policy = definePolicy({
resources: {
organization: { actions: ["read"] },
workspace: { parent: "organization", actions: ["read", "update"] },
project: { parent: "workspace", actions: ["read", "update", "delete"] },
},
roles: {
workspace_viewer: {
scope: "workspace",
rules: [{ effect: "allow", permission: "project:read" }],
},
workspace_editor: {
scope: "workspace",
inherits: ["workspace_viewer"],
rules: [
{
effect: "allow",
permission: "project:update",
where: { fact: "resource.ownerId", operator: "equals", value: { fact: "subject.id" } },
},
],
},
contractor: {
scope: "workspace",
rules: [{ effect: "deny", permission: "project:delete" }],
},
},
});02 · Authorization
Scoped roles and inheritance
- What it does
- Each role belongs to one kind of thing, such as a workspace, and can build on other roles at the same level: an Editor can include everything a Viewer has.
- Why it matters
- The same person can be a Viewer in one workspace and an Editor in another, and your roles stay short because shared rules are written once.
- How it works
- A role names its scope and its inherited roles. It takes over their rules exactly, blocks included. There is no hidden super-role: more access is always an explicit role with explicit rules.
Sam in Acme · Viewer
Sam in Acme · Viewer: View allowed, Edit not allowed, Share not allowed, Delete not allowed.
Sam in Globex · Editor
Sam in Globex · Editor: View allowed, Edit allowed, Share allowed, Delete not allowed.
03 · Authorization
Assignments across your hierarchy
- What it does
- An assignment gives one person or team one role on one item, for that item alone or for everything inside it.
- Why it matters
- Give someone access to a whole workspace once, and every project in it follows, without copying roles onto each project.
- How it works
- A person's record never holds roles. For each request your app passes their assignments and the item with its parents, from the nearest one up to the organization. Rolebook validates every assignment before it uses any of them.
- Organization Acme
- Workspace DesignAda: Editor, for the workspace and everything in it
- Project Brand assetsEditor applies
- Project Q3 reportEditor applies
- Workspace DesignAda: Editor, for the workspace and everything in it
import { createAuthorizer, trustedResource } from "rolebook";
import { policy } from "./policy";
const ada = { type: "user", id: "ada" } as const;
const acme = { type: "workspace", id: "acme" } as const;
const inAcme = { resource: acme, appliesTo: "descendants" } as const;
export const authorization = createAuthorizer(policy).bind({
subject: ada,
assignments: [
{ subject: ada, role: "workspace_editor", scope: inAcme },
{ subject: ada, role: "contractor", scope: inAcme },
],
});
export const project = trustedResource(
{ type: "project", id: "q3_report" },
[acme, { type: "organization", id: "org_1" }],
{ ownerId: "ada" },
);
authorization.check({ permission: "project:update", resource: project });
// { allowed: true, code: "ALLOWED", ... }
authorization.check({ permission: "project:delete", resource: project });
// { allowed: false, code: "EXPLICIT_DENY", ... }04 · Authorization
Conditions on facts
- What it does
- A rule can depend on facts about the person, the item or the request, such as only the owner may edit, or only while a project is not archived.
- Why it matters
- Real products have rules like these. Writing them into the policy keeps them visible, testable and enforced everywhere.
- How it works
- A condition compares facts with values or with other facts, and combines them with all, any, none and not. A fact that is missing or malformed never grants access, and an unknown condition still lets a block apply.
05 · Authorization
Field-level permissions
- What it does
- Rules can allow or block single fields of an item, such as the title but not the billing details.
- Why it matters
- One record often holds data with different sensitivity. Field rules protect part of it without splitting it up.
- How it works
- A rule lists field paths, with * for one level and ** for any depth. An allow for some fields never grants the whole item, and a block on a field also stops requests that could expose it.
// In the project_editor role: the title and any one metadata field, nothing else.
rules: [{ effect: "allow", permission: "project:update", fields: ["title", "metadata.*"] }],
authorization.check({ permission: "project:update", resource: project, field: "title" });
// { allowed: true, code: "ALLOWED", ... }
authorization.check({ permission: "project:update", resource: project, field: "owner" });
// { allowed: false, code: "NOT_GRANTED", ... }
authorization.check({ permission: "project:update", resource: project });
// { allowed: false, code: "NOT_GRANTED", ... }: a field rule never grants the whole project06 · Authorization
A no always wins
- What it does
- If any of a person's roles blocks an action, the answer is no, whatever their other roles allow.
- Why it matters
- You can rely on a block: a contractor role that may never delete stays that way, even when the same person is also an admin.
- How it works
- Rolebook checks blocks before allows, across every role the person holds directly or through a team.
Sam Lee has both roles
Admin
Admin: View allowed, Edit allowed, Share allowed, Delete allowed.
Contractor
Contractor: View allowed, Edit allowed, Share not allowed, Delete blocked.
Sam Lee → delete: Denied
07 · Authorization
Closed by default, with hard limits
- What it does
- Anything Rolebook cannot read with certainty becomes a no: a malformed request, an unknown role, an item that does not match its chain.
- Why it matters
- Malformed or unexpected input can never open access by accident.
- How it works
- check() never throws. It returns a denial with its own code. Every input has a size limit, and inputs beyond it are denied safely.
- ALLOWED
- EXPLICIT_DENY
- NOT_GRANTED
- INVALID_REQUEST
- INVALID_SUBJECT
- INVALID_ASSIGNMENT
- UNKNOWN_ROLE
- RESOURCE_MISMATCH
08 · Authorization
Decisions that explain themselves
- What it does
- Every answer carries a stable code and a reason, and names the rule and role that decided it when one did.
- Why it matters
- When someone asks why they cannot open a page, your support team can answer, and your logs show more than a bare no.
- How it works
- explain() runs the same check and adds a trace of every rule it evaluated, up to 64 entries. Ask for condition detail and each rule also shows how its conditions came out, without the values they compared. enforce() throws an error that carries the decision.
import { authorization, project } from "./check";
const explanation = authorization.explain({ permission: "project:delete", resource: project });
explanation.code; // "EXPLICIT_DENY"
explanation.reason; // "A deny rule matched."
explanation.matched; // { effect: "deny", role: "contractor", permission: "project:delete" }
explanation.trace; // every rule Rolebook evaluated, and whether it matched
authorization.enforce({ permission: "project:delete", resource: project });
// throws AuthorizationError, with the decision on error.decision09 · Authorization
Catch policy mistakes before they ship
- What it does
- List the facts your rules read, such as a project's status or owner, and check a policy against that list before it goes live.
- Why it matters
- A mistyped fact name is still a valid rule: it just never matches. The check finds it before you deploy, while it is still a one-line fix.
- How it works
- The optional rolebook/authoring entry point reports each problem with a code and a pointer to the rule, such as an unknown fact or a comparison that can never hold. It can also check the facts of one request before you use them. It never changes a decision and never grants anything.
- With an error, compilePolicy() returns no policy. A warning, such as a comparison that only some values can satisfy, does not stop it.
- At most 128 diagnostics per check, in a fixed order, with messages that never contain a fact's value.
- A policy the engine accepts stays accepted by the engine. The checks run only where you call them.
import { compilePolicy, defineFactSchema } from "rolebook/authoring";
const facts = defineFactSchema({
format: "rolebook-fact-schema",
version: 1,
id: "projects",
facts: { "resource.status": { types: ["string"] } },
});
const draft = compilePolicy(
{
resources: { project: { actions: ["read", "update"] } },
roles: {
editor: {
scope: "project",
rules: [
{
effect: "allow",
permission: "project:update",
where: { fact: "resource.stauts", operator: "equals", value: "draft" },
},
],
},
},
},
facts,
);
// draft.ok === false
// draft.diagnostics[0]: { severity: "error", code: "UNKNOWN_FACT", pointer: "/roles/editor/rules/0/where/fact", ... }10 · Authorization
Expected decisions as data
- What it does
- Write the answers you expect as a JSON file: a policy, a person with their roles, an item, an action, and the answer Rolebook should give.
- Why it matters
- Your expectations become data that anyone can read and review, and a failing case names itself and what differed.
- How it works
- A command in the Rolebook repository runs every case in order through the real engine and reports passed or failed, as text or JSON. One file holds up to 64 policies and 1,000 cases, and the command exits with 1 when a case does not match. It runs from a checkout of the repository; it is not part of an installed package.
{
"format": "rolebook-core-fixtures",
"version": 1,
"suiteId": "projects",
"policies": [
{
"id": "projects",
"policy": {
"resources": { "project": { "actions": ["read", "delete"] } },
"roles": {
"viewer": {
"scope": "project",
"rules": [{ "effect": "allow", "permission": "project:read" }]
}
}
}
}
],
"cases": [
{
"id": "viewer-cannot-delete",
"policyId": "projects",
"binding": {
"subject": { "type": "user", "id": "ada" },
"assignments": [
{
"subject": { "type": "user", "id": "ada" },
"role": "viewer",
"scope": { "resource": { "type": "project", "id": "q3_report" }, "appliesTo": "self" }
}
]
},
"resource": { "ref": { "type": "project", "id": "q3_report" }, "ancestors": [] },
"request": { "permission": "project:delete" },
"expect": { "allowed": false, "code": "NOT_GRANTED" }
}
]
}11 · Authorization
Policy diff and versions
- What it does
- Compare two versions of your rules, and, with Rolebook Sharing, publish new versions and preview who would gain or lose access first.
- Why it matters
- Changing a role is safer when you can see exactly what changes, and who it affects, before it ships.
- How it works
- diffPolicies() lists every resource, role and rule that changed. The versioned sharing setup keeps versions in your own database, previews the impact on a sample of people, and never saves a change under a version that is no longer active.
import { diffPolicies } from "rolebook";
// current and next: two definePolicy() results. In next, admin no longer has project:delete.
const diff = diffPolicies(current, next);
diff.identical; // false
diff.roles.changed; // [{ role: "admin", rulesRemoved: [{ effect: "allow", permission: "project:delete" }], ... }]