refactor(auth): standardize session management and security

- Centralize secret retrieval using `getAdminSessionSecret`
- Replace manual session parsing with `verifyRequestAdminSession` utility
- Tighten API validation and improve security headers handling
- Enforce environment variable security for production deployments
This commit is contained in:
Team Dark Devil committed 2026-11-18 05:18:55 +06:00
1 parent 34519c07df
commit 177ed4a844
78 files changed
+6748 -3157

No files matched your search

+46 -4
View File
@@ -1,23 +1,54 @@
import { verifyRequestAdminSession } from "@/core/auth/adminUsers";
import { SDKManager } from "@/core/sdk/sdkManager";
import { NextRequest, NextResponse } from "next/server";
const SESSION_COOKIE = "datastack_admin_session";
export async function GET(
_req: NextRequest,
req: NextRequest,
{ params }: { params: Promise<{ id: string }> },
) {
const auth = await verifyRequestAdminSession(
req.cookies.get(SESSION_COOKIE)?.value,
);
if (!auth.valid) {
return NextResponse.json(
{ error: "Authentication required." },
{ status: auth.reason === "UNAUTHORIZED" ? 401 : 503 },
);
}
const { id } = await params;
const client = await SDKManager.getClientAsync(id);
if (!client) {
return NextResponse.json({ error: "Client not found" }, { status: 404 });
}
const { secret, token, ...clientSummary } = client;
return NextResponse.json({ client: clientSummary });
void secret;
const includeToken = req.nextUrl.searchParams.get("includeToken") === "true";
return NextResponse.json({
client: {
...clientSummary,
tokenPreview: token ? `${token.slice(0, 12)}...` : "",
...(includeToken ? { token } : {}),
},
});
}
export async function PATCH(
req: NextRequest,
{ params }: { params: Promise<{ id: string }> },
) {
const auth = await verifyRequestAdminSession(
req.cookies.get(SESSION_COOKIE)?.value,
);
if (!auth.valid) {
return NextResponse.json(
{ error: "Authentication required." },
{ status: auth.reason === "UNAUTHORIZED" ? 401 : 503 },
);
}
const { id } = await params;
try {
const body = await req.json();
@@ -46,11 +77,22 @@ export async function PATCH(
}
export async function DELETE(
_req: NextRequest,
req: NextRequest,
{ params }: { params: Promise<{ id: string }> },
) {
const auth = await verifyRequestAdminSession(
req.cookies.get(SESSION_COOKIE)?.value,
);
if (!auth.valid) {
return NextResponse.json(
{ error: "Authentication required." },
{ status: auth.reason === "UNAUTHORIZED" ? 401 : 503 },
);
}
const { id } = await params;
const deleted = await SDKManager.deleteClientAsync(id);
const purgeFiles = req.nextUrl.searchParams.get("purgeFiles") === "true";
const deleted = await SDKManager.deleteClientAsync(id, { purgeFiles });
if (!deleted) {
return NextResponse.json({ error: "Client not found" }, { status: 404 });
}
+43 -4
View File
@@ -1,12 +1,28 @@
import { verifyRequestAdminSession } from "@/core/auth/adminUsers";
import { SDKManager } from "@/core/sdk/sdkManager";
import { NextRequest, NextResponse } from "next/server";
export async function GET() {
const SESSION_COOKIE = "datastack_admin_session";
export async function GET(req: NextRequest) {
const auth = await verifyRequestAdminSession(
req.cookies.get(SESSION_COOKIE)?.value,
);
if (!auth.valid) {
return NextResponse.json(
{ error: "Authentication required." },
{ status: auth.reason === "UNAUTHORIZED" ? 401 : 503 },
);
}
try {
const allClients = await SDKManager.listClientsAsync();
const clients = allClients.map(({ secret, ...client }) => {
const clients = allClients.map(({ secret, token, ...client }) => {
void secret;
return client;
return {
...client,
tokenPreview: token ? `${token.slice(0, 12)}...` : "",
};
});
return NextResponse.json({ clients, count: clients.length });
} catch (err: unknown) {
@@ -17,8 +33,31 @@ export async function GET() {
}
export async function POST(req: NextRequest) {
const auth = await verifyRequestAdminSession(
req.cookies.get(SESSION_COOKIE)?.value,
);
if (!auth.valid) {
return NextResponse.json(
{ error: "Authentication required." },
{ status: auth.reason === "UNAUTHORIZED" ? 401 : 503 },
);
}
let body: Record<string, unknown>;
try {
const parsed = await req.json();
body =
parsed && typeof parsed === "object" && !Array.isArray(parsed)
? (parsed as Record<string, unknown>)
: {};
} catch {
return NextResponse.json(
{ error: "Invalid request payload." },
{ status: 400 },
);
}
try {
const body = await req.json();
const { clientId, name, rateLimit } = body;
if (!clientId || !name) {