Skip to content

Commit

Permalink
feat(middleware): emit "error" event on verification error
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Dec 25, 2017
1 parent 4870c65 commit c83c774
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions middleware/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = middleware

const isntWebhook = require('./isnt-webhook')
const getMissingHeaders = require('./get-missing-headers')
const verify = require('../verify')
const verifyAndReceive = require('./verify-and-receive')

const debug = require('debug')('webhooks:receiver')
function middleware (state, request, response, next) {
Expand Down Expand Up @@ -49,22 +49,12 @@ function middleware (state, request, response, next) {

request.on('end', () => {
const payload = Buffer.concat(dataChunks).toString()
const matchesSignature = verify(
state.secret,
payload,
signature
)

if (!matchesSignature) {
response.statusCode = 400
response.end('x-hub-signature does not match event payload and secret')
return
}

state.eventHandler.receive({
verifyAndReceive(state, {
id: id,
name: eventName,
payload: JSON.parse(payload)
payload: JSON.parse(payload),
signature
})

.then(() => {
Expand Down

0 comments on commit c83c774

Please sign in to comment.