-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
28 lines (21 loc) · 888 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
const crypto = require("crypto")
module.exports = secret => {
return (req, res, next) => {
req.rawBody = ""
req.on("data", chunk => {
req.rawBody += chunk
})
req.on("end", () => {
try {
req.body = JSON.parse(req.rawBody)
if (!req.headers["x-shoppy-signature"]) return res.status(400).send("Missing signature header")
const hmac = crypto.createHmac("sha512", secret)
const signed = hmac.update(Buffer.from(req.rawBody, "utf-8")).digest("hex")
if (signed !== req.headers["x-shoppy-signature"]) return res.status(401).send("Invalid signature")
next() //valid signature
} catch (err) { //something went wrong
res.status(500).send("Error during signature verification")
}
})
}
}