settermjd/twilio-eventstreams-webhook-validator-slim

Slim middleware for validating Twilio Event Streams Webhooks

0.0.2 2024-09-10 03:47 UTC

This package is auto-updated.

Last update: 2024-12-11 00:58:42 UTC


README

testing workflow

This is a small piece of middleware for the Slim Framework (v4) that validates a Twilio Event Streams Webhook.

How Does It Work?

After adding the middleware to one or more of your application's routes, when a route is dispatched, the webhook is validated. If the webhook is valid, the next request is called. Otherwise, an HTTP 400 Bad Request is returned, along with a problem details response, formatted as JSON or XML based on the request's Accept header.

Why Use It?

You could code up the functionality yourself; and check out the tutorial on the Twilio blog if you'd like to. But, this middleware avoids the need to do so. Instead, just initialise it and add it to the relevant routes, then focus on the remaining functionality of your application.

Prerequisites

To use this middleware, you're going to need a Twilio account (either free or paid). If you are new to Twilio, create a free account.

⚡️ Quick Start

To use the middleware in your Slim (v4) application, first add the project as a dependency by running the following command:

composer require settermjd/twilio-eventstreams-webhook-validator-slim

Then, in the appropriate part of your application, initialise a new EventStreams\EventStreamsWebhookValidatorMiddleware object, which requires three parameters:

  • The application's public URI. During development you could use ngrok to expose the application to the public internet and generate the public URI.
  • The webhook URI's path
  • A \Twilio\Security\RequestValidator object, initialised with your Twilio Auth Token

Note: The first two parameters to EventStreamsWebhookValidatorMiddleware ensure that the app's correct public URI is used as part of the webhook validation process.

Then, follow the Slim Middleware documentation to add the middleware to the respective route. Below, you can find examples of adding it as Route Middleware:

<?php

declare(strict_types=1);

require_once('vendor/autoload.php');

use EventStreams\EventStreamsWebhookValidatorMiddleware;
use Twilio\Security\RequestValidator;

$app = new \Slim\App();

$eventStreamsWebhookValidatorMiddleware = new EventStreamsWebhookValidatorMiddleware(
    "<<THE APP'S PUBLIC URL>>",
    "<<THE WEBHOOK PATH>>",
     new RequestValidator("<<YOUR TWILIO AUTH TOKEN>>")
);

$app->get('/', function ($request, $response, $args) {
    $response->getBody()->write(' Hello ');
    return $response;
})->add($eventStreamsWebhookValidatorMiddleware);

$app->run();

Then, replace <<THE APP'S PUBLIC URL>>" with the public URI of the application, <<THE WEBHOOK PATH>> with he path of the route that handles the webhook request, and <<YOUR TWILIO AUTH TOKEN>> with your Twilio Auth Token.

Contributing

If you want to contribute to the project, whether you have found issues with it or just want to improve it, here's how:

  • Issues: ask questions and submit your feature requests, bug reports, etc
  • Pull requests: send your improvements

Did You Find The Project Useful?

If the project was useful and you want to say thank you and/or support its active development, here's how:

  • Add a GitHub Star to the project
  • Write an interesting article about the project wherever you blog