Lemonldap/NG/Handler/Main/Run.pm

filter: Statementstmt 69.3% 296 / 427 Branchbran 59.8% 128 / 214 Conditioncond 58.0% 65 / 112 MC/DCmcdc 30.7% 20 / 65 Subroutinesub 80.0% 36 / 45 Podpod 0.0% 0 / 32 totaltotal 60.8% 545 / 895 CC 153 SCAR 28.3
CC
153
Cov
65%
postJavascript
52.9
run
38.1
retrieveSession
33.0
CRAP 17.0
« Lemonldap/NG/Handler/Main/Reload.pm ↑ Summary Lemonldap/NG/Handler/Lib/DevOps.pm »
1 # Main running methods file
2 package Lemonldap::NG::Handler::Main::Run;
3  
4 26 our $VERSION = '2.22.0';
5  
6 package Lemonldap::NG::Handler::Main;
7  
8 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #use AutoLoader 'AUTOLOAD';
11 26 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 26 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 26 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 26 ▶use Lemonldap::NG::Common::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 26 ▶use Lemonldap::NG::Handler::Main::MsgActions;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 26 ▶use Time::HiRes 'usleep';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # Methods that must be overloaded
19  
20 sub handler {
21 0 ▶ die "Must be overloaded" unless ($#_);
Subroutine handler: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless $#_ 0 0 0
22 0 my ($res) = $_[0]->run( $_[1] );
23 0 return $res;
24 }
25  
26 sub logout {
27 0 ▶ my $class;
Subroutine logout: not called CC=2 SCAR=17.9
Pod undocumented
28 0 ▶ $class = $#_ ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
$#_ ? : 0 0 0
29 0 return $class->unlog(@_);
30 }
31  
32 # Public methods
33  
34 # Method that must be called by base packages (Handler::ApacheMP2,...) to get
35 # type of handler to call (Main, AuthBasic,...)
36  
37 sub checkType {
38 123 ▶ my ( $class, $req ) = @_;
Subroutine checkType: called CC=6 SCAR=18.0
Pod undocumented
39  
40 # Always launch "newConf" task if never started
41 msgActions->{newConf}->( $class, {}, $req )
42 123 ▶ unless $class->tsv and %{ $class->tsv };
Branch: 1 branch
BranchTrueFalseTotal
unless $class->tsv and %{$class->tsv;} 1 122 123
Condition66%2/3
$class->tsv and %{$class->tsv;}
!ll&&!rl&&r
01122
MC/DC50%1/2
$class->tsv and %{$class->tsv;}
$class->tsv %{$class->tsv;}
Truth table66%2/3
A and B
A= $class->tsvB= %{$class->tsv;}
ABresult
0X0
100
111
43  
44 # Check for event in events queue every 5 seconds
45 123 ▶ if ( time - $class->lastCheckMsg > $class->checkMsg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (time - $class->lastCheckMsg > $class->checkMsg) 34 89 123
46 34 ▶ defined( $class->checkEvent($req) ) or return 'Fail';
Branch: 1 branch
BranchTrueFalseTotal
defined $class->checkEvent($req) or return "Fail" 1 33 34
47 }
48 122 my $vhost = $class->resolveAlias($req);
49 return ( defined $class->tsv->{type}->{$vhost} )
50 122 ▶ ? $class->tsv->{type}->{$vhost}
Branch: 1 branch
BranchTrueFalseTotal
defined $class->tsv->{'type'}{$vhost} ? : 0 122 122
51 : 'Main';
52 }
53  
54 # Method to check for event in events queue
55 sub checkEvent {
56 34 ▶ my ( $class, $req, $delay ) = @_;
Subroutine checkEvent: called CC=5 SCAR=16.3
Pod undocumented
57 34 $class->logger->debug('Checking for events');
58 34 ▶ unless ( $class->tsv->{msgBrokerReader} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->tsv->{"msgBrokerReader"}) 1 33 34
59 1 $class->logger->error('Not initialized');
60 1 return;
61 }
62 33 my $ret = '';
63 33 ▶ while ( my $msg =
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = $class->tsv->{"msgBrokerReader"}->getNextMessage($class->tsv->{"eventQueueName"}, $delay)) 14 33 47
64 $class->tsv->{msgBrokerReader}
65 ->getNextMessage( $class->tsv->{eventQueueName}, $delay ) )
66 {
67 14 ▶ if ( $msg->{action} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg->{'action'}) { } 14 0 14
68 14 $class->logger->debug("Processing event $msg->{action}");
69 14 ▶ if ( my $sub = msgActions->{ $msg->{action} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = msgActions()->{$msg->{'action'}}) { } 14 0 14
70 14 $sub->( $class, $msg, $req );
71 14 $ret = $msg->{action};
72 }
73 else {
74 0 $class->logger->debug("Unknown action $msg->{action}");
75 }
76 }
77 else {
78 0 $class->logger->error(
79 'Malformed message: ' . JSON::to_json($msg) );
80 }
81 }
82 33 $class->lastCheckMsg(time);
83 33 return $ret;
84 }
85  
86 # Method to push an event into message queue.
87 sub publishEvent {
88 0 ▶ my ( $class, $req, $msg ) = @_;
Subroutine publishEvent: not called CC=5 SCAR=34.0
Pod undocumented
89 0 ▶ die unless $msg;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg 0 0 0
90 0 $class->logger->debug("Publishing event $msg->{action}");
91 0 ▶ unless ( $class->tsv->{msgBrokerWriter} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->tsv->{"msgBrokerWriter"}) 0 0 0
92 0 $class->logger->error('Not initialized');
93 0 return;
94 }
95 $class->tsv->{msgBrokerWriter}
96 0 ->publish( $class->tsv->{eventQueueName}, $msg );
97 0 my $ret;
98 0 my $start = time;
99  
100 # After pushing message, let's pop message queue for this node and
101 # then launch the concerning method
102 0 $ret = $class->checkEvent( $req, 0.1 );
103 0 ▶ if ( !$ret or $ret ne $msg->{action} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ret or $ret ne $msg->{"action"}) 0 0 0
Condition0%0/3
not $ret or $ret ne $msg->{"action"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $ret or $ret ne $msg->{"action"}
not $ret $ret ne $msg->{"action"}
Truth table0%0/3
not(A) or B
A= not $retB= $ret ne $msg->{"action"}
ABresult
1X1
011
000
104 0 usleep 100000;
105 0 $class->checkEvent( $req, 0.1 );
106 }
107 }
108  
109 sub publishStatus {
110 111 ▶ my ( $class, %msg ) = @_;
Subroutine publishStatus: called CC=2 SCAR=8.1
Pod undocumented
111 111 ▶ return unless $class->tsv->{eventStatus};
Branch: 1 branch
BranchTrueFalseTotal
unless $class->tsv->{"eventStatus"} 111 0 111
112 $class->tsv->{msgBrokerWriter}
113 0 ->publish( $class->tsv->{statusQueueName}, \%msg );
114 }
115  
116 ## @rmethod int run
117 # Check configuration and launch Lemonldap::NG::Handler::Main::run().
118 # Each $checkTime, server child verifies if its configuration is the same
119 # as the configuration stored in the local storage.
120 # @param $rule optional Perl expression to grant access
121 # @return constant
122  
123 sub run {
124 122 ▶ my ( $class, $req, $rule, $protection ) = @_;
Subroutine run: called CC=24 SCAR=36.4
Pod undocumented
125 122 my ( $id, $session );
126 122 my $vhost = $class->resolveAlias($req);
127  
128 122 ▶ return $class->DECLINED unless ( $class->is_initial_req($req) );
Branch: 1 branch
BranchTrueFalseTotal
unless $class->is_initial_req($req) 0 122 122
129  
130 # Direct return if maintenance mode is enabled
131 122 ▶ if ( $class->checkMaintenanceMode($req) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->checkMaintenanceMode($req)) 0 119 119
132  
133 0 ▶ if ( $class->tsv->{useRedirectOnError} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->tsv->{'useRedirectOnError'}) { } 0 0 0
134 0 $class->logger->debug("Go to portal with maintenance error code");
135 0 return $class->goToError( $req, '/', $class->MAINTENANCE );
136 }
137 else {
138 0 $class->logger->debug("Return maintenance error code");
139 0 return $class->MAINTENANCE;
140 }
141 }
142  
143 # Authentication process
144 119 my $uri = $req->{env}->{REQUEST_URI};
145 119 my ($cond);
146  
147 119 ▶ ( $cond, $protection ) = $class->conditionSub($rule) if ($rule);
Branch: 1 branch
BranchTrueFalseTotal
if $rule 0 119 119
148 119 ▶ $protection = $class->isUnprotected( $req, $uri ) || 0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $protection 119 0 119
Condition100%2/2
$class->isUnprotected($req, $uri) || 0
l!l
4115
MC/DC100%1/1
$class->isUnprotected($req, $uri) || 0
$class->isUnprotected($req, $uri)
Truth table100%2/2
A
A= $class->isUnprotected($req, $uri)
Aresult
11
00
149 unless ( defined $protection );
150  
151 119 ▶ if ( $protection == $class->SKIP ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($protection == $class->SKIP) 0 119 119
152 0 $class->logger->debug("Access control skipped");
153 0 $class->updateStatus( $req, 'SKIP' );
154 0 $class->hideCookie($req);
155 0 $class->cleanHeaders($req);
156 0 return $class->OK;
157 }
158  
159 # Try to recover cookie and user session
160 119 $id = $class->fetchId($req);
161 119 ▶ $class->data( {} ) unless ($id);
Branch: 1 branch
BranchTrueFalseTotal
unless $id 36 83 119
162 119 ▶ if ( $id
Branch: 3 branches
BranchTrueFalseTotal
if ($id and $session = $class->retrieveSession($req, $id)) { } 78 41 119
elsif ($protection == $class->UNPROTECT) { } 0 41 41
elsif ($protection == $class->MAYSKIP and $class->grant($req, $session, $uri, $cond) eq '999_SKIP') { } 3 38 41
Condition100%3/3
$id and $session = $class->retrieveSession($req, $id)
!ll&&!rl&&r
36578
MC/DC100%2/2
$id and $session = $class->retrieveSession($req, $id)
$id $session = $class->retrieveSession($req, $id)
Truth table100%3/3
A and B
A= $idB= $session = $class->retrieveSession($req, $id)
ABresult
0X0
100
111
163 and $session = $class->retrieveSession( $req, $id ) )
164 {
165  
166 # AUTHENTICATION done
167  
168 # Local macros
169 78 my $kc = keys %{$session}; # in order to detect new local macro
170  
171 # ACCOUNTING (1. Inform web server)
172 78 $class->set_user( $req, $session->{ $class->tsv->{whatToTrace} } );
173  
174 78 my $custom;
175 $custom = $session->{ $class->tsv->{customToTrace} }
176 if ( $class->tsv->{customToTrace}
177 78 ▶ and $session->{ $class->tsv->{customToTrace} } );
Branch: 1 branch
BranchTrueFalseTotal
if $class->tsv->{"customToTrace"} and $$session{$class->tsv->{"customToTrace"}} 78 0 78
Condition33%1/3
$class->tsv->{"customToTrace"} and $$session{$class->tsv->{"customToTrace"}}
!ll&&!rl&&r
0078
MC/DC0%0/2
$class->tsv->{"customToTrace"} and $$session{$class->tsv->{"customToTrace"}}
$class->tsv->{"customToTrace"} $$session{$class->tsv->{"customToTrace"}}
Truth table33%1/3
A and B
A= $class->tsv->{"customToTrace"}B= $$session{$class->tsv->{"customToTrace"}}
ABresult
0X0
100
111
178 78 ▶ if ( $class->tsv->{accessToTrace}->{$vhost} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->tsv->{"accessToTrace"}{$vhost}) 3 75 78
179 my ( $function, @params ) = split /\s*,\s*/,
180 3 $class->tsv->{accessToTrace}->{$vhost};
181 3 ▶ if ( $function =~ qr/^(?:\w+(?:::\w+)*(?:\s+\w+(?:::\w+)*)*)?$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($function =~ qr/^(?:\w+(?:::\w+)*(?:\s+\w+(?:::\w+)*)*)?$/) { } 3 0 3
182 3 my $c = eval {
183 26 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
184 3 &{$function}( {
185 req => $req,
186 vhost => $vhost,
187 session => $session,
188 custom => $custom,
189 params => \@params
190 }
191 );
192 };
193 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 3 3
194 0 $class->logger->error(
195 "Failed to overwrite customToTrace: $@");
196 }
197 else {
198 3 $class->logger->debug("Overwrite customToTrace with: $c");
199 3 $custom = $c;
200 }
201 }
202 else {
203 0 $class->logger->error(
204 "accessToTrace: Bad custom function name");
205 }
206 }
207 78 ▶ $class->set_custom( $req, $custom ) if $custom;
Branch: 1 branch
BranchTrueFalseTotal
if $custom 78 0 78
208 78 $req->userData($session);
209  
210 # AUTHORIZATION
211 78 ▶ return ( $class->forbidden( $req, $session ), $session )
Branch: 1 branch
BranchTrueFalseTotal
unless $class->grant($req, $session, $uri, $cond) 32 46 78
212 unless ( $class->grant( $req, $session, $uri, $cond ) );
213 $class->updateStatus( $req, 'OK',
214 46 $session->{ $class->tsv->{whatToTrace} } );
215  
216 # ACCOUNTING (2. Inform remote application)
217 46 $class->sendHeaders( $req, $session );
218  
219 # Store local macros
220 46 ▶ if ( keys %$session > $kc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$session > $kc) 0 46 46
221 0 $class->logger->debug("Update local cache");
222 0 $req->data->{session}->update( $session, { updateCache => 2 } );
223 }
224  
225 # Hide Lemonldap::NG cookie
226 46 $class->hideCookie($req);
227  
228 # Log access granted
229 $class->logger->debug( "User "
230 . $session->{ $class->tsv->{whatToTrace} }
231 46 . " was granted access to $uri" );
232  
233 # Catch POST rules
234 46 $class->postOutputFilter( $req, $session, $uri );
235 46 $class->postInputFilter( $req, $session, $uri );
236  
237 46 return ( $class->OK, $session );
238 }
239  
240 elsif ( $protection == $class->UNPROTECT ) {
241  
242 # Ignore unprotected URIs
243 0 $class->logger->debug("No valid session but unprotected access");
244 0 $class->updateStatus( $req, 'UNPROTECT' );
245 0 $class->hideCookie($req);
246 0 $class->cleanHeaders($req);
247 0 return $class->OK;
248 }
249  
250 ▶ elsif ( $protection == $class->MAYSKIP
Condition100%3/3
$protection == $class->MAYSKIP and $class->grant($req, $session, $uri, $cond) eq "999_SKIP"
!ll&&!rl&&r
3713
MC/DC100%2/2
$protection == $class->MAYSKIP and $class->grant($req, $session, $uri, $cond) eq "999_SKIP"
$protection == $class->MAYSKIP $class->grant($req, $session, $uri, $cond) eq "999_SKIP"
Truth table100%3/3
A and B
A= $protection == $class->MAYSKIPB= $class->grant($req, $session, $uri, $cond) eq "999_SKIP"
ABresult
0X0
100
111
251 and $class->grant( $req, $session, $uri, $cond ) eq '999_SKIP' )
252 {
253 3 $class->logger->debug("Access control skipped");
254 3 $class->updateStatus( $req, 'SKIP' );
255 3 $class->hideCookie($req);
256 3 $class->cleanHeaders($req);
257 3 return $class->OK;
258 }
259  
260 else {
261  
262 # Redirect user to the portal
263 38 ▶ $class->logger->debug("No cookie found")
Branch: 1 branch
BranchTrueFalseTotal
unless $id 33 5 38
264 unless ($id);
265  
266 # if the cookie was fetched, a log is sent by retrieveSession()
267 38 ▶ $class->updateStatus( $req, $id ? 'EXPIRED' : 'REDIRECT' );
Branch: 1 branch
BranchTrueFalseTotal
$id ? : 5 33 38
268 38 return $class->goToPortal( $req, $req->{env}->{REQUEST_URI} );
269 }
270 }
271  
272 ## @rmethod protected int unlog()
273 # Call localUnlog() then goToPortal() to unlog the current user.
274 # @return Constant value returned by goToPortal()
275 sub unlog {
276 0 ▶ my ( $class, $req ) = @_;
Subroutine unlog: not called CC=1 SCAR=6.9
Pod undocumented
277 0 $class->localUnlog( $req, @_ );
278 0 $class->updateStatus( $req, 'LOGOUT' );
279 0 return $class->goToPortal( $req, '/', 'logout=1' );
280 }
281  
282 # INTERNAL METHODS
283  
284 ## @rmethod protected void updateStatus(string action,string user,string url)
285 # Inform the status process of the result of the request if it is available
286 # @param action string Result of access control (as $class->OK, $class->SKIP, LOGOUT...)
287 # @param optional user string Username to log, if undefined defaults to remote IP
288 # @param optional url string URL to log, if undefined defaults to request URI
289 sub updateStatus {
290 111 ▶ my ( $class, $req, $action, $user, $url ) = @_;
Subroutine updateStatus: called CC=3 SCAR=11.8
Pod undocumented
291 111 ▶ $user ||= $req->address;
Condition66%2/3
$user ||= $req->address
l!l&&r!l&&!r
69420
MC/DC0%0/2
$user ||= $req->address
$user $req->address
Truth table66%2/3
A ||= B
A= $userB= $req->address
ABresult
1X1
011
000
292 111 ▶ $url ||= $req->{env}->{REQUEST_URI};
Condition33%1/3
$url ||= $req->{"env"}{"REQUEST_URI"}
l!l&&r!l&&!r
01110
MC/DC0%0/2
$url ||= $req->{"env"}{"REQUEST_URI"}
$url $req->{"env"}{"REQUEST_URI"}
Truth table33%1/3
A ||= B
A= $urlB= $req->{"env"}{"REQUEST_URI"}
ABresult
1X1
011
000
293 $class->publishStatus(
294 user => $user,
295 111 url => $req->{env}->{HTTP_HOST} . $url,
296 handlerAction => $action
297 );
298 }
299  
300 ## @rmethod void lmLog(string msg, string level)
301 # Wrapper for Apache log system
302 # @param $msg message to log
303 # @param $level string (emerg|alert|crit|error|warn|notice|info|debug)
304 sub lmLog {
305 0 ▶ my ( $class, $msg, $level ) = @_;
Subroutine lmLog: not called CC=1 SCAR=6.9
Pod undocumented
306 0 return $class->logger->$level($msg);
307 }
308  
309 sub auditLog {
310 33 ▶ my ( $class, $req, %info ) = @_;
Subroutine auditLog: called CC=1 SCAR=0.0
Pod undocumented
311 33 $class->_auditLogger->log( $req, %info );
312 }
313  
314 ## @rmethod protected boolean checkMaintenanceMode
315 # Check if we are in maintenance mode
316 # @return true if maintenance mode is enabled
317 sub checkMaintenanceMode {
318 121 ▶ my ( $class, $req ) = @_;
Subroutine checkMaintenanceMode: called CC=3 SCAR=11.6
Pod undocumented
319 121 my $vhost = $class->resolveAlias($req);
320 my $_maintenance =
321 ( defined $class->tsv->{maintenance}->{$vhost} )
322 ? $class->tsv->{maintenance}->{$vhost}
323 121 ▶ : $class->tsv->{maintenance}->{_};
Branch: 1 branch
BranchTrueFalseTotal
defined $class->tsv->{'maintenance'}{$vhost} ? : 24 97 121
324  
325 121 ▶ if ($_maintenance) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_maintenance) 0 121 121
326 0 $class->logger->debug("Maintenance mode enabled");
327 0 return 1;
328 }
329 121 return 0;
330 }
331  
332 ## @rmethod int getLevel(string uri, string $vhost)
333 # Return required authentication level for this URI
334 # default to vhost authentication level
335 # @param $uri URI
336 # @param $vhost vhost name, default to current request
337 sub getLevel {
338 82 ▶ my ( $class, $req, $uri, $vhost ) = @_;
Subroutine getLevel: called CC=7 SCAR=19.6
Pod undocumented
339 82 my $level;
340 82 ▶ $vhost ||= $class->resolveAlias($req);
Condition33%1/3
$vhost ||= $class->resolveAlias($req)
l!l&&r!l&&!r
0820
MC/DC0%0/2
$vhost ||= $class->resolveAlias($req)
$vhost $class->resolveAlias($req)
Truth table33%1/3
A ||= B
A= $vhostB= $class->resolveAlias($req)
ABresult
1X1
011
000
341  
342 # Using URL authentification level if exists
343 82 ▶ for (
Branch: 1 branch
BranchTrueFalseTotal
if ($i < ($class->tsv->{"locationCount"}{$vhost} || 0)) 396 34 430
Condition100%2/2
$class->tsv->{'locationCount'}{$vhost} || 0
l!l
42010
MC/DC100%1/1
$class->tsv->{'locationCount'}{$vhost} || 0
$class->tsv->{'locationCount'}{$vhost}
Truth table100%2/2
A
A= $class->tsv->{'locationCount'}{$vhost}
Aresult
11
00
344 my $i = 0 ;
345 $i < ( $class->tsv->{locationCount}->{$vhost} || 0 ) ;
346 $i++
347 )
348 {
349 396 ▶ if ( $uri && $uri =~ $class->tsv->{locationRegexp}->{$vhost}->[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri and $uri =~ /$class->tsv->{"locationRegexp"}{$vhost}[$i]/) 48 348 396
Condition66%2/3
$uri and $uri =~ /$class->tsv->{"locationRegexp"}{$vhost}[$i]/
!ll&&!rl&&r
034848
MC/DC50%1/2
$uri and $uri =~ /$class->tsv->{"locationRegexp"}{$vhost}[$i]/
$uri $uri =~ /$class->tsv->{"locationRegexp"}{$vhost}[$i]/
Truth table66%2/3
A and B
A= $uriB= $uri =~ /$class->tsv->{"locationRegexp"}{$vhost}[$i]/
ABresult
0X0
100
111
350 48 $level = $class->tsv->{locationAuthnLevel}->{$vhost}->[$i];
351 48 last;
352 }
353 }
354 82 ▶ if ($level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level) { } 12 70 82
355 12 $class->logger->debug("Found AuthnLevel=$level for \"$vhost$uri\"");
356 12 return $level;
357 }
358 else {
359 70 $class->logger->debug("No URL authentication level found...");
360 70 return $class->tsv->{authnLevel}->{$vhost};
361 }
362 }
363  
364 ## @rmethod boolean grant(string uri, string cond)
365 # Grant or refuse client using compiled regexp and functions
366 # @param $uri URI
367 # @param $cond optional Function granting access
368 # @return True if the user is granted access to the current URL
369 sub grant {
370 82 ▶ my ( $class, $req, $session, $uri, $cond, $vhost ) = @_;
Subroutine grant: called CC=10 SCAR=23.1
Pod undocumented
371  
372 82 ▶ return $cond->( $req, $session ) if $cond;
Branch: 1 branch
BranchTrueFalseTotal
if $cond 0 82 82
373  
374 82 ▶ $vhost ||= $class->resolveAlias($req);
Condition33%1/3
$vhost ||= $class->resolveAlias($req)
l!l&&r!l&&!r
0820
MC/DC0%0/2
$vhost ||= $class->resolveAlias($req)
$vhost $class->resolveAlias($req)
Truth table33%1/3
A ||= B
A= $vhostB= $class->resolveAlias($req)
ABresult
1X1
011
000
375 82 my $level = $class->getLevel( $req, $uri );
376  
377 # Using VH authentification level if exists
378 82 ▶ if ($level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level) 16 66 82
379 16 ▶ if ( $session->{authenticationLevel} < $level ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->{"authenticationLevel"} < $level) 8 8 16
380 8 $class->logger->debug(
381 "User authentication level = $session->{authenticationLevel}");
382 8 $class->logger->debug("Required authentication level = $level");
383 8 $class->logger->warn(
384 'User rejected due to insufficient authentication level');
385 8 ▶ if ( $class->tsv->{upgradeSession} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->tsv->{"upgradeSession"}) 8 0 8
386 8 $class->logger->warn(' -> Session upgrade enabled');
387 8 $session->{_upgrade} = 1;
388 }
389 8 return 0;
390 }
391 }
392 74 ▶ for (
Branch: 1 branch
BranchTrueFalseTotal
if ($i < ($class->tsv->{"locationCount"}{$vhost} || 0)) 384 30 414
Condition100%2/2
$class->tsv->{'locationCount'}{$vhost} || 0
l!l
40410
MC/DC100%1/1
$class->tsv->{'locationCount'}{$vhost} || 0
$class->tsv->{'locationCount'}{$vhost}
Truth table100%2/2
A
A= $class->tsv->{'locationCount'}{$vhost}
Aresult
11
00
393 my $i = 0 ;
394 $i < ( $class->tsv->{locationCount}->{$vhost} || 0 ) ;
395 $i++
396 )
397 {
398 384 ▶ if ( $uri =~ $class->tsv->{locationRegexp}->{$vhost}->[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ /$class->tsv->{"locationRegexp"}{$vhost}[$i]/) 44 340 384
399 $class->logger->debug( 'Regexp "'
400 44 . $class->tsv->{locationConditionText}->{$vhost}->[$i]
401 . '" match' );
402 44 return $class->tsv->{locationCondition}->{$vhost}->[$i]
403 ->( $req, $session );
404 }
405 }
406 30 ▶ unless ( $class->tsv->{defaultCondition}->{$vhost} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->tsv->{"defaultCondition"}{$vhost}) 1 29 30
407 1 $class->logger->warn(
408 "User rejected because VirtualHost \"$vhost\" has no configuration"
409 );
410 1 return 0;
411 }
412 29 $class->logger->debug("$vhost: Apply default rule");
413 29 return $class->tsv->{defaultCondition}->{$vhost}->( $req, $session );
414 }
415  
416 ## @rmethod protected int forbidden(string uri)
417 # Used to reject non authorized requests.
418 # Inform the status process and call logForbidden().
419 # @param $uri URI
420 # @return Constant $class->FORBIDDEN
421 sub forbidden {
422 32 ▶ my ( $class, $req, $session, $vhost ) = @_;
Subroutine forbidden: called CC=7 SCAR=22.1
Pod undocumented
423 32 my $uri = $req->{env}->{REQUEST_URI};
424 32 my $portal = $class->tsv->{portal}->($req);
425 32 $portal = ( $portal =~ m#^https?://([^/]*).*# )[0];
426 32 $portal =~ s/:\d+$//;
427 32 ▶ $vhost ||= $class->resolveAlias($req);
Condition33%1/3
$vhost ||= $class->resolveAlias($req)
l!l&&r!l&&!r
0320
MC/DC0%0/2
$vhost ||= $class->resolveAlias($req)
$vhost $class->resolveAlias($req)
Truth table33%1/3
A ||= B
A= $vhostB= $class->resolveAlias($req)
ABresult
1X1
011
000
428  
429 32 ▶ if ( $session->{_logout} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->{"_logout"}) 0 32 32
430 $class->updateStatus( $req, 'LOGOUT',
431 0 $session->{ $class->tsv->{whatToTrace} } );
432 0 my $u = $session->{_logout};
433 0 $class->localUnlog($req);
434 0 return $class->goToPortal( $req, $u, 'logout=1' );
435 }
436  
437 32 ▶ if ( $session->{_upgrade} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->{"_upgrade"}) 8 24 32
438 8 $class->localUnlog($req);
439 8 return $class->goToPortal( $req, $uri, undef, '/upgradesession' );
440 }
441  
442 # Log forbidding
443 $class->auditLog(
444 $req,
445 message => (
446 "User "
447 . $session->{ $class->tsv->{whatToTrace} }
448 . " was forbidden access to $vhost$uri"
449 ),
450 code => "ACCESS_REFUSED",
451 user => $session->{ $class->tsv->{whatToTrace} },
452  
453 24 );
454 $class->updateStatus( $req, 'REJECT',
455 24 $session->{ $class->tsv->{whatToTrace} } );
456 24 $class->localUnlog($req);
457  
458 # Redirect or Forbidden?
459 24 ▶ if ( $class->tsv->{useRedirectOnForbidden} && $vhost ne $portal ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->tsv->{'useRedirectOnForbidden'} and $vhost ne $portal) { } 0 24 24
Condition33%1/3
$class->tsv->{"useRedirectOnForbidden"} and $vhost ne $portal
!ll&&!rl&&r
2400
MC/DC0%0/2
$class->tsv->{"useRedirectOnForbidden"} and $vhost ne $portal
$class->tsv->{"useRedirectOnForbidden"} $vhost ne $portal
Truth table33%1/3
A and B
A= $class->tsv->{"useRedirectOnForbidden"}B= $vhost ne $portal
ABresult
0X0
100
111
460 0 $class->logger->debug("Use redirect for forbidden access ");
461 0 return $class->goToError( $req, $uri, 403 );
462 }
463 else {
464 24 ▶ $class->logger->debug("Self protected Portal URL") if $vhost eq $portal;
Branch: 1 branch
BranchTrueFalseTotal
if $vhost eq $portal 0 24 24
465 24 $class->logger->debug("Return forbidden access");
466 24 return $class->FORBIDDEN;
467 }
468 }
469  
470 ## @rmethod protected void hideCookie()
471 # Hide Lemonldap::NG cookie to the protected application.
472 sub hideCookie {
473 49 ▶ my ( $class, $req ) = @_;
Subroutine hideCookie: called CC=3 SCAR=11.0
Pod undocumented
474 49 $class->logger->debug("removing cookie");
475 49 ▶ my $cookie = $req->env->{HTTP_COOKIE} || '';
Condition100%2/2
$req->env->{'HTTP_COOKIE'} || ''
l!l
3712
MC/DC100%1/1
$req->env->{'HTTP_COOKIE'} || ''
$req->env->{'HTTP_COOKIE'}
Truth table100%2/2
A
A= $req->env->{'HTTP_COOKIE'}
Aresult
11
00
476 49 $class->logger->debug("Cookies -> $cookie");
477 49 my $cn = $class->tsv->{cookieName};
478 49 $class->logger->debug("CookieName -> $cn");
479 49 $cookie =~ s/\b$cn(http)?=[^,;]*[,;\s]*//og;
480 49 $class->logger->debug("newCookies -> $cookie");
481  
482 49 ▶ if ($cookie) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cookie) { } 1 48 49
483 1 $class->set_header_in( $req, 'Cookie' => $cookie );
484 }
485 else {
486 48 $class->unset_header_in( $req, 'Cookie' );
487 }
488 }
489  
490 ## @rmethod protected string encodeUrl(string url)
491 # Encode URL in the format used by Lemonldap::NG::Portal for redirections.
492 # @return Base64 encoded string
493 sub encodeUrl {
494 41 ▶ my ( $class, $req, $url ) = @_;
Subroutine encodeUrl: called CC=2 SCAR=7.1
Pod undocumented
495 41 ▶ $url = $class->_buildUrl( $req, $url ) if ( $url !~ m#^https?://# );
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ m[^https?://] 41 0 41
496 41 return uri_escape( encode_base64( $url, '' ) );
497 }
498  
499 ## @rmethod protected int goToPortal(string url, string arg)
500 # Redirect non-authenticated users to the portal by setting "Location:" header.
501 # @param $url Url requested
502 # @param $arg optionnal GET parameters
503 # @return Constant $class->REDIRECT
504 sub goToPortal {
505 42 ▶ my ( $class, $req, $url, $arg, $path ) = @_;
Subroutine goToPortal: called CC=5 SCAR=16.1
Pod undocumented
506  
507 42 my $portal = $class->tsv->{portal}->($req);
508 42 ▶ if ( !$class->tsv->{useRedirectAjaxOnUnauthorized} and $req->wantJSON ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $class->tsv->{"useRedirectAjaxOnUnauthorized"} and $req->wantJSON) 1 41 42
Condition100%3/3
not $class->tsv->{"useRedirectAjaxOnUnauthorized"} and $req->wantJSON
!ll&&!rl&&r
3921
MC/DC100%2/2
not $class->tsv->{"useRedirectAjaxOnUnauthorized"} and $req->wantJSON
not $class->tsv->{"useRedirectAjaxOnUnauthorized"} $req->wantJSON
Truth table100%3/3
not(A) and B
A= not $class->tsv->{"useRedirectAjaxOnUnauthorized"}B= $req->wantJSON
ABresult
0X0
100
111
509 1 $class->set_header_out( $req, 'WWW-Authenticate' => "SSO $portal" );
510 1 return $class->AUTH_REQUIRED;
511 }
512 41 ▶ $path ||= '';
Condition100%2/2
$path ||= ""
l!l
833
MC/DC100%1/1
$path ||= ""
$path
Truth table100%2/2
A
A= $path
Aresult
11
00
513 41 my ( $ret, $msg );
514 41 my $urlc_init = $class->encodeUrl( $req, $url );
515 41 $class->logger->debug(
516 'Redirect ' . $req->address . " to portal (url was $url)" );
517 41 ▶ $class->set_header_out( $req,
Branch: 1 branch
BranchTrueFalseTotal
$arg ? : 0 41 41
518 'Location' => $portal
519 . "$path?url=$urlc_init"
520 . ( $arg ? "&$arg" : "" ) );
521 41 return $class->REDIRECT;
522 }
523  
524 sub goToError {
525 0 ▶ my ( $class, $req, $url, $code ) = @_;
Subroutine goToError: not called CC=1 SCAR=6.9
Pod undocumented
526 0 my $urlc_init = $class->encodeUrl( $req, $url );
527 0 $class->logger->debug(
528 "Redirect $req->{env}->{REMOTE_ADDR} to lmError (url was $url)");
529 $class->set_header_out( $req,
530 0 'Location' => $class->tsv->{portal}->($req)
531 . "/lmerror/$code"
532 . "?url=$urlc_init" );
533 0 return $class->REDIRECT;
534 }
535  
536 ## @rmethod protected fetchId()
537 # Get user cookies and search for Lemonldap::NG cookie.
538 # @return Value of the cookie if found, 0 else
539 sub fetchId {
540 128 ▶ my ( $class, $req ) = @_;
Subroutine fetchId: called CC=13 SCAR=33.6
Pod undocumented
541 128 ▶ my $t = $req->{env}->{HTTP_COOKIE} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $req->{"env"}{"HTTP_COOKIE"} or return 0 20 108 128
542 108 my $vhost = $class->resolveAlias($req);
543 108 ▶ $class->logger->debug("VH $vhost is HTTPS")
Branch: 1 branch
BranchTrueFalseTotal
if $class->_isHttps($req, $vhost) 1 107 108
544 if $class->_isHttps( $req, $vhost );
545 108 ▶ my $lookForHttpCookie = ( $class->tsv->{securedCookie} =~ /^(2|3)$/
Condition66%2/3
$class->tsv->{'securedCookie'} =~ /^(2|3)$/ && !$class->_isHttps($req, $vhost)
!ll&&!rl&&r
10710
MC/DC0%0/2
$class->tsv->{'securedCookie'} =~ /^(2|3)$/ && !$class->_isHttps($req, $vhost)
$class->tsv->{'securedCookie'} =~ /^(2|3)$/ !$class->_isHttps($req, $vhost)
Truth table66%2/3
A && not(B)
A= $class->tsv->{'securedCookie'} =~ /^(2|3)$/B= !$class->_isHttps($req, $vhost)
ABresult
0X0
100
111
546 and not $class->_isHttps( $req, $vhost ) );
547 108 my $cn = $class->tsv->{cookieName};
548 108 ▶ my $value = $lookForHttpCookie # Avoid prefix and bad cookie name (#2417)
Branch: 3 branches
BranchTrueFalseTotal
$lookForHttpCookie ? : 0 108 108
$t =~ /(?<![-.~])\b${cn}http=([^,; ]+)/ ? : 0 0 0
$t =~ /(?<![-.~])\b$cn=([^,; ]+)/ ? : 103 5 108
549 ? ( $t =~ /(?<![-.~])\b${cn}http=([^,; ]+)/ ? $1 : 0 )
550 : ( $t =~ /(?<![-.~])\b$cn=([^,; ]+)/ ? $1 : 0 );
551  
552 108 ▶ if ( $value && $lookForHttpCookie && $class->tsv->{securedCookie} == 3 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value and $lookForHttpCookie and $class->tsv->{'securedCookie'} == 3) { } 0 108 108
elsif ($value =~ s/^c://) { } 0 108 108
Condition50%3/6
$value and $lookForHttpCookie and $class->tsv->{"securedCookie"} == 3
!ll&&!rl&&r
10800
$value and $lookForHttpCookie
!ll&&!rl&&r
51030
MC/DC0%0/3
$value and $lookForHttpCookie and $class->tsv->{"securedCookie"} == 3
$value $lookForHttpCookie $class->tsv->{"securedCookie"} == 3
Truth table50%2/4
A and B and C
A= $valueB= $lookForHttpCookieC= $class->tsv->{"securedCookie"} == 3
ABCresult
0XX0
10X0
1100
1111
553 0 $value = $class->tsv->{cipher}->decryptHex( $value, "http" );
554 }
555 elsif ( $value =~ s/^c:// ) {
556 0 $value = $class->tsv->{cipher}->decrypt($value);
557 0 ▶ unless ( $value =~ s/^(.*)? (.*)$/$1/ and $2 eq $vhost ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ s/^(.*)? (.*)$/$1/ and $2 eq $vhost) 0 0 0
Condition0%0/3
$value =~ s/^(.*)? (.*)$/$1/ and $2 eq $vhost
!ll&&!rl&&r
000
MC/DC0%0/2
$value =~ s/^(.*)? (.*)$/$1/ and $2 eq $vhost
$value =~ s/^(.*)? (.*)$/$1/ $2 eq $vhost
Truth table0%0/3
A and B
A= $value =~ s/^(.*)? (.*)$/$1/B= $2 eq $vhost
ABresult
0X0
100
111
558 0 $class->auditLog(
559 $req,
560 message =>
561 ("Bad CDA cookie: available for $2 instead of $vhost"),
562 code => "CDA_BAD_HOST",
563 vhost => $vhost,
564 cda_vhost => $2,
565 );
566 0 return undef;
567 }
568 }
569 108 return $value;
570 }
571  
572 ## @rmethod protected boolean retrieveSession(id)
573 # Tries to retrieve the session whose index is id
574 # @return true if the session was found, false else
575 sub retrieveSession {
576 82 ▶ my ( $class, $req, $id ) = @_;
Subroutine retrieveSession: called CC=18 SCAR=35.0
Pod undocumented
577 82 my $now = time();
578  
579 # 1. Search if the user was the same as previous (very efficient in
580 # persistent connection).
581 # NB: timout is here the same value as current HTTP/1.1 Keep-Alive timeout
582 # (15 seconds)
583 82 ▶ if ( defined $class->data->{_session_id}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class->data->{'_session_id'} and $id eq $class->data->{'_session_id'} and $now - $class->dataUpdate < $class->tsv->{'handlerInternalCache'}) { } 33 49 82
Condition83%5/6
defined $class->data->{"_session_id"} and $id eq $class->data->{"_session_id"} and $now - $class->dataUpdate < $class->tsv->{"handlerInternalCache"}
!ll&&!rl&&r
47233
defined $class->data->{"_session_id"} and $id eq $class->data->{"_session_id"}
!ll&&!rl&&r
47035
MC/DC66%2/3
defined $class->data->{"_session_id"} and $id eq $class->data->{"_session_id"} and $now - $class->dataUpdate < $class->tsv->{"handlerInternalCache"}
defined $class->data->{"_session_id"} $id eq $class->data->{"_session_id"} $now - $class->dataUpdate < $class->tsv->{"handlerInternalCache"}
Truth table75%3/4
A and B and C
A= defined $class->data->{"_session_id"}B= $id eq $class->data->{"_session_id"}C= $now - $class->dataUpdate < $class->tsv->{"handlerInternalCache"}
ABCresult
0XX0
10X0
1100
1111
584 and $id eq $class->data->{_session_id}
585 and ( $now - $class->dataUpdate < $class->tsv->{handlerInternalCache} )
586 )
587 {
588 33 $class->logger->debug("Get session $id from Handler internal cache");
589 33 return $class->data;
590 }
591 else {
592 49 $class->data( {} );
593 }
594  
595 # 2. Get the session from cache or backend
596 my $session = $req->data->{session} = (
597 Lemonldap::NG::Common::Session->new( {
598 hashStore => $class->tsv->{hashedSessionStore},
599 storageModule => $class->tsv->{sessionStorageModule},
600 storageModuleOptions => $class->tsv->{sessionStorageOptions},
601 cacheModule => $class->tsv->{sessionCacheModule},
602 cacheModuleOptions => $class->tsv->{sessionCacheOptions},
603 49 id => $id,
604 kind => "SSO",
605 }
606 )
607 );
608  
609 49 ▶ unless ( $session->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->error) { } 5 44 49
610  
611 44 $class->data( $session->data );
612 44 $class->logger->debug("Get session $id from Handler::Main::Run");
613  
614 # Verify that session is valid
615 $class->logger->error(
616 "_utime is not defined. This should not happen. Check if it is well transmitted to handler"
617 44 ▶ ) unless $session->data->{_utime};
Branch: 1 branch
BranchTrueFalseTotal
unless $session->data->{"_utime"} 0 44 44
618  
619 44 $class->logger->debug("Check session validity from Handler");
620 44 $class->logger->debug( "Session timeout -> " . $class->tsv->{timeout} );
621 $class->logger->debug( "Session timeoutActivity -> "
622 . $class->tsv->{timeoutActivity}
623 . "s" )
624 44 ▶ if ( $class->tsv->{timeoutActivity} );
Branch: 1 branch
BranchTrueFalseTotal
if $class->tsv->{"timeoutActivity"} 0 44 44
625 $class->logger->debug(
626 44 "Session _utime -> " . $session->data->{_utime} );
627 44 $class->logger->debug( "now -> " . $now );
628 $class->logger->debug( "_lastSeen -> " . $session->data->{_lastSeen} )
629 44 ▶ if ( $session->data->{_lastSeen} );
Branch: 1 branch
BranchTrueFalseTotal
if $session->data->{"_lastSeen"} 0 44 44
630 my $delta = $now - $session->data->{_lastSeen}
631 44 ▶ if ( $session->data->{_lastSeen} );
Branch: 1 branch
BranchTrueFalseTotal
if $session->data->{"_lastSeen"} 0 44 44
632 $class->logger->debug( "now - _lastSeen = " . $delta )
633 44 ▶ if ( $session->data->{_lastSeen} );
Branch: 1 branch
BranchTrueFalseTotal
if $session->data->{"_lastSeen"} 0 44 44
634 $class->logger->debug( "Session timeoutActivityInterval -> "
635 . $class->tsv->{timeoutActivityInterval} )
636 44 ▶ if ( $class->tsv->{timeoutActivityInterval} );
Branch: 1 branch
BranchTrueFalseTotal
if $class->tsv->{"timeoutActivityInterval"} 44 0 44
637 44 my $ttl = $class->tsv->{timeout} - $now + $session->data->{_utime};
638 44 $class->logger->debug( "Session TTL = " . $ttl );
639  
640 44 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($now - $session->data->{"_utime"} > $class->tsv->{"timeout"} or $class->tsv->{"timeoutActivity"} and $session->data->{"_lastSeen"} and $delta > $class->tsv->{"timeoutActivity"}) 0 44 44
Condition33%3/9
$now - $session->data->{"_utime"} > $class->tsv->{"timeout"} or $class->tsv->{"timeoutActivity"} and $session->data->{"_lastSeen"} and $delta > $class->tsv->{"timeoutActivity"}
l!l&&r!l&&!r
0044
$class->tsv->{"timeoutActivity"} and $session->data->{"_lastSeen"} and $delta > $class->tsv->{"timeoutActivity"}
!ll&&!rl&&r
4400
$class->tsv->{"timeoutActivity"} and $session->data->{"_lastSeen"}
!ll&&!rl&&r
4400
MC/DC0%0/4
$now - $session->data->{"_utime"} > $class->tsv->{"timeout"} or $class->tsv->{"timeoutActivity"} and $session->data->{"_lastSeen"} and $delta > $class->tsv->{"timeoutActivity"}
$now - $session->data->{"_utime"} > $class->tsv->{"timeout"} $class->tsv->{"timeoutActivity"} $session->data->{"_lastSeen"} $delta > $class->tsv->{"timeoutActivity"}
Truth table0%0/5
A or B and C and D
A= $now - $session->data->{"_utime"} > $class->tsv->{"timeout"}B= $class->tsv->{"timeoutActivity"}C= $session->data->{"_lastSeen"}D= $delta > $class->tsv->{"timeoutActivity"}
ABCDresult
1XXX1
01111
00XX0
010X0
01100
641 $now - $session->data->{_utime} > $class->tsv->{timeout}
642 or ( $class->tsv->{timeoutActivity}
643 and $session->data->{_lastSeen}
644 and $delta > $class->tsv->{timeoutActivity} )
645 )
646 {
647 0 $class->logger->info("Session $id expired");
648  
649 # Clean cached data
650 0 $class->data( {} );
651 0 return 0;
652 }
653  
654 # Update the session to notify activity, if necessary
655 44 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($class->tsv->{"timeoutActivity"} and $now - $session->data->{"_lastSeen"} > $class->tsv->{"timeoutActivityInterval"}) 0 44 44
Condition33%1/3
$class->tsv->{"timeoutActivity"} and $now - $session->data->{"_lastSeen"} > $class->tsv->{"timeoutActivityInterval"}
!ll&&!rl&&r
4400
MC/DC0%0/2
$class->tsv->{"timeoutActivity"} and $now - $session->data->{"_lastSeen"} > $class->tsv->{"timeoutActivityInterval"}
$class->tsv->{"timeoutActivity"} $now - $session->data->{"_lastSeen"} > $class->tsv->{"timeoutActivityInterval"}
Truth table33%1/3
A and B
A= $class->tsv->{"timeoutActivity"}B= $now - $session->data->{"_lastSeen"} > $class->tsv->{"timeoutActivityInterval"}
ABresult
0X0
100
111
656 $class->tsv->{timeoutActivity}
657 and ( $now - $session->data->{_lastSeen} >
658 $class->tsv->{timeoutActivityInterval} )
659 )
660 {
661 0 $req->data->{session}->update( { '_lastSeen' => $now } );
662 0 $class->data( $session->data );
663  
664 0 ▶ if ( $session->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->error) { } 0 0 0
665 0 $class->logger->error("Cannot update session $id");
666 0 $class->logger->error( $req->data->{session}->error );
667 }
668 else {
669 0 $class->logger->debug("Update _lastSeen with $now");
670 }
671 }
672  
673 44 $class->dataUpdate($now);
674 44 return $session->data;
675 }
676 else {
677 5 $class->logger->info("Session $id can't be retrieved");
678 5 $class->logger->info( $session->error );
679  
680 5 return 0;
681 }
682 }
683  
684 ## @cmethod private int _getPort(string s)
685 # Returns the port on which this vhost is accessed
686 # @param $s VHost name
687 # @return PORT
688 sub _getPort {
689 41 ▶ my ( $class, $req, $vhost ) = @_;
Subroutine _getPort: called CC=5 SCAR=17.2
690 41 ▶ if ( defined $class->tsv->{port}->{$vhost}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class->tsv->{'port'}{$vhost} and $class->tsv->{'port'}{$vhost} > 0) { } 9 32 41
Condition66%2/3
defined $class->tsv->{"port"}{$vhost} and $class->tsv->{"port"}{$vhost} > 0
!ll&&!rl&&r
3209
MC/DC50%1/2
defined $class->tsv->{"port"}{$vhost} and $class->tsv->{"port"}{$vhost} > 0
defined $class->tsv->{"port"}{$vhost} $class->tsv->{"port"}{$vhost} > 0
Truth table66%2/3
A and B
A= defined $class->tsv->{"port"}{$vhost}B= $class->tsv->{"port"}{$vhost} > 0
ABresult
0X0
100
111
691 and ( $class->tsv->{port}->{$vhost} > 0 ) )
692 {
693 9 return $class->tsv->{port}->{$vhost};
694 }
695 else {
696 return ( defined $class->tsv->{port}->{_}
697 and ( $class->tsv->{port}->{_} > 0 ) )
698 ? $class->tsv->{port}->{_}
699 32 ▶ : $req->port;
Branch: 1 branch
BranchTrueFalseTotal
defined $class->tsv->{'port'}{'_'} && $class->tsv->{'port'}{'_'} > 0 ? : 0 32 32
Condition33%1/3
defined $class->tsv->{"port"}{"_"} and $class->tsv->{"port"}{"_"} > 0
!ll&&!rl&&r
0320
MC/DC0%0/2
defined $class->tsv->{"port"}{"_"} and $class->tsv->{"port"}{"_"} > 0
defined $class->tsv->{"port"}{"_"} $class->tsv->{"port"}{"_"} > 0
Truth table33%1/3
A and B
A= defined $class->tsv->{"port"}{"_"}B= $class->tsv->{"port"}{"_"} > 0
ABresult
0X0
100
111
700 }
701 }
702  
703 ## @cmethod private bool _isHttps(string s)
704 # Returns whether this VHost should be accessed
705 # via HTTPS
706 # @param $s VHost name
707 # @return TRUE if the vhost should be accessed over HTTPS
708 sub _isHttps {
709 150 ▶ my ( $class, $req, $vhost ) = @_;
Subroutine _isHttps: called CC=5 SCAR=16.2
710 150 ▶ if ( defined $class->tsv->{https}->{$vhost}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class->tsv->{'https'}{$vhost} and $class->tsv->{'https'}{$vhost} > -1) { } 11 139 150
Condition66%2/3
defined $class->tsv->{"https"}{$vhost} and $class->tsv->{"https"}{$vhost} > -1
!ll&&!rl&&r
139011
MC/DC50%1/2
defined $class->tsv->{"https"}{$vhost} and $class->tsv->{"https"}{$vhost} > -1
defined $class->tsv->{"https"}{$vhost} $class->tsv->{"https"}{$vhost} > -1
Truth table66%2/3
A and B
A= defined $class->tsv->{"https"}{$vhost}B= $class->tsv->{"https"}{$vhost} > -1
ABresult
0X0
100
111
711 and ( $class->tsv->{https}->{$vhost} > -1 ) )
712 {
713 11 return $class->tsv->{https}->{$vhost};
714 }
715 else {
716 return ( defined $class->tsv->{https}->{_}
717 and ( $class->tsv->{https}->{_} > -1 ) )
718 ? $class->tsv->{https}->{_}
719 139 ▶ : $req->secure;
Branch: 1 branch
BranchTrueFalseTotal
defined $class->tsv->{'https'}{'_'} && $class->tsv->{'https'}{'_'} > -1 ? : 138 1 139
Condition66%2/3
defined $class->tsv->{"https"}{"_"} and $class->tsv->{"https"}{"_"} > -1
!ll&&!rl&&r
01138
MC/DC50%1/2
defined $class->tsv->{"https"}{"_"} and $class->tsv->{"https"}{"_"} > -1
defined $class->tsv->{"https"}{"_"} $class->tsv->{"https"}{"_"} > -1
Truth table66%2/3
A and B
A= defined $class->tsv->{"https"}{"_"}B= $class->tsv->{"https"}{"_"} > -1
ABresult
0X0
100
111
720 }
721 }
722  
723 ## @cmethod private string _buildUrl(string s)
724 # Transform /<s> into http(s?)://<host>:<port>/s
725 # @param $s path
726 # @return URL
727 sub _buildUrl {
728 41 ▶ my ( $class, $req, $s ) = @_;
Subroutine _buildUrl: called CC=7 SCAR=19.5
729 41 my $realvhost = $req->{env}->{HTTP_HOST};
730 41 my $vhost = $class->resolveAlias($req);
731 41 my $_https = $class->_isHttps( $req, $vhost );
732 41 my $portString = $class->_getPort( $req, $vhost );
733 41 ▶ $portString = (
Branch: 1 branch
BranchTrueFalseTotal
$realvhost =~ /:\d+/ || $_https && $portString == 443 || !$_https && $portString == 80 ? : 39 2 41
Condition83%10/12
$realvhost =~ /:\d+/ or $_https and $portString == 443 or not $_https and $portString == 80
l!l&&r!l&&!r
2372
$realvhost =~ /:\d+/ or $_https and $portString == 443
l!l&&r!l&&!r
0239
$_https and $portString == 443
!ll&&!rl&&r
3722
not $_https and $portString == 80
!ll&&!rl&&r
2037
MC/DC20%1/5
$realvhost =~ /:\d+/ or $_https and $portString == 443 or not $_https and $portString == 80
$realvhost =~ /:\d+/ $_https $portString == 443 not $_https $portString == 80
Truth table37%3/8
A or B and C or not(D) and E
A= $realvhost =~ /:\d+/B= $_httpsC= $portString == 443D= not $_httpsE= $portString == 80
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
734 ( $realvhost =~ /:\d+/ )
735 or ( $_https && $portString == 443 )
736 or ( !$_https && $portString == 80 )
737 ) ? '' : ":$portString";
738 41 ▶ my $url = "http" . ( $_https ? "s" : "" ) . "://$realvhost$portString$s";
Branch: 1 branch
BranchTrueFalseTotal
$_https ? : 4 37 41
739 41 $class->logger->debug("Build URL $url");
740 41 return URI->new($url)->as_string;
741 }
742  
743 ## @rmethod protected int isUnprotected()
744 # @param $uri URI
745 # @return 0 if URI is protected,
746 # $class->UNPROTECT if it is unprotected by "unprotect",
747 # SKIP if unprotected by "skip"
748 sub isUnprotected {
749 119 ▶ my ( $class, $req, $uri ) = @_;
Subroutine isUnprotected: called CC=4 SCAR=13.9
Pod undocumented
750 119 my $vhost = $class->resolveAlias($req);
751 119 ▶ for (
Branch: 1 branch
BranchTrueFalseTotal
if ($i < ($class->tsv->{"locationCount"}{$vhost} || 0)) 623 68 691
Condition100%2/2
$class->tsv->{'locationCount'}{$vhost} || 0
l!l
67714
MC/DC100%1/1
$class->tsv->{'locationCount'}{$vhost} || 0
$class->tsv->{'locationCount'}{$vhost}
Truth table100%2/2
A
A= $class->tsv->{'locationCount'}{$vhost}
Aresult
11
00
752 my $i = 0 ;
753 $i < ( $class->tsv->{locationCount}->{$vhost} || 0 ) ;
754 $i++
755 )
756 {
757 return $class->tsv->{locationProtection}->{$vhost}->[$i]
758 623 ▶ if ( $uri =~ $class->tsv->{locationRegexp}->{$vhost}->[$i] );
Branch: 1 branch
BranchTrueFalseTotal
if $uri =~ /$class->tsv->{"locationRegexp"}{$vhost}[$i]/ 51 572 623
759 }
760 68 return $class->tsv->{defaultProtection}->{$vhost};
761 }
762  
763 ## @rmethod void sendHeaders()
764 # Launch function compiled by headersInit() for the current virtual host
765 sub sendHeaders {
766 46 ▶ my ( $class, $req, $session ) = @_;
Subroutine sendHeaders: called CC=4 SCAR=14.0
Pod undocumented
767 46 my $vhost = $class->resolveAlias($req);
768 46 ▶ if ( defined $class->tsv->{forgeHeaders}->{$vhost} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class->tsv->{"forgeHeaders"}{$vhost}) 35 11 46
769  
770 # Log headers in debug mode
771 my %headers =
772 35 $class->tsv->{forgeHeaders}->{$vhost}->( $req, $session );
773 35 foreach my $h ( sort keys %headers ) {
774 50 ▶ if ( defined( my $v = $headers{$h} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $v = $headers{$h})) { } 50 0 50
775 50 $class->logger->debug("Send header '$h' with value '$v'");
776 }
777 else {
778 0 $class->logger->debug("Send header '$h' with an empty value");
779 }
780 }
781 35 $class->set_header_in( $req, %headers );
782 }
783 }
784  
785 ## @rfunction array ref checkHeaders()
786 # Return computed headers by headersInit() for the current virtual host
787 # [ { key => 'header1', value => 'value1' }, { key => 'header2', value => 'value2' }, ...]
788 sub checkHeaders {
789 0 ▶ my ( $class, $req, $session ) = @_;
Subroutine checkHeaders: not called CC=4 SCAR=30.0
Pod undocumented
790 0 my $vhost = $class->resolveAlias($req);
791 0 my $array_headers = [];
792 0 ▶ if ( defined $class->tsv->{forgeHeaders}->{$vhost} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class->tsv->{"forgeHeaders"}{$vhost}) 0 0 0
793  
794 # Create array of hashes with headers
795 my %headers =
796 0 $class->tsv->{forgeHeaders}->{$vhost}->( $req, $session );
797 0 foreach my $h ( sort keys %headers ) {
798 defined $headers{$h}
799 0 ▶ ? push @$array_headers, { key => $h, value => $headers{$h} }
Branch: 1 branch
BranchTrueFalseTotal
defined $headers{$h} ? : 0 0 0
800 : push @$array_headers, { key => $h, value => '' };
801 }
802 }
803 0 return $array_headers;
804 }
805  
806 ## @rmethod void cleanHeaders()
807 # Unset HTTP headers, when sendHeaders is skipped
808 sub cleanHeaders {
809 3 ▶ my ( $class, $req ) = @_;
Subroutine cleanHeaders: called CC=2 SCAR=7.0
Pod undocumented
810 3 my $vhost = $class->resolveAlias($req);
811 3 ▶ if ( defined( $class->tsv->{headerList}->{$vhost} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class->tsv->{"headerList"}{$vhost}) 3 0 3
812 3 $class->logger->debug("Remove headers relative to $vhost");
813 $class->unset_header_in( $req,
814 3 @{ $class->tsv->{headerList}->{$vhost} } );
815 }
816 }
817  
818 ## @rmethod string resolveAlias
819 # returns vhost whose current hostname is an alias
820 sub resolveAlias {
821 989 ▶ my ( $class, $req ) = @_;
Subroutine resolveAlias: called CC=6 SCAR=18.0
Pod undocumented
822 989 ▶ my $vhost = ref $req ? $req->{env}->{HTTP_HOST} : $req;
Branch: 1 branch
BranchTrueFalseTotal
ref $req ? : 989 0 989
823  
824 989 $vhost =~ s/:\d+//;
825 return $class->tsv->{vhostAlias}->{$vhost}
826 989 ▶ if $class->tsv->{vhostAlias}->{$vhost};
Branch: 1 branch
BranchTrueFalseTotal
if $class->tsv->{"vhostAlias"}{$vhost} 0 989 989
827 989 ▶ return $vhost if $class->tsv->{defaultCondition}->{$vhost};
Branch: 1 branch
BranchTrueFalseTotal
if $class->tsv->{"defaultCondition"}{$vhost} 848 141 989
828 141 foreach ( @{ $class->tsv->{vhostReg} } ) {
829 428 ▶ return $_->[1] if $vhost =~ $_->[0];
Branch: 1 branch
BranchTrueFalseTotal
if $vhost =~ /$_->[0]/ 101 327 428
830 }
831 40 return $vhost;
832 }
833  
834 #__END__
835  
836 ## @rmethod int abort(string msg)
837 # Logs message and exit or redirect to the portal if "useRedirectOnError" is
838 # set to true.
839 # @param $msg Message to log
840 # @return Constant ($class->REDIRECT, $class->SERVER_ERROR)
841 sub abort {
842 0 ▶ my ( $class, $req, $msg ) = @_;
Subroutine abort: not called CC=3 SCAR=24.8
Pod undocumented
843  
844 # If abort is called without a valid request, fall to die
845 0 eval {
846 0 my $uri = $req->{env}->{REQUEST_URI};
847  
848 0 $class->logger->error($msg);
849  
850 # Redirect or die
851 0 ▶ if ( $class->tsv->{useRedirectOnError} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->tsv->{'useRedirectOnError'}) { } 0 0 0
852 0 $class->logger->debug("Use redirect for error");
853 0 return $class->goToError( $req, $uri, 500 );
854 }
855 else {
856 0 return $class->SERVER_ERROR;
857 }
858 };
859 0 ▶ die $msg if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
860 }
861  
862 ## @rmethod protected void localUnlog()
863 # Delete current user from local cache entry.
864 sub localUnlog {
865 32 ▶ my ( $class, $req, $id ) = @_;
Subroutine localUnlog: called CC=7 SCAR=31.4
Pod undocumented
866 32 $class->logger->debug('Local handler logout');
867  
868 # Delete thread data
869 32 delete $req->data->{session};
870 32 $class->data( {} );
871 32 ▶ if ( $id //= $class->fetchId($req) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id //= $class->fetchId($req)) 32 0 32
Condition33%1/3
$id //= $class->fetchId($req)
l!l&&r!l&&!r
0320
MC/DC0%0/2
$id //= $class->fetchId($req)
$id $class->fetchId($req)
Truth table33%1/3
A //= B
A= $idB= $class->fetchId($req)
ABresult
1X1
011
000
872  
873 # Delete local cache
874 32 ▶ if ( $class->tsv->{sessionCacheModule} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->tsv->{"sessionCacheModule"}) 0 32 32
875 0 my $module = $class->tsv->{sessionCacheModule};
876 0 my $options = $class->tsv->{sessionCacheOptions};
877 0 eval "use $module;";
878 0 my $cache = $module->new($options);
879 0 ▶ if ( $cache->get($id) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache->get($id)) 0 0 0
880 0 $cache->remove($id);
881 }
882  
883 # Modules like Apache::Session::REST stores hashed id in cache
884 0 ▶ if ( $class->tsv->{hashedSessionStore} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->tsv->{"hashedSessionStore"}) 0 0 0
885 0 $id = id2storage($id);
886 0 ▶ if ( $cache->get($id) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache->get($id)) 0 0 0
887 0 $cache->remove($id);
888 }
889 }
890 }
891 }
892 }
893  
894 ## @rmethod buildAndLoadType(string package)
895 # Try to load type or build it
896 26 my %alreadyBuilt;
897  
898 sub buildAndLoadType {
899 123 ▶ my ( $class, $package ) = @_;
Subroutine buildAndLoadType: called CC=6 SCAR=18.3
Pod undocumented
900 123 ▶ unless ( $package =~ /^[\w:]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($package =~ /^[\w:]+$/) 0 123 123
901 0 $class->logger->error("Bad clas name: $package");
902 0 return;
903 }
904 123 ▶ return if $alreadyBuilt{$package};
Branch: 1 branch
BranchTrueFalseTotal
if $alreadyBuilt{$package} 102 21 123
905 21 eval "require $package";
906 21 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 10 11 21
907 10 my $lastError = $@;
908 10 my $main = $package;
909 10 $main =~ s/::([^:]*)$/::Main/;
910 10 ▶ my $type = $1 or die "Malformed package $package";
Branch: 1 branch
BranchTrueFalseTotal
my $type = $1 or die "Malformed package $package" 0 10 10
911 10 ▶ eval <<EOF;
Subroutine BEGIN: called CC=1 SCAR=0.0
912 package $package;
913 use base 'Lemonldap::NG::Handler::Lib::$type',
914 '$main';
915 1;
916 EOF
917 10 ▶ die "Unable to build wrapper.\n First try: $lastError\n Auto-build: $@"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 10 10
918 if ($@);
919 }
920 21 $alreadyBuilt{$package} = 1;
921 }
922  
923 ## @rmethod protected postOutputFilter(string uri)
924 # Add a javascript to html page in order to fill html form with fake data
925 # @param uri URI to catch
926 sub postOutputFilter {
927 46 ▶ my ( $class, $req, $session, $uri ) = @_;
Subroutine postOutputFilter: called CC=2 SCAR=11.1
Pod undocumented
928 46 my $vhost = $class->resolveAlias($req);
929  
930 46 ▶ if ( defined( $class->tsv->{outputPostData}->{$vhost}->{$uri} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class->tsv->{"outputPostData"}{$vhost}{$uri}) 0 46 46
931 0 $class->logger->debug("Filling a html form with fake data");
932  
933 0 $class->unset_header_in( $req, "Accept-Encoding" );
934 my %postdata =
935 0 $class->tsv->{outputPostData}->{$vhost}->{$uri}->( $req, $session );
936 0 my $formParams = $class->tsv->{postFormParams}->{$vhost}->{$uri};
937 0 my $js = $class->postJavascript( $req, \%postdata, $formParams );
938 0 $class->addToHtmlHead( $req, $js );
939 }
940 }
941  
942 ## @rmethod protected postInputFilter(string uri)
943 # Replace request body with form data defined in configuration
944 # @param uri URI to catch
945 sub postInputFilter {
946 46 ▶ my ( $class, $req, $session, $uri ) = @_;
Subroutine postInputFilter: called CC=3 SCAR=18.4
Pod undocumented
947 46 my $vhost = $class->resolveAlias($req);
948  
949 46 ▶ if ( defined( $class->tsv->{inputPostData}->{$vhost}->{$uri} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class->tsv->{"inputPostData"}{$vhost}{$uri}) 0 46 46
950 0 $class->logger->debug("Replacing fake data with real form data");
951  
952 my %data =
953 0 $class->tsv->{inputPostData}->{$vhost}->{$uri}->( $req, $session );
954 0 foreach ( keys %data ) {
955 0 my $post_key = uri_escape($_);
956 0 my $post_value = uri_escape( $data{$_} );
957 0 delete $data{$_};
958 0 $data{$post_key} = $post_value;
959 0 $class->logger->debug("Send key $post_key with value $post_value");
960 }
961 0 $class->setPostParams( $req, \%data );
962 }
963 }
964  
965 ## @rmethod protected postJavascript(hashref data)
966 # build a javascript to fill a html form with fake data
967 # @param data hashref containing input => value
968 sub postJavascript {
969 0 ▶ my ( $class, $req, $data, $formParams ) = @_;
Subroutine postJavascript: not called CC=8 SCAR=39.7
Pod undocumented
970  
971 0 ▶ my $form = $formParams->{formSelector} || "form";
Condition0%0/2
$formParams->{'formSelector'} || 'form'
l!l
00
MC/DC0%0/1
$formParams->{'formSelector'} || 'form'
$formParams->{'formSelector'}
Truth table0%0/2
A
A= $formParams->{'formSelector'}
Aresult
11
00
972  
973 0 my $filler;
974 0 foreach my $name ( keys %$data ) {
975 26 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
976 0 my @characterSet = ( '0' .. '9', 'A' .. 'Z', 'a' .. 'z' );
977 my $value = join '' => map $characterSet[ rand @characterSet ],
978 0 1 .. bytes::length( $data->{$name} );
979 0 $filler .=
980 "form.find('input[name=\"$name\"], select[name=\"$name\"], textarea[name=\"$name\"]').val('$value')\n";
981 }
982  
983 my $submitter =
984 $formParams->{buttonSelector} eq "none" ? ""
985 : $formParams->{buttonSelector}
986 0 ▶ ? "form.find('$formParams->{buttonSelector}').click();\n"
Branch: 2 branches
BranchTrueFalseTotal
$formParams->{'buttonSelector'} eq 'none' ? : 0 0 0
$formParams->{'buttonSelector'} ? : 0 0 0
987 : "form.submit();\n";
988  
989 0 ▶ my $jqueryUrl = $formParams->{jqueryUrl} || "";
Condition0%0/2
$formParams->{'jqueryUrl'} || ''
l!l
00
MC/DC0%0/1
$formParams->{'jqueryUrl'} || ''
$formParams->{'jqueryUrl'}
Truth table0%0/2
A
A= $formParams->{'jqueryUrl'}
Aresult
11
00
990 $jqueryUrl =
991 0 ▶ $class->tsv->{portal}->($req) . "static/bwr/jquery/dist/jquery.min.js"
Branch: 1 branch
BranchTrueFalseTotal
if $jqueryUrl eq "default" 0 0 0
992 if ( $jqueryUrl eq "default" );
993 0 ▶ $jqueryUrl = "<script type='text/javascript' src='$jqueryUrl'></script>\n"
Branch: 1 branch
BranchTrueFalseTotal
if $jqueryUrl 0 0 0
994 if ($jqueryUrl);
995  
996 return
997 0 $jqueryUrl
998 . "<script type='text/javascript'>\n"
999 . "/* script added by Lemonldap::NG */\n"
1000 . "jQuery(window).on('load', function() {\n"
1001 . "var form = jQuery('$form');\n"
1002 . "form.attr('autocomplete', 'off');\n"
1003 . $filler
1004 . $submitter . "})\n"
1005 . "</script>\n";
1006 }
1007  
1008 26 1;
« Lemonldap/NG/Handler/Main/Reload.pm ↑ Summary Lemonldap/NG/Handler/Lib/DevOps.pm »