d0841c8601
Replace the hardcoded role enum (admin/operator/cashier/readonly, checked
literally as requireRole("admin",...) across ~15 routes) with dynamic RBAC:
roles are DATA, route guards check a PERMISSION.
@parking/shared defines a code-defined grid: RESOURCES (user/role/tariff/
subscription/site/device/shift/payment/session/event/report) × Action
(create/read/update/delete + domain verbs void/cash) -> PERMISSIONS
(resource:action, e.g. tariff:update, payment:create, event:void).
DB: new roles + role_permissions tables; users.role enum -> role_id FK;
migration 0007_rbac (create tables, seed the builtin admin role + all 26
perms, seed operator/cashier/readonly composable roles matching old
behaviour, rebuild users to swap the column copying all rows).
auth.ts: JWT payload role -> roleId; permissionsFor(roleId) with an
in-memory cache + bumpPermsCache(); requirePermission(...perms) preHandler;
requireAuth for /me & /language; initAuth(db) wires the resolver once. Every
route guard mapped to a permission; device ingress (devices/qr-reader) stays
auth-free by design. New routes/users.ts (user:* CRUD, bcrypt 12, last-admin
guard) + routes/roles.ts (role:* CRUD, builtin-protected, perms validated
against the grid, cache bump on write). auth/me + /login return
{roleId, roleName, permissions, language}. seed-admin -> roleId:'admin'.
Frontend: SessionUser carries permissions + can() helper; router nav/route
guards gate by permission (requirePerm replaces adminOnly); SiteSettings
edit gated by site:update; new UsersManager + RolesManager (permission
checkbox grid; admin role locked); i18n nav.users/roles + blocks (sq+en).
Decisions: one role per user; protected built-in admin (no-lockout: the last
admin can't be deleted/downgraded); JWT carries roleId, perms resolved
per-request so role edits apply immediately (no re-login).
Verified: full build green; 20-assertion inject test passes (cashier 403s on
tariff publish + user list, admin passes, granting a perm applies on the next
request, last-admin + builtin-role protections return 409); migration 0007
applied to a copy of the live DB (incl WAL/shm) — existing admin maps to
role_id='admin', all rows preserved. Append-only event chain untouched
(event:void gates appending a void, not a delete).
Claude-Session: https://claude.ai/code/session_01Xcm6ikLgGoCxxHrxtjkk5V
186 lines
7.3 KiB
TypeScript
186 lines
7.3 KiB
TypeScript
import { randomBytes } from "node:crypto";
|
|
import type { FastifyReply, FastifyRequest } from "fastify";
|
|
import { eq, rolePermissions, type Db } from "@parking/db";
|
|
import { ADMIN_ROLE_ID, PERMISSIONS, type Permission } from "@parking/shared";
|
|
|
|
// Local JWT auth helpers — fully local, no external identity provider
|
|
// (offline-first). The JWT is carried in an HttpOnly cookie (JS can't read it);
|
|
// a separate readable CSRF cookie + matching header defends mutations
|
|
// (double-submit). See wiki/entities/local-jwt-auth.md.
|
|
//
|
|
// Authorization is DYNAMIC RBAC: the token carries the user's `roleId`, and each
|
|
// guarded route resolves that role's PERMISSION SET (cached in memory) and checks
|
|
// the permission it requires. Editing a role takes effect on the next request —
|
|
// no re-login, no token bloat, no stale perms. See @parking/shared PERMISSIONS.
|
|
|
|
declare module "@fastify/jwt" {
|
|
interface FastifyJWT {
|
|
payload: { sub: string; username: string; roleId: string; csrf: string };
|
|
user: { sub: string; username: string; roleId: string; csrf: string };
|
|
}
|
|
}
|
|
|
|
export const TOKEN_COOKIE = "parking_token";
|
|
export const CSRF_COOKIE = "parking_csrf";
|
|
export const CSRF_HEADER = "x-csrf-token";
|
|
|
|
// Session lifetime: the JWT has NO expiry — a login is valid until explicit
|
|
// logout. Booth reality breaks any fixed clock (relief late/absent, forced double
|
|
// shifts), and a shift is a separate explicit boundary, not the token's lifetime.
|
|
// See wiki/entities/local-jwt-auth.md + wiki/concepts/shift.md.
|
|
//
|
|
// The cookie still needs a maxAge so it survives a browser restart (a session
|
|
// cookie would log out an active operator on browser close — the opposite of
|
|
// "until logout"). Use a long fixed window; the server clears it on logout.
|
|
export const COOKIE_MAX_AGE_SECONDS = 30 * 24 * 60 * 60; // 30 days
|
|
|
|
/**
|
|
* Resolve the JWT signing secret, refusing to start without a strong one.
|
|
* There is deliberately no fallback default — a missing, short, or placeholder
|
|
* secret throws so the server never runs with forgeable tokens.
|
|
*/
|
|
export function requireJwtSecret(): string {
|
|
const secret = process.env.JWT_SECRET;
|
|
if (!secret || secret.length < 32 || /change.?me|insecure|dev-only/i.test(secret)) {
|
|
throw new Error(
|
|
"JWT_SECRET must be set to a strong random value (>=32 chars). " +
|
|
"Generate one with: openssl rand -hex 32",
|
|
);
|
|
}
|
|
return secret;
|
|
}
|
|
|
|
/** Cookies are secure in production; relaxed for local http dev. */
|
|
function secureCookies(): boolean {
|
|
return process.env.NODE_ENV === "production";
|
|
}
|
|
|
|
export function newCsrfToken(): string {
|
|
return randomBytes(32).toString("hex");
|
|
}
|
|
|
|
/** Set the auth (HttpOnly) + CSRF (readable) cookies after a successful login. */
|
|
export function setAuthCookies(reply: FastifyReply, jwt: string, csrf: string): void {
|
|
const secure = secureCookies();
|
|
reply.setCookie(TOKEN_COOKIE, jwt, {
|
|
httpOnly: true,
|
|
sameSite: "strict",
|
|
secure,
|
|
path: "/",
|
|
maxAge: COOKIE_MAX_AGE_SECONDS,
|
|
});
|
|
// Readable by JS so the SPA can echo it back in the CSRF header (double-submit).
|
|
reply.setCookie(CSRF_COOKIE, csrf, {
|
|
httpOnly: false,
|
|
sameSite: "strict",
|
|
secure,
|
|
path: "/",
|
|
maxAge: COOKIE_MAX_AGE_SECONDS,
|
|
});
|
|
}
|
|
|
|
export function clearAuthCookies(reply: FastifyReply): void {
|
|
reply.clearCookie(TOKEN_COOKIE, { path: "/" });
|
|
reply.clearCookie(CSRF_COOKIE, { path: "/" });
|
|
}
|
|
|
|
const MUTATING = new Set(["POST", "PUT", "PATCH", "DELETE"]);
|
|
|
|
/**
|
|
* Double-submit CSRF check: the X-CSRF-Token header must match the CSRF cookie.
|
|
* The CSRF token is bound into the JWT at login, so a stolen/forged cookie pair
|
|
* still can't pass unless it matches the signed token. Only enforced on
|
|
* state-changing methods (safe reads are exempt).
|
|
*/
|
|
function assertCsrf(req: FastifyRequest): void {
|
|
if (!MUTATING.has(req.method)) return;
|
|
const header = req.headers[CSRF_HEADER];
|
|
const cookie = req.cookies[CSRF_COOKIE];
|
|
const tokenCsrf = (req.user as { csrf?: string } | undefined)?.csrf;
|
|
if (!header || !cookie || header !== cookie || (tokenCsrf && header !== tokenCsrf)) {
|
|
throw Object.assign(new Error("invalid CSRF token"), { statusCode: 403 });
|
|
}
|
|
}
|
|
|
|
// --- Permission resolution + cache -------------------------------------------
|
|
// A role's permission set is read from `role_permissions` and cached in memory.
|
|
// SQLite is single-writer/single-process here, so a module-level Map is a correct
|
|
// cache: every role / role-permission mutation calls bumpPermsCache() to clear it,
|
|
// and the next request re-reads. The built-in `admin` role always resolves to the
|
|
// FULL permission set in code (never trusts the DB rows for it), so administration
|
|
// can't be accidentally narrowed.
|
|
|
|
const ADMIN_PERMS: ReadonlySet<Permission> = new Set(PERMISSIONS);
|
|
const permsCache = new Map<string, ReadonlySet<Permission>>();
|
|
|
|
// The DB handle the permission resolver reads from. Set ONCE at startup via
|
|
// initAuth() so route guards don't each have to thread `db` (several route
|
|
// modules only receive a monitor/service, not the db). Single-process server.
|
|
let authDb: Db | null = null;
|
|
|
|
/** Wire the permission resolver to the app's DB. Call once in buildServer(). */
|
|
export function initAuth(db: Db): void {
|
|
authDb = db;
|
|
permsCache.clear();
|
|
}
|
|
|
|
/** Clear the permission cache. Call after ANY write to roles / role_permissions
|
|
* (or a user's roleId) so the change takes effect on the next request. */
|
|
export function bumpPermsCache(): void {
|
|
permsCache.clear();
|
|
}
|
|
|
|
/** The permission set for a role id, cached. `admin` is always the full set. */
|
|
export function permissionsFor(roleId: string): ReadonlySet<Permission> {
|
|
if (roleId === ADMIN_ROLE_ID) return ADMIN_PERMS;
|
|
const hit = permsCache.get(roleId);
|
|
if (hit) return hit;
|
|
if (!authDb) throw new Error("auth not initialised (call initAuth)");
|
|
const rows = authDb
|
|
.select({ permission: rolePermissions.permission })
|
|
.from(rolePermissions)
|
|
.where(eq(rolePermissions.roleId, roleId))
|
|
.all();
|
|
const set = new Set(rows.map((r) => r.permission as Permission));
|
|
permsCache.set(roleId, set);
|
|
return set;
|
|
}
|
|
|
|
/** True if the role grants every listed permission. */
|
|
export function roleHasPermissions(
|
|
roleId: string,
|
|
required: readonly Permission[],
|
|
): boolean {
|
|
const granted = permissionsFor(roleId);
|
|
return required.every((p) => granted.has(p));
|
|
}
|
|
|
|
/**
|
|
* preHandler permission guard. Verifies the JWT (from the HttpOnly cookie),
|
|
* enforces CSRF on mutations, then requires the user's role to grant ALL of the
|
|
* listed permissions. Authorization is a per-route permission check against the
|
|
* dynamic, admin-composed role grid — no Casbin/RBAC engine needed at this scale.
|
|
*/
|
|
export function requirePermission(...required: Permission[]) {
|
|
return async (req: FastifyRequest, _reply: FastifyReply) => {
|
|
await req.jwtVerify(); // reads the token cookie (configured in server.ts)
|
|
assertCsrf(req);
|
|
if (!req.user || !roleHasPermissions(req.user.roleId, required)) {
|
|
throw Object.assign(new Error("forbidden"), { statusCode: 403 });
|
|
}
|
|
};
|
|
}
|
|
|
|
/**
|
|
* preHandler that requires a valid signed-in session but NO specific permission —
|
|
* for "about me" routes (/me, change own language) every authenticated user may
|
|
* call regardless of role. Still enforces CSRF on mutations.
|
|
*/
|
|
export async function requireAuth(
|
|
req: FastifyRequest,
|
|
_reply: FastifyReply,
|
|
): Promise<void> {
|
|
await req.jwtVerify();
|
|
assertCsrf(req);
|
|
}
|