archenstack-rbac-nodejs
archenstack-rbac-nodejs is the server-side TypeScript port of Archenstack — a roles × features → level access matrix with a zero-dependency core and, alongside it, a thin Express adapter when you want an HTTP surface. It's the Node sibling of the Python and Java cores, and it speaks the same wire_version 1 contract down to the byte.
The core has no runtime dependencies at all; you bring the storage, the cache, and (unless you use the bundled adapter) the HTTP. This is the back-end companion to archenstack-rbac-web — that one restricts a browser UI from the current user's permissions; this one resolves and serves them.
Install
npm install @archenstack/rbac-node
Point npm at Archenstack's package registry first — see Installation. Working on the library itself? Build it from a checkout instead: npm install && npm run build (tsc → dist/); npm test runs node:test against the shared conformance corpus.
The core is imported from the package root; the Express adapter lives behind the @archenstack/rbac-node/adapter subpath, so a project that only needs the resolver never loads Express.
Quickstart
Build a matrix in code, declare its defaults spreadsheet-style, and query it. The facade methods return values you can compare directly — level gives an ordered AccessLevel, and the can* helpers are just comparisons on top of it.
import { Matrix, F, V, AccessLevel } from "@archenstack/rbac-node";
const matrix = new Matrix({
roles: ["manager", "staff"],
features: { reports: "Reports" },
superusers: ["admin"], // always FULL, never a column
aliases: { temp: "manager" }, // 'temp' resolves as 'manager'
});
matrix.defineRows(["manager", "staff"], { reports: [F, V] });
matrix.level("reports", "manager"); // AccessLevel.FULL
matrix.canWrite("reports", "staff"); // false — VIEW isn't writable
matrix.level("reports", "admin"); // FULL — superuser short-circuits
matrix.level("reports", "temp"); // FULL — mirrors manager
matrix.override("reports", "staff", F); // layer a runtime override
matrix.canWrite("reports", "staff"); // true
matrix.override("reports", "staff", V); // back to the default clears it
matrix.isOverridden("reports", "staff");// false
exportOverrides() returns the canonical, sorted override delta ([{ feature, role, level }]) ready to persist; loadOverrides() reads it back. For policy hygiene, diff, orphanedOverrides, and pruneOrphans are pure helpers over a wire policy.
The spec-conformant core
Under the facade is the same normative core every implementation shares: MatrixSnapshot (with fromSnapshot / toSnapshot / decide / applyOverride / isOverridden / reseed / checkMutation), resolveDecision, evalScope + predicateFromTrueAtoms, validate, and canonicalJson. Reach for it when you need the exact wire snapshot shape or the resolution algorithm that has to agree across languages; reach for Matrix in everyday code.
The Express adapter
@archenstack/rbac-node/adapter mounts the wire-protocol HTTP API over an in-memory store — enough to run, demo, or test against without wiring a database.
import { createApp, jwtClaimsRoleResolver } from "@archenstack/rbac-node/adapter";
// FAIL-CLOSED default: with no resolver every caller is denied (401) and a WARN
// is logged at startup — an X-Role header or bearer is NEVER trusted as the role.
const app = createApp(); // an Express app with the routes below
// A real deployment wires a resolver mapping its authenticated principal to a role
// slug. When your JWT middleware has ALREADY verified the token and attached its
// claims, read the role off them (Archenstack never validates the token itself):
const secured = createApp({
resolveRole: jwtClaimsRoleResolver({ roleClaim: "role" }),
});
secured.listen(8080);
| Method + path | Does |
|---|---|
GET /me/ | the caller's resolved permissions envelope |
POST /check/ | decide a feature — single, or a batch (fail-closed per item) |
GET /matrix/ · PUT /matrix/ | read the grid · write a cell override |
POST /matrix/reset/ | drop overrides back to the code defaults |
GET+POST /roles/ · /features/ · /groups/ | list + create |
The caller's role comes from a small pluggable RoleResolver. In 0.2.x the adapter is fail-closed: with no resolver supplied every request is denied 401 — an X-Role header or bearer is never trusted as the role, and a WARN is logged at startup so a misconfigured deployment is obvious. Wire your own resolver mapping an authenticated principal to a role slug, or opt into the shipped jwtClaimsRoleResolver, which reads the role claim off a token your middleware has already verified (Archenstack never validates the token itself). By default it looks for those host-verified claims on req.auth.payload, req.auth, or req.user, and a missing or blank claim resolves to deny. Being single-org and assignment-store-free, this adapter reads only the direct role claim — the org / subject / platform claims of the Spring resolver don't map here.
The old header-trusting headerRoleResolver still exists, but 0.2.x relocated it to the insecure @archenstack/rbac-node/adapter/dev subpath — a header is not authentication, so it is for local demos, examples, and tests only and is deliberately absent from the default adapter surface. Responses are the standard { wire_version, kind, … } envelopes with the shared error taxonomy.
Configure it from the environment
For a real deployment, createAppFromEnv() builds the whole adapter from the environment. src/adapter/config.ts is the single place that reads process.env; it validates every ARCHENSTACK_RBAC_* variable and fails loud at boot on any missing or malformed required var — never a silent fallback. It seeds the store for the configured org, verifies the deployment has a working first administrator, and wires the JWT-claims resolver only when it's explicitly enabled (otherwise the fail-closed default stands).
import { createAppFromEnv } from "@archenstack/rbac-node/adapter";
const app = createAppFromEnv(); // throws unless the required ARCHENSTACK_RBAC_* vars are set
app.listen(8080);
| Variable | Required | Purpose |
|---|---|---|
ARCHENSTACK_RBAC_DEFAULT_ORG | ✓ | The tenant/org slug the single-org in-memory matrix serves. |
ARCHENSTACK_RBAC_CHECK_BATCH_MAX | ✓ | Max items a single POST /check/ batch may carry (positive integer). |
ARCHENSTACK_RBAC_BOOTSTRAP_ADMIN_ROLE | ✓ | The role that MUST resolve rbac.manage FULL at startup — the first administrator (lock-out safety). |
ARCHENSTACK_RBAC_JWT_RESOLVER_ENABLED | — | Opt in (true) to the built-in JWT-claims resolver; unset / false ⇒ the fail-closed default. |
ARCHENSTACK_RBAC_JWT_ROLE_CLAIM | — | The claim holding the caller's role slug when the resolver is enabled (defaults to role). |
Because the in-memory adapter resolves the role directly and carries no assignment store, the bootstrap-admin step is a verify-not-insert check: assertBootstrapAdmin confirms ARCHENSTACK_RBAC_BOOTSTRAP_ADMIN_ROLE exists in the seeded matrix and resolves rbac.manage FULL, throwing at boot if the deployment would start locked out. (The Django and Spring adapters, which persist assignments, instead bind a real first-admin row.) Copy .env.example to a per-developer .env and load it — e.g. node --env-file=.env your-server.js.
The core passes the shared corpus byte-for-byte — every behavioral vector (resolution, aliases, lock-out, scope truth-tables, validation) and every canonical-JSON golden — plus its own adapter and facade tests. If this port and the specification ever disagree, the spec wins.
Releasing
Releases publish to Archenstack's GitLab npm registry from CI. Bump the version (npm version patch / minor / major), then push a matching archenstack-rbac-nodejs/vX.Y.Z tag — the pipeline runs npm run build (tsc → dist/, the only thing the files field ships) and publishes.
The published entry points are the exports map — @archenstack/rbac-node (the core), @archenstack/rbac-node/adapter (Express), and @archenstack/rbac-node/adapter/dev (the insecure headerRoleResolver, dev/demo/test only) — so a resolver-only consumer never pulls in Express.
See also
- The model — roles, features, levels, defaults vs. overrides.
- Resolution, scopes & the authorizer — how a cell is computed and enforced.
- The wire protocol — envelopes, the batch
/check/, and the error taxonomy. - The specification — the exhaustive normative contract.