|
| 1 | +import * as fs from "fs"; |
| 2 | +import { setGlobalDispatcher, Agent } from "undici"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Load client certificates for mutual TLS authentication. This function must be called before any HTTP requests are made. |
| 6 | + * This is a global setting that affects all HTTP requests made by the application using the native fetch API. |
| 7 | + * |
| 8 | + * @param clientCertPath Path to client certificate |
| 9 | + * @param clientKeyPath Path to client key |
| 10 | + * @param caCertPath Path to CA certificate [optional] |
| 11 | + * @param clientKeyPassword Password for client key [optional] |
| 12 | + * @param rejectUnauthorized Reject unauthorized certificates. |
| 13 | + * Only use for testing/development, not recommended in production environments [optional] |
| 14 | + * |
| 15 | + * @returns void |
| 16 | + * |
| 17 | + * @example |
| 18 | + * ```typescript |
| 19 | + * loadClientCertificates("cert.pem", "key.pem", "ca.pem", "password", false); |
| 20 | + * ``` |
| 21 | + * |
| 22 | + * @see |
| 23 | + * [Undici Agent](https://undici.nodejs.org/#/docs/api/Agent) |
| 24 | + * @see |
| 25 | + * [Undici Dispatcher](https://undici.nodejs.org/#/docs/api/Dispatcher) |
| 26 | + * @see |
| 27 | + * [NodeJS Native Fetch API](https://nodejs.org/docs/latest-v19.x/api/globals.html#fetch) |
| 28 | + */ |
| 29 | +export function loadClientCertificates( |
| 30 | + clientCertPath: string, |
| 31 | + clientKeyPath: string, |
| 32 | + caCertPath?: string, |
| 33 | + clientKeyPassword?: string, |
| 34 | + rejectUnauthorized?: boolean |
| 35 | +): void { |
| 36 | + const clientCert = fs.readFileSync(clientCertPath); |
| 37 | + const clientKey = fs.readFileSync(clientKeyPath); |
| 38 | + const caCert = caCertPath ? fs.readFileSync(caCertPath) : undefined; |
| 39 | + const agent = new Agent({ |
| 40 | + connect: { |
| 41 | + cert: clientCert, |
| 42 | + key: clientKey, |
| 43 | + ca: caCert, |
| 44 | + passphrase: clientKeyPassword, |
| 45 | + rejectUnauthorized, |
| 46 | + }, |
| 47 | + }); |
| 48 | + |
| 49 | + setGlobalDispatcher(agent); |
| 50 | +} |
0 commit comments