neophp / ip-whitelist-package
IP whitelist middleware for NeoPHP routes
Package info
github.com/NeoPHP-Dev/neo-ip-whitelist-package
pkg:composer/neophp/ip-whitelist-package
Requires
- php: >=8.5
README
A middleware for NeoPHP that restricts access to specific routes based on the client's IP address.
Structure
ip-whitelist-package/
├── composer.json
├── README.md
├── src/
│ ├── IpWhitelistPackage.php
│ └── Middleware/
│ └── IpWhitelistMiddleware.php
└── config/
└── ipwhitelist.config.php
How it works
IpWhitelistMiddleware implements NeoPHP's MiddlewareInterface directly —
no scanning, no wiring through the package system is needed for this. It is
resolved by MiddlewareManager the exact same way as any project-defined
middleware: through the #[Middleware(use: ...)] attribute, backed by
class_exists() and the DI container.
If allowed_ips is empty, the middleware allows every request through
(fail-open by default) — configure at least one IP before relying on it in
production.
Installation
Published (Packagist)
composer require neophp/ip-whitelist-package
Via package:require
php bin/neo package:require neophp/ip-whitelist-package --project=MyProject
Local development (path repository)
Root composer.json of the NeoPHP framework:
{
"repositories": [
{ "type": "path", "url": "packages/ip-whitelist-package" }
]
}
Target project's composer.json:
{
"require": {
"neophp/ip-whitelist-package": "@dev"
}
}
composer update
Enabling the package
// src/MyProject/Config/app.config.php return [ // ... 'packages' => [ \Vendor\NeoPHP\IpWhitelistPackage\IpWhitelistPackage::class, ], ];
Configuration
config/ipwhitelist.config.php is copied once to
Config/Packages/IpWhitelist/ipwhitelist.config.php in the target project:
<?php declare(strict_types=1); return [ 'allowed_ips' => [ '127.0.0.1', '203.0.113.42', ], ];
Usage
Apply the middleware to any controller or a single action, using NeoPHP's
built-in #[Middleware] attribute:
use Neo\Core\Security\Middleware\Attribute\Middleware; use Vendor\NeoPHP\IpWhitelistPackage\Middleware\IpWhitelistMiddleware; #[Middleware( use: IpWhitelistMiddleware::class, message: 'Access restricted to whitelisted IPs.', onError: 'block', )] final class AdminController extends AbstractController { // every action in this controller now requires a whitelisted IP }
Apply it to a single action instead of the whole controller by placing the attribute directly on the method:
final class ReportsController extends AbstractController { #[Middleware(use: IpWhitelistMiddleware::class, onError: 'block')] #[Route(path: '/export', name: 'export', methods: ['GET'])] public function export(): Response { // ... } }
License
MIT