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(),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/validate-webhook-url", () => ({
|
||||
@@ -33,7 +33,7 @@ vi.mock("@epicure/db", () => ({
|
||||
and: vi.fn((...args) => ({ args, op: "and" })),
|
||||
}));
|
||||
|
||||
const { requireSession } = await import("@/lib/api-auth");
|
||||
const { requireDeveloper } = await import("@/lib/api-auth");
|
||||
const { validateWebhookUrl } = await import("@/lib/validate-webhook-url");
|
||||
import { DELETE, PATCH } from "../route";
|
||||
|
||||
@@ -49,7 +49,7 @@ 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 });
|
||||
vi.mocked(validateWebhookUrl).mockResolvedValue(null);
|
||||
mockSelectChain.limit.mockResolvedValue([{ id: "wh-1" }]);
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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(),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/webhooks", () => ({
|
||||
@@ -29,7 +29,7 @@ vi.mock("@epicure/db", () => ({
|
||||
and: vi.fn((...args) => ({ args, op: "and" })),
|
||||
}));
|
||||
|
||||
const { requireSession } = await import("@/lib/api-auth");
|
||||
const { requireDeveloper } = await import("@/lib/api-auth");
|
||||
const { dispatchWebhook } = await import("@/lib/webhooks");
|
||||
import { POST } from "../route";
|
||||
|
||||
@@ -47,7 +47,7 @@ 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 });
|
||||
mockWebhookFindFirst.mockResolvedValue({ id: "wh-1" });
|
||||
mockDeliveryFindFirst.mockResolvedValue({
|
||||
id: VALID_DELIVERY_ID,
|
||||
@@ -58,7 +58,7 @@ beforeEach(() => {
|
||||
|
||||
describe("POST /api/v1/webhooks/[id]/redeliver", () => {
|
||||
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,7 +1,7 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { db, webhooks, webhookDeliveries, eq, and } from "@epicure/db";
|
||||
import { requireSession } from "@/lib/api-auth";
|
||||
import { requireDeveloper } from "@/lib/api-auth";
|
||||
import { dispatchWebhook, type WebhookEvent } from "@/lib/webhooks";
|
||||
|
||||
const Schema = z.object({ deliveryId: z.string().uuid() });
|
||||
@@ -9,7 +9,7 @@ const Schema = z.object({ deliveryId: z.string().uuid() });
|
||||
type Params = { params: Promise<{ id: string }> };
|
||||
|
||||
export async function POST(req: NextRequest, { params }: Params) {
|
||||
const { session, response } = await requireSession();
|
||||
const { session, response } = await requireDeveloper();
|
||||
if (response) return response;
|
||||
|
||||
const { id } = await params;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { db, webhooks, eq, and } from "@epicure/db";
|
||||
import { requireSession } from "@/lib/api-auth";
|
||||
import { requireDeveloper } from "@/lib/api-auth";
|
||||
import { validateWebhookUrl } from "@/lib/validate-webhook-url";
|
||||
import { WEBHOOK_EVENTS } from "@/lib/webhooks";
|
||||
|
||||
@@ -15,7 +15,7 @@ export async function DELETE(
|
||||
_req: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
const { session, response } = await requireSession();
|
||||
const { session, response } = await requireDeveloper();
|
||||
if (response) return response;
|
||||
|
||||
const { id } = await params;
|
||||
@@ -41,7 +41,7 @@ export async function PATCH(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
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