It's okay with using body-parser + expressjs. But my problem is: how can I get the buffer of the raw request body in Hapijs?
The thing I am talking to is like the buf
param in this function of body-parser npm package: verify(req,res, buf, encoding)
I need it for this function in messenger-platform-samples example:
function verifyRequestSignature(req, res, buf) {
var signature = req.headers["x-hub-signature"];
if (!signature) {
console.error("Couldn't validate the signature.");
} else {
var elements = signature.split('=');
var method = elements[0];
var signatureHash = elements[1];
var expectedHash = crypto.createHmac('sha1', APP_SECRET)
.update(buf)
.digest('hex');
if (signatureHash != expectedHash) {
throw new Error("Couldn't validate the request signature.");
}
}
}
EDIT:
I need to use this in my middle ware using server.ext()
, like this:
server.ext({
type: 'onRequest',
method: (request, reply) => {
var signature = request.headers["x-hub-signature"];
if (!signature) {
console.error("Couldn't validate the signature.");
} else {
var elements = signature.split('=');
var method = elements[0];
var signatureHash = elements[1];
var expectedHash = crypto.createHmac('sha1', APP_SECRET)
.update(request.payload)
.digest('hex');
if (signatureHash != expectedHash) {
throw new Error("Couldn't validate the request signature.");
}
return reply.continue();
}
}
});
request.payload
, so then I have to parse it myself? – Arrowroot