Files
Epicure/apps/web/lib/api-auth.ts
T
Arnaud eb99faf655 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>
2026-07-22 09:49:07 +02:00

173 lines
5.7 KiB
TypeScript

import crypto from "node:crypto";
import { headers } from "next/headers";
import { NextRequest, NextResponse } from "next/server";
import { auth } from "@/lib/auth/server";
import { db, apiKeys, users, eq } from "@epicure/db";
import { applyRateLimit } from "@/lib/rate-limit";
import { hasDeveloperAccess } from "@/lib/permissions";
export async function requireSession() {
const session = await auth.api.getSession({ headers: await headers() });
if (!session) {
return { session: null, response: NextResponse.json({ error: "Unauthorized" }, { status: 401 }) };
}
return { session, response: null };
}
/** Like requireSession, but never 401s — for endpoints that also accept anonymous
* access via a resource-scoped capability (e.g. a public-editable share link). */
export async function getOptionalSession() {
return auth.api.getSession({ headers: await headers() });
}
export async function requireAdmin(opts?: { allowModerator?: boolean }) {
const { session, response } = await requireSession();
if (response) return { session: null, response };
// Don't trust session.user.role — it comes from a 5-minute cookieCache
// (see lib/auth/server.ts), so a just-demoted admin would keep access for
// up to 5 minutes. Query the current role directly.
const [dbUser] = await db
.select({ role: users.role })
.from(users)
.where(eq(users.id, session!.user.id))
.limit(1);
const allowed = dbUser?.role === "admin" || (opts?.allowModerator && dbUser?.role === "moderator");
if (!allowed) {
return { session: null, response: NextResponse.json({ error: "Forbidden" }, { status: 403 }) };
}
return { session, response: null };
}
/** Gates webhooks, self-serve API keys, and BYOK routes behind
* users.isDeveloper — re-queried fresh for the same reason requireAdmin
* re-queries role (session.user's cookieCache can be up to 5 minutes
* stale, e.g. right after an admin grants access). */
export async function requireDeveloper() {
const { session, response } = await requireSession();
if (response) return { session: null, response };
const [dbUser] = await db
.select({ isDeveloper: users.isDeveloper })
.from(users)
.where(eq(users.id, session!.user.id))
.limit(1);
if (!dbUser || !hasDeveloperAccess(dbUser)) {
return {
session: null,
response: NextResponse.json({ error: "Developer access required — ask an admin to enable it" }, { status: 403 }),
};
}
return { session, response: null };
}
type SessionLike = {
user: {
id: string;
email: string;
name: string;
tier: string;
role?: string;
image?: string | null;
};
};
type RateLimitOpts = { limit: number; windowSeconds: number };
export async function requireSessionOrApiKey(
req: NextRequest,
opts?: { rateLimit?: RateLimitOpts }
): Promise<{ session: SessionLike; response: null } | { session: null; response: NextResponse }> {
// 1. Try Bearer API key
const authHeader = req.headers.get("authorization");
if (authHeader?.startsWith("Bearer ")) {
const rawKey = authHeader.slice(7).trim();
if (rawKey.startsWith("ek_")) {
const keyHash = crypto.createHash("sha256").update(rawKey).digest("hex");
const [keyRow] = await db
.select({ id: apiKeys.id, userId: apiKeys.userId, scope: apiKeys.scope })
.from(apiKeys)
.where(eq(apiKeys.keyHash, keyHash))
.limit(1);
if (keyRow) {
// Read-scoped keys can't make any state-changing request — enforced
// once here rather than in every route, since a route can't tell
// whether it's being called by a "read" key without this check.
if (keyRow.scope === "read" && !["GET", "HEAD", "OPTIONS"].includes(req.method)) {
return {
session: null,
response: NextResponse.json({ error: "This API key is read-only" }, { status: 403 }),
};
}
// Update lastUsedAt asynchronously — don't block response
void db
.update(apiKeys)
.set({ lastUsedAt: new Date() })
.where(eq(apiKeys.id, keyRow.id))
.catch((err) => console.error("[api-auth] failed to update apiKeys.lastUsedAt", err));
const [user] = await db
.select({
id: users.id,
email: users.email,
name: users.name,
tier: users.tier,
role: users.role,
})
.from(users)
.where(eq(users.id, keyRow.userId))
.limit(1);
if (user) {
// Rate limit per API key (not per user — a user's other keys shouldn't
// share this bucket).
if (opts?.rateLimit) {
const { limit, windowSeconds } = opts.rateLimit;
const rateLimitResponse = await applyRateLimit(
`rl:api:key:${keyRow.id}`,
limit,
windowSeconds
);
if (rateLimitResponse) {
return { session: null, response: rateLimitResponse };
}
}
return {
session: { user: { ...user, image: null } },
response: null,
};
}
}
return {
session: null,
response: NextResponse.json({ error: "Unauthorized" }, { status: 401 }),
};
}
}
// 2. Fall back to session cookie
const result = await requireSession();
if (result.response) return result;
if (opts?.rateLimit) {
const { limit, windowSeconds } = opts.rateLimit;
const rateLimitResponse = await applyRateLimit(
`rl:api:session:${result.session!.user.id}`,
limit,
windowSeconds
);
if (rateLimitResponse) {
return { session: null, response: rateLimitResponse };
}
}
return result;
}