Github WebHook PSR-7 / PSR-15 middleware
, (*1)
This library offers a PSR-7 style & PSR-15 middleware which will verify if the incoming GitHub web hook request is correctly signed., (*2)
The provided PSR-7 request will have its X-Hub-Signature header checked in order to see if the request was originally performed by GitHub using the correct secret to sign the request., (*3)
If the request signature validation fails, a 401 JSON response will be send back., (*4)
Installation
The recommended way to install this library is through Composer:, (*5)
composer require "swop/github-webhook-middleware"
Usage
Ex: PSR-7 style middleware using Zend Diactoros Server
```php
<?php, (*6)
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;, (*7)
use Swop\GitHubWebHook\Security\SignatureValidator;
use Swop\GitHubWebHookMiddleware\GithubWebHook;, (*8)
$request = \Zend\Diactoros\ServerRequestFactory::fromGlobals();, (*9)
$middleware = new GithubWebHook(new SignatureValidator(), 'my_secret');, (*10)
$next = function (RequestInterface $request, ResponseInterface $response) {
// The security has been check.
// Do some stuff with the web hook...
return new \Zend\Diactoros\Response\JsonResponse(['status' => 'ok']);
};, (*11)
$server = \Zend\Diactoros\Server::createServerFromRequest($middleware, $request);, (*12)
$server->listen($next);, (*13)
### Ex: PSR-15 middleware using Zend Stratigility
````php
<?php
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Zend\Stratigility\MiddlewarePipe;
use Zend\Stratigility\NoopFinalHandler;
use Zend\Diactoros\Server;
use Zend\Diactoros\Response\JsonResponse;
use Swop\GitHubWebHook\Security\SignatureValidator;
use Swop\GitHubWebHookMiddleware\GithubWebHook;
$app = (new MiddlewarePipe())
->pipe(new GithubWebHook(new SignatureValidator(), 'my_secret'))
->pipe('/', function (RequestInterface $request, ResponseInterface $response) {
// The security has been check.
// Do some stuff with the web hook...
return new JsonResponse(['status' => 'OK']);
});
$request = \Zend\Diactoros\ServerRequestFactory::fromGlobals();
Server::createServerFromRequest($app, $request)
->listen(new NoopFinalHandler())
;
Contributing
See CONTRIBUTING file., (*14)
Original Credits
License
This library is released under the MIT license. See the complete license in the bundled LICENSE file., (*15)