Skip to content

Commit 009a8e3

Browse files
committed
✨ Add verifyWebhookRequest to adapter
1 parent 172861c commit 009a8e3

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/models/adapter.model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,5 @@ export interface Adapter {
8585
res?: Response,
8686
) => Promise<{ apiKey: string; apiUrl: string }>;
8787
handleWebhook?: (req: Request) => Promise<ContactsChangedData>;
88+
verifyWebhookRequest?: (req: Request) => Promise<boolean>;
8889
}

src/models/controller.model.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,11 +1350,20 @@ export class Controller {
13501350
}
13511351

13521352
public async handleWebhook(req: Request, res: Response): Promise<void> {
1353-
try {
1354-
if (!this.adapter.handleWebhook) {
1355-
throw new ServerError(501, 'Webhook handling not implemented');
1356-
}
1353+
if (!this.adapter.handleWebhook) {
1354+
throw new ServerError(501, 'Webhook handling not implemented');
1355+
}
1356+
1357+
if (!this.adapter.verifyWebhookRequest) {
1358+
throw new ServerError(501, 'Webhook verification not implemented');
1359+
}
1360+
1361+
const verified = await this.adapter.verifyWebhookRequest(req);
1362+
if (!verified) {
1363+
throw new ServerError(403, 'Webhook verification failed');
1364+
}
13571365

1366+
try {
13581367
const contactsChangedData: ContactsChangedData =
13591368
await this.adapter.handleWebhook(req);
13601369

0 commit comments

Comments
 (0)