Language: English | Español
Get a Node handler protected by UMS authorization in five minutes. For NestJS, see the NestJS Quickstart. For the full reference, see README.md.
npm install @ums/sdk-authorization @ums/sdk-contracts
npm install --save-dev @ums/sdk-testingimport {
AsyncLocalAuthGraphAccessor,
AuthorizationValidator,
configureAuthorization,
} from "@ums/sdk-authorization";
const accessor = new AsyncLocalAuthGraphAccessor();
const validator = new AuthorizationValidator();
configureAuthorization({ accessor, validator });
export { accessor };import express from "express";
import { parseAuthorizationGraph } from "@ums/sdk-authorization";
import { accessor } from "./auth";
const app = express();
app.use(async (req, _res, next) => {
const token = req.headers.authorization?.replace(/^Bearer\s+/i, "");
if (!token) return next();
const graph = await parseAuthorizationGraph(token); // calls /api/v1/client/authenticate if no cached
accessor.run(graph, () => next());
});The helper validates schemaVersion against the SDK's compatibility range and throws AUTH_205 if incompatible — handle it as a 401 in your error middleware.
import { requireScope } from "@ums/sdk-authorization";
app.post(
"/orders/:id/approve",
requireScope("PURCHASE_ORDER.APPROVE", async (req, res) => {
// business logic — runs only if authorized
res.json({ ok: true });
})
);If the user lacks the scope, the handler throws AuthorizationDeniedError before running.
import { RequiresScope } from "@ums/sdk-authorization";
class OrderService {
@RequiresScope("PURCHASE_ORDER.APPROVE")
async approveOrder(orderId: string): Promise<void> {
// ...
}
}import { describe, it, expect } from "vitest";
import { AuthGraphBuilder } from "@ums/sdk-testing";
import {
MemoryAuthGraphAccessor,
AuthorizationValidator,
AuthorizationDeniedError,
configureAuthorization,
} from "@ums/sdk-authorization";
describe("approveOrder", () => {
it("denies when scope is missing", async () => {
const graph = AuthGraphBuilder
.forTenant("LOGISTICS_CORE")
.withUser("ana.flores@example.com")
.withScope("PURCHASE_ORDER.VIEW") // VIEW only, no APPROVE
.build();
const accessor = new MemoryAuthGraphAccessor();
accessor.set(graph);
configureAuthorization({ accessor, validator: new AuthorizationValidator() });
await expect(approveOrder("order-id"))
.rejects.toThrow(AuthorizationDeniedError);
});
});const approveOrder = requireScope(
"PURCHASE_ORDER.APPROVE",
async (orderId: string): Promise<Result> => {
return { ok: true, value: undefined };
},
{ onDenied: "returnFailure" }
);configureAuthorization({ accessor, validator, mode: "audit-only" });import { MemoryAuthGraphAccessor } from "@ums/sdk-authorization";
const accessor = new MemoryAuthGraphAccessor();
accessor.set(graph); // after login
// ...
accessor.clear(); // on logout