Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
62 lines
2.5 KiB
TypeScript
62 lines
2.5 KiB
TypeScript
import "server-only";
|
|
import { callMobileApi } from "@/app/lib/server/mobileApi";
|
|
|
|
// enforce-session-timeouts: the idle limit and cap saved on Login Security ("Session policy", admin group), read from
|
|
// mobile-api with the signed-in admin's token. One value for the whole portal, so one cache per process; a change on
|
|
// the Login Security tab reaches open sessions within CACHE_MS. The env values are only the fallback.
|
|
|
|
export interface SessionPolicy {
|
|
idleTtlSeconds: number;
|
|
absoluteTtlSeconds: number;
|
|
}
|
|
|
|
const CACHE_MS = 60_000;
|
|
let cached: { policy: SessionPolicy; at: number } | null = null;
|
|
|
|
function envSeconds(name: string, fallback: number): number {
|
|
const raw = process.env[name];
|
|
const parsed = raw ? Number(raw) : NaN;
|
|
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
|
|
}
|
|
|
|
/** Idle limit when the saved policy cannot be read (env SESSION_IDLE_TTL, default 3600 = the JWT validity). */
|
|
export function sessionIdleTtlSeconds(): number {
|
|
return envSeconds("SESSION_IDLE_TTL", 3600);
|
|
}
|
|
|
|
/** Cap when the saved policy cannot be read (env SESSION_ABSOLUTE_TTL, default 28800 = 8 h). */
|
|
export function sessionAbsoluteTtlSeconds(): number {
|
|
return envSeconds("SESSION_ABSOLUTE_TTL", 28800);
|
|
}
|
|
|
|
function fallbackPolicy(): SessionPolicy {
|
|
return { idleTtlSeconds: sessionIdleTtlSeconds(), absoluteTtlSeconds: sessionAbsoluteTtlSeconds() };
|
|
}
|
|
|
|
const positive = (v: unknown): v is number => typeof v === "number" && Number.isFinite(v) && v > 0;
|
|
|
|
/** The saved policy, cached for 60 seconds; the env values when mobile-api cannot answer. */
|
|
export async function getSessionPolicy(token: string): Promise<SessionPolicy> {
|
|
if (cached && Date.now() - cached.at < CACHE_MS) return cached.policy;
|
|
const result = await callMobileApi<{ idleTtlSeconds?: unknown; absoluteTtlSeconds?: unknown }>(
|
|
"/api/admin/auth/session-policy",
|
|
{ token },
|
|
);
|
|
if (!result.ok || !positive(result.data?.idleTtlSeconds) || !positive(result.data?.absoluteTtlSeconds)) {
|
|
return cached?.policy ?? fallbackPolicy();
|
|
}
|
|
const policy = { idleTtlSeconds: result.data.idleTtlSeconds, absoluteTtlSeconds: result.data.absoluteTtlSeconds };
|
|
cached = { policy, at: Date.now() };
|
|
return policy;
|
|
}
|
|
|
|
/** The last policy read (even if older than 60 s), else the env values. Never calls out: for the sign-in path. */
|
|
export function cachedSessionPolicy(): SessionPolicy {
|
|
return cached?.policy ?? fallbackPolicy();
|
|
}
|
|
|
|
/** Tests only. */
|
|
export function resetSessionPolicyCache(): void {
|
|
cached = null;
|
|
}
|