mirror of
https://github.com/outline/outline.git
synced 2026-11-21 13:34:02 +03:00
* Listen to GitHub webhooks to update issue-sources cache * Add `GitHubWebhookTask` * review
37 lines
936 B
TypeScript
37 lines
936 B
TypeScript
import crypto from "crypto";
|
|
import { Next } from "koa";
|
|
import { APIContext } from "@server/types";
|
|
import { safeEqual } from "@server/utils/crypto";
|
|
|
|
export default function validateWebhook({
|
|
secretKey,
|
|
getSignatureFromHeader,
|
|
}: {
|
|
secretKey: string;
|
|
getSignatureFromHeader: (ctx: APIContext) => string | undefined;
|
|
}) {
|
|
return async function validateWebhookMiddleware(ctx: APIContext, next: Next) {
|
|
const { body } = ctx.request;
|
|
const signatureFromHeader = getSignatureFromHeader(ctx);
|
|
|
|
if (!signatureFromHeader) {
|
|
ctx.status = 401;
|
|
ctx.body = "Missing signature header";
|
|
return;
|
|
}
|
|
|
|
const computedSignature = crypto
|
|
.createHmac("sha256", secretKey)
|
|
.update(JSON.stringify(body))
|
|
.digest("hex");
|
|
|
|
if (!safeEqual(computedSignature, signatureFromHeader)) {
|
|
ctx.status = 401;
|
|
ctx.body = "Invalid signature";
|
|
return;
|
|
}
|
|
|
|
return next();
|
|
};
|
|
}
|