Files
DataStack/app/api/clients/route.ts
T

158 lines
4.9 KiB
TypeScript
Raw Normal View History

import { verifyRequestAdminSession } from "@/core/auth/adminUsers";
2026-10-23 19:56:38 -07:00
import { SDKManager } from "@/core/sdk/sdkManager";
import {
validateAllowedIpsInput,
validateClientExpiresAtInput,
} from "@/core/Security/ipAllowlist";
2026-10-23 19:56:38 -07:00
import { NextRequest, NextResponse } from "next/server";
2026-10-23 11:43:49 -05:00
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 },
);
}
2026-10-23 11:43:49 -05:00
try {
const allClients = await SDKManager.listClientsAsync();
const clients = allClients.map(({ secret, token, ...client }) => {
void secret;
return {
...client,
tokenPreview: token ? `${token.slice(0, 12)}...` : "",
};
});
2026-10-23 11:43:49 -05:00
return NextResponse.json({ clients, count: clients.length });
} catch (err: unknown) {
2026-10-23 19:56:38 -07:00
const message =
err instanceof Error ? err.message : "Failed to list clients";
2026-10-23 11:43:49 -05:00
return NextResponse.json({ error: message }, { status: 500 });
}
}
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 },
);
}
2026-10-23 11:43:49 -05:00
try {
const {
clientId,
name,
rateLimit,
storageQuotaMb,
allowedIps,
expiresAt,
} = body;
2026-10-23 11:43:49 -05:00
if (typeof clientId !== "string" || typeof name !== "string" || !clientId.trim() || !name.trim()) {
2026-10-23 11:43:49 -05:00
return NextResponse.json(
{ error: "clientId and name are required strings", code: "VALIDATION_ERROR" },
{ status: 400 },
);
}
const safeClientId = SDKManager.normalizeClientId(clientId);
if (!safeClientId) {
return NextResponse.json(
{ error: "clientId is invalid", code: "VALIDATION_ERROR" },
{ status: 400 },
);
}
const parsedRateLimit = rateLimit === undefined ? 100 : Number(rateLimit);
if (!Number.isInteger(parsedRateLimit) || parsedRateLimit < 1 || parsedRateLimit > 100000) {
return NextResponse.json(
{ error: "rateLimit must be an integer between 1 and 100000.", code: "VALIDATION_ERROR" },
2026-10-23 19:56:38 -07:00
{ status: 400 },
2026-10-23 11:43:49 -05:00
);
}
const parsedQuotaMb = storageQuotaMb === undefined ? 0 : Number(storageQuotaMb);
if (!Number.isInteger(parsedQuotaMb) || parsedQuotaMb < 0 || parsedQuotaMb > 1048576) {
return NextResponse.json(
{ error: "storageQuotaMb must be an integer between 0 and 1048576.", code: "VALIDATION_ERROR" },
{ status: 400 },
);
}
let normalizedAllowedIps = "";
if (allowedIps !== undefined && allowedIps !== null) {
if (typeof allowedIps !== "string") {
return NextResponse.json(
{ error: "allowedIps must be a string.", code: "VALIDATION_ERROR" },
{ status: 400 },
);
}
const ipValidation = validateAllowedIpsInput(allowedIps);
if (!ipValidation.valid) {
return NextResponse.json(
{ error: ipValidation.error || "Invalid allowedIps.", code: "VALIDATION_ERROR" },
{ status: 400 },
);
}
normalizedAllowedIps = ipValidation.normalized;
}
let normalizedExpiresAt: string | null = null;
if (expiresAt !== undefined) {
const expValidation = validateClientExpiresAtInput(expiresAt);
if (!expValidation.valid) {
return NextResponse.json(
{ error: expValidation.error || "Invalid expiresAt.", code: "VALIDATION_ERROR" },
{ status: 400 },
);
}
normalizedExpiresAt = expValidation.normalized;
}
const existing = await SDKManager.getClientAsync(safeClientId);
2026-10-23 11:43:49 -05:00
if (existing) {
return NextResponse.json(
{ error: `Client '${safeClientId}' already exists` },
2026-10-23 19:56:38 -07:00
{ status: 409 },
2026-10-23 11:43:49 -05:00
);
}
const newClient = await SDKManager.registerClientAsync({
clientId: safeClientId,
name: name.trim().slice(0, 120),
rateLimit: parsedRateLimit,
storageQuotaMb: parsedQuotaMb,
allowedIps: normalizedAllowedIps,
expiresAt: normalizedExpiresAt,
2026-10-23 11:43:49 -05:00
});
return NextResponse.json({ client: newClient }, { status: 201 });
} catch (err: unknown) {
2026-10-23 19:56:38 -07:00
const message =
err instanceof Error ? err.message : "Client creation failed";
2026-10-23 11:43:49 -05:00
return NextResponse.json({ error: message }, { status: 500 });
}
}