-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhapi.js
More file actions
48 lines (42 loc) · 1.1 KB
/
hapi.js
File metadata and controls
48 lines (42 loc) · 1.1 KB
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
'use strict';
const got = require('got');
const crypto = require('crypto');
const Hapi = require('hapi');
const server = new Hapi.Server();
server.connection({
host: 'localhost',
port: 3000
});
server.route({
method: 'POST',
path:'/travis',
handler: function (request, reply) {
let travisSignature = Buffer.from(request.headers.signature, 'base64');
let payload = request.payload.payload;
let status = false;
got('https://api.travis-ci.org/config', {
timeout: 10000
})
.then(response => {
let travisPublicKey = JSON.parse(response.body).config.notifications.webhook.public_key;
let verifier = crypto.createVerify('sha1');
verifier.update(payload);
status = verifier.verify(travisPublicKey, travisSignature);
})
.catch(error => {
console.log('Something went wrong:\n' + error)
})
.then(() => {
if (status) {
// Handle request here now that it has been verified...
}
reply(200);
});
}
});
server.start((err) => {
if (err) {
throw err;
}
console.log('Server running at:', server.info.uri);
});