Skip to content

Commit e41dd75

Browse files
committed
feat: adding webhook verification to TS quickstart
1 parent 78c35cf commit e41dd75

File tree

3 files changed

+165
-4
lines changed

3 files changed

+165
-4
lines changed

typescript/inbound/index.ts

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,54 @@
1-
import express, { Request, Response, Application } from "express";
1+
import express, { Request, Response, NextFunction, Application } from "express";
2+
3+
declare module 'express-serve-static-core' {
4+
interface Request {
5+
rawBody: string;
6+
}
7+
}
8+
29
import dotenv from "dotenv";
10+
import { verifyWebhookSignature } from "@hookdeck/sdk/webhooks";
311

412
dotenv.config();
513

14+
const HOOKDECK_WEBHOOK_SECRET = process.env.HOOKDECK_WEBHOOK_SECRET;
15+
616
const app: Application = express();
7-
app.use(express.json());
17+
18+
const verify = async (req: Request, res: Response, next: NextFunction) => {
19+
if(!HOOKDECK_WEBHOOK_SECRET) {
20+
console.warn("No HOOKDECK_WEBHOOK_SECRET found in environment variables. Skipping verification.")
21+
return;
22+
}
23+
24+
const verified = await verifyWebhookSignature({
25+
headers: req.headers as { [key: string]: string; },
26+
rawBody: req.rawBody,
27+
signingSecret: HOOKDECK_WEBHOOK_SECRET
28+
});
29+
30+
if(verified) {
31+
next();
32+
}
33+
else {
34+
res.status(401).send("Unauthorized");
35+
}
36+
}
37+
38+
app.use(express.json({
39+
verify: (req: Request, res: Response, buf: Buffer, encoding: string) => {
40+
req.rawBody = buf.toString();
41+
}
42+
}));
843

944
const port = process.env.PORT || 3030;
1045

11-
app.post("*", (req: Request, res: Response) => {
12-
console.log({ webhook_received: new Date().toISOString(), body: req.body });
46+
app.post("*", verify, (req: Request, res: Response) => {
47+
console.log({
48+
webhook_received: new Date().toISOString(),
49+
path: req.path,
50+
body: req.body
51+
});
1352

1453
res.json({ status: "ACCEPTED" });
1554
});

typescript/inbound/package-lock.json

Lines changed: 121 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

typescript/inbound/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"author": "",
1313
"license": "ISC",
1414
"dependencies": {
15+
"@hookdeck/sdk": "^0.4.0",
1516
"dotenv": "^16.3.1",
1617
"express": "^4.18.2",
1718
"nodemon": "^3.0.1"

0 commit comments

Comments
 (0)