rm redundant work

This commit is contained in:
Moon Man 2023-12-31 07:31:58 -05:00
parent 37daf35110
commit 007635213a
2 changed files with 21 additions and 13 deletions

2
module.d.ts vendored
View File

@ -8,7 +8,7 @@ declare module "activitypub-http-signatures" {
export class Sha256Signer { export class Sha256Signer {
constructor(options: { publicKeyId: string, privateKey: string, headerNames?: string[] }); constructor(options: { publicKeyId: string, privateKey: string, headerNames?: string[] });
sign: (options: { url: string, method: string, headers: any[] }) => string; sign: (options: { url: string, method: string, headers: HeadersInit }) => string;
}; };
type parse = (params: { url: string, method: string, headers: Record<string, string> }) => { type parse = (params: { url: string, method: string, headers: Record<string, string> }) => {

View File

@ -19,24 +19,22 @@ export interface SignedInit {
// TODO: Handle redirects. // TODO: Handle redirects.
export const signedFetch = async (url: string, init: RequestInit, signedInit: SignedInit): Promise<globalThis.Response> => { export const signedFetch = async (url: string, init: RequestInit, signedInit: SignedInit): Promise<globalThis.Response> => {
const signedHeaders: HeadersInit = [ const signedHeaders: HeadersInit = {};
["Date", new Date().toUTCString()]
];
const headerNames = ["(request-target)", "host", "date"]; const headerNames = ["(request-target)", "host", "date"];
if (init.method === "POST") { if (init.method === "POST") {
headerNames.push("content-type", "digest"); headerNames.push("content-type", "digest");
signedHeaders.push(["Content-Type", `application/ld+json; profile="${CONTEXT}"`]); signedHeaders["content-type"] = `application/ld+json; profile="${CONTEXT}"`;
if (init.body) { if (init.body) {
if (signedInit.digest) { if (signedInit.digest) {
signedHeaders.push(["Digest", signedInit.digest]); signedHeaders["digest"] = signedInit.digest;
} }
else { else {
if (Buffer.isBuffer(init.body) || typeof init.body === "string") { if (Buffer.isBuffer(init.body) || typeof init.body === "string") {
signedHeaders.push(["Digest", hashDigest(init.body)]); signedHeaders["digest"] = hashDigest(init.body);
} }
else throw "unsupported body type"; else throw "unsupported body type";
} }
@ -53,19 +51,29 @@ export const signedFetch = async (url: string, init: RequestInit, signedInit: Si
const signature = signer.sign({ const signature = signer.sign({
url, url,
method: init.method as string, method: init.method as string,
headers: [ headers: signedHeaders
["Host", new URL(url).host],
...signedHeaders
]
}); });
// Retain order for picky servers.
const newHeaders: HeadersInit = const newHeaders: HeadersInit =
[ [
...signedHeaders, ["date", signedHeaders.date],
["Signature", signature]
] ]
; ;
if (signedHeaders["content-type"]) newHeaders.push([
"content-type",
signedHeaders["content-type"]
]);
if (signedHeaders.digest) newHeaders.push([
"digest",
signedHeaders.digest
]);
newHeaders.push(["signature", signature]);
if (Array.isArray(init.headers)) if (Array.isArray(init.headers))
init.headers init.headers
.filter((header) => Array.isArray(header)) .filter((header) => Array.isArray(header))