All files / src/lib service-auth.ts

100% Statements 10/10
100% Branches 10/10
100% Functions 2/2
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49                                                  10x 10x 10x 6x 4x               2x   4x         6x 6x 6x    
import { timingSafeEqual } from "crypto";
import { NextResponse } from "next/server";
import { requireAuth, type AuthSession } from "@/lib/auth-guard";
 
/**
 * A session, or a machine credential, for the one route a machine calls.
 *
 * Every API route needs a session (ADR-044). The catalog crawler is a
 * machine with none; it POSTs to /api/mock-dsp/<participant>/catalog/request
 * every five minutes. It sends `Authorization: Bearer <token>` instead, the
 * token kept once in Key Vault and referenced by the UI and the crawler job
 * through a managed identity (ADR-036, scripts/azure/wire-dsp-catalog-token.sh).
 *
 * - A bearer header that matches the token in `envName` admits the caller as
 *   a service, with no roles.
 * - A bearer header that does not match is refused with 401, rather than
 *   falling back to the session, so a wrong token is never mistaken for a
 *   missing one.
 * - With no bearer header, or with the token unset in this environment, the
 *   caller needs a session, exactly as requireAuth() decides.
 */
export async function requireSessionOrToken(
  request: Request,
  envName: string,
): Promise<{ session: AuthSession } | NextResponse> {
  const header = request.headers.get("authorization") ?? "";
  const expected = process.env[envName];
  if (expected && header.startsWith("Bearer ")) {
    if (sameSecret(header.slice("Bearer ".length), expected)) {
      return {
        session: {
          user: { id: `service:${envName}` },
          roles: [],
          accessToken: "",
        },
      };
    }
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }
  return requireAuth();
}
 
/** Constant-time comparison, so the response time does not leak a prefix. */
function sameSecret(given: string, expected: string): boolean {
  const a = Buffer.from(given);
  const b = Buffer.from(expected);
  return a.length === b.length && timingSafeEqual(a, b);
}