From 57b48704946f13504180195dcdfb7c8db87bf747 Mon Sep 17 00:00:00 2001 From: Andres Iglesias Date: Mon, 8 Jul 2024 21:54:31 -0500 Subject: [PATCH 1/6] Add psr/container to composer.json --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 0ccc579..3d8d91c 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": "^2.0" }, "require-dev": { "laminas/laminas-diactoros": "^2.4 || ^3.0", From ec6cf9f8aa6a5e100f3ced969331f2f3e1a1e590 Mon Sep 17 00:00:00 2001 From: Andres Iglesias Date: Mon, 8 Jul 2024 21:56:50 -0500 Subject: [PATCH 2/6] Add Mezzio "extra" configuration in composer.json to install the ConfigProvider configuration in a Mezzio Application --- composer.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/composer.json b/composer.json index 3d8d91c..1c4276f 100644 --- a/composer.json +++ b/composer.json @@ -32,5 +32,10 @@ "psr-4": { "RKA\\Middleware\\": "src" } + }, + "extra": { + "laminas": { + "config-provider": "RKA\\Middleware\\Mezzio\\ConfigProvider" + } } } From 1e26122fb556b737f51389f918025df3006217a8 Mon Sep 17 00:00:00 2001 From: Andres Iglesias Date: Mon, 8 Jul 2024 21:59:24 -0500 Subject: [PATCH 3/6] Add IpAddressFactory and ConfigProvider for Mezzio Apps IpAddressFactory defines how we instantiate IpAddress Class with the provided - if any - configuration in the Mezzio Application. --- src/Mezzio/ConfigProvider.php | 26 ++++++++++++++++++++++ src/Mezzio/IpAddressFactory.php | 38 +++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/Mezzio/ConfigProvider.php create mode 100644 src/Mezzio/IpAddressFactory.php 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 + ); + } +} From 23284ca8b904ceb78241b41caaf70001db2ff8b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Iglesias?= Date: Thu, 11 Jul 2024 15:08:17 -0500 Subject: [PATCH 4/6] Update psr/container version constraints --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 1c4276f..d3ab097 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "php": "^7.2 || ^8.0", "psr/http-message": "^1.0 || ^2.0", "psr/http-server-middleware": "^1.0", - "psr/container": "^2.0" + "psr/container": "^1.0 || ^2.0" }, "require-dev": { "laminas/laminas-diactoros": "^2.4 || ^3.0", From b0b4d6fa39e0ad9eb61bf961e21ed1f54775dcf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Iglesias?= Date: Thu, 11 Jul 2024 15:47:41 -0500 Subject: [PATCH 5/6] Add dist file with Mezzio configuration --- src/Mezzio/config/ip_address.global.php.dist | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/Mezzio/config/ip_address.global.php.dist 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'] ?? ''), + ], + ], +]; From ce0e7f1f2fb502979b34411e719ba6e7da719935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Iglesias?= Date: Thu, 11 Jul 2024 15:48:53 -0500 Subject: [PATCH 6/6] Update README.md The README.md file was updated to include the steps for a Mezzio Application configuration --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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