-
Notifications
You must be signed in to change notification settings - Fork 124
/
github-webhook-handler.js
148 lines (116 loc) · 3.3 KB
/
github-webhook-handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const EventEmitter = require('events')
const crypto = require('crypto')
const bl = require('bl')
function findHandler (url, arr) {
if (!Array.isArray(arr)) {
return arr
}
let ret = arr[0]
for (let i = 0; i < arr.length; i++) {
if (url === arr[i].path) {
ret = arr[i]
}
}
return ret
}
function checkType (options) {
if (typeof options !== 'object') {
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') {
throw new TypeError('must provide a \'secret\' option')
}
}
function create (initOptions) {
let options
// validate type of options
if (Array.isArray(initOptions)) {
for (let i = 0; i < initOptions.length; i++) {
checkType(initOptions[i])
}
} else {
checkType(initOptions)
}
// make it an EventEmitter
Object.setPrototypeOf(handler, EventEmitter.prototype)
EventEmitter.call(handler)
handler.sign = sign
handler.verify = verify
return handler
function sign (data) {
return `sha1=${crypto.createHmac('sha1', options.secret).update(data).digest('hex')}`
}
function verify (signature, data) {
const sig = Buffer.from(signature)
const signed = Buffer.from(sign(data))
if (sig.length !== signed.length) {
return false
}
return crypto.timingSafeEqual(sig, signed)
}
function handler (req, res, callback) {
let events
options = findHandler(req.url, initOptions)
if (typeof options.events === 'string' && options.events !== '*') {
events = [options.events]
} else if (Array.isArray(options.events) && options.events.indexOf('*') === -1) {
events = options.events
}
if (req.url !== options.path || req.method !== 'POST') {
return callback()
}
function hasError (msg) {
res.writeHead(400, { 'content-type': 'application/json' })
res.end(JSON.stringify({ error: msg }))
const err = new Error(msg)
handler.emit('error', err, req)
callback(err)
}
const sig = req.headers['x-hub-signature']
const event = req.headers['x-github-event']
const id = req.headers['x-github-delivery']
if (!sig) {
return hasError('No X-Hub-Signature found on request')
}
if (!event) {
return hasError('No X-Github-Event found on request')
}
if (!id) {
return hasError('No X-Github-Delivery found on request')
}
if (events && events.indexOf(event) === -1) {
return hasError('X-Github-Event is not acceptable')
}
req.pipe(bl((err, data) => {
if (err) {
return hasError(err.message)
}
let obj
if (!verify(sig, data)) {
return hasError('X-Hub-Signature does not match blob signature')
}
try {
obj = JSON.parse(data.toString())
} catch (e) {
return hasError(e)
}
res.writeHead(200, { 'content-type': 'application/json' })
res.end('{"ok":true}')
const emitData = {
event: event,
id: id,
payload: obj,
protocol: req.protocol,
host: req.headers.host,
url: req.url,
path: options.path
}
handler.emit(event, emitData)
handler.emit('*', emitData)
}))
}
}
module.exports = create