handle signed get

This commit is contained in:
Moon Man 2023-12-31 00:17:53 -05:00
parent 6ef1b5c9cc
commit 9c2ce04ae1
1 changed files with 16 additions and 18 deletions

View File

@ -25,18 +25,16 @@ export const signedFetch = async (url: string, init: RequestInit, signedInit: Si
["Content-Type", `application/ld+json; profile="${CONTEXT}"`] ["Content-Type", `application/ld+json; profile="${CONTEXT}"`]
]; ];
const newHeaders = new Headers();
if (signedInit.digest && init.body) { if (signedInit.digest && init.body) {
signedHeaders.push(["Digest", signedInit.digest]); signedHeaders.push(["Digest", signedInit.digest]);
} }
else if (init.body) { else if (init.method === "POST" && init.body) {
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.push(["Digest", hashDigest(init.body)]);
} }
else throw "unsupported body type"; else throw "unsupported body type";
}
else {
throw "no body provided";
}
const signer = new Sha256Signer({ const signer = new Sha256Signer({
privateKey: signedInit.privateKey, privateKey: signedInit.privateKey,
@ -49,8 +47,8 @@ export const signedFetch = async (url: string, init: RequestInit, signedInit: Si
headers: signedHeaders headers: signedHeaders
}); });
const newHeaders = new Headers();
newHeaders.set("Signature", signature); newHeaders.set("Signature", signature);
}
if (Array.isArray(init.headers) || !init.headers) { if (Array.isArray(init.headers) || !init.headers) {
for (const header of (init.headers || [])) { for (const header of (init.headers || [])) {