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.
This commit is contained in:
1 parent
d4bb274982
commit
de41571b5c
33 files changed
+1083
-20
No files matched your search
@@ -1,5 +1,7 @@
|
||||
import "reflect-metadata";
|
||||
import { plainToInstance } from "class-transformer";
|
||||
import {
|
||||
IsBoolean,
|
||||
IsEnum,
|
||||
IsInt,
|
||||
IsString,
|
||||
@@ -10,6 +12,17 @@ import {
|
||||
|
||||
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",
|
||||
@@ -74,15 +87,62 @@ export class AppConfig {
|
||||
@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 validated = plainToInstance(AppConfig, config, {
|
||||
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;
|
||||
}
|
||||
Reference in new issue
Block a user