serverless-sam
Version:
Serverless framework plugin to export AWS SAM templates for a service
80 lines (68 loc) • 2.18 kB
JavaScript
const crypto = require('crypto');
function signRequestBody(key, body) {
return `sha1=${crypto.createHmac('sha1', key).update(body, 'utf-8').digest('hex')}`;
}
module.exports.githubWebhookListener = (event, context, callback) => {
var errMsg; // eslint-disable-line
const token = process.env.GITHUB_WEBHOOK_SECRET;
const headers = event.headers;
const sig = headers['X-Hub-Signature'];
const githubEvent = headers['X-GitHub-Event'];
const id = headers['X-GitHub-Delivery'];
const calculatedSig = signRequestBody(token, event.body);
if (typeof token !== 'string') {
errMsg = 'Must provide a \'GITHUB_WEBHOOK_SECRET\' env variable';
return callback(null, {
statusCode: 401,
headers: { 'Content-Type': 'text/plain' },
body: errMsg,
});
}
if (!sig) {
errMsg = 'No X-Hub-Signature found on request';
return callback(null, {
statusCode: 401,
headers: { 'Content-Type': 'text/plain' },
body: errMsg,
});
}
if (!githubEvent) {
errMsg = 'No X-Github-Event found on request';
return callback(null, {
statusCode: 422,
headers: { 'Content-Type': 'text/plain' },
body: errMsg,
});
}
if (!id) {
errMsg = 'No X-Github-Delivery found on request';
return callback(null, {
statusCode: 401,
headers: { 'Content-Type': 'text/plain' },
body: errMsg,
});
}
if (sig !== calculatedSig) {
errMsg = 'X-Hub-Signature incorrect. Github webhook token doesn\'t match';
return callback(null, {
statusCode: 401,
headers: { 'Content-Type': 'text/plain' },
body: errMsg,
});
}
/* eslint-disable */
console.log('---------------------------------');
console.log(`Github-Event: "${githubEvent}" with action: "${event.body.action}"`);
console.log('---------------------------------');
console.log('Payload', event.body);
/* eslint-enable */
// Do custom stuff here with github event data
// For more on events see https://developer.github.com/v3/activity/events/types/
const response = {
statusCode: 200,
body: JSON.stringify({
input: event,
}),
};
return callback(null, response);
};