-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
38 lines (28 loc) · 1.1 KB
/
Copy pathserver.js
File metadata and controls
38 lines (28 loc) · 1.1 KB
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
31
32
33
34
35
36
37
38
var express = require("express")
var bodyParser = require("body-parser")
var crypto = require("crypto")
var app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
app.listen(8080, () => {
console.log("Server running on port 8080")
});
app.post("/", (req, res) => {
const signature = req.headers.signature
// This is the secret you entered when adding the webhook on RapidCompact.
const secret = 'secret'
const bodyString = JSON.stringify(req.body)
console.log(bodyString)
// NOTE: This is necessary, as our webhook server is escaping `/` before signing the request.
const bodyEscaped = bodyString.replaceAll('/', '\\/')
// Calculate the HMAC signature using SHA256 and the given secret.
const calculatedSignature = crypto
.createHmac("sha256", secret)
.update(bodyEscaped)
.digest("hex")
// Check if the signature is valid.
const valid = signature == calculatedSignature
console.log('signature valid:', valid)
// Here you can respond differently depending on if the signature is valid or not.
res.sendStatus(200)
});