Skip to content

Commit ea984d5

Browse files
committed
make it so
0 parents  commit ea984d5

File tree

7 files changed

+448
-0
lines changed

7 files changed

+448
-0
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

Diff for: .jshintrc

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"predef": [ ]
3+
, "bitwise": false
4+
, "camelcase": false
5+
, "curly": false
6+
, "eqeqeq": false
7+
, "forin": false
8+
, "immed": false
9+
, "latedef": false
10+
, "noarg": true
11+
, "noempty": true
12+
, "nonew": true
13+
, "plusplus": false
14+
, "quotmark": true
15+
, "regexp": false
16+
, "undef": true
17+
, "unused": true
18+
, "strict": false
19+
, "trailing": true
20+
, "maxlen": 120
21+
, "asi": true
22+
, "boss": true
23+
, "debug": true
24+
, "eqnull": true
25+
, "esnext": true
26+
, "evil": true
27+
, "expr": true
28+
, "funcscope": false
29+
, "globalstrict": false
30+
, "iterator": false
31+
, "lastsemic": true
32+
, "laxbreak": true
33+
, "laxcomma": true
34+
, "loopfunc": true
35+
, "multistr": false
36+
, "onecase": false
37+
, "proto": false
38+
, "regexdash": false
39+
, "scripturl": true
40+
, "smarttabs": false
41+
, "shadow": false
42+
, "sub": true
43+
, "supernew": false
44+
, "validthis": true
45+
, "browser": true
46+
, "couch": false
47+
, "devel": false
48+
, "dojo": false
49+
, "mootools": false
50+
, "node": true
51+
, "nonstandard": true
52+
, "prototypejs": false
53+
, "rhino": false
54+
, "worker": true
55+
, "wsh": false
56+
, "nomen": false
57+
, "onevar": false
58+
, "passfail": false
59+
}

