( req: IncomingMessage, res: ServerResponse, apiKey: string, )
| 15 | import { writeJson } from "./io.js"; |
| 16 | |
| 17 | export function enforceApiKey( |
| 18 | req: IncomingMessage, |
| 19 | res: ServerResponse, |
| 20 | apiKey: string, |
| 21 | ): boolean { |
| 22 | const presented = |
| 23 | extractBearer(req.headers["authorization"]) ?? |
| 24 | headerValue(req.headers["x-api-key"]) ?? |
| 25 | ""; |
| 26 | if (presented === apiKey) return true; |
| 27 | writeJson(res, 401, { error: { code: "unauthenticated", message: "api key required" } }); |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | function extractBearer(h: string | string[] | undefined): string | undefined { |
| 32 | const v = headerValue(h); |
no test coverage detected