kanban-dev-webapp/src/lib/agent/auth.ts
Dave ff23890809 feat(agent-api): bearer token check and owner-resolution helper
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2026-09-25 22:27:46 +02:00

28 lines
888 B
TypeScript

import "server-only";
import { jsonError } from "@/lib/http/json";
/**
* Checks the Authorization header against AGENT_API_TOKEN.
*
* Same pattern as /api/cron/reconcile's CRON_SECRET check — a second secret
* of the same kind, for a second machine caller. Returns the Response to
* send when the caller must be refused, or null when the request may
* proceed, so every route starts with:
*
* const denied = requireAgentToken(request);
* if (denied) return denied;
*/
export function requireAgentToken(request: Request): Response | null {
const secret = process.env.AGENT_API_TOKEN;
if (!secret) {
console.error("[agent-api] AGENT_API_TOKEN is not set; refusing all requests");
return jsonError("Not configured", 500);
}
if (request.headers.get("authorization") !== `Bearer ${secret}`) {
return jsonError("Unauthorized", 401);
}
return null;
}