diff --git a/README.md b/README.md index ff177bb..a33d335 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,8 @@ This library cannot by design ensure you get correct and trustworthy results if `composer require akrabat/ip-address-middleware` +In Mezzio, copy `Mezzio/config/ip_address.global.php.dist` into your Mezzio Application `config/autoload` directory as `ip_address.global.php` + ## Usage In Slim 3: @@ -78,12 +80,13 @@ $app->get('/', function ($request, $response, $args) { }); ``` -In Laminas, add to your `pipeline.php` config at the correct stage, usually just before the `DispatchMiddleware`: +In Laminas or Mezzio, add to your `pipeline.php` config at the correct stage, usually just before the `DispatchMiddleware`: ```php # config/pipeline.php # using default config $app->add(RKA\Middleware\IpAddress::class); ``` +If required, update your `.env` file with the environmental variables found in `/config/autoload/ip_address.global.php`. ## Testing diff --git a/composer.json b/composer.json index 0ccc579..d3ab097 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,8 @@ "require": { "php": "^7.2 || ^8.0", "psr/http-message": "^1.0 || ^2.0", - "psr/http-server-middleware": "^1.0" + "psr/http-server-middleware": "^1.0", + "psr/container": "^1.0 || ^2.0" }, "require-dev": { "laminas/laminas-diactoros": "^2.4 || ^3.0", @@ -31,5 +32,10 @@ "psr-4": { "RKA\\Middleware\\": "src" } + }, + "extra": { + "laminas": { + "config-provider": "RKA\\Middleware\\Mezzio\\ConfigProvider" + } } } diff --git a/src/Mezzio/ConfigProvider.php b/src/Mezzio/ConfigProvider.php new file mode 100644 index 0000000..03a2e7a --- /dev/null +++ b/src/Mezzio/ConfigProvider.php @@ -0,0 +1,26 @@ + $this->getDependencies(), + ]; + } + + private function getDependencies(): array + { + return [ + 'factories' => [ + IpAddress::class => IpAddressFactory::class, + ] + ]; + } +} diff --git a/src/Mezzio/IpAddressFactory.php b/src/Mezzio/IpAddressFactory.php new file mode 100644 index 0000000..e8d7379 --- /dev/null +++ b/src/Mezzio/IpAddressFactory.php @@ -0,0 +1,38 @@ +has('config')) { + $config = $container->get('config'); + } + + $checkProxyHeaders = $config['rka']['ip_address']['check_proxy_headers'] ?? false; + $trustedProxies = $config['rka']['ip_address']['trusted_proxies'] ?? null; + $attributeName = $config['rka']['ip_address']['attribute_name'] ?? null; + $headersToInspect = $config['rka']['ip_address']['headers_to_inspect'] ?? []; + + return new IpAddress( + $checkProxyHeaders, + $trustedProxies, + $attributeName, + $headersToInspect + ); + } +} diff --git a/src/Mezzio/config/ip_address.global.php.dist b/src/Mezzio/config/ip_address.global.php.dist new file mode 100644 index 0000000..5fe92df --- /dev/null +++ b/src/Mezzio/config/ip_address.global.php.dist @@ -0,0 +1,20 @@ + [ + 'ip_address' => [ + 'check_proxy_headers' => (bool) ($_ENV['IP_ADDRESS_CHECK_PROXY_HEADERS'] ?? false), + 'trusted_proxies' => $_ENV['IP_ADDRESS_TRUSTED_PROXIES'] ?? null, + 'attribute_name' => $_ENV['IP_ADDRESS_ATTRIBUTE_NAME'] ?? null, + 'headers_to_inspect' => explode(',', $_ENV['IP_ADDRESS_HEADERS_TO_INSPECT'] ?? ''), + ], + ], +];