feat(auth): enhance security and audit logging

- Integrate webhook alerts for admin login lockouts.
- Implement detailed audit logging for login, logout, and setup actions.
- Improve client revocation event tracking.
- Standardize error handling in profile updates.
- Enforce 'operator' role for event API access.
This commit is contained in:
Team Dark Devil committed 2026-11-21 00:06:55 +06:00
1 parent 5c977d6977
commit 8132087085
52 files changed
+4434 -407

No files matched your search

+12
View File
@@ -10,6 +10,7 @@ import {
clearAdminLoginAttempts,
consumeAdminLoginAttempt,
} from "@/core/auth/adminUsers";
import { SettingsManager } from "@/core/adminSettings/settingsManager";
import { recordAdminAuditLogAsync } from "@/core/auth/auditLog";
import { extractClientIp } from "@/lib/adminAuth";
import { NextRequest, NextResponse } from "next/server";
@@ -70,6 +71,17 @@ export async function POST(request: NextRequest) {
);
}
if (!throttleDecision.allowed) {
SettingsManager.triggerWebhookAlert({
event: "ADMIN_LOGIN_LOCKOUT",
severity: "warning",
title: "Admin Login Lockout Triggered",
message: `Admin sign-in throttled for ${email} from IP ${clientIp} (${throttleDecision.retryAfterSeconds}s lockout).`,
details: {
email,
clientIp,
retryAfterSeconds: throttleDecision.retryAfterSeconds,
},
});
return NextResponse.json(
{ error: "Too many sign-in attempts. Try again later." },
{
+16 -1
View File
@@ -1,8 +1,10 @@
import { getSessionCookieOptions } from "@/core/auth/adminSession";
import { recordAdminAuditLogAsync } from "@/core/auth/auditLog";
import {
revokeAdminSessions,
verifyRequestAdminSession,
} from "@/core/auth/adminUsers";
import { extractClientIp } from "@/lib/adminAuth";
import { NextRequest, NextResponse } from "next/server";
const SESSION_COOKIE = "datastack_admin_session";
@@ -13,7 +15,20 @@ export async function POST(request: NextRequest) {
try {
const check = await verifyRequestAdminSession(token);
if (check.valid) {
await revokeAdminSessions();
await revokeAdminSessions({
memberId: check.session.memberId,
email: check.session.email,
});
await recordAdminAuditLogAsync({
actorEmail: check.session.email,
actorRole: check.session.role || "owner",
action: "ADMIN_LOGOUT",
target: check.session.email,
ipAddress: extractClientIp(request),
metadata: {
memberId: check.session.memberId || "owner-1",
},
});
}
} catch {
// Still clear client cookie even if server revocation encounters transient DB error
+6 -2
View File
@@ -125,9 +125,13 @@ export async function PUT(request: NextRequest) {
});
if (!result.ok) {
const isConflict = result.code === "ADMIN_EMAIL_CONFLICT";
return NextResponse.json(
{ error: result.error, code: "INVALID_PASSWORD" },
{ status: 401 },
{
error: result.error,
code: result.code || "INVALID_PASSWORD",
},
{ status: isConflict ? 409 : 401 },
);
}
+12
View File
@@ -3,6 +3,7 @@ import {
getAdminSessionSecret,
getSessionCookieOptions,
} from "@/core/auth/adminSession";
import { recordAdminAuditLogAsync } from "@/core/auth/auditLog";
import {
AdminDatabaseRequiredError,
clearAdminLoginAttempts,
@@ -128,6 +129,17 @@ export async function POST(request: NextRequest) {
clientIp,
).catch(() => undefined);
await recordAdminAuditLogAsync({
actorEmail: email,
actorRole: "owner",
action: "ADMIN_SETUP",
target: email,
ipAddress: clientIp,
metadata: {
memberId: "owner-1",
},
});
const token = await createAdminSessionToken(
email,
sessionSecret,