import { b as private_env } from './shared-server-sSGG17Df.js';
import { timingSafeEqual } from 'crypto';

function validateAuth(request) {
  const auth = request.headers.get("Authorization") || request.headers.get("authorization");
  if (!auth?.startsWith("Bearer ")) {
    return new Response(JSON.stringify({ error: "Missing authorization" }), {
      headers: { "Content-Type": "application/json" },
      status: 401
    });
  }
  const token = auth.slice(7);
  const expectedKey = private_env.API_KEY?.trim();
  if (!expectedKey) {
    return new Response(JSON.stringify({ error: "Invalid API key" }), {
      headers: { "Content-Type": "application/json" },
      status: 401
    });
  }
  const tokenBuf = Buffer.from(token);
  const expectedBuf = Buffer.from(expectedKey);
  if (tokenBuf.length !== expectedBuf.length || !timingSafeEqual(tokenBuf, expectedBuf)) {
    return new Response(JSON.stringify({ error: "Invalid API key" }), {
      headers: { "Content-Type": "application/json" },
      status: 401
    });
  }
  return null;
}

export { validateAuth as v };
//# sourceMappingURL=auth-DeCdZ83n.js.map
