Plack::Middleware::Access - Restrict access depending on remote ip or other parameters
version 0.2
# in your app.psgi use Plack::Builder; builder { enable "Access" rules => [ allow => "goodhost.com", allow => sub { <some code that returns true, false, or undef> }, allow => "192.168.1.5", deny => "192.168.1.0/24", allow => "192.0.0.10", deny => "all" ]; $app; };
This middleware is intended for restricting access to your app by some users. It is very similar with allow/deny directives in web-servers.
A reference to an array of rules. Each rule consists of directive allow or deny and their argument. Rules are checked in the order of their record to the first match. Code rules always match if they return a defined value. Access is granted if no rule matched.
allow
deny
Argument for the rule is a one of four possibilites:
Always matched. Typical use-case is a deny => "all" in the end of rules.
Matches on domain or subdomain of remote_host if it can be resolved. If $env{REMOTE_HOST} is not set, the rule is skipped.
$env{REMOTE_HOST}
Matches on one ip or ip range. See Net::IP for detailed description of possible variants.
An arbitrary code reference for checking arbitrary properties of the request. This function takes $env as parameter. The rule is skipped if the code returns undef.
$env
Either an error message which is returned with HTTP status code 403 ("Forbidden" by default), or a code reference with a PSGI app to return a PSGI-compliant response if access was denied.
You can also the allow method of use this module just to check PSGI requests whether they match some rules:
my $check = Plack::Middleware::Access->new( rules => [ ... ] ); if ( $check->allow( $env ) ) { ... }
If your app runs behind a reverse proxy, you should wrap it with Plack::Middleware::ReverseProxy to get the original request IP. There are several modules in the Plack::Middleware::Auth:: namespace to enable authentification for access restriction.
Jakob Voss
Yury Zavarin <yury.zavarin@gmail.com>
This software is copyright (c) 2010 by Yury Zavarin.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Plack::Middleware::Access, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Plack::Middleware::Access
CPAN shell
perl -MCPAN -e shell install Plack::Middleware::Access
For more information on module installation, please visit the detailed CPAN module installation guide.