Public
Back
Version 35
3/1/2024
import { createSign, KeyLike } from "node:crypto";
import * as jose from "npm:jose";
import { base64 } from "npm:multiformats/bases/base64";
export type SignatureOptions = {
pkcs8: string;
keyId: string | URL;
algorithm?: "rsa-sha256";
signFn?: (message: string, pkcs8: string, algorithm: SignatureOptions["algorithm"]) => Promise<string>;
};
type SignedRequestInit = RequestInit & SignatureOptions;
export function createSignedFetch(fetchImpl = fetch) {
return async function signedFetch(
input: Request | URL | string,
init: SignedRequestInit,
): Promise<Response> {
const signedReq = await signRequest(input, init);
return fetchImpl(signedReq);
};
}
export async function signRequest(
input: Request | URL | string,
init: SignedRequestInit,
): Promise<Request> {
let request: Request;
if (input instanceof Request) {
request = input;
} else {
request = new Request(input, init);
}
if (!request.headers.has("date")) {
request.headers.set("date", new Date().toUTCString());
}
Updated: April 16, 2024