apburnes / hapi-auth-twilio-signature

Hapi.js plugin to authenticate Twilio webhooks requests.
1 stars 0 forks source link

hapi-auth-twilio-signature

A hapi.js authentication plugin for Twilio

About

Use this authentication plugin for Twilio's webhooks to authenticate requests coming from Twilio https://www.twilio.com/platform/webhooks. This plugin will intercept the "X-Twilio-Signature" header token to be validated in the route.

Usage

Twilio Signature authentication requires validating the "X-Twilio-Signature" header token. The 'twilio-signature' scheme takes the following options:

var twilio = require('twilio');

var token = 'YOUR_TWILIO_AUTH_TOKEN';

var validate = function (signature, callback) {

    if (!signature) {
        return callback(null, false);
    }

    var credentials = twilio.validateRequest(token, signature, url, params);

    if (twilio.validateRequest(token, signature, 'example.com', 'webhooks')) {
        callback(null, true, credentials);
    }
};

server.pack.register(require('hapi-auth-twilio-signature'), function (err) {
    server.auth.strategy('twilio', 'twilio-signature', { validateFunc: validate });
    server.route({ method: 'POST', path: '/webhooks', config: { auth: 'twilio' } });
});