2026-11-20 18:48:54 +06:00
|
|
|
import { recordAdminAuditLogAsync } from "@/core/auth/auditLog";
|
2026-10-23 19:56:38 -07:00
|
|
|
import { SDKManager } from "@/core/sdk/sdkManager";
|
2026-11-19 23:02:27 +06:00
|
|
|
import {
|
|
|
|
|
validateAllowedIpsInput,
|
|
|
|
|
validateClientExpiresAtInput,
|
|
|
|
|
} from "@/core/Security/ipAllowlist";
|
2026-11-20 18:48:54 +06:00
|
|
|
import { extractClientIp, requireAdminApiSession } from "@/lib/adminAuth";
|
2026-10-23 19:56:38 -07:00
|
|
|
import { NextRequest, NextResponse } from "next/server";
|
2026-10-23 11:43:49 -05:00
|
|
|
|
2026-11-18 05:18:55 +06:00
|
|
|
export async function GET(req: NextRequest) {
|
2026-11-20 18:48:54 +06:00
|
|
|
const auth = await requireAdminApiSession(req, { minRole: "viewer" });
|
2026-11-18 05:18:55 +06:00
|
|
|
if (!auth.valid) {
|
2026-11-20 18:48:54 +06:00
|
|
|
return auth.response;
|
2026-11-18 05:18:55 +06:00
|
|
|
}
|
|
|
|
|
|
2026-10-23 11:43:49 -05:00
|
|
|
try {
|
2026-11-17 20:05:00 +06:00
|
|
|
const allClients = await SDKManager.listClientsAsync();
|
2026-11-18 05:18:55 +06:00
|
|
|
const clients = allClients.map(({ secret, token, ...client }) => {
|
2026-11-17 20:05:00 +06:00
|
|
|
void secret;
|
2026-11-18 05:18:55 +06:00
|
|
|
return {
|
|
|
|
|
...client,
|
|
|
|
|
tokenPreview: token ? `${token.slice(0, 12)}...` : "",
|
|
|
|
|
};
|
2026-11-17 20:05:00 +06:00
|
|
|
});
|
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) {
|
2026-11-20 18:48:54 +06:00
|
|
|
const auth = await requireAdminApiSession(req, { minRole: "operator" });
|
2026-11-18 05:18:55 +06:00
|
|
|
if (!auth.valid) {
|
2026-11-20 18:48:54 +06:00
|
|
|
return auth.response;
|
2026-11-18 05:18:55 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
2026-11-19 23:02:27 +06:00
|
|
|
const {
|
|
|
|
|
clientId,
|
|
|
|
|
name,
|
|
|
|
|
rateLimit,
|
|
|
|
|
storageQuotaMb,
|
|
|
|
|
allowedIps,
|
|
|
|
|
expiresAt,
|
|
|
|
|
} = body;
|
2026-10-23 11:43:49 -05:00
|
|
|
|
2026-11-20 18:48:54 +06:00
|
|
|
if (
|
|
|
|
|
typeof clientId !== "string" ||
|
|
|
|
|
typeof name !== "string" ||
|
|
|
|
|
!clientId.trim() ||
|
|
|
|
|
!name.trim()
|
|
|
|
|
) {
|
2026-10-23 11:43:49 -05:00
|
|
|
return NextResponse.json(
|
2026-11-20 18:48:54 +06:00
|
|
|
{
|
|
|
|
|
error: "clientId and name are required strings",
|
|
|
|
|
code: "VALIDATION_ERROR",
|
|
|
|
|
},
|
2026-11-19 20:22:01 +06:00
|
|
|
{ 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);
|
2026-11-20 18:48:54 +06:00
|
|
|
if (
|
|
|
|
|
!Number.isInteger(parsedRateLimit) ||
|
|
|
|
|
parsedRateLimit < 1 ||
|
|
|
|
|
parsedRateLimit > 100000
|
|
|
|
|
) {
|
2026-11-19 20:22:01 +06:00
|
|
|
return NextResponse.json(
|
2026-11-20 18:48:54 +06:00
|
|
|
{
|
|
|
|
|
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
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-20 18:48:54 +06:00
|
|
|
const parsedQuotaMb =
|
|
|
|
|
storageQuotaMb === undefined ? 0 : Number(storageQuotaMb);
|
|
|
|
|
if (
|
|
|
|
|
!Number.isInteger(parsedQuotaMb) ||
|
|
|
|
|
parsedQuotaMb < 0 ||
|
|
|
|
|
parsedQuotaMb > 1048576
|
|
|
|
|
) {
|
2026-11-19 23:02:27 +06:00
|
|
|
return NextResponse.json(
|
2026-11-20 18:48:54 +06:00
|
|
|
{
|
|
|
|
|
error: "storageQuotaMb must be an integer between 0 and 1048576.",
|
|
|
|
|
code: "VALIDATION_ERROR",
|
|
|
|
|
},
|
2026-11-19 23:02:27 +06:00
|
|
|
{ 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(
|
2026-11-20 18:48:54 +06:00
|
|
|
{
|
|
|
|
|
error: ipValidation.error || "Invalid allowedIps.",
|
|
|
|
|
code: "VALIDATION_ERROR",
|
|
|
|
|
},
|
2026-11-19 23:02:27 +06:00
|
|
|
{ status: 400 },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
normalizedAllowedIps = ipValidation.normalized;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let normalizedExpiresAt: string | null = null;
|
|
|
|
|
if (expiresAt !== undefined) {
|
|
|
|
|
const expValidation = validateClientExpiresAtInput(expiresAt);
|
|
|
|
|
if (!expValidation.valid) {
|
|
|
|
|
return NextResponse.json(
|
2026-11-20 18:48:54 +06:00
|
|
|
{
|
|
|
|
|
error: expValidation.error || "Invalid expiresAt.",
|
|
|
|
|
code: "VALIDATION_ERROR",
|
|
|
|
|
},
|
2026-11-19 23:02:27 +06:00
|
|
|
{ status: 400 },
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-11-20 18:48:54 +06:00
|
|
|
normalizedExpiresAt = expValidation.iso ?? null;
|
2026-11-19 23:02:27 +06:00
|
|
|
}
|
|
|
|
|
|
2026-11-17 20:05:00 +06:00
|
|
|
const existing = await SDKManager.getClientAsync(safeClientId);
|
2026-10-23 11:43:49 -05:00
|
|
|
if (existing) {
|
|
|
|
|
return NextResponse.json(
|
2026-11-17 20:05:00 +06:00
|
|
|
{ error: `Client '${safeClientId}' already exists` },
|
2026-10-23 19:56:38 -07:00
|
|
|
{ status: 409 },
|
2026-10-23 11:43:49 -05:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-17 20:05:00 +06:00
|
|
|
const newClient = await SDKManager.registerClientAsync({
|
|
|
|
|
clientId: safeClientId,
|
2026-11-19 20:22:01 +06:00
|
|
|
name: name.trim().slice(0, 120),
|
|
|
|
|
rateLimit: parsedRateLimit,
|
2026-11-19 23:02:27 +06:00
|
|
|
storageQuotaMb: parsedQuotaMb,
|
|
|
|
|
allowedIps: normalizedAllowedIps,
|
|
|
|
|
expiresAt: normalizedExpiresAt,
|
2026-10-23 11:43:49 -05:00
|
|
|
});
|
|
|
|
|
|
2026-11-20 18:48:54 +06:00
|
|
|
await recordAdminAuditLogAsync({
|
|
|
|
|
actorEmail: auth.auth.session.email,
|
|
|
|
|
actorRole: auth.auth.session.role || "owner",
|
|
|
|
|
action: "CLIENT_CREATED",
|
|
|
|
|
target: newClient.clientId,
|
|
|
|
|
ipAddress: extractClientIp(req),
|
|
|
|
|
metadata: {
|
|
|
|
|
name: newClient.name,
|
|
|
|
|
rateLimit: newClient.rateLimit,
|
|
|
|
|
storageQuotaMb: newClient.storageQuotaMb,
|
|
|
|
|
allowedIps: newClient.allowedIps,
|
|
|
|
|
expiresAt: newClient.expiresAt,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
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 });
|
|
|
|
|
}
|
|
|
|
|
}
|