Subjects, principals, and assignments
Who is asking, which groups they act through, and which scoped roles apply.
A binding is everything Rolebook needs to know about the caller for one request: the subject, its optional group principals, its role assignments, and request context.
const bound = authorizer.bind({
subject: { type: "user", id: "user_123", attributes: { mfa: true, suspended: false } },
principals: [{ type: "team", id: "design" }],
assignments: [
{
subject: { type: "team", id: "design" },
role: "workspace_editor",
scope: { resource: { type: "workspace", id: "ws_456" }, appliesTo: "descendants" },
},
],
context: { requestId: "req_789", ipCountry: "NL" },
}); A binding accepts exactly these four keys. An unknown key denies every check on it with
INVALID_REQUEST, so map database rows to these fields rather than spreading them.
Subject
A subject has a required type (a policy name), an opaque application id
(a non-empty string of up to 128 characters), and optional attributes. It never contains roles.
attributesis a plain object of informational facts. Conditions read them directly under thesubjectroot:subject.mfa, notsubject.attributes.mfa.typeandidare reserved fact names. An attribute namedtypeoridcannot replace them.- A subject with any other key, a missing
typeorid, or unsafe values is denied withINVALID_SUBJECT. Unsafe values include an attribute key outside the policy-name grammar (_id,$ref, a key with a space or a non-ASCII letter), aDateor other non-plain object, a string over 512 characters, more than 64 keys, or anundefinedvalue. See keys and values inside snapshots.
Attributes are inputs you vouch for. Load them from the authenticated session or an authoritative store, never from the request body.
Assignments
An assignment gives one subject one role on one scope resource:
import type { RoleAssignment } from "rolebook";
const assignment: RoleAssignment = {
subject: { type: "user", id: "user_123" },
role: "workspace_editor",
scope: {
resource: { type: "workspace", id: "ws_456" },
appliesTo: "descendants",
},
}; subjectis a{ type, id }reference. An assignment applies when it names the bound subject or one of the binding’s principals.rolemust name a role in the policy.scope.resource.typemust equal the role’sscope.scope.appliesTois"self"or"descendants"; see assignment scope.
Rolebook does not store assignments. Load the subject’s current assignments for the tenant on every request, from the same authoritative source your application trusts for membership.
The whole collection is validated
Rolebook validates the entire assignment collection before it filters assignments by subject or scope. One malformed entry, unknown role, or role/scope mismatch fails the binding closed, even when that entry belongs to another subject or would not apply to the resource being checked:
| Problem | Code |
|---|---|
| Not an array, more than 256 entries, an entry with a missing or extra key, unsafe values | INVALID_ASSIGNMENT |
| A role the policy does not define | UNKNOWN_ROLE |
| A scope resource type that does not match the role’s scope | INVALID_ASSIGNMENT |
A corrupted assignment set is not a safe basis for any decision, so Rolebook refuses to decide rather than skip the bad entry.
Group principals
principals lists flat groups, such as teams, that your application has established the subject
belongs to. An assignment to any of them applies to the subject:
const decision = authorizer
.bind({
subject: { type: "user", id: "alice" },
principals: [{ type: "team", id: "design" }],
assignments: [
{
subject: { type: "team", id: "design" },
role: "workspace_editor",
scope: { resource: { type: "workspace", id: "ws_456" }, appliesTo: "descendants" },
},
],
})
.check({ permission: "project:update", resource: project });
// decision.matched?.via -> { type: "team", id: "design" } - At most
MAX_PRINCIPALS(64) principals, each a{ type, id }reference with the subject grammar, all distinct, none equal to the subject. Anything else denies withINVALID_SUBJECT. - Principals are flat. A principal is only compared with assignment subjects and is never expanded into further principals, so groups cannot nest.
- Deny wins across every applicable assignment, whichever principal contributed it.
- Conditions still read the bound subject’s own facts, not the group’s.
matched.via(and each trace entry’svia) names the group whose assignment contributed the rule. A rule the subject holds directly is attributed to the subject, with novia, even when a group also holds it.
Group membership is an input like any assignment. Load it from an authoritative, tenant-scoped source. The teams package does this for sharing.
Context
context is an optional plain object of request facts, read by conditions under the context root
(context.ipCountry). Decide for each value whether a client could have influenced it before you put
it there: a header the client controls is not evidence.
An explicit undefined at the top level (context: undefined) counts as omitted. An undefined value
inside context, subject attributes, an assignment, or a resource is not JSON-like and denies, so
omit optional values rather than setting them to undefined.
Reusing a validated collection
Every bind() deep-copies and validates the assignments it receives, which makes a decision
independent of anything the caller does afterwards. When the same assignments are bound again and
again, for example in one binding per resource, prepareAssignments() does the copy once:
import { prepareAssignments, type RoleAssignment, type TrustedResource } from "rolebook";
declare const assignments: readonly RoleAssignment<"workspace_editor">[];
declare const candidates: readonly TrustedResource[];
const prepared = prepareAssignments(assignments) ?? assignments;
for (const resource of candidates) {
const decision = authorizer
.bind({ subject: { type: "user", id: "user_123" }, assignments: prepared })
.check({ permission: "project:read", resource });
} The result is a frozen copy Rolebook produced. A later bind() reuses it instead of copying it, but
still validates every entry and still enforces the assignment count and size budgets across reused and
fresh entries together. prepareAssignments() returns undefined for a collection that would not bind,
so the caller keeps its original value and bind() returns its usual closed decision: preparation is
an optimisation and never decides a request.
The ACL recipe walks one tenant, three people, a direct grant, a deny, and a revocation on this binding model.