Skip to content

Instantly share code, notes, and snippets.

@gregbarcza
Forked from shawncarr/mandrill_validate_webhook
Last active August 29, 2015 14:23
Show Gist options
  • Save gregbarcza/b4dc27c53af5eef43144 to your computer and use it in GitHub Desktop.
Save gregbarcza/b4dc27c53af5eef43144 to your computer and use it in GitHub Desktop.
var Crypto = require('crypto');
/**
* validates a mandrill webhook request
* @param url string the url that mandrill sent the request to
* @param key string the web hook key from https://mandrillapp.com/settings/webhooks
* @param params array the request's POST parameters i.e. req.params
* @param compareTo string the x-mandrill-signature header value
*/
var validate = function validate(url, key, params, compareTo) {
var data = url;
for(var postKey in params) {
data += postKey;
data += params[postKey];
}
var signer = Crypto.createHmac('sha1', key);
var signature = signer.update(data).digest('base64');
return compareTo && signature === compareTo;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment