diff --git a/src/app/api/api-keys/[id]/route.ts b/src/app/api/api-keys/[id]/route.ts new file mode 100644 index 00000000..e14b5c24 --- /dev/null +++ b/src/app/api/api-keys/[id]/route.ts @@ -0,0 +1,40 @@ +import { NextResponse } from "next/server"; +import { auth } from "@/lib/auth"; +import { prisma } from "@/lib/db"; +import { isDemo } from "@/core/edition"; + +// --------------------------------------------------------------------------- +// DELETE /api/api-keys/[id] — KEY-03 (ownership-scoped revoke, hard delete) +// --------------------------------------------------------------------------- + +export async function DELETE( + _request: Request, + { params }: { params: { id: string } }, +) { + if (isDemo) { + return NextResponse.json({ error: "Not available in demo edition" }, { status: 403 }); + } + + const session = await auth(); + if (!session?.user?.id) { + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + } + + try { + // Atomic ownership-scoped delete — T-16-06 (BOLA prevention). + // Using deleteMany (not delete) so a non-matching id returns count 0 + // instead of throwing a NotFound error, eliminating TOCTOU. + const deleted = await prisma.userApiKey.deleteMany({ + where: { id: params.id, userId: session.user.id }, + }); + + if (deleted.count === 0) { + return NextResponse.json({ error: "not_found" }, { status: 404 }); + } + + return NextResponse.json({ success: true }); + } catch (err) { + console.error("[api-keys] DELETE error:", err); + return NextResponse.json({ error: "Failed to revoke API key" }, { status: 500 }); + } +}