Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 16x 16x 6x 2x 2x 6x 4x 4x 16x 16x 2x 2x 2x 2x 2x 3x 3x 3x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 8x 8x 8x 8x 5x 8x 5x 5x 3x 3x 8x 3x 3x 3x 3x 8x 3x 3x 3x 3x 8x 8x 3x 3x 3x 3x 3x 3x 3x 8x 8x 1x 1x 1x 1x 1x 1x 8x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x | import basic from '@fastify/basic-auth';
import bearer from '@fastify/bearer-auth';
import cookie from '@fastify/cookie';
import {type Errors, type ILocal} from '@feasibleone/blong/types';
import type {FastifyInstance, FastifyPluginOptions, FastifyReply, FastifyRequest} from 'fastify';
import fp from 'fastify-plugin';
import {LRUCache} from 'lru-cache';
import isPublic from './public.ts';
import {type IGatewayCodec} from './GatewayCodec.ts';
declare module 'fastify' {
interface FastifyRequest {
auth: {
credentials: {
language?: unknown;
mlek?: object | 'header';
mlsk?: object | 'header';
permissionMap?: Buffer;
actorId?: string | number;
sessionId?: string;
/** Audit record id of the access-check audit for this request (set by `recordAccessAudit`). */
auditId?: string;
/** Allowed action methodIds — populated by the authorize handler. */
actions?: string[];
};
};
}
interface FastifyReply {
unstate: (name: string, options?: unknown) => this;
state: (name: string, value: string, options: unknown) => this;
}
interface FastifyContextConfig {
methodName?: string;
audit?: boolean;
skipAuthorize?: boolean;
}
}
/**
* Methods that must never be recorded by the access-check audit — they are
* part of the access-control machinery itself and would recurse.
*/
const AUDIT_HARD_EXCLUDE = ['access.audit.record', 'access.authorization.list'];
/** Entity tables whose DML carries a sanitised detail summary in the audit. */
const AUDIT_DML_ENTITIES = [
'user',
'role',
'capability',
'action',
'credential',
'access',
'policy',
'flow',
];
const AUDIT_WRITE_OPS = ['add', 'edit', 'remove', 'insert', 'update', 'delete', 'merge'];
/**
* Sanitised DML context for `access.*` write methods — the entity plus its
* id/name keys only. Full params (credentials, hashes, descriptions) are
* deliberately never recorded.
*/
function auditDetail(methodName: string, params: unknown): object | undefined {
const [subject, object, operation] = String(methodName).split('.');
if (
subject !== 'access' ||
!AUDIT_DML_ENTITIES.includes(object) ||
!AUDIT_WRITE_OPS.includes(operation)
) {
return undefined;
}
const p = (params && typeof params === 'object' ? params : {}) as Record<string, unknown>;
const summary: Record<string, unknown> = {entity: object};
for (const [key, value] of Object.entries(p)) {
if (
/Id$|Name$|Key$/.test(key) &&
(typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean')
) {
summary[key] = value;
}
}
return summary;
}
/**
* Best-effort recording of an access-control decision at the gateway access
* check. Resolves the configured audit handler (like `authorize`) and hands
* it a single audit entry with the actor/session/method/outcome plus sanitised
* DML context. The inserted record key is exposed on `request.auth.credentials.
* auditId` (→ `$meta.auth.auditId`). Callers await it but must never let an
* audit failure fail the request.
*/
async function recordAccessAudit(
request: FastifyRequest,
audit: {handler: string; exclude?: string[]},
methodName: string,
allowed: boolean,
statusCode: number,
local: ILocal,
methodIdFn: (name: string) => string,
methodPartsFn: (name: string) => string,
): Promise<void> {
// Route-level opt-out (e.g. operations that audit themselves).
if (request.routeOptions.config.audit === false) return;
const methodIdName = methodIdFn(methodName);
if (AUDIT_HARD_EXCLUDE.includes(methodIdName)) return;
const excluded = (audit.exclude ?? []).some(pattern => {
if (pattern.endsWith('*')) return methodIdName.startsWith(pattern.slice(0, -1));
return methodIdName === methodIdFn(pattern);
});
if (excluded) return;
const handlerName = methodPartsFn(audit.handler);
const reqName = `ports.${handlerName.split('.', 1)[0]}.request`;
const handler = local.get(reqName);
if (!handler) return;
const credentials = (request.auth?.credentials ?? {}) as {
actorId?: string;
sessionId?: string;
};
const forwarded = (request.headers['x-forwarded-for'] ?? '') as string | string[];
const ipAddress =
([] as string[]).concat(forwarded)[0]?.split(',')[0] || request.socket.remoteAddress || '';
const params = (request.body as {params?: unknown} | undefined)?.params;
const detail = auditDetail(methodName, params);
const result = (await handler.method(
{
audit: [
{
actorId: credentials.actorId,
sessionId: credentials.sessionId,
actionName: methodName,
isSuccess: allowed,
statusCode,
ipAddress,
...(detail && {detail}),
},
],
},
{
method: handlerName,
mtid: 'request',
auth: credentials,
ipAddress,
httpRequest: {url: request.raw?.url || request.url},
},
)) as {auditIds?: string[]} | undefined;
// Expose the inserted audit record key on the request auth — the gateway's
// `_meta()` spreads `req.auth.credentials` into the handler `$meta.auth`, so
// the audited handler sees it as `$meta.auth.auditId`.
const auditId = result?.auditIds?.[0];
if (auditId && request.auth) {
request.auth.credentials = {...request.auth.credentials, auditId};
}
}
export default fp<{
cache: object | false;
audience: string;
verify: IGatewayCodec['verify'];
errors: Errors<object>;
authorize?: string;
audit?: {handler: string; exclude?: string[]};
local?: ILocal;
methodId?: (name: string) => string;
methodParts?: (name: string) => string;
}>(
async function jwtPlugin(
fastify: FastifyInstance,
{
cache: cacheConfig,
audience,
verify,
errors,
authorize,
audit,
local,
methodId,
methodParts,
}: FastifyPluginOptions,
) {
const cache =
![0, false, 'false'].includes(cacheConfig as string | number | boolean) &&
new LRUCache({max: 1000, ...cacheConfig});
await fastify.register(basic, {
async validate(_username: string, _password: string, _req: unknown, _reply: unknown) {},
});
fastify.addHook(
'preValidation',
function (request: FastifyRequest, reply: FastifyReply, done: (err?: Error) => void) {
const auth = request.routeOptions.config.auth;
if (auth !== false && !isPublic(request.originalUrl)) {
if (auth === 'login') {
request.auth = {credentials: {mlek: 'header', mlsk: 'header'}};
done();
} else {
return this?.verifyBearerAuth?.(request, reply, done);
}
} else done();
},
);
await fastify.register(bearer, {
keys: new Set([]),
addHook: false,
auth: async (token, req) => {
if (!token) throw errors['gateway.jwtMissingHeader']();
const cachedCredentials = cache && cache.get(token);
if (cachedCredentials) {
req.auth = {credentials: cachedCredentials};
return true;
}
const decoded = await verify(token, {audience});
const {
// standard
exp,
aud, // eslint-disable-line @typescript-eslint/no-unused-vars
iss, // eslint-disable-line @typescript-eslint/no-unused-vars
iat, // eslint-disable-line @typescript-eslint/no-unused-vars
jti, // eslint-disable-line @typescript-eslint/no-unused-vars
nbf, // eslint-disable-line @typescript-eslint/no-unused-vars
sub: actorId,
// headers
typ, // eslint-disable-line @typescript-eslint/no-unused-vars
cty, // eslint-disable-line @typescript-eslint/no-unused-vars
alg, // eslint-disable-line @typescript-eslint/no-unused-vars
// custom
sig: mlsk,
enc: mlek,
ses: sessionId,
per = '',
// arbitrary
...rest
} = decoded;
const permissionMap = Buffer.from(per, 'base64');
const credentials: Record<string, unknown> = {
mlek,
mlsk,
permissionMap,
actorId,
sessionId,
...rest,
};
// Call the authorize handler to resolve the list of allowed actions
if (authorize && local && methodId && methodParts) {
const handlerName = methodParts(authorize);
const reqName = `ports.${handlerName.split('.', 1)[0]}.request`;
const handler = local.get(reqName);
if (handler) {
const result = await handler.method(
{permissionMap},
{
method: handlerName,
mtid: 'request',
},
);
credentials.actions = Array.isArray(result) ? result[0] : result;
}
}
if (cache) cache.set(token, credentials, {ttl: exp * 1000 - Date.now()});
req.auth = {credentials: credentials as FastifyRequest['auth']['credentials']};
return true;
},
});
// Authorization hook: check the called method against allowed actions
if (authorize && local && methodId) {
fastify.addHook('preHandler', async function (request, _reply) {
// Routes with auth: false or auth: 'login' don't go through bearer auth,
// so credentials have no actions. Skip the authorization check.
if (
!request.routeOptions.config.auth ||
request.routeOptions.config.auth === 'login'
) {
return;
}
// Self-service methods (e.g. logout) are bearer-authenticated but
// operate only on the caller's own session — no RBAC action needed.
if (request.routeOptions.config.skipAuthorize) {
return;
}
// The authorize handler itself must be accessible without authorization,
// otherwise we have a chicken-and-egg problem — you'd need authorization
// to call the handler that resolves authorization.
const methodName = request.routeOptions.config.methodName;
if (authorize && methodName && methodParts) {
const normalizedMethod = methodParts(authorize);
if (methodName === normalizedMethod) {
return;
}
}
const credentials = request.auth?.credentials;
if (!credentials?.actions) {
throw new Error('Authorization denied: no actions resolved');
}
if (!methodName) {
return; // no method configured — allow (backward compat)
}
const requestedId = methodId(methodName);
const allowed = credentials.actions.includes(requestedId);
// Record the access decision along with the access check — applies to
// every operation controlled through it. Best-effort (an audit failure
// must never fail the request) but awaited so the inserted `auditId`
// lands on `request.auth.credentials.auditId` (→ `$meta.auth.auditId`)
// before the business handler runs.
if (audit && methodName && local && methodId && methodParts) {
try {
await recordAccessAudit(
request,
audit,
methodName,
allowed,
allowed ? 200 : 403,
local,
methodId,
methodParts,
);
} catch {
// ignore — audit is best-effort
}
}
if (!allowed) {
const error = new Error(
`Authorization denied: method "${methodName}" not allowed`,
) as Error & {statusCode: number};
error.statusCode = 403;
throw error;
}
});
}
await fastify.register(cookie, {});
fastify.decorateRequest('auth');
fastify.decorateReply('unstate', function (name: string, options?: unknown) {
return this.clearCookie(name, options as {path?: string});
});
(fastify.decorateReply as (name: string, fn: unknown) => void)(
'state',
function (this: FastifyReply, name: string, value: string, options: unknown) {
const {
ttl: maxAge,
isSecure: secure,
isHttpOnly: httpOnly,
isSameSite: sameSite,
path,
domain,
} = options as {
ttl?: number;
isSecure?: boolean;
isHttpOnly?: boolean;
isSameSite?: boolean;
path?: string;
domain?: string;
};
return this.setCookie(
name,
value,
Object.fromEntries(
Object.entries({
maxAge: maxAge && Math.floor(maxAge / 1000),
secure,
httpOnly,
sameSite,
path,
domain,
}).filter(([, value]) => value != null),
),
);
},
);
},
{
fastify: '5.x',
name: 'blong-jwt',
},
);
|