Files
co-lite/solvway-admin-portal/app/lib/services/loginSecurity.ts
T

81 lines
2.7 KiB
TypeScript

import { apiGet, apiPost, apiPut } from "@/app/lib/clientApi";
export const LOGIN_EVENTS = ["LOGIN_SUCCESS", "LOGIN_FAIL", "LOGIN_LOCKED", "LOGOUT", "UNLOCK_RESET", "PASSWORD_CHANGED"] as const;
export const LOCKOUT_UNITS = ["S", "M", "H", "D"] as const;
export interface AuditRow {
id: number;
username: string;
/** Server-local "yyyy-MM-ddTHH:mm:ss". */
datetime: string;
event: string;
ipAddress: string | null;
userAgent: string | null;
message: string | null;
}
export interface AuditParams {
username?: string;
event?: string;
/** "yyyy-MM-dd". */
from?: string;
to?: string;
/** 1-based. */
page: number;
pageSize: number;
}
export interface LockedAccount {
username: string;
failures: number;
lastFailure: string | null;
lockedUntil: string | null;
}
export interface LockoutConfig {
enabled: boolean;
maxAttempts: number;
period: number;
unit: (typeof LOCKOUT_UNITS)[number];
}
export interface PasswordRule {
id: number;
param: string;
status: "Y" | "N";
value: string | null;
}
export type PasswordRuleUpdate = Pick<PasswordRule, "id" | "status" | "value">;
const BASE = "/api/login-security";
export async function listAudit(params: AuditParams): Promise<{ items: AuditRow[]; total: number }> {
const qs = new URLSearchParams();
for (const key of ["username", "event", "from", "to"] as const) if (params[key]) qs.set(key, params[key]);
qs.set("page", String(Math.max(0, params.page - 1)));
qs.set("size", String(params.pageSize));
const raw = await apiGet<{ content?: AuditRow[]; totalElements?: number } | null>(`${BASE}/audit?${qs}`);
const items = Array.isArray(raw?.content) ? raw.content : [];
return { items, total: typeof raw?.totalElements === "number" ? raw.totalElements : items.length };
}
export const listLocked = () => apiGet<LockedAccount[]>(`${BASE}/locked`);
export const unlock = (username: string) => apiPost<void>(`${BASE}/locked/${encodeURIComponent(username)}/unlock`);
export const getConfig = () => apiGet<LockoutConfig>(`${BASE}/config`);
export const saveConfig = (cfg: LockoutConfig) => apiPut<LockoutConfig>(`${BASE}/config`, cfg);
export const getPasswordRules = () => apiGet<PasswordRule[]>(`${BASE}/password-rules`);
export const savePasswordRules = (rules: PasswordRuleUpdate[]) => apiPut<PasswordRule[]>(`${BASE}/password-rules`, rules);
export const LOGIN_EVENT_KEYS: Record<string, string> = {
LOGIN_SUCCESS: "LsEventLoginSuccess",
LOGIN_FAIL: "LsEventLoginFail",
LOGIN_LOCKED: "LsEventLoginLocked",
LOGOUT: "LsEventLogout",
UNLOCK_RESET: "LsEventUnlockReset",
PASSWORD_CHANGED: "LsEventPasswordChanged",
};
/** Params with no value to edit: the rule is only on or off. */
export const FLAG_RULE_PARAMS = ["NO_OTHER_LANGUAGE", "SPACE_NOT_ALLOWED"];