Files
liqa/server/src/config/app.config.ts
T
agent de41571b5c feat(auth): add optional authentication boundary
- Add account, JWT session, revocation, cookie and bearer handling.
- Protect server surfaces while preserving public health, manifest and disabled mode.
- Add SQLite migration and focused server regression coverage.
2026-11-04 15:43:33 -05:00

149 lines
3.7 KiB
TypeScript

import "reflect-metadata";
import { plainToInstance } from "class-transformer";
import {
IsBoolean,
IsEnum,
IsInt,
IsString,
Max,
Min,
validateSync,
} from "class-validator";
import { appPackage } from "../common/app-package";
export function authDurationSeconds(value: string): number | null {
const match = /^(\d+)(s|m|h|d)$/.exec(value);
if (!match) return null;
const amount = Number(match[1]);
const multiplier = { s: 1, m: 60, h: 3600, d: 86400 }[
match[2] as "s" | "m" | "h" | "d"
];
const seconds = amount * multiplier;
return Number.isSafeInteger(seconds) && seconds > 0 ? seconds : null;
}
export enum TraceMode {
On = "on",
RetainOnFailure = "retain-on-failure",
Off = "off",
}
export class AppConfig {
/** `production` in the Docker image; also selects the default CORS origins. */
@IsString()
NODE_ENV: string = "development";
/** Comma-separated origins allowed to call the API cross-origin; empty = default for NODE_ENV. */
@IsString()
CORS_ORIGINS: string = "";
@IsInt()
@Min(1)
@Max(65535)
PORT: number = 3000;
@IsString()
APP_NAME: string = appPackage().name;
@IsString()
APP_VERSION: string = appPackage().version;
@IsString()
DB_PATH: string = "data/liqa.db";
@IsInt()
@Min(1)
SESSION_IDLE_TIMEOUT_MINUTES: number = 30;
@IsInt()
@Min(1)
SESSION_DELETE_CLOSED_DAYS: number = 7;
@IsString()
FILES_DIR: string = "data/files";
@IsInt()
@Min(1)
FILE_RUN_EXPIRATION_DAYS: number = 7;
@IsString()
FILE_CLEANUP_CRON: string = "0 3 * * *";
/** Directory with the built client to serve at `/`; empty to not serve it. */
@IsString()
CLIENT_DIR: string = "";
@IsEnum(TraceMode)
// Off by default: traces with screenshots and snapshots easily reach ~10 MB per run
PLAYWRIGHT_TRACE_MODE: TraceMode = TraceMode.Off;
/** Run log data blocks over this many bytes (pretty-printed) start collapsed in the UI. */
@IsInt()
@Min(0)
RUN_LOG_DATA_COLLAPSE_BYTES: number = 256;
/** Run log data blocks over this many lines (pretty-printed) start collapsed in the UI. */
@IsInt()
@Min(0)
RUN_LOG_DATA_COLLAPSE_LINES: number = 5;
/** Authentication is opt-in so existing deployments remain unchanged. */
@IsBoolean()
AUTH_ENABLED: boolean = false;
/** Required only when AUTH_ENABLED is true. */
@IsString()
AUTH_JWT_SECRET: string = "";
@IsString()
AUTH_JWT_EXPIRES_IN: string = "24h";
@IsString()
AUTH_REVOKED_TOKEN_CLEANUP_CRON: string = "0 3 * * *";
}
export function validateAppConfig(config: Record<string, unknown>): AppConfig {
const authEnabled = config.AUTH_ENABLED;
if (
authEnabled !== undefined &&
authEnabled !== true &&
authEnabled !== false &&
authEnabled !== "true" &&
authEnabled !== "false"
) {
throw new Error("AUTH_ENABLED must be either true or false.");
}
const normalized = {
...config,
AUTH_ENABLED: authEnabled === true || authEnabled === "true",
};
const validated = plainToInstance(AppConfig, normalized, {
enableImplicitConversion: true,
});
const errors = validateSync(validated, { skipMissingProperties: false });
if (errors.length > 0) {
throw new Error(errors.toString());
}
if (validated.AUTH_ENABLED && !validated.AUTH_JWT_SECRET.trim()) {
throw new Error("AUTH_JWT_SECRET must be set when AUTH_ENABLED=true.");
}
if (
validated.AUTH_ENABLED &&
authDurationSeconds(validated.AUTH_JWT_EXPIRES_IN) === null
) {
throw new Error(
"AUTH_JWT_EXPIRES_IN must be a positive duration such as 30m, 1h, or 24h.",
);
}
if (
validated.AUTH_ENABLED &&
!validated.AUTH_REVOKED_TOKEN_CLEANUP_CRON.trim()
) {
throw new Error(
"AUTH_REVOKED_TOKEN_CLEANUP_CRON must be set when AUTH_ENABLED=true.",
);
}
return validated;
}