ScuteLocalAuthz decides checks from a signed snapshot of the app's policy, without a request per check. What the snapshot cannot answer goes to the API.
ScuteLocalAuthz#
import { ScuteAdminApi, ScuteLocalAuthz } from "@scute/js-core";const admin = new ScuteAdminApi({ appId: process.env.SCUTE_APP_ID, secretKey: process.env.SCUTE_SECRET });const authz = new ScuteLocalAuthz(admin);const decision = await authz.check({ userId, action: "edit", resource: "document", user: { region: "eu" }, // the user's attributes, for conditions});if (!decision?.allowed) return deny();check({ userId, action, resource?, context?, user? }) returns a local decision, the API's AuthzDecision, or null when the API call failed. Treat null as a deny.
| Option | Default | |
|---|---|---|
| refreshMs | 30000 | Fetch the snapshot again after this long. |
| rolesTtlMs | 30000 | Cache each user's roles this long. |
| strict | false | Deny on a missing attribute, like the server, instead of asking the API. |
| maxStaleMs | 5 × refreshMs, at least 60 s | Stop deciding locally when the last verified snapshot is older than this. A snapshot past its exp is never used. |
authz.refresh() fetches and verifies the snapshot now; it rejects and keeps the previous policy when the snapshot cannot be fetched or does not verify. authz.policyVersion is the version in use, or null before the first fetch.
What goes to the API:
- roles held on one object (the relationship graph lives on the server);
- conditions that read an attribute you did not pass (unless strict);
- any check while no fresh verified snapshot is available.
A step-up verification is not in the snapshot either, so local step-up answers stay allow_with_step_up. Settle those with admin.authzCheck.
Edge functions#
For runtimes where you fetch and cache the snapshot yourself, the pieces are exported:
import { decideLocally, verifySnapshotToken } from "@scute/js-core";const { data: snap } = await admin.authzSnapshot(); // { version, token, expires_at, jwks }const { data: jwks } = await admin.authzJwks(); // GET /v1/auth/:app_id/jwksconst claims = await verifySnapshotToken(snap.token, jwks, String(process.env.SCUTE_APP_ID));const decision = decideLocally(claims.policy, { roles: ["editor"], action: "edit", resource: "document", user: { region: "eu" },});// { decision: "allow" | "deny" | "allow_with_step_up" | "allow_with_approval" | "unknown", allowed, reason, permission?, roles? }| Export | Does |
|---|---|
| verifySnapshotToken(token, jwks, appId, now?) | Verifies the RS256 signature with WebCrypto, the audience, the type and the expiry. Throws on any failure. Returns the claims { typ, aud, iat, exp, version, policy }. |
| decodeSnapshotToken(token) | Reads the claims without verifying. |
| decideLocally(policy, check) | Decides one check. "unknown" with reason needs_server means ask the API. |
| admin.verifiedAuthzSnapshot() | Fetches the snapshot and verifies it, caching the JWKS and fetching it again once when the key is not found. |
decideLocally's check: roles (default roles are added for you), user, action, resource, context, now (for context.now, hour_utc, weekday_utc; default now), strict. A permission marked blocked_while_impersonating is denied when context.impersonated is set.