auth: allow service-token search definition administration

This commit is contained in:
2026-09-07 18:12:16 +02:00
parent 84945a2ff7
commit 6d02a5cd34
+8 -10
View File
@@ -2,9 +2,9 @@
* Next.js auth seam for the complete /api/0pt capability surface. * Next.js auth seam for the complete /api/0pt capability surface.
* *
* Customer calls use verified Zitadel bearer identity and receive immutable * Customer calls use verified Zitadel bearer identity and receive immutable
* x-0pt-verified-* headers. Explicit machine-only routes may instead use the * x-0pt-verified-* headers. Explicit machine routes may instead use the
* shared X-Internal-Token convention; those routes must also verify that token * shared X-Internal-Token convention; mutation handlers verify it again for
* in their own handler for defence in depth. * defence in depth.
*/ */
import { NextResponse, type NextRequest } from 'next/server'; import { NextResponse, type NextRequest } from 'next/server';
import { assertAuthConfigSafe, resolveAuthContext } from '@/src/auth/context'; import { assertAuthConfigSafe, resolveAuthContext } from '@/src/auth/context';
@@ -22,22 +22,21 @@ const EXEMPT_PATTERNS: RegExp[] = [
/^\/api\/admin\/metrics(\/|$)/, /^\/api\/admin\/metrics(\/|$)/,
]; ];
/** Machine-to-machine surfaces. A valid internal token is required. */ /** Routes that may be reached with a service token instead of end-user bearer auth. */
const INTERNAL_ONLY_ROUTES: RegExp[] = [ const INTERNAL_SERVICE_ROUTES: RegExp[] = [
/^\/api\/0pt\/quality\/bridge-run$/, /^\/api\/0pt\/quality\/bridge-run$/,
/^\/api\/0pt\/internal\/evidence-outbox\/dispatch$/, /^\/api\/0pt\/internal\/evidence-outbox\/dispatch$/,
/^\/api\/0pt\/internal\/er\/candidates$/, /^\/api\/0pt\/internal\/er\/candidates$/,
/^\/api\/0pt\/search\/definitions$/,
]; ];
let _authDiagCount = 0; let _authDiagCount = 0;
export async function middleware(req: NextRequest): Promise<NextResponse> { export async function middleware(req: NextRequest): Promise<NextResponse> {
const path = req.nextUrl.pathname; const path = req.nextUrl.pathname;
if (EXEMPT_PATTERNS.some((re) => re.test(path))) { if (EXEMPT_PATTERNS.some((re) => re.test(path))) return NextResponse.next();
return NextResponse.next();
}
if (INTERNAL_ONLY_ROUTES.some((re) => re.test(path)) && verifyInternalServiceToken(req.headers)) { if (INTERNAL_SERVICE_ROUTES.some((re) => re.test(path)) && verifyInternalServiceToken(req.headers)) {
return NextResponse.next(); return NextResponse.next();
} }
@@ -52,7 +51,6 @@ export async function middleware(req: NextRequest): Promise<NextResponse> {
} }
const decision = await resolveAuthContext(req.headers); const decision = await resolveAuthContext(req.headers);
if (!decision.ok) { if (!decision.ok) {
return NextResponse.json( return NextResponse.json(
{ error: decision.code, message: decision.message, ...(decision.details ? { details: decision.details } : {}) }, { error: decision.code, message: decision.message, ...(decision.details ? { details: decision.details } : {}) },