| 1 | package Mojolicious::Plugin::AccessControl; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Net::CIDR::Lite; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use constant CONDITION_NAME => 'access'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 1 | my ( $self, $app ) = @_; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | $app->routes->add_condition( | ||||||||||||||||||||||||||||
| 16 | 54 | ▶ | CONDITION_NAME() => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=13 SCAR=26.1
| |||||||||||||||||||||||||||||
| 17 | 54 | my ( $r, $c, $cap, $args ) = @_; | |||||||||||||||||||||||||||
| 18 | 54 | ▶ | $args ||= []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args ||= []
MC/DC0%0/1
$args ||= []
$args
Truth table50%1/2
A
A= $args
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 54 | my $opt = {}; | |||||||||||||||||||||||||||
| 21 | 54 | my @rule_list = @$args; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 54 | ▶ | if ( ref $args->[0] eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 3 | $opt = $args->[0]; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # Remove option ref from rule list | ||||||||||||||||||||||||||||
| 27 | 3 | @rule_list = splice(@$args, 1); | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 54 | ▶ | if ( $opt->{on_deny} && ref $opt->{on_deny} ne 'CODE' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opt->{"on_deny"} and ref $opt->{"on_deny"} ne "CODE"
MC/DC0%0/2
$opt->{"on_deny"} and ref $opt->{"on_deny"} ne "CODE"
$opt->{"on_deny"}
ref $opt->{"on_deny"} ne "CODE"
Truth table66%2/3
A and B
A= $opt->{"on_deny"}B= ref $opt->{"on_deny"} ne "CODE"
| |||||||||||||||||||||||||||||
| 31 | 0 | Carp::croak "on_deny must be a CODEREF"; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 54 | ▶ | $opt->{cache} = 1 unless ( defined $opt->{cache} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | my $rules = ( $opt->{cache} ) | ||||||||||||||||||||||||||||
| 36 | 54 | ▶ | ? ( $r->{ __PACKAGE__ . '._rules' } ||= $self->_rules(@rule_list) ) # caches to Mojolicious::Routes::Route | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$r->{"Mojolicious::Plugin::AccessControl._rules"} ||= $self->_rules(@rule_list)
MC/DC0%0/2
$r->{"Mojolicious::Plugin::AccessControl._rules"} ||= $self->_rules(@rule_list)
$r->{"Mojolicious::Plugin::AccessControl._rules"}
$self->_rules(@rule_list)
Truth table66%2/3
A ||= B
A= $r->{"Mojolicious::Plugin::AccessControl._rules"}B= $self->_rules(@rule_list)
| |||||||||||||||||||||||||||||
| 37 | : $self->_rules(@rule_list); | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 54 | for my $rule (@$rules) { | |||||||||||||||||||||||||||
| 40 | 33 | my ( $check, $allow ) = @{$rule}; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 33 | my $result = $check->($c); | |||||||||||||||||||||||||||
| 43 | 33 | ▶ | if ( defined $result && $result ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $result and $result
MC/DC50%1/2
defined $result and $result
defined $result
$result
Truth table66%2/3
A and B
A= defined $resultB= $result
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # denied | ||||||||||||||||||||||||||||
| 46 | 30 | ▶ | if ( !$allow && $opt->{on_deny} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $allow and $opt->{"on_deny"}
MC/DC100%2/2
not $allow and $opt->{"on_deny"}
not $allow
$opt->{"on_deny"}
Truth table100%3/3
not(A) and B
A= not $allowB= $opt->{"on_deny"}
| |||||||||||||||||||||||||||||
| 47 | 3 | $opt->{on_deny}->($c); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 30 | return $allow; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 24 | return 1; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 1 | ); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 8 | ▶ | sub _rules { | ||||||||||||||||||||||||||
|
Subroutine
_rules: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 60 | 8 | my ($self, @args) = @_; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 8 | my @rules; | |||||||||||||||||||||||||||
| 63 | 8 | ▶ | for ( my $i = 0; $i < @args; $i += 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 11 | my ( $allowing, $rule ) = ( $args[$i], $args[ $i + 1 ] ); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 11 | ▶ | Carp::croak "must be allow or deny" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | unless $allowing =~ /^(allow|deny)$/; | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 11 | ▶ | $allowing = ( $allowing eq 'allow' ) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 11 | my $check = $rule; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 11 | ▶ | if ( $rule =~ /^ALL$/i ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 73 | 3 | ▶ | $check = sub {1}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | elsif ( $rule =~ /[A-Z]$/i ) { | ||||||||||||||||||||||||||||
| 76 | 6 | ▶ | $check = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 77 | 6 | my $host = $_[0]->req->env->{'REMOTE_HOST'}; | |||||||||||||||||||||||||||
| 78 | 6 | ▶ | return unless defined $host; # skip | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 6 | return $host =~ /^(.*\.)?\Q${rule}\E$/; | |||||||||||||||||||||||||||
| 80 | 2 | }; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | elsif ( ref($rule) ne 'CODE' ) { | ||||||||||||||||||||||||||||
| 83 | 5 | my $cidr = Net::CIDR::Lite->new(); | |||||||||||||||||||||||||||
| 84 | 5 | $cidr->add_any($rule); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 21 | ▶ | $check = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.7
| |||||||||||||||||||||||||||||
| 87 | 21 | my $addr = $_[0]->tx->remote_address; | |||||||||||||||||||||||||||
| 88 | 21 | ▶ | if ( defined $addr ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 21 | ▶ | return ( $cidr->find($addr) ) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 5 | }; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 11 | push @rules, [ $check => $allowing ]; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 8 | return \@rules; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 2 | 1; | |||||||||||||||||||||||||||
| 101 | __END__ | ||||||||||||||||||||||||||||