Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions github-webhook-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ function signBlob (key, blob) {
return 'sha1=' + crypto.createHmac('sha1', key).update(blob).digest('hex')
}

function isObject(obj) {
return Object.prototype.toString.apply(obj) === '[object Object]'
}

function create (options) {
if (typeof options != 'object')
if (!isObject(options))
throw new TypeError('must provide an options object')

if (typeof options.path != 'string')
throw new TypeError('must provide a \'path\' option')

if (typeof options.secret != 'string')
if (typeof options.secret != 'string' && !isObject(options))
throw new TypeError('must provide a \'secret\' option')

var events
Expand Down Expand Up @@ -70,8 +73,14 @@ function create (options) {
return hasError(err.message)
}

var obj
var computedSig = new Buffer(signBlob(options.secret, data))
var obj, appId, appSecret
if (isObject(options.secret)) {
appId = req.url.split('id=').pop().split('&').shift()
appSecret = options.secret[appId]
} else {
appSecret = options.secret
}
var computedSig = new Buffer(signBlob(appSecret, data))

if (!bufferEq(new Buffer(sig), computedSig))
return hasError('X-Hub-Signature does not match blob signature')
Expand All @@ -92,6 +101,7 @@ function create (options) {
, protocol: req.protocol
, host : req.headers['host']
, url : req.url
, appId : appId
}

handler.emit(event, emitData)
Expand All @@ -101,4 +111,4 @@ function create (options) {
}


module.exports = create
module.exports = create