-
Notifications
You must be signed in to change notification settings - Fork 780
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: event webhook interface to process raw body (#1153)
- Loading branch information
1 parent
0c0bd1c
commit 865f09d
Showing
6 changed files
with
85 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
First, follow the guide to [set up signature verification for event webhooks](https://sendgrid.com/docs/for-developers/tracking-events/getting-started-event-webhook-security-features/) | ||
|
||
An example of a server to process incoming event webhooks: | ||
```javascript | ||
const bodyParser = require("body-parser"); | ||
const express = require('express'); | ||
const functions = require("firebase-functions"); | ||
const app = express(); | ||
|
||
const {EventWebhook, EventWebhookHeader} = require('@sendgrid/eventwebhook'); | ||
|
||
const verifyRequest = function (publicKey, payload, signature, timestamp) { | ||
const eventWebhook = EventWebhook(); | ||
const ecPublicKey = eventWebhook.convertPublicKeyToECDSA(publicKey); | ||
return eventWebhook.verifySignature(ecPublicKey, payload, signature, timestamp); | ||
} | ||
|
||
// Using bodyParser middleware to process the body | ||
app.use(bodyParser.text({ type: 'application/json' })); | ||
|
||
app.post("/sendgrid/webhook", async (req, resp) => { | ||
try { | ||
const key = '<your_public_key>'; | ||
// Alternatively, you can get your key from your firebase function cloud config | ||
// const key = getConfig().sendgrid.webhook_verification_key; | ||
|
||
const signature = req.get(EventWebhookHeader.SIGNATURE()); | ||
const timestamp = req.get(EventWebhookHeader.TIMESTAMP()); | ||
|
||
const requestBody = req.body; | ||
// Alternatively, if using firebase cloud functions, remove the middleware and use: | ||
// const requestBody = (req as functions.https.Request).rawBody; | ||
|
||
if (verifyRequest(key, requestBody, signature, timestamp)) { | ||
resp.send(204); | ||
} else { | ||
resp.send(403); | ||
} | ||
} catch (error) { | ||
resp.status(500).send(error); | ||
} | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
import EventWebhook = require('./src/eventwebhook'); | ||
import {EventWebhook, EventWebhookHeader} = require('./src/eventwebhook'); | ||
|
||
export = EventWebhook; | ||
export { | ||
EventWebhook, | ||
EventWebhookHeader | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters