Lemonldap/NG/Handler/Main/Run.pm

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