-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
30 lines (25 loc) · 927 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const crypto = require('crypto');
const isValidSignature = (req, secret) => {
if(!req || !secret){
return false;
}
const givenSignature = req.headers['x-kc-signature'];
const computedSignature = crypto.createHmac('sha256', secret)
.update(req.rawBody)
.digest();
return crypto.timingSafeEqual(Buffer.from(givenSignature, 'base64'), computedSignature);
}
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
if (!isValidSignature(req, process.env['KONTENT_WEBHOOK_SECRET'])) {
context.log('Signature was invalid');
context.res = {
status: 503,
body: "Signature was invalid"
};
}
// Adjust the webhook processing logic
console.log(`Webhook received with content:`);
console.log(JSON.stringify(req.body, undefined, 2));
return req.body;
};