sa-kws-node-sdk
Advanced tools
Comparing version 0.1.4 to 0.1.5
@@ -5,2 +5,3 @@ var BPromise = require('bluebird'); | ||
var utils = require('./utils'); | ||
var crypto = require('crypto'); | ||
@@ -96,3 +97,2 @@ function KwsSdk(opts) { | ||
//********************************************************************************* | ||
// This functions are called when an API call is made | ||
@@ -161,2 +161,48 @@ //********************************************************************************* | ||
//********************************************************************************* | ||
// Webhooks part | ||
//********************************************************************************* | ||
// validate if the call is coming from KWS API | ||
KwsSdk.prototype.validWebhookSignature = function(secretKey) { | ||
var self = this; | ||
return function(req, res, next) { | ||
var signature = req.headers['x-kwsapi-signature']; | ||
if (signature === undefined) { | ||
res.sendStatus('401'); | ||
if (self.opts.debug) { | ||
self.opts.logger('Error in: validWebhookSignature middleware'); | ||
self.opts.logger('headers[x-kwsapi-signature] is undefined'); | ||
} | ||
} else { | ||
var signedData = req.originalUrl; | ||
for(var key in req.body) { | ||
signedData += key; | ||
signedData += data[key]; | ||
} | ||
signedData += secretKey; | ||
var currentSignature = crypto.createHmac('sha1', secretKey).update(signedData).digest('hex').toString('base64'); | ||
if (currentSignature !== signature) { | ||
res.sendStatus('401'); | ||
if (self.opts.debug) { | ||
self.opts.logger('Error in: validWebhookSignature middleware'); | ||
self.opts.logger('headers[x-kwsapi-signature] is invalid, check your secretKey'); | ||
} | ||
} else { | ||
next(); | ||
} | ||
} | ||
} | ||
}; | ||
//********************************************************************************* | ||
module.exports = KwsSdk; |
{ | ||
"name": "sa-kws-node-sdk", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "KWS Node App SDK", | ||
@@ -5,0 +5,0 @@ "main": "./lib/kwsSdk.js", |
37768
954