Skip to content

Instantly share code, notes, and snippets.

@gjohnson
Created March 13, 2014 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gjohnson/9524126 to your computer and use it in GitHub Desktop.
Save gjohnson/9524126 to your computer and use it in GitHub Desktop.
Express middleware for verifying a webhook from Mandrill.
var crypto = require('crypto');
var assert = require('assert');
var debug = require('debug')('mandrill-verify');
module.exports = function (config) {
assert(config, 'missing config');
assert(config.secret, 'missing secret')
assert(config.url, 'missing url');
return function (req, res, next) {
var key = 'mandrill_events';
var thiers = req.get('x-mandrill-signature');
var ours = crypto
.createHmac('sha1', config.secret)
.update(config.url)
.update(key)
.update(req.body[key])
.digest('base64');
if (thiers !== ours) {
debug('unauthorized');
res.send(401);
} else {
debug('authorized');
next();
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment