Brings back the token API that 0.15 removed with deployments, for models only, ported onto the current code (local models, lean client bundles, MiB-rounded disk space): - Scoped bearer tokens (models:read, downloads:create, models:verify, tags:write), stored as SHA-256, managed in Settings -> API tokens. Only /api/v1 accepts them; the rest stays OIDC-only. - /api/v1: Hub lookup, library with the search filters, download requests through the add-model/queue path (idempotent), repair of missing or broken files, verify / deep verify, tags, job status with ?wait=, cancelling or withdrawing the token's own jobs, disk space. - Guardrails for downloads and repairs: approval (default ask), max size per request, minimum free space incl. reserved bytes, agent jobs at once, author allowlist (new downloads only). Approve/reject in the UI, "requested by" badges and filter, activity log per token. - Migration 0027 adds api_tokens, agent_activity and requested_by columns on models and jobs; job state pending_approval. - docs/api.md, docs/agent/SKILL.md and the kaname curl helper; DB-backed API tests. The login gate test now expects /api/v1 to be left to token auth. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
34 lines
1.5 KiB
TypeScript
34 lines
1.5 KiB
TypeScript
import { type NextFetchEvent, type NextRequest, NextResponse } from "next/server";
|
|
import { auth } from "@/auth";
|
|
|
|
// Gate every page and API route. Unauthenticated page requests go to /login,
|
|
// API requests get a 401. Route handlers check the session again (see lib/api-auth.ts).
|
|
const gate = auth((req) => {
|
|
if (req.auth) return;
|
|
|
|
const { pathname, search } = req.nextUrl;
|
|
if (pathname.startsWith("/api/")) {
|
|
return NextResponse.json({ error: "unauthorized" }, { status: 401 });
|
|
}
|
|
const loginUrl = new URL("/login", req.url);
|
|
if (pathname !== "/") loginUrl.searchParams.set("callbackUrl", pathname + search);
|
|
return NextResponse.redirect(loginUrl);
|
|
});
|
|
|
|
// Next.js requires a literal function export here; `export default auth(...)` isn't recognized.
|
|
// With a lazy (function) config, next-auth's `auth(handler)` returns a Promise of the handler,
|
|
// even though its types say otherwise, hence the await.
|
|
type ProxyHandler = (req: NextRequest, event: NextFetchEvent) => Promise<Response | undefined>;
|
|
|
|
export async function proxy(req: NextRequest, event: NextFetchEvent) {
|
|
const handler = (await gate) as unknown as ProxyHandler;
|
|
return handler(req, event);
|
|
}
|
|
|
|
export const config = {
|
|
// Everything except the Auth.js endpoints, the token-authenticated API (/api/v1, checked by
|
|
// withToken in each route), the login page and static assets.
|
|
// Exact exclusions only: an org like "loginx" or a path like "/api/v1x" must still go through the gate.
|
|
matcher: ["/((?!api/auth/|api/v1/|api/v1$|login$|_next/static/|_next/image/|favicon\\.ico$).*)"],
|
|
};
|