feat: developer access permission gates webhooks/API keys/BYOK (v0.71.0)
Webhooks, self-serve API keys, and BYOK AI provider keys had zero access gating -- any logged-in user, any tier. Adds users.isDeveloper (boolean, admin-toggled in admin/users/[id] alongside role/tier), checked via a single hasDeveloperAccess() (lib/permissions.ts) so a future subscription-tier auto-grant is a one-line change there, not a redesign across call sites. requireDeveloper() (lib/api-auth.ts) wraps requireSession() with a fresh isDeveloper check (same reasoning as requireAdmin re-querying role: session.user's cookieCache can be up to 5 minutes stale) and replaces requireSession in all 8 gated routes: webhooks CRUD + deliveries + redeliver, api-keys CRUD, ai-keys CRUD. Settings UI: the sidebar hides API Keys/Webhooks nav entries for non-developers; those pages and the BYOK section of Settings -> AI show a locked notice instead of the manager component when accessed directly. Migration grandfathers in anyone who already has a webhook, API key, or BYOK key row -- ships as a new gate on existing features, not a silent lockout of active integrations. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -4,7 +4,7 @@ import { NextRequest } from "next/server";
|
||||
const mockSession = { user: { id: "user-1" } };
|
||||
|
||||
vi.mock("@/lib/api-auth", () => ({
|
||||
requireSession: vi.fn(),
|
||||
requireDeveloper: vi.fn(),
|
||||
}));
|
||||
|
||||
const { mockFindFirst, mockSelectChain } = vi.hoisted(() => {
|
||||
@@ -29,21 +29,21 @@ vi.mock("@epicure/db", () => ({
|
||||
desc: vi.fn((a) => ({ a, op: "desc" })),
|
||||
}));
|
||||
|
||||
const { requireSession } = await import("@/lib/api-auth");
|
||||
const { requireDeveloper } = await import("@/lib/api-auth");
|
||||
import { GET } from "../route";
|
||||
|
||||
const ctx = { params: Promise.resolve({ id: "wh-1" }) };
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(requireSession).mockResolvedValue({ session: mockSession as never, response: null });
|
||||
vi.mocked(requireDeveloper).mockResolvedValue({ session: mockSession as never, response: null });
|
||||
mockFindFirst.mockResolvedValue({ id: "wh-1" });
|
||||
mockSelectChain.limit.mockResolvedValue([]);
|
||||
});
|
||||
|
||||
describe("GET /api/v1/webhooks/[id]/deliveries", () => {
|
||||
it("returns 401 when not authenticated", async () => {
|
||||
vi.mocked(requireSession).mockResolvedValue({
|
||||
vi.mocked(requireDeveloper).mockResolvedValue({
|
||||
session: null,
|
||||
response: new Response(JSON.stringify({ error: "Unauthorized" }), { status: 401 }),
|
||||
} as never);
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { db, webhooks, webhookDeliveries, eq, and, desc } from "@epicure/db";
|
||||
import { requireSession } from "@/lib/api-auth";
|
||||
import { requireDeveloper } from "@/lib/api-auth";
|
||||
|
||||
type Params = { params: Promise<{ id: string }> };
|
||||
|
||||
export async function GET(_req: NextRequest, { params }: Params) {
|
||||
const { session, response } = await requireSession();
|
||||
const { session, response } = await requireDeveloper();
|
||||
if (response) return response;
|
||||
|
||||
const { id } = await params;
|
||||
|
||||
Reference in New Issue
Block a user