fabro/apps/arc-web/app/api-client.ts
Bryan Helmkamp 6d869aa8da Add asymmetric JWT service-to-service auth between arc-web and arc-attractor
Ed25519 asymmetric JWT: arc-web signs with private key, arc-attractor verifies
with public key. Adds AuthenticatedService axum extractor to all routes, jose
dependency for TypeScript signing, and key generation script.

Startup behavior: ARC_JWT_PUBLIC_KEY set → enforce JWT auth; not set +
ARC_INSECURE_DISABLE_AUTHENTICATION=true → allow unauthenticated; neither →
refuse to start with clear error.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-28 18:05:49 -05:00

45 lines
1.2 KiB
TypeScript

import { importPKCS8, SignJWT } from "jose";
const ARC_API_BASE_URL = process.env.ARC_API_BASE_URL;
const ARC_JWT_PRIVATE_KEY = process.env.ARC_JWT_PRIVATE_KEY;
let cachedKey: CryptoKey | null = null;
async function getSigningKey(): Promise<CryptoKey> {
if (cachedKey) return cachedKey;
if (!ARC_JWT_PRIVATE_KEY) {
throw new Error("ARC_JWT_PRIVATE_KEY environment variable is not set");
}
cachedKey = await importPKCS8(ARC_JWT_PRIVATE_KEY, "EdDSA");
return cachedKey;
}
async function signToken(): Promise<string> {
const key = await getSigningKey();
return new SignJWT({ iss: "arc-web" })
.setProtectedHeader({ alg: "EdDSA" })
.setIssuedAt()
.setExpirationTime("30s")
.sign(key);
}
/**
* Fetch wrapper that signs requests with a JWT for service-to-service auth.
*/
export async function apiFetch(
path: string,
init?: RequestInit
): Promise<Response> {
if (!ARC_API_BASE_URL) {
throw new Error("ARC_API_BASE_URL environment variable is not set");
}
const token = await signToken();
const headers = new Headers(init?.headers);
headers.set("Authorization", `Bearer ${token}`);
return fetch(`${ARC_API_BASE_URL}${path}`, {
...init,
headers,
});
}