Diff for: LICENSE.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
The MIT License (MIT)
2+
=====================
3+
4+
Copyright (c) 2014 Rod Vagg
5+
---------------------------
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
9+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10+
11+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Diff for: README.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# github-webhook-handler
2+
3+
[![NPM](https://nodei.co/npm/github-webhook-handler.svg)](https://nodei.co/npm/github-webhook-handler/)
4+
5+
GitHub allows you to register **[Webhooks](https://developer.github.com/webhooks/)** for your repositories. Each time an event occurs on your repository, whether it be pushing code, filling issues or creating pull requests, the webhook address you register can be configured to be pinged with details.
6+
7+
This library is a small handler (or "middleware" if you must) for Node.js web servers that handles all the logic of receiving and verifying webhook requests from GitHub.
8+
9+
## Example
10+
11+
```js
12+
var http = require('http')
13+
var createHandler = require('github-webhook-handler')
14+
var handler = createHandler({ url: '/webhook', secret: 'myhashsecret' })
15+
16+
http.createServer(function (req, res) {
17+
handler(req, res, function (err) {
18+
res.statusCode = 404
19+
res.end('no such location')
20+
})
21+
}).listen(7777)
22+
23+
handler.on('error', function (err) {
24+
console.err('Error:', err.message)
25+
})
26+
27+
handler.on('push', function (event) {
28+
console.log('Received a push event for %s to %s',
29+
event.payload.repository.name,
30+
event.payload.ref)
31+
})
32+
33+
handler.on('issues', function (event) {
34+
console.log('Received an issue event for % action=%s: #%d %s',
35+
event.payload.repository.name,
36+
event.payload.action
37+
event.payload.issue.number,
38+
event.payload.issue.title)
39+
})
40+
```
41+
42+
## API
43+
44+
github-webhook-handler exports a single function, use this function to *create* a webhook handler by passing in an *options* object. Your options object should contain:
45+
46+
* `"url"`: the complete case sensitive URL to match when looking at `req.url` for incoming requests. Any request not matching this URL will cause the callback function to the handler to be called (sometimes called the `next` handler).
47+
* `"secret"`: this is a hash key used for creating the SHA-1 HMAC signature of the JSON blob sent by GitHub. You should register the same secret key with GitHub. Any request not delivering a `X-Hub-Signature` that matches the signature generated using this key against the blob will be rejected and cause an `'error'` event (also the callback will be called with an `Error` object).
48+
49+
The resulting **handler** function acts like a common "middleware" handler that you can insert into a processing chain. It takes `request`, `response`, and `callback` arguments. The `callback` is not called if the request is successfully handled, otherwise it is called either with an `Error` or no arguments.
50+
51+
The **handler** function is also an `EventEmitter` that you can register to listen to any of the GitHub event types. Note you can be specific in your GitHub configuration about which events you wish to receive, or you can send them all. Note that the `"error"` event will be liberally used, even if someone tries the end-point and they can't generate a proper signature, so you should at least register a listener for it or it will throw.
52+
53+
See the [GitHub Webhooks documentation](https://developer.github.com/webhooks/) for more details on the events you can receive.
54+
55+
## License
56+
57+
**github-webhook-handler** is Copyright (c) 2014 Rod Vagg [@rvagg](https://twitter.com/rvagg) and licensed under the MIT License. All rights not explicitly granted in the MIT License are reserved. See the included [LICENSE.md](./LICENSE.md) file for more details.

Diff for: github-webhook-handler.js

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
const EventEmitter = require('events').EventEmitter
2+
, inherits = require('util').inherits
3+
, crypto = require('crypto')
4+
, bl = require('bl')
5+
6+
7+
function signBlob (key, blob) {
8+
return 'sha1=' + crypto.createHmac('sha1', key).update(blob).digest('hex')
9+
}
10+
11+
12+
function create (options) {
13+
if (typeof options != 'object')
14+
throw new TypeError('must provide an options object')
15+
16+
if (typeof options.url != 'string')
17+
throw new TypeError('must provide a \'url\' option')
18+
19+
if (typeof options.secret != 'string')
20+
throw new TypeError('must provide a \'secret\' option')
21+
22+
// make it an EventEmitter, sort of
23+
EventEmitter.call(handler)
24+
Object.keys(EventEmitter.prototype).forEach(function (k) {
25+
handler[k] = EventEmitter.prototype[k]
26+
})
27+
28+
return handler
29+
30+
31+
function handler (req, res, callback) {
32+
if (req.url !== options.url)
33+
return callback()
34+
35+
function hasError (msg) {
36+
res.writeHead(400, { 'content-type': 'application/json' })
37+
res.end(JSON.stringify({ error: msg }))
38+
39+
var err = new Error(msg)
40+
41+
handler.emit('error', err, req)
42+
callback(err)
43+
}
44+
45+
var sig = req.headers['x-hub-signature']
46+
, event = req.headers['x-github-event']
47+
, id = req.headers['x-github-delivery']
48+
49+
if (!sig)
50+
return hasError('No X-Hub-Signature found on request')
51+
52+
if (!event)
53+
return hasError('No X-Github-Event found on request')
54+
55+
if (!id)
56+
return hasError('No X-Github-Delivery found on request')
57+
58+
req.pipe(bl(function (err, data) {
59+
if (err) {
60+
handler.emit('error', err, req)
61+
return callback(err)
62+
}
63+
64+
var obj
65+
66+
if (sig !== signBlob(options.secret, data))
67+
return hasError('X-Hub-Signature does not match blob signature')
68+
69+
try {
70+
obj = JSON.parse(data.toString())
71+
} catch (e) {
72+
return hasError(e)
73+
}
74+
75+
res.writeHead(200, { 'content-type': 'application/json' })
76+
res.end('{"ok":true}')
77+
78+
handler.emit(event, {
79+
event : event
80+
, id : id
81+
, payload : obj
82+
})
83+
}))
84+
}
85+
}
86+
87+
88+
module.exports = create

Diff for: package.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "github-webhook-handler",
3+
"version": "0.0.0",
4+
"description": "Web handler / middleware for processing GitHub Webhooks",
5+
"main": "github-webhook-handler.js",
6+
"scripts": {
7+
"test": "node test.js"
8+
},
9+
"keywords": [
10+
"github",
11+
"webhooks"
12+
],
13+
"author": "Rod Vagg <[email protected]> (http://r.va.gg)",
14+
"license": "MIT",
15+
"dependencies": {
16+
"bl": "^0.8.0"
17+
},
18+
"devDependencies": {
19+
"tape": "^2.12.3",
20+
"through2": "^0.4.1"
21+
}
22+
}

0 commit comments

Comments
 (0)