feat(16-03): implement DELETE /api/api-keys/[id] ownership-scoped revoke

- deleteMany where { id, userId } — atomic BOLA prevention (T-16-06)
- count 0 -> 404 not_found; count 1 -> 200 success: true
- isDemo gate (403) before session check
- All 4 [id]/route.test.ts tests GREEN (KEY-03)
- Full suite 403/403 tests green, no regressions
This commit is contained in:
Antigravity AI committed 2026-05-29 21:52:33 +12:00
1 parent 476befe4b4
commit 4e6ff0704f
1 file changed
+40
+40
View File
@@ -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 });
}
}