-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhandler.js
89 lines (71 loc) · 2.63 KB
/
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
var EventEmitter = require('events').EventEmitter;
var NodeRSA = require('node-rsa');
var formBody = require('body/form');
function bindEmitter(obj, emitter) {
var methods = 'addListener,on,once,removeListener,removeAllListeners,setMaxListeners,listeners,emit';
methods.split(',').forEach(function (fn) {
obj[fn] = emitter[fn].bind(emitter);
});
}
function create(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.public_key !== 'string') {
throw new TypeError('must provide a \'public_key\' option');
}
var handler = function (req, res, callback) {
if (req.url.split('?').shift() !== options.path) {
return callback();
}
function hasError (msg) {
res.writeHead(400, { 'content-type': 'application/json' });
res.end(JSON.stringify({ error: msg }));
var err = new Error(msg);
callback(err);
}
var repoSlug = req.headers['travis-repo-slug'];
var sig = req.headers['signature']
if (!sig) {
return hasError('No Signature found on request');
}
if (!repoSlug) {
return hasError('No repo found on request');
}
formBody(req, {}, function (err, data) {
if (err) {
return hasError(err.message);
}
var key = new NodeRSA(options.public_key, {signingScheme: 'sha1'});
if (!key.verify(JSON.parse(data.payload), sig, 'base64', 'base64'))
return hasError('Signed payload does not match signature')
var result;
try {
result = JSON.parse(data.payload);
} catch (err) {
return hasError(err.message);
}
res.writeHead(200, { 'content-type': 'application/json' });
res.end('{"ok":true}');
var event = (result.status === 0) ? 'success' : 'failure';
if (event === 'failure' && result.status_message === 'Pending') {
event = 'start';
}
var emitData = {
payload: result,
host: req.headers['host'],
url: req.url,
event: event
};
handler.emit('*', emitData);
handler.emit(event, emitData);
});
};
handler.emitter = new EventEmitter();
bindEmitter(handler, handler.emitter);
return handler;
}
module.exports = create;