joskoomen / laravel-abstract-api

A Security package for Headless service connections

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Laravel & Lumen Abstract API Security

Laravel

  1. Add the Service Provider to config/app.php
/*
 * Package Service Providers...
 */
\Ypa\AbstractApi\AbstractApiServiceProvider::class,
  1. Run php artisan vendor:publish to publish the config file;
  2. Add the .env variables and their values;
  3. Add the middleware in App\Http\Kernel.php to $routeMiddleware
'abstract.api' => \Ypa\AbstractApi\AbstractApiMiddleware::class,
  1. And i advice to add it to the api group in the same file:
'api' => [
    'throttle:60,1',
    'bindings',
    'abstract.api
],
  1. For sending you can add the AbstractApiValidationTrait to your controller.
  2. Right before your API request you can use the following method:
$form_params = $this->addTimeAndSignature(request()->all());

That's it!

Lumen

  1. Add the .env variables and their values;
YPA_ABSTRACT_API_TIME_DIFFERENCES=30
YPA_ABSTRACT_API_HASH_SECRET="${APP_KEY}"
YPA_ABSTRACT_API_HASHTYPE=sha512
YPA_ABSTRACT_API_DEBUG=true
YPA_ABSTRACT_API_DISABLE=false
  1. Add the middleware in your bootstrap file.
$app->routeMiddleware([
    'abstract.api' => \Ypa\AbstractApi\AbstractApiMiddleware::class,
]);,
  1. Add the middleware to your routes you want to secure like any other middleware in Lumen.
  2. For sending you can add the AbstractApiValidationTrait to your controller.
  3. Right before your API request you can use the following method:
$form_params = $this->addTimeAndSignature(request()->all());

That's it!

About

A Security package for Headless service connections

License:MIT License


Languages

Language:PHP 100.0%