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