This val has been created to avoid certain shortcomings of @vtdocs.verifyGithubWebhookSignature. So it was created as a mix/evolution of two sources:
This code is covered by tests which you can copy to run them, see @karfau.test_SignatureCheck
This val does not contain any val.town specific code (@-imports, console.email...), so it should be possible to run in Deno as is, potentially even in modern browsers (that support crypto and TextEncoder and modern ES syntax).
const myGithubWebhook = (req: Request) => {
const {verify} = @karfau.SignatureCheck(); // you have to call it to get the verify function!
const body = await req.text();
const signature = req.headers.get("X-Hub-Signature-256");
const verified = await verify(
{payload:body, signature},
@me.secrets.myGithubWebhookSecret,
// optionally provide fallback secrets (as many as needed)
// @me.secrets.myGithubWebhookSecretFallback
);
if (!verified) {
return new Response(`Not verified`, 401);
}
const payload = JSON.parse(body);
// actually do things in your webhook
};
By default the reason for failing verification is logged to console.error, but you can pass it a different handler:
const {verify} = @karfau.SignatureCheck((reason) => { throw new Error(reason); });
(be aware that it will silently fail if you don't try catch it in an endpoint and the return code will be 502)
@vtdocs.verifyGithubWebhookSignature has the following issues:
verify method of the outdated @octokit/webhooks-methods@3.0.2 which has (at least) two bugs that can make a difference when used in a webhook
false, which can be triggered by sending an invalid signaturesha1=Migrated from folder: SignatureCheck/SignatureCheck