arubacao/aws-ip-range-middleware

Laravel Middleware for Amazon Web Services (AWS) IP Address Range

1.0.2 2021-05-03 00:05 UTC

This package is auto-updated.

Last update: 2024-04-29 03:53:45 UTC


README

Latest Version on Packagist Run Tests Codecov Total Downloads

This package allows for validation of incoming requests against the official Amazon Web Services (AWS) IP Address Range.
Use this to determine if an incoming request actually comes from the AWS infrastructure e.g. for Simple Notification Service (SNS) payloads.

Features

  • Passes incoming HTTP requests from AWS, rejects everything else
  • AWS ip address range is fetched on demand and therefore always up-to-date
  • Caching of ip address range --> only fetched once per day
  • Retry with exponential back-off on network issues while fetching the ip address range from AWS

Notes

  • arubacao/aws-ip-range-middleware is functional and fully tested for Laravel 5.0 - 7.* and PHP 7.0 - 7.3.

Installation

Install this package via composer:

composer require arubacao/aws-ip-range-middleware

Registering Middleware

First assign the aws-ip-range-middleware a key in your app/Http/Kernel.php file to the $routeMiddleware property.

// Within App\Http\Kernel Class...

protected $routeMiddleware = [
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    // .
    // .
    // .
    'aws-ip-range' => \Arubacao\AwsIpRange\AwsIpRangeMiddleware::class,
];

Usage

Once the aws-ip-range-middleware has been defined in the HTTP kernel, you may use the middleware method to assign aws-ip-range-middleware to a route:

Route::post('api/sns', function () {
    //
})->middleware('aws-ip-range');


// Older Laravel Versions:
Route::post('api/sns', ['middleware' => 'aws-ip-range', function () {
    //
}]);

When assigning middleware, you may also pass the fully qualified class name:
Note: In this case you do not need to register the aws-ip-range-middleware in the HTTP kernel

use Arubacao\AwsIpRange\AwsIpRangeMiddleware;

Route::post('api/sns', function () {
    //
})->middleware(AwsIpRangeMiddleware::class);


// Older Laravel Versions:
Route::post('api/sns', ['middleware' => AwsIpRangeMiddleware::class, function () {
    //
}]);

Todo's

  • Enable/Disable caching
  • Choose cache storage
  • Command to fetch ip address range and store locally

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.