| 1 | package Catalyst::Authentication::Credential::Remote; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | with 'MooseX::Emulate::Class::Accessor::Fast'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Try::Tiny qw/ try catch /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | __PACKAGE__->mk_accessors( | |||||||||||||||||||||||||||
| 10 | qw/allow_re deny_re cutname_re source realm username_field/); | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=9 SCAR=24.5
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 2 | my ( $class, $config, $app, $realm ) = @_; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | my $self = { }; | |||||||||||||||||||||||||||
| 16 | 2 | bless $self, $class; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # we are gonna compile regular expresions defined in config parameters | ||||||||||||||||||||||||||||
| 19 | # and explicitly throw an exception saying what parameter was invalid | ||||||||||||||||||||||||||||
| 20 | 2 | ▶ | if (defined($config->{allow_regexp}) && ($config->{allow_regexp} ne "")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $config->{"allow_regexp"} and $config->{"allow_regexp"} ne ""
MC/DC0%0/2
defined $config->{"allow_regexp"} and $config->{"allow_regexp"} ne ""
defined $config->{"allow_regexp"}
$config->{"allow_regexp"} ne ""
Truth table33%1/3
A and B
A= defined $config->{"allow_regexp"}B= $config->{"allow_regexp"} ne ""
| |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | try { $self->allow_re( qr/$config->{allow_regexp}/ ) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 23 | 0 | Catalyst::Exception->throw( "Invalid regular expression in ". | |||||||||||||||||||||||||||
| 24 | "'allow_regexp' configuration parameter"); | ||||||||||||||||||||||||||||
| 25 | 2 | }; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | 2 | ▶ | if (defined($config->{deny_regexp}) && ($config->{deny_regexp} ne "")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $config->{"deny_regexp"} and $config->{"deny_regexp"} ne ""
MC/DC0%0/2
defined $config->{"deny_regexp"} and $config->{"deny_regexp"} ne ""
defined $config->{"deny_regexp"}
$config->{"deny_regexp"} ne ""
Truth table33%1/3
A and B
A= defined $config->{"deny_regexp"}B= $config->{"deny_regexp"} ne ""
| |||||||||||||||||||||||||||||
| 28 | 2 | ▶ | try { $self->deny_re( qr/$config->{deny_regexp}/ ) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 30 | 0 | Catalyst::Exception->throw( "Invalid regular expression in ". | |||||||||||||||||||||||||||
| 31 | "'deny_regexp' configuration parameter"); | ||||||||||||||||||||||||||||
| 32 | 2 | }; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 2 | ▶ | if (defined($config->{cutname_regexp}) && ($config->{cutname_regexp} ne "")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $config->{"cutname_regexp"} and $config->{"cutname_regexp"} ne ""
MC/DC0%0/2
defined $config->{"cutname_regexp"} and $config->{"cutname_regexp"} ne ""
defined $config->{"cutname_regexp"}
$config->{"cutname_regexp"} ne ""
Truth table33%1/3
A and B
A= defined $config->{"cutname_regexp"}B= $config->{"cutname_regexp"} ne ""
| |||||||||||||||||||||||||||||
| 35 | 2 | ▶ | try { $self->cutname_re( qr/$config->{cutname_regexp}/ ) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 37 | 0 | Catalyst::Exception->throw( "Invalid regular expression in ". | |||||||||||||||||||||||||||
| 38 | "'cutname_regexp' configuration parameter"); | ||||||||||||||||||||||||||||
| 39 | 2 | }; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 2 | ▶ | $self->source($config->{source} || 'REMOTE_USER'); | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{'source'} || 'REMOTE_USER'
MC/DC100%1/1
$config->{'source'} || 'REMOTE_USER'
$config->{'source'}
Truth table100%2/2
A
A= $config->{'source'}
| |||||||||||||||||||||||||||||
| 42 | 2 | $self->realm($realm); | |||||||||||||||||||||||||||
| 43 | 2 | ▶ | $self->username_field($config->{username_field} || 'username'); | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{'username_field'} || 'username'
MC/DC100%1/1
$config->{'username_field'} || 'username'
$config->{'username_field'}
Truth table100%2/2
A
A= $config->{'username_field'}
| |||||||||||||||||||||||||||||
| 44 | 2 | return $self; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 11 | ▶ | sub authenticate { | ||||||||||||||||||||||||||
|
Subroutine
authenticate: called CC=22 SCAR=38.5
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 11 | my ( $self, $c, $realm, $authinfo ) = @_; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 11 | my $remuser; | |||||||||||||||||||||||||||
| 51 | 11 | ▶ | if ($self->source eq "REMOTE_USER") { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 52 | 8 | ▶ | if ($c->req->can('remote_user')) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 53 | # $c->req->remote_user was introduced in 5.80005; if not evailable we are | ||||||||||||||||||||||||||||
| 54 | # gonna use $c->req->user that is deprecated but more or less works as well | ||||||||||||||||||||||||||||
| 55 | 8 | $remuser = $c->req->remote_user; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | # compatibility hack: | ||||||||||||||||||||||||||||
| 58 | ▶ | elsif ($c->engine->can('env') && defined($c->engine->env)) { | |||||||||||||||||||||||||||
|
Condition0%0/3
$c->engine->can("env") and defined $c->engine->env
MC/DC0%0/2
$c->engine->can("env") and defined $c->engine->env
$c->engine->can("env")
defined $c->engine->env
Truth table0%0/3
A and B
A= $c->engine->can("env")B= defined $c->engine->env
| |||||||||||||||||||||||||||||
| 59 | # BEWARE: $c->engine->env was broken prior 5.80005 | ||||||||||||||||||||||||||||
| 60 | 0 | $remuser = $c->engine->env->{REMOTE_USER}; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | elsif ($c->req->can('user')) { | ||||||||||||||||||||||||||||
| 63 | # maybe show warning that we are gonna use DEPRECATED $req->user | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if (ref($c->req->user)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | # I do not know exactly when this happens but it happens | ||||||||||||||||||||||||||||
| 66 | 0 | Catalyst::Exception->throw( "Cannot get remote user from ". | |||||||||||||||||||||||||||
| 67 | "\$c->req->user as it seems to be a reference not a string" ); | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | else { | ||||||||||||||||||||||||||||
| 70 | 0 | $remuser = $c->req->user; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | elsif ($self->source =~ /^(SSL_CLIENT_.*|CERT_.*|AUTH_USER)$/) { | ||||||||||||||||||||||||||||
| 75 | # if you are using 'exotic' webserver or if the user is | ||||||||||||||||||||||||||||
| 76 | # authenticated e.g via SSL certificate his name could be avaliable | ||||||||||||||||||||||||||||
| 77 | # in different variables | ||||||||||||||||||||||||||||
| 78 | # BEWARE: $c->engine->env was broken prior 5.80005 | ||||||||||||||||||||||||||||
| 79 | 3 | my $nam=$self->source; | |||||||||||||||||||||||||||
| 80 | 3 | ▶ | if ($c->request->can('env')) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 81 | 3 | $remuser = $c->request->env->{$nam}; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | elsif ($c->engine->can('env')) { | ||||||||||||||||||||||||||||
| 84 | 0 | $remuser = $c->engine->env->{$nam}; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | else { | ||||||||||||||||||||||||||||
| 87 | # this happens on Catalyst 5.80004 and before (when using FastCGI) | ||||||||||||||||||||||||||||
| 88 | 0 | Catalyst::Exception->throw( "Cannot handle parameter 'source=$nam'". | |||||||||||||||||||||||||||
| 89 | " as running Catalyst engine has broken \$c->engine->env" ); | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | else { | ||||||||||||||||||||||||||||
| 93 | 0 | Catalyst::Exception->throw( "Invalid value of 'source' parameter"); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 11 | ▶ | return unless defined($remuser); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 10 | ▶ | return if ($remuser eq ""); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # $authinfo hash can contain item username (it is optional) - if it is so | ||||||||||||||||||||||||||||
| 99 | # this username has to be equal to remote_user | ||||||||||||||||||||||||||||
| 100 | 9 | my $authuser = $authinfo->{username}; | |||||||||||||||||||||||||||
| 101 | 9 | ▶ | return if (defined($authuser) && ($authuser ne $remuser)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $authuser and $authuser ne $remuser
MC/DC0%0/2
defined $authuser and $authuser ne $remuser
defined $authuser
$authuser ne $remuser
Truth table33%1/3
A and B
A= defined $authuserB= $authuser ne $remuser
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # handle deny / allow checks | ||||||||||||||||||||||||||||
| 104 | 9 | ▶ | return if (defined($self->deny_re) && ($remuser =~ $self->deny_re)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->deny_re and $remuser =~ $self->deny_re
MC/DC50%1/2
defined $self->deny_re and $remuser =~ $self->deny_re
defined $self->deny_re
$remuser =~ $self->deny_re
Truth table66%2/3
A and B
A= defined $self->deny_reB= $remuser =~ $self->deny_re
| |||||||||||||||||||||||||||||
| 105 | 8 | ▶ | return if (defined($self->allow_re) && ($remuser !~ $self->allow_re)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->allow_re and not $remuser =~ $self->allow_re
MC/DC50%1/2
defined $self->allow_re and not $remuser =~ $self->allow_re
defined $self->allow_re
not $remuser =~ $self->allow_re
Truth table66%2/3
A and not(B)
A= defined $self->allow_reB= not $remuser =~ $self->allow_re
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # if param cutname_regexp is specified we try to cut the final usename as a | ||||||||||||||||||||||||||||
| 108 | # substring from remote_user | ||||||||||||||||||||||||||||
| 109 | 7 | my $usr = $remuser; | |||||||||||||||||||||||||||
| 110 | 7 | ▶ | if (defined($self->cutname_re)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 7 | ▶ | if (($remuser =~ $self->cutname_re) && ($1 ne "")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$remuser =~ $self->cutname_re and $1 ne ""
MC/DC100%2/2
$remuser =~ $self->cutname_re and $1 ne ""
$remuser =~ $self->cutname_re
$1 ne ""
Truth table100%3/3
A and B
A= $remuser =~ $self->cutname_reB= $1 ne ""
| |||||||||||||||||||||||||||||
| 112 | 5 | $usr = $1; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 7 | $authinfo->{ $self->username_field } = $usr; | |||||||||||||||||||||||||||
| 117 | 7 | my $user_obj = $realm->find_user( $authinfo, $c ); | |||||||||||||||||||||||||||
| 118 | 7 | ▶ | return ref($user_obj) ? $user_obj : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 2 | 1; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | __END__ | ||||||||||||||||||||||||||||