The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Mojolicious::Plugin::AccessControl - Access control

SYNOPSIS

# Mojolicious
sub stratup {
  my $self = shift;
  $self->plugin('AccessControl');
  ...
  ...
  ...
  my $r = $self->routes;
  $r->get('/')->to('example#welcome')->over( 'access' => [
      allow => 'allowhost.com',
      allow => '127.0.0.1'
      allow => '192.168.0.3',
      deny  => '192.168.0.0/24',
      allow => sub { $_[0]->req->headers->user_agent =~ /Firefox/ },
      deny  => 'all',
  ] )->name('index');
}

# Mojolicious::Lite
plugin 'AccessControl';

get '/' => sub {
  my $self = shift;
  ...
  ...
  ...
},
'access' => [
    allow => 'allowhost.com',
    allow => '127.0.0.1'
    allow => '192.168.0.3',
    deny  => '192.168.0.0/24',
    allow => sub { $_[0]->req->headers->user_agent =~ /Firefox/ },
    deny  => 'all',
],
'index';

DESCRIPTION

Mojolicious::Plugin::AccessControl is intended for restricting access to app routes.

This adds the condition to Mojolicious::Routes, which is named 'access'.

ARGUMENTS

An arrayref 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 non-zero value. Access is granted if no rule matched.

"all"

always matched.

ip

matches on one ip or ip range.

See Net::CIDR::Lite.

remote_host

matches on domain or subdomain of remote_host if it can be resolved.

If Mojo::Message::Request#env->{REMOTE_HOST} is not set, the rule is skipped.

code

an arbitrary code reference for checking arbitrary properties of the request.

this function takes Mojolicious::Controller as parameter. The rule is skipped if the code returns undef.

AUTHOR

hayajo <hayajo@cpan.org>

SEE ALSO

Mojolicious, Plack::Middleware::Access, Plack::Builder::Conditionals,

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.