|
1 |
| -const jwt = require("jsonwebtoken") |
| 1 | +require('dotenv').config({ path: __dirname + '/../.env' }) |
| 2 | +const NEXMO_API_SIGNATURE_SECRET = process.env.NEXMO_API_SIGNATURE_SECRET || '' |
| 3 | +const jwt = require("jsonwebtoken"); |
2 | 4 | const sha256 = require('js-sha256');
|
3 |
| - |
4 |
| -var claims = jwt.verify(signature,'SECRET') |
5 |
| -if (sha256(payload) != claims["payload_hash"]) { |
6 |
| - // payload has been tampered with |
7 |
| - } |
| 5 | +const app = require('express')() |
| 6 | +const bodyParser = require('body-parser') |
| 7 | +app.use(bodyParser.json()) |
| 8 | +app.use(bodyParser.urlencoded({ |
| 9 | + extended: true |
| 10 | +})) |
| 11 | +app |
| 12 | + .route('/webhooks/inbound-message') |
| 13 | + .post(handleInboundMessage); |
| 14 | +function handleInboundMessage(request, response){ |
| 15 | + const payload = Object.assign(request.query, request.body) |
| 16 | + let token = request.headers.authorization.split(" ")[1] |
| 17 | + try{ |
| 18 | + var decoded = jwt.verify(token, NEXMO_API_SIGNATURE_SECRET, {algorithms:['HS256']}); |
| 19 | + if(sha256(JSON.stringify(payload))!=decoded["payload_body"]){ |
| 20 | + console.log("tampering detected"); |
| 21 | + response.status(401).send(); |
| 22 | + } |
| 23 | + else{ |
| 24 | + console.log("Success"); |
| 25 | + response.status(204).send(); |
| 26 | + } |
| 27 | + } |
| 28 | + catch(err){ |
| 29 | + console.log('Bad token detected') |
| 30 | + response.status(401).send() |
| 31 | + } |
| 32 | +} |
| 33 | +app.listen(process.env.PORT || 3000) |
0 commit comments