Lemonldap/NG/Handler/Lib/PSGI.pm

filter: Statementstmt 86.7% 85 / 98 Branchbran 72.2% 26 / 36 Conditioncond 41.6% 15 / 36 MC/DCmcdc 16.6% 3 / 18 Subroutinesub 78.5% 11 / 14 Podpod 0.0% 0 / 7 totaltotal 66.9% 140 / 209 CC 33 SCAR 21.3
CC
33
Cov
74%
init
16.6
user
12.0
_authAndTrace
9.0
CRAP 8.4
« Lemonldap/NG/Handler/Main/Init.pm ↑ Summary Lemonldap/NG/Handler/Lib/OAuth2.pm »
1 package Lemonldap::NG::Handler::Lib::PSGI;
2  
3 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 #use Lemonldap::NG::Handler::Main qw(:jailSharedVars);
7  
8 23 our $VERSION = '2.23.0';
9  
10 23 has protection => ( is => 'rw', isa => 'Str' );
11 23 has rule => ( is => 'rw', isa => 'Str' );
12 23 has api => ( is => 'rw', isa => 'Str' );
13  
14 ## @method boolean init($args)
15 # Initalize main handler
16 45 ▶sub init {
Subroutine init: called CC=11 SCAR=28.1
Pod undocumented
17 45 my ( $self, $args ) = @_;
18 45 eval { $self->api->init($args) };
19 45 ▶ if ( $@ and not( $args->{protection} and $args->{protection} eq 'none' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and not $args->{"protection"} && $args->{"protection"} eq "none") 0 45 45
Condition16%1/6
$@ and not $args->{"protection"} && $args->{"protection"} eq "none"
!ll&&!rl&&r
4500
$args->{'protection'} && $args->{'protection'} eq 'none'
!ll&&!rl&&r
000
MC/DC0%0/3
$@ and not $args->{"protection"} && $args->{"protection"} eq "none"
$@ $args->{'protection'} $args->{'protection'} eq 'none'
Truth table25%1/4
A and not(B && C)
A= $@B= $args->{'protection'}C= $args->{'protection'} eq 'none'
ABCresult
0XX0
1110
10X1
1101
20 0 $self->error($@);
21 0 return 0;
22 }
23 45 ▶ unless ( $self->api->checkConf($self)
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->api->checkConf($self) or $args->{"protection"} and $args->{"protection"} eq "none") 2 43 45
Condition50%3/6
$self->api->checkConf($self) or $args->{"protection"} and $args->{"protection"} eq "none"
l!l&&r!l&&!r
4302
$args->{"protection"} and $args->{"protection"} eq "none"
!ll&&!rl&&r
200
MC/DC33%1/3
$self->api->checkConf($self) or $args->{"protection"} and $args->{"protection"} eq "none"
$self->api->checkConf($self) $args->{"protection"} $args->{"protection"} eq "none"
Truth table50%2/4
A or B and C
A= $self->api->checkConf($self)B= $args->{"protection"}C= $args->{"protection"} eq "none"
ABCresult
1XX1
0111
00X0
0100
24 or ( $args->{protection} and $args->{protection} eq 'none' ) )
25 {
26 2 $self->error(
27 "Unable to protect this server ($Lemonldap::NG::Common::Conf::msg)"
28 );
29 2 return 0;
30 }
31  
32 43 eval { $self->portal( $self->api->tsv->{portal}->() ) };
33 my $rule =
34 43 ▶ $args->{protection} || $self->api->localConfig->{protection} || '';
Condition80%4/5
$args->{'protection'} || $self->api->localConfig->{'protection'} || ''
l!l
241
$args->{"protection"} or $self->api->localConfig->{"protection"}
l!l&&r!l&&!r
2041
MC/DC50%1/2
$args->{'protection'} || $self->api->localConfig->{'protection'} || ''
$args->{"protection"} $self->api->localConfig->{"protection"}
Truth table66%2/3
A or B
A= $args->{"protection"}B= $self->api->localConfig->{"protection"}
ABresult
1X1
011
000
35 43 ▶ $self->rule(
Branch: 2 branches
BranchTrueFalseTotal
$rule eq 'authenticate' ? : 0 43 43
$rule eq 'manager' ? : 0 43 43
36 $rule eq 'authenticate' ? 1 : $rule eq 'manager' ? '' : $rule );
37 43 return 1;
38 }
39  
40 ## @methodi void _run()
41 # Check if protecton is activated then return a code ref that will launch
42 # _authAndTrace() if protection in on or handler() else
43 #@return code-ref
44 10 ▶sub _run {
Subroutine _run: called CC=5 SCAR=17.5
45 10 my $self = shift;
46  
47 # Override _run() only if protection != 'none'
48 10 ▶ if ( !$self->rule or $self->rule ne 'none' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->rule or $self->rule ne 'none') { } 9 1 10
Condition66%2/3
not $self->rule or $self->rule ne "none"
l!l&&r!l&&!r
901
MC/DC50%1/2
not $self->rule or $self->rule ne "none"
not $self->rule $self->rule ne "none"
Truth table66%2/3
not(A) or B
A= not $self->ruleB= $self->rule ne "none"
ABresult
1X1
011
000
49 9 $self->logger->debug('PSGI app is protected');
50  
51 # Handle requests
52 # Developers, be careful: Only this part is executed at each request
53 return $self->psgiAdapter(
54 51 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
55 51 return $self->_authAndTrace( $_[0] );
56 }
57 9 );
58 }
59  
60 else {
61 1 $self->logger->debug('PSGI app is not protected');
62  
63 # Check if main handler initialization has been done
64 1 ▶ unless ( $self->api->tsv ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->api->tsv) 0 1 1
65 0 $self->logger->debug('Checking conf');
66 0 eval { $self->api->checkConf() };
67 0 ▶ $self->logger->error($@) if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
68 }
69  
70 # Handle unprotected requests
71 return $self->psgiAdapter(
72 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
73 1 my $req = $_[0];
74 1 my $res = $self->handler($req);
75 1 push @{ $res->[1] }, $req->spliceHdrs;
76 1 return $res;
77 }
78 1 );
79 }
80 }
81  
82 22 ▶sub reload {
Subroutine reload: called CC=4 SCAR=13.9
Pod undocumented
83 22 my ( $class, $args ) = @_;
84 22 ▶ $args //= {};
Condition50%1/2
$args //= {}
l!l
220
MC/DC0%0/1
$args //= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
85 22 my $self = $class->new($args);
86 22 $self->init($args);
87  
88 # Check if main handler initialization has been done
89 22 ▶ unless ( %{ $self->api->tsv } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%{$self->api->tsv;}) 1 21 22
90 1 eval { $self->api->checkConf() };
91 1 ▶ $self->logger->error($@) if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
92 }
93 return $self->psgiAdapter(
94 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
95 1 my $req = $_[0];
96 1 $self->api->reload($req);
97 1 return [ 200, [ $req->spliceHdrs ], [ $req->{respBody} ] ];
98 }
99 22 );
100 }
101  
102 ## @method private PSGI-Response _authAndTrace($req)
103 # Launch $self->api::run() and then handler() if
104 # response is 200.
105 162 ▶sub _authAndTrace {
Subroutine _authAndTrace: called CC=9 SCAR=22.0
106 162 my ( $self, $req, $noCall ) = @_;
107 162 ▶ unless ( eval { $self->api->tsv->{portal} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->api->tsv->{"portal"} } }) 2 160 162
108 2 $self->api->checkConf(1);
109 }
110  
111 # TODO: handle types
112 162 my $type = $self->api->checkType($req);
113 162 ▶ if ( my $t = $req->env->{VHOSTTYPE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $req->env->{"VHOSTTYPE"}) 44 118 162
114 44 $type = $t;
115 }
116 162 my $tmp = $self->api;
117 162 $tmp =~ s/::\w+$/::/;
118 162 $type = $tmp . $type;
119 162 Lemonldap::NG::Handler::Main->buildAndLoadType($type);
120 162 ▶ die $@ if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 162 162
121 162 my ( $res, $session ) = $type->run( $req, $self->{rule} );
122 162 my $portal = eval { $type->tsv->{portal}->($req) };
123 162 ▶ $self->logger->warn($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 161 162
124  
125 162 ▶ if ( $res < 300 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res < 300) { } 61 101 162
elsif ($res < 400) { } 48 53 101
126 61 ▶ if ($noCall) {
Branch: 1 branch
BranchTrueFalseTotal
if ($noCall) { } 2 59 61
127 2 return [ $res, [ $req->spliceHdrs ], [] ];
128 }
129 else {
130 59 $self->logger->debug('User authenticated, calling handler()');
131 59 $res = $self->handler($req);
132 59 push @{ $res->[1] }, $req->spliceHdrs;
133 59 return $res;
134 }
135 }
136 elsif ( $res < 400 ) {
137 48 return [ $res, [ $req->spliceHdrs ], [] ];
138 }
139 else {
140 53 ▶ my $s = ( $portal ? $portal . "/lmerror/$res" : '' );
Branch: 1 branch
BranchTrueFalseTotal
$portal ? : 52 1 53
141 53 $s =
142 '<html><head><title>Redirection</title></head><body>'
143 . qq{<script type="text/javascript">window.location='$s'</script>}
144 . '<h1>Please wait</h1>'
145 . qq{<p>An error occurs, you're going to be redirected to <a href="$s">$s</a>.</p>}
146 . '</body></html>';
147 return [
148 53 $res,
149 [
150 $req->spliceHdrs,
151 'Content-Type' => 'text/html',
152 'Content-Length' => length $s
153 ],
154 [$s]
155 ];
156 }
157 }
158  
159 ## @method hashRef user()
160 # @return hash of user data
161 0 ▶sub user {
Subroutine user: not called CC=3 SCAR=24.8
Pod undocumented
162 0 my ( $self, $req ) = @_;
163 return $req->userData
164 || { $Lemonldap::NG::Handler::Main::tsv->{whatToTrace}
165 0 ▶ || _whatToTrace => 'anonymous' };
Condition0%0/4
$req->userData || {$Lemonldap::NG::Handler::Main::tsv->{'whatToTrace'} || '_whatToTrace', 'anonymous'}
l!l
00
$Lemonldap::NG::Handler::Main::tsv->{'whatToTrace'} || '_whatToTrace'
l!l
00
MC/DC0%0/1
$req->userData || {$Lemonldap::NG::Handler::Main::tsv->{'whatToTrace'} || '_whatToTrace', 'anonymous'}
$req->userData
MC/DC0%0/1
$Lemonldap::NG::Handler::Main::tsv->{'whatToTrace'} || '_whatToTrace'
$Lemonldap::NG::Handler::Main::tsv->{'whatToTrace'}
Truth table0%0/2
A
A= $req->userData
Aresult
11
00
Truth table0%0/2
A
A= $Lemonldap::NG::Handler::Main::tsv->{'whatToTrace'}
Aresult
11
00
166 }
167  
168 ## @method hashRef custom()
169 # @return hash of custom data
170 0 ▶sub custom {
Subroutine custom: not called CC=1 SCAR=6.9
Pod undocumented
171 0 my ( $self, $req ) = @_;
172 0 return { $Lemonldap::NG::Handler::Main::tsv->{customToTrace} };
173 }
174  
175 ## @method string userId()
176 # @return user identifier to log
177 1 ▶sub userId {
Subroutine userId: called CC=4 SCAR=15.6
Pod undocumented
178 1 my ( $self, $req ) = @_;
179 my $userId =
180 $req->userData->{ $Lemonldap::NG::Handler::Main::tsv->{whatToTrace}
181 || '_whatToTrace' }
182 1 ▶ || $req->userData->{'_user'} # Fix 2377
Condition42%3/7
$req->userData->{$Lemonldap::NG::Handler::Main::tsv->{'whatToTrace'} or '_whatToTrace'} || $req->userData->{'_user'} || 'anonymous'
l!l
01
$req->userData->{$Lemonldap::NG::Handler::Main::tsv->{"whatToTrace"} or "_whatToTrace"} or $req->userData->{"_user"}
l!l&&r!l&&!r
001
$Lemonldap::NG::Handler::Main::tsv->{'whatToTrace'} || '_whatToTrace'
l!l
01
MC/DC0%0/2
$req->userData->{$Lemonldap::NG::Handler::Main::tsv->{'whatToTrace'} or '_whatToTrace'} || $req->userData->{'_user'} || 'anonymous'
$req->userData->{$Lemonldap::NG::Handler::Main::tsv->{"whatToTrace"} or "_whatToTrace"} $req->userData->{"_user"}
MC/DC0%0/1
$Lemonldap::NG::Handler::Main::tsv->{'whatToTrace'} || '_whatToTrace'
$Lemonldap::NG::Handler::Main::tsv->{'whatToTrace'}
Truth table33%1/3
A or B
A= $req->userData->{$Lemonldap::NG::Handler::Main::tsv->{"whatToTrace"} or "_whatToTrace"}B= $req->userData->{"_user"}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $Lemonldap::NG::Handler::Main::tsv->{'whatToTrace'}
Aresult
11
00
183 || 'anonymous';
184  
185 1 $self->logger->debug("Returned userId: $userId");
186 1 return $userId;
187 }
188  
189 ## @method boolean group(string group)
190 # @param $group name of the Lemonldap::NG group to test
191 # @return boolean : true if user is in this group
192 0 ▶sub group {
Subroutine group: not called CC=2 SCAR=17.9
Pod undocumented
193 0 my ( $self, $req, $group ) = @_;
194 0 ▶ return () unless ( $req->userData->{groups} );
Branch: 1 branch
BranchTrueFalseTotal
unless $req->userData->{"groups"} 0 0 0
195 0 return ( $req->userData->{groups} =~ /\b$group\b/ );
196 }
197  
198 ## @method PSGI::Response sendError($req,$err,$code)
199 # Add user id to $err before calling Lemonldap::NG::Common::PSGI::sendError()
200 # @param $req Lemonldap::NG::Common::PSGI::Request
201 # @param $err String to push
202 # @code int HTTP error code (default to 500)
203 1 ▶sub sendError {
Subroutine sendError: called CC=2 SCAR=7.4
Pod undocumented
204 1 my ( $self, $req, $err, $code ) = @_;
205 1 ▶ $err ||= $req->error;
Condition33%1/3
$err ||= $req->error
l!l&&r!l&&!r
100
MC/DC0%0/2
$err ||= $req->error
$err $req->error
Truth table33%1/3
A ||= B
A= $errB= $req->error
ABresult
1X1
011
000
206 1 $self->logger->warn( '[' . $self->userId($req) . "] $err" );
207 1 return $self->Lemonldap::NG::Common::PSGI::sendError( $req, $err, $code );
208 }
209  
210 23 1;
« Lemonldap/NG/Handler/Main/Init.pm ↑ Summary Lemonldap/NG/Handler/Lib/OAuth2.pm »