File tree 1 file changed +33
-0
lines changed
1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ require ( 'dotenv' ) . config ( { path : __dirname + '/../.env' } )
2
+ const NEXMO_API_SIGNATURE_SECRET = process . env . NEXMO_API_SIGNATURE_SECRET || ''
3
+ const jwt = require ( "jsonwebtoken" ) ;
4
+ const sha256 = require ( 'js-sha256' ) ;
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 )
You can’t perform that action at this time.
0 commit comments