blib/lib/Lemonldap/NG/Portal/Main/Run.pm

filter: Statementstmt 74.0% 480 / 648 Branchbran 58.9% 171 / 290 Conditioncond 50.9% 112 / 220 MC/DCmcdc 19.8% 24 / 121 Subroutinesub 83.7% 62 / 74 Podpod 5.0% 3 / 60 totaltotal 60.3% 852 / 1413 CC 237 SCAR 31.8
CC
237
Cov
66%
registerLogin
210.0
updatePersistentSession
72.0
do
38.0
CRAP 24.1
« blib/lib/Lemonldap/NG/Portal/Plugins/GrantSession.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/CrowdSec.pm »
1 ##@class Lemonldap::NG::Portal::Main::Run
2 # Serve request part of Lemonldap::NG portal
3 #
4 # Parts of this file:
5 # - response handler
6 # - main entry points
7 # - running methods
8 # - utilities
9 #
10 package Lemonldap::NG::Portal::Main::Run;
11  
12 442 our $VERSION = '2.23.0';
13  
14 package Lemonldap::NG::Portal::Main;
15  
16 442 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 442 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 442 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 442 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 442 ▶use Lemonldap::NG::Common::Util qw(getPSessionID getSameSite);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 485 ▶has trOverCache => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
23  
24 # The execution order between groups and macros can be
25 # modified in config (#1877)
26 sub groupsAndMacros {
27 return (
28 $_[0]->conf->{groupsBeforeMacros}
29 450 ▶ ? qw(setGroups setMacros)
Subroutine groupsAndMacros: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->conf->{'groupsBeforeMacros'} ? : 0 450 450
30 : qw(setMacros setGroups)
31 );
32 }
33  
34 # List constants
35 446 ▶sub authProcess { qw(extractFormInfo getUser authenticate) }
Subroutine authProcess: called CC=1 SCAR=0.0
Pod undocumented
36  
37 sub sessionData {
38 446 ▶ return qw(setAuthSessionInfo setSessionInfo), $_[0]->groupsAndMacros,
Subroutine sessionData: called CC=1 SCAR=0.0
Pod undocumented
39 qw(setPersistentSessionInfo setLocalGroups rememberBrowserCheck store secondFactor);
40 }
41  
42 sub validSession {
43 446 ▶ qw(storeHistory rememberBrowser buildCookie);
Subroutine validSession: called CC=1 SCAR=0.0
Pod undocumented
44 }
45  
46 # RESPONSE HANDLER
47 # ----------------
48 #
49 # - replace Lemonldap::NG::Common::PSGI::Request request by
50 # Lemonldap::NG::Portal::Main::Request
51 # - launch Lemonldap::NG::Common::PSGI::Request::handler()
52 sub handler {
53 3105 ▶ my ( $self, $req ) = @_;
Subroutine handler: called CC=15 SCAR=29.7
Pod undocumented
54  
55 3105 bless $req, 'Lemonldap::NG::Portal::Main::Request';
56 3105 $req->init( $self->conf );
57  
58 # Set portal URL dynamically
59 3105 $req->portal( HANDLER->tsv->{portal}->($req) );
60 3104 my $sp = 0;
61  
62 # Restore pdata
63 3104 ▶ if ( my $v = $req->cookies->{ $self->conf->{cookieName} . 'pdata' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $v = $req->cookies->{$self->conf->{"cookieName"} . 'pdata'}) 6 3098 3104
64 6 $sp = 1;
65 6 eval { $req->pdata( JSON::from_json( uri_unescape($v) ) ); };
66 6 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 6 6
67 0 $self->logger->error("Bad JSON content in cookie pdata");
68 0 $req->pdata( {} );
69 }
70  
71 # Avoid fatal errors when using old keepPdata format
72 6 ▶ if ( $req->pdata->{keepPdata}
Branch: 1 branch
BranchTrueFalseTotal
if ($req->pdata->{"keepPdata"} and not ref $req->pdata->{"keepPdata"} eq "ARRAY") 0 6 6
Condition66%2/3
$req->pdata->{"keepPdata"} and not ref $req->pdata->{"keepPdata"} eq "ARRAY"
!ll&&!rl&&r
330
MC/DC0%0/2
$req->pdata->{"keepPdata"} and not ref $req->pdata->{"keepPdata"} eq "ARRAY"
$req->pdata->{"keepPdata"} not ref $req->pdata->{"keepPdata"} eq "ARRAY"
Truth table66%2/3
A and not(B)
A= $req->pdata->{"keepPdata"}B= not ref $req->pdata->{"keepPdata"} eq "ARRAY"
ABresult
0X0
100
111
73 and not( ref $req->pdata->{keepPdata} eq "ARRAY" ) )
74 {
75 0 $req->pdata->{keepPdata} = [];
76 }
77 }
78 3104 my $res = $self->Lemonldap::NG::Common::PSGI::Router::handler($req);
79  
80 # Avoid permanent loop 'Portal <-> _url' if pdata cookie is not removed
81 3103 ▶ if ( $req->userData->{_url}
Branch: 1 branch
BranchTrueFalseTotal
if ($req->userData->{"_url"} and not $req->pdata->{"keepPdata"} and $req->userData->{"_session_id"} and $req->{"env"}{"HTTP_COOKIE"} and $req->{"env"}{"HTTP_COOKIE"} eq &encode_base64($req->userData->{"_url"}, "")) 0 3103 3103
Condition58%7/12
$req->userData->{"_url"} and not $req->pdata->{"keepPdata"} and $req->userData->{"_session_id"} and $req->{"env"}{"HTTP_COOKIE"} and $req->{"env"}{"HTTP_COOKIE"} eq &encode_base64($req->userData->{"_url"}, "")
!ll&&!rl&&r
310300
$req->userData->{"_url"} and not $req->pdata->{"keepPdata"} and $req->userData->{"_session_id"} and $req->{"env"}{"HTTP_COOKIE"}
!ll&&!rl&&r
3086170
$req->userData->{"_url"} and not $req->pdata->{"keepPdata"} and $req->userData->{"_session_id"}
!ll&&!rl&&r
3086017
$req->userData->{"_url"} and not $req->pdata->{"keepPdata"}
!ll&&!rl&&r
3086017
MC/DC0%0/5
$req->userData->{"_url"} and not $req->pdata->{"keepPdata"} and $req->userData->{"_session_id"} and $req->{"env"}{"HTTP_COOKIE"} and $req->{"env"}{"HTTP_COOKIE"} eq &encode_base64($req->userData->{"_url"}, "")
$req->userData->{"_url"} not $req->pdata->{"keepPdata"} $req->userData->{"_session_id"} $req->{"env"}{"HTTP_COOKIE"} $req->{"env"}{"HTTP_COOKIE"} eq &encode_base64($req->userData->{"_url"}, "")
Truth table33%2/6
A and not(B) and C and D and E
A= $req->userData->{"_url"}B= not $req->pdata->{"keepPdata"}C= $req->userData->{"_session_id"}D= $req->{"env"}{"HTTP_COOKIE"}E= $req->{"env"}{"HTTP_COOKIE"} eq &encode_base64($req->userData->{"_url"}, "")
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
82 and !$req->pdata->{keepPdata}
83 and $req->userData->{_session_id}
84 and $req->{env}->{HTTP_COOKIE}
85 and $req->{env}->{HTTP_COOKIE} eq
86 encode_base64( $req->userData->{_url}, '' ) )
87 {
88 0 $self->logger->info("Force cleaning pdata");
89 $self->logger->warn("pdata cookie domain must be set")
90 0 ▶ unless ( $self->conf->{pdataDomain} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->conf->{"pdataDomain"} 0 0 0
91 0 $req->pdata( {} );
92 }
93  
94 # Save pdata
95 3103 ▶ if ( $sp or %{ $req->pdata } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sp or %{$req->pdata;}) 35 3068 3103
Condition100%3/3
$sp or %{$req->pdata;}
l!l&&r!l&&!r
6293068
MC/DC100%2/2
$sp or %{$req->pdata;}
$sp %{$req->pdata;}
Truth table100%3/3
A or B
A= $spB= %{$req->pdata;}
ABresult
1X1
011
000
96 35 my %pdata_options = ();
97 $pdata_options{domain} = $self->conf->{pdataDomain}
98 35 ▶ if $self->conf->{pdataDomain};
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{"pdataDomain"} 0 35 35
99  
100 35 ▶ if ( %{ $req->pdata } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%{$req->pdata;}) { } 30 5 35
101 30 push @{ $res->[1] }, 'Set-Cookie',
102 $self->cookie(
103 name => $self->conf->{cookieName} . 'pdata',
104 secure => $self->conf->{securedCookie},
105 30 value => uri_escape( JSON::to_json( $req->pdata ) ),
106 %pdata_options,
107 );
108  
109 }
110 else {
111 5 push @{ $res->[1] }, 'Set-Cookie',
112 $self->cookie(
113 name => $self->conf->{cookieName} . 'pdata',
114 secure => $self->conf->{securedCookie},
115 5 value => '',
116 expires => 'Wed, 21 Oct 2015 00:00:00 GMT',
117 %pdata_options,
118 );
119  
120 # Avoid regressions with #3228
121 5 push @{ $res->[1] }, 'Set-Cookie',
122 $self->genDomainCookie(
123 $req,
124 name => $self->conf->{cookieName} . 'pdata',
125 secure => $self->conf->{securedCookie},
126 5 value => '',
127 expires => 'Wed, 21 Oct 2015 00:00:00 GMT',
128 %pdata_options,
129 );
130 }
131 }
132 3103 return $res;
133 }
134  
135 # MAIN ENTRY POINTS (declared in Lemonldap::NG::Portal::Main::Init)
136 # -----------------
137 #
138 # Entry points:
139 # - "/ping": - authenticated() for already authenticated users
140 # - pleaseAuth() for others
141 # - "/": - login() ~first access
142 # - postLogin(), same for POST requests
143 # - authenticatedRequest() for authenticated users
144  
145 sub authenticated {
146 0 ▶ my ( $self, $req ) = @_;
Subroutine authenticated: not called CC=1 SCAR=6.9
Pod documented
147  
148 return $self->do(
149 $req,
150 [
151 'importHandlerData',
152 'controlUrl',
153 0 @{ $self->forAuthUser },
154 sub {
155 0 ▶ $req->response(
Subroutine __ANON__: not called CC=1 SCAR=6.9
156 $self->sendJSONresponse( $req, { status => 1 } ) );
157 0 return PE_SENDRESPONSE;
158 }
159 0 ]
160 );
161 }
162  
163 sub pleaseAuth {
164 1 ▶ my ( $self, $req ) = @_;
Subroutine pleaseAuth: called CC=1 SCAR=0.0
Pod undocumented
165 1 return $self->sendJSONresponse( $req, { status => 0 } );
166 }
167  
168 sub login {
169 289 ▶ my ( $self, $req ) = @_;
Subroutine login: called CC=1 SCAR=0.0
Pod undocumented
170 return $self->do(
171 $req,
172 [
173 'checkUnauthLogout', 'checkCancel',
174 289 'controlUrl', @{ $self->beforeAuth },
175 289 $self->authProcess, @{ $self->betweenAuthAndData },
176 289 $self->sessionData, @{ $self->afterData },
177 289 $self->validSession, @{ $self->endAuth }
178 ]
179 );
180 }
181  
182 sub postLogin {
183 137 ▶ my ( $self, $req ) = @_;
Subroutine postLogin: called CC=1 SCAR=0.0
Pod undocumented
184 return $self->do(
185 $req,
186 [
187 'checkUnauthLogout', 'restoreArgs', # Fix 2342
188 137 'controlUrl', @{ $self->beforeAuth },
189 137 $self->authProcess, @{ $self->betweenAuthAndData },
190 137 $self->sessionData, @{ $self->afterData },
191 137 $self->validSession, @{ $self->endAuth }
192 ]
193 );
194 }
195  
196 sub authenticatedRequest {
197 53 ▶ my ( $self, $req ) = @_;
Subroutine authenticatedRequest: called CC=1 SCAR=0.0
Pod undocumented
198 return $self->do(
199 $req,
200 [
201 'importHandlerData', 'controlUrl',
202 53 'checkLogout', @{ $self->forAuthUser }
203 ]
204 );
205 }
206  
207 sub postAuthenticatedRequest {
208 11 ▶ my ( $self, $req ) = @_;
Subroutine postAuthenticatedRequest: called CC=1 SCAR=0.0
Pod undocumented
209 return $self->do(
210 $req,
211 [
212 'importHandlerData', 'restoreArgs',
213 'controlUrl', 'checkLogout',
214 11 @{ $self->forAuthUser }
215 ]
216 );
217 }
218  
219 sub processRefreshSession {
220 2 ▶ my ( $self, $req ) = @_;
Subroutine processRefreshSession: called CC=6 SCAR=18.0
Pod undocumented
221  
222 2 my %data = %{ $req->userData };
223 2 ▶ $req->user( $data{_user} || $data{ $self->conf->{whatToTrace} } );
Condition33%1/3
$data{'_user'} || $data{$self->conf->{'whatToTrace'}}
l!l&&r!l&&!r
200
MC/DC0%0/2
$data{'_user'} || $data{$self->conf->{'whatToTrace'}}
$data{'_user'} $data{$self->conf->{'whatToTrace'}}
Truth table33%1/3
A || B
A= $data{'_user'}B= $data{$self->conf->{'whatToTrace'}}
ABresult
1X1
011
000
224 2 $req->id( $data{_session_id} );
225  
226 2 $self->auditLog(
227 $req,
228 message => ( 'Refresh request for ' . $req->user ),
229 code => "SESSION_REFRESH",
230 user => $req->user,
231 );
232  
233 2 foreach ( keys %data ) {
234  
235 # Variables that start with _ are kept accross refresh
236 53 ▶ if (/^_/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^_/) { } 27 26 53
237  
238 # But not OIDC tokens, which can be refreshed
239 27 ▶ delete $data{$_}
Branch: 1 branch
BranchTrueFalseTotal
if /^(_oidc_access_token|_oidc_refresh_token|_oidc_access_token_eol)$/ 0 27 27
240 if (
241 /^(_oidc_access_token|_oidc_refresh_token|_oidc_access_token_eol)$/
242 );
243 }
244  
245 # Other variables should be refreshed
246 else {
247 # But not these two
248 26 ▶ if (/^(?:startTime|authenticationLevel)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(?:startTime|authenticationLevel)$/) { } 2 24 26
249 2 next;
250 }
251 else {
252 24 delete $data{$_};
253 }
254 }
255 }
256 2 $data{_updateTime} = strftime( "%Y%m%d%H%M%S", localtime() );
257 2 $self->logger->debug(
258 "Set session $req->{id} _updateTime with $data{_updateTime}");
259  
260 # Avoid interferences when refresh is run on multiple sessions
261 # in the same request
262 2 $req->sessionInfo( {} );
263  
264 # Transmit refreshed session data to setRefreshedSessionData
265 2 $req->data->{refreshedSessionData} = \%data;
266 $req->steps( [
267 2 'getUser', @{ $self->betweenAuthAndData },
268 'setSessionInfo', 'setRefreshedSessionData',
269 $self->groupsAndMacros, 'setLocalGroups',
270 ]
271 );
272 2 my $res = $self->process($req);
273 2 delete $req->data->{refreshedSessionData};
274  
275 2 return $res;
276 }
277  
278 sub setRefreshedSessionData {
279 2 ▶ my ( $self, $req ) = @_;
Subroutine setRefreshedSessionData: called CC=2 SCAR=6.9
Pod undocumented
280 2 my %data = %{ $req->data->{refreshedSessionData} };
281  
282 2 $req->sessionInfo->{$_} = $data{$_} foreach ( keys %data );
283 2 $req->refresh(1);
284  
285 2 return PE_OK;
286 }
287  
288 sub refresh {
289 0 ▶ my ( $self, $req ) = @_;
Subroutine refresh: not called CC=3 SCAR=24.8
Pod undocumented
290 0 $req->mustRedirect(1);
291  
292 $self->userLogger->notice( 'Refresh request for '
293 0 . $req->userData->{ $self->conf->{whatToTrace} } );
294  
295 0 my $res = $self->processRefreshSession($req);
296  
297 # Run additional 'store' step We need to call process() here in case
298 # 'store' is hooked by aroundSub or afterSub
299 0 ▶ if ( $res == PE_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res == 0) 0 0 0
300 0 $req->steps( ['store'] );
301 0 $res = $self->process($req);
302 }
303  
304 0 $req->error($res);
305 0 ▶ if ($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) 0 0 0
306 0 $req->info(
307 $self->loadTemplate(
308 $req,
309 'simpleInfo', params => { trspan => 'rightsReloadNeedsLogout' }
310 )
311 );
312 0 $req->urldc( $req->portal );
313 0 return $self->doPE( $req, PE_INFO );
314 }
315 0 return $self->doPE( $req, PE_OK );
316 }
317  
318 sub logout {
319 0 ▶ my ( $self, $req ) = @_;
Subroutine logout: not called CC=1 SCAR=6.9
Pod undocumented
320 return $self->do(
321 $req,
322 [
323 'importHandlerData', 'controlUrl',
324 0 @{ $self->beforeLogout }, 'authLogout',
325 'deleteSession'
326 ],
327 1, # No fail: logout should continue even if errors
328 );
329 }
330  
331 sub unauthLogout {
332 5 ▶ my ( $self, $req ) = @_;
Subroutine unauthLogout: called CC=2 SCAR=7.1
Pod undocumented
333 5 my $err = $self->_unauthLogout($req);
334 5 ▶ return $self->do( $req,
Branch: 1 branch
BranchTrueFalseTotal
$err > 0 ? : 0 5 5
335 [ 'controlUrl', [ returnPE => ( $err > 0 ? $err : PE_LOGOUT_OK ) ] ] );
336 }
337  
338 sub _unauthLogout {
339 35 ▶ my ( $self, $req ) = @_;
Subroutine _unauthLogout: called CC=1 SCAR=0.0
340 35 my $err = $self->processHook( $req, 'unauthLogout' );
341 35 $self->logger->debug('Unauthenticated logout request');
342 35 $self->logger->debug('Cleaning pdata');
343 35 $self->logger->debug("Removing $self->{conf}->{cookieName} cookie");
344 35 $req->pdata( {} );
345 $req->addCookie(
346 $self->genDomainCookie(
347 $req,
348 name => $self->conf->{cookieName},
349 secure => $self->conf->{securedCookie},
350 35 expires => 'Wed, 21 Oct 2015 00:00:00 GMT',
351 value => 0
352 )
353 );
354 35 return $err;
355 }
356  
357 # RUNNING METHODS
358 # ---------------
359  
360 sub do {
361 700 ▶ my ( $self, $req, $steps, $nofail ) = @_;
Subroutine do: called CC=27 SCAR=36.4
Pod undocumented
362 700 $req->steps($steps);
363 700 $req->data->{activeTimer} = $self->conf->{activeTimer};
364 700 my $err = $req->error( $self->process( $req, nofail => $nofail ) );
365  
366 # Update status
367 $self->HANDLER->publishStatus(
368 user => ( $req->user ? $req->user : $req->address ),
369 url => $req->{env}->{HTTP_HOST} . $req->{env}->{REQUEST_URI},
370 699 ▶ portalCode => $err,
Branch: 1 branch
BranchTrueFalseTotal
$req->user ? : 259 440 699
371 );
372  
373 699 ▶ return $req->response if $err == PE_SENDRESPONSE;
Branch: 1 branch
BranchTrueFalseTotal
if $err == -4 6 693 699
374  
375 # Remove userData if authentication fails
376 693 ▶ $req->userData( {} ) if ( $err == PE_BADCREDENTIALS or $err == PE_BADOTP );
Branch: 1 branch
BranchTrueFalseTotal
if $err == 5 or $err == 96 13 680 693
Condition66%2/3
$err == 5 or $err == 96
l!l&&r!l&&!r
130680
MC/DC50%1/2
$err == 5 or $err == 96
$err == 5 $err == 96
Truth table66%2/3
A or B
A= $err == 5B= $err == 96
ABresult
1X1
011
000
377  
378 693 ▶ if ( !$self->conf->{noAjaxHook} and $req->wantJSON ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->conf->{'noAjaxHook'} and $req->wantJSON) { } 171 522 693
Condition66%2/3
not $self->conf->{"noAjaxHook"} and $req->wantJSON
!ll&&!rl&&r
0522171
MC/DC50%1/2
not $self->conf->{"noAjaxHook"} and $req->wantJSON
not $self->conf->{"noAjaxHook"} $req->wantJSON
Truth table66%2/3
not(A) and B
A= not $self->conf->{"noAjaxHook"}B= $req->wantJSON
ABresult
0X0
100
111
379 171 $self->logger->debug('Processing to JSON response');
380 171 ▶ if ( ( $err > 0 and !$req->id and !( $err == PE_LOGOUT_OK ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($err > 0 and not $req->id and not $err == 47 or $err eq 41 or $err eq 1) { } 80 91 171
elsif ($err > 0 and $err != 35 and $err != 47) { } 0 91 91
Condition75%9/12
$err > 0 and not $req->id and not $err == 47 or $err eq 41 or $err eq 1
l!l&&r!l&&!r
80091
$err > 0 and not $req->id and not $err == 47 or $err eq 41
l!l&&r!l&&!r
80091
$err > 0 and not $req->id and not $err == 47
!ll&&!rl&&r
91080
$err > 0 and not $req->id
!ll&&!rl&&r
89280
MC/DC40%2/5
$err > 0 and not $req->id and not $err == 47 or $err eq 41 or $err eq 1
$err > 0 not $req->id not $err == 47 $err eq 41 $err eq 1
Truth table30%3/10
A and not(B) and not(C) or D or E
A= $err > 0B= not $req->idC= not $err == 47D= $err eq 41E= $err eq 1
ABCDEresult
111XX1
0XX1X1
10X1X1
1101X1
0XX011
10X011
110011
0XX000
10X000
110000
381 or $err eq PE_SESSIONNOTGRANTED
382 or $err eq PE_SESSIONEXPIRED )
383 {
384 80 my $json = { result => 0, error => $err };
385 80 ▶ if ( $req->wantErrorRender ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->wantErrorRender) 0 80 80
386 0 $json->{html} = $self->loadTemplate( $req, 'errormsg',
387 params => { $self->getErrorTplParams($req) } );
388 }
389 80 return $self->sendJSONresponse(
390 $req, $json,
391 code => 401,
392 headers => [
393 'WWW-Authenticate' => "SSO " . $req->portal,
394 "Content-Type" => "application/json"
395 ],
396 );
397 }
398 ▶ elsif ( $err > 0 and $err != PE_PASSWORD_OK and $err != PE_LOGOUT_OK ) {
Condition50%3/6
$err > 0 and $err != 35 and $err != 47
!ll&&!rl&&r
9100
$err > 0 and $err != 35
!ll&&!rl&&r
8920
MC/DC0%0/3
$err > 0 and $err != 35 and $err != 47
$err > 0 $err != 35 $err != 47
Truth table50%2/4
A and B and C
A= $err > 0B= $err != 35C= $err != 47
ABCresult
0XX0
10X0
1100
1111
399 0 ▶ return $self->sendJSONresponse(
Branch: 1 branch
BranchTrueFalseTotal
$err == 36 && $req->id ? : 0 0 0
Condition0%0/3
$err == 36 and $req->id
!ll&&!rl&&r
000
MC/DC0%0/2
$err == 36 and $req->id
$err == 36 $req->id
Truth table0%0/3
A and B
A= $err == 36B= $req->id
ABresult
0X0
100
111
400 $req,
401 {
402 result => 0,
403 error => $err,
404 (
405 $err == PE_NOTIFICATION && $req->id
406 ? ( ciphered_id => $req->id )
407 : ()
408 )
409 },
410 code => 400
411 );
412 }
413 else {
414 91 my $res = { result => 1, error => $err };
415 91 ▶ if ( $req->data->{newAuth} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->data->{"newAuth"}) 68 23 91
416 68 $res->{id} = $req->id;
417 $res->{id_http} = $req->sessionInfo->{_httpSession}
418 68 ▶ if $req->sessionInfo->{_httpSession};
Branch: 1 branch
BranchTrueFalseTotal
if $req->sessionInfo->{"_httpSession"} 0 68 68
419 }
420 91 return $self->sendJSONresponse( $req, $res );
421 }
422 }
423 else {
424 522 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($req->info or $err and $err != 47 and $err != -2 || $err == -2 && $req->data->{'redirectFormMethod'} && $req->data->{'redirectFormMethod'} eq 'post') { } 319 203 522
Condition72%13/18
$req->info or $err and $err != 47 and $err != -2 || $err == -2 && $req->data->{"redirectFormMethod"} && $req->data->{"redirectFormMethod"} eq "post"
l!l&&r!l&&!r
14305203
$err and $err != 47 and $err != -2 || $err == -2 && $req->data->{"redirectFormMethod"} && $req->data->{"redirectFormMethod"} eq "post"
!ll&&!rl&&r
15152305
$err and $err != 47
!ll&&!rl&&r
8566357
$err != -2 or $err == -2 and $req->data->{"redirectFormMethod"} and $req->data->{"redirectFormMethod"} eq "post"
l!l&&r!l&&!r
305052
$err == -2 and $req->data->{"redirectFormMethod"} and $req->data->{"redirectFormMethod"} eq "post"
!ll&&!rl&&r
5200
$err == -2 and $req->data->{"redirectFormMethod"}
!ll&&!rl&&r
0520
MC/DC57%4/7
$req->info or $err and $err != 47 and $err != -2 || $err == -2 && $req->data->{"redirectFormMethod"} && $req->data->{"redirectFormMethod"} eq "post"
$req->info $err $err != 47 $err != -2 $err == -2 $req->data->{"redirectFormMethod"} $req->data->{"redirectFormMethod"} eq "post"
Truth table62%5/8
A or B and C and D or E and F and G
A= $req->infoB= $errC= $err != 47D= $err != -2E= $err == -2F= $req->data->{"redirectFormMethod"}G= $req->data->{"redirectFormMethod"} eq "post"
ABCDEFGresult
1XXXXXX1
0111XXX1
01101111
00XXXXX0
010XXXX0
01100XX0
011010X0
01101100
425 $req->info
426 or (
427 $err
428 and $err != PE_LOGOUT_OK
429 and (
430 $err != PE_REDIRECT
431 or ( $err == PE_REDIRECT
432 and $req->data->{redirectFormMethod}
433 and $req->data->{redirectFormMethod} eq 'post' )
434 )
435 )
436 )
437 {
438 319 my ( $tpl, $prms ) = $self->display($req);
439 319 $self->logger->debug("Calling sendHtml with template $tpl");
440 319 return $self->sendHtml( $req, $tpl, params => $prms );
441 }
442 else {
443 203 $self->logger->debug('Calling autoredirect');
444 203 return $self->autoRedirect($req);
445 }
446 }
447 }
448  
449 # Utilities
450 # ---------
451  
452 sub doPE {
453 133 ▶ my ( $self, $req, $res ) = @_;
Subroutine doPE: called CC=1 SCAR=0.0
Pod undocumented
454 133 return $self->do( $req, [ [ returnPE => $res ] ] );
455 }
456  
457 sub getModule {
458 380 ▶ my ( $self, $req, $type ) = @_;
Subroutine getModule: called CC=4 SCAR=15.2
Pod undocumented
459  
460 380 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if (my $mod = {'auth', '_authentication', 'user', '_userDB', 'password', '_passwordDB'}->{$type}) { } 380 0 380
elsif ($type eq 'issuer') { } 0 0 0
461 my $mod = {
462 auth => '_authentication',
463 user => '_userDB',
464 password => '_passwordDB'
465 }->{$type}
466 )
467 {
468 380 ▶ if ( my $sub = $self->$mod->can('name') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $self->$mod->can('name')) { } 78 302 380
469 78 return $sub->( $self->$mod, $req, $type );
470 }
471 else {
472 302 my $s = ref( $self->$mod );
473 302 $s =~
474 s/^Lemonldap::NG::Portal::(?:(?:Issuer|UserDB|Auth|Password)::)?//;
475 302 return $s;
476 }
477 }
478 elsif ( $type eq 'issuer' ) {
479 0 return $req->{_activeIssuerDB};
480 }
481 else {
482 0 die "Unknown type $type";
483 }
484 }
485  
486 sub autoRedirect {
487 203 ▶ my ( $self, $req ) = @_;
Subroutine autoRedirect: called CC=13 SCAR=27.7
Pod undocumented
488  
489 # Set redirection URL if needed
490 203 ▶ if ( $req->mustRedirect and not( $req->info ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->mustRedirect and not $req->info) 87 116 203
Condition66%2/3
$req->mustRedirect and not $req->info
!ll&&!rl&&r
116087
MC/DC50%1/2
$req->mustRedirect and not $req->info
$req->mustRedirect not $req->info
Truth table66%2/3
A and not(B)
A= $req->mustRedirectB= not $req->info
ABresult
0X0
100
111
491 87 ▶ if ( $req->error == PE_LOGOUT_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->error == 47) { } 6 81 87
492 6 ▶ if ( !$req->urldc or !$self->_isExternalUrl( $req, $req->urldc ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($req->urldc and $self->_isExternalUrl($req, $req->urldc)) 2 4 6
Condition66%2/3
$req->urldc and $self->_isExternalUrl($req, $req->urldc)
!ll&&!rl&&r
204
MC/DC50%1/2
$req->urldc and $self->_isExternalUrl($req, $req->urldc)
$req->urldc $self->_isExternalUrl($req, $req->urldc)
Truth table66%2/3
A and B
A= $req->urldcB= $self->_isExternalUrl($req, $req->urldc)
ABresult
0X0
100
111
493 2 $req->urldc(
494 $self->buildUrl( $req->portal, { "logout" => 1 } ) );
495 }
496 }
497 else {
498 81 ▶ $req->{urldc} ||= $req->portal;
Condition66%2/3
$req->{"urldc"} ||= $req->portal
l!l&&r!l&&!r
31500
MC/DC0%0/2
$req->{"urldc"} ||= $req->portal
$req->{"urldc"} $req->portal
Truth table66%2/3
A ||= B
A= $req->{"urldc"}B= $req->portal
ABresult
1X1
011
000
499 }
500 }
501  
502 # Redirection should be made if urldc defined
503 203 ▶ if ( $req->{urldc} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->{"urldc"}) 134 69 203
504 134 ▶ if ( $req->{urldc} =~ /^\s*((?:java|vb)script|data):/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->{'urldc'} =~ /^\s*((?:java|vb)script|data):/) { } 0 134 134
505 $self->auditLog(
506 $req,
507 message => "Redirection to $req->{urldc} blocked",
508 code => "UNAUTHORIZED_REDIRECT",
509 url => $req->{urldc},
510 user => $req->sessionInfo->{ $self->conf->{whatToTrace} }
511 0 ▶ || $req->user,
Condition0%0/3
$req->sessionInfo->{$self->conf->{'whatToTrace'}} || $req->user
l!l&&r!l&&!r
000
MC/DC0%0/2
$req->sessionInfo->{$self->conf->{'whatToTrace'}} || $req->user
$req->sessionInfo->{$self->conf->{'whatToTrace'}} $req->user
Truth table0%0/3
A || B
A= $req->sessionInfo->{$self->conf->{'whatToTrace'}}B= $req->user
ABresult
1X1
011
000
512 );
513 0 delete $req->{urldc};
514 }
515 else {
516 134 $self->logger->debug("Building redirection to $req->{urldc}");
517 134 ▶ if ( $req->{pdata}->{_url}
Branch: 1 branch
BranchTrueFalseTotal
if ($req->{"pdata"}{"_url"} and $req->{"pdata"}{"_url"} eq &encode_base64($req->{"urldc"}, "")) 7 127 134
Condition66%2/3
$req->{"pdata"}{"_url"} and $req->{"pdata"}{"_url"} eq &encode_base64($req->{"urldc"}, "")
!ll&&!rl&&r
12707
MC/DC50%1/2
$req->{"pdata"}{"_url"} and $req->{"pdata"}{"_url"} eq &encode_base64($req->{"urldc"}, "")
$req->{"pdata"}{"_url"} $req->{"pdata"}{"_url"} eq &encode_base64($req->{"urldc"}, "")
Truth table66%2/3
A and B
A= $req->{"pdata"}{"_url"}B= $req->{"pdata"}{"_url"} eq &encode_base64($req->{"urldc"}, "")
ABresult
0X0
100
111
518 and $req->{pdata}->{_url} eq encode_base64( $req->{urldc}, '' )
519 )
520 {
521 7 $self->logger->info("Force cleaning pdata");
522 7 delete $req->{pdata}->{_url};
523 }
524 134 ▶ if ( $self->_jsRedirect->( $req, $req->sessionInfo ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_jsRedirect->($req, $req->sessionInfo)) { } 0 134 134
525 0 $req->error(PE_REDIRECT);
526 0 $req->data->{redirectFormMethod} = "get";
527 }
528 else {
529 return $self->sendRedirection( $req,
530 134 URI->new( $req->{urldc} )->as_string,
531 );
532 }
533 }
534 }
535 69 my ( $tpl, $prms ) = $self->display($req);
536 69 $self->logger->debug("Calling sendHtml with template $tpl");
537 69 return $self->sendHtml( $req, $tpl, params => $prms );
538 }
539  
540 # Try to recover the session corresponding to id and return session data.
541 # If $id is set to undef or if $args{force} is true, return a new session.
542 sub getApacheSession {
543 261 ▶ my ( $self, $id, %args ) = @_;
Subroutine getApacheSession: called CC=18 SCAR=29.4
Pod undocumented
544 261 ▶ $args{kind} //= "SSO";
Condition100%2/2
$args{"kind"} //= "SSO"
l!l
25236
MC/DC100%1/1
$args{"kind"} //= "SSO"
$args{"kind"}
Truth table100%2/2
A
A= $args{"kind"}
Aresult
11
00
545 261 ▶ if ($id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id) { } 117 144 261
546 117 $self->logger->debug("Try to get $args{kind} session $id");
547 }
548 else {
549 144 $self->logger->debug("Try to get a new $args{kind} session");
550 }
551  
552 my $as = Lemonldap::NG::Common::Session->new( {
553  
554 # The $args{hashStore} permits one to override the
555 # hashedSessionStore parameter. It is used when sessions are given
556 # by searchOn (like in Refresh plugin)
557 hashStore => $args{hashStore} // $self->conf->{hashedSessionStore},
558 storageModule => $self->conf->{globalStorage},
559 storageModuleOptions => $self->conf->{globalStorageOptions},
560 cacheModule => $self->conf->{localSessionStorage},
561 cacheModuleOptions => $self->conf->{localSessionStorageOptions},
562 id => $id,
563 force => $args{force},
564 kind => $args{kind},
565 261 ▶ ( $args{info} ? ( info => $args{info} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
$args{'info'} ? : 162 99 261
Condition100%3/3
$args{"hashStore"} // $self->conf->{"hashedSessionStore"}
l!l&&r!l&&!r
1127223
MC/DC100%2/2
$args{"hashStore"} // $self->conf->{"hashedSessionStore"}
$args{"hashStore"} $self->conf->{"hashedSessionStore"}
Truth table100%3/3
A // B
A= $args{"hashStore"}B= $self->conf->{"hashedSessionStore"}
ABresult
1X1
011
000
566 }
567 );
568  
569 261 ▶ if ( my $err = $as->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $as->error) 2 259 261
570 2 ▶ $self->lmLog(
Branch: 1 branch
BranchTrueFalseTotal
$err =~ /(?:Object does not exist|Invalid session ID|Session kind mismatch)/ ? : 1 1 2
571 $err,
572 (
573 $err =~
574 /(?:Object does not exist|Invalid session ID|Session kind mismatch)/
575 ? 'notice'
576 : 'error'
577 )
578 );
579 2 return;
580 }
581  
582 259 ▶ if ( $id and !$args{force} and !$as->data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id and not $args{"force"} and not $as->data) 0 259 259
Condition66%4/6
$id and not $args{"force"} and not $as->data
!ll&&!rl&&r
1431160
$id and not $args{"force"}
!ll&&!rl&&r
1430116
MC/DC0%0/3
$id and not $args{"force"} and not $as->data
$id not $args{"force"} not $as->data
Truth table50%2/4
A and not(B) and not(C)
A= $idB= not $args{"force"}C= not $as->data
ABCresult
0XX0
10X0
1100
1111
583 0 $self->logger->debug("Session $args{kind} $id not found");
584 0 return;
585 }
586 259 ▶ $self->logger->debug("Get session $id from Portal::Main::Run")
Branch: 1 branch
BranchTrueFalseTotal
if $id 116 143 259
587 if ($id);
588 $self->logger->debug(
589 "Check session validity -> " . $self->conf->{timeoutActivity} . "s" )
590 259 ▶ if ( $self->conf->{timeoutActivity} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{"timeoutActivity"} 4 255 259
591 259 my $now = time;
592 259 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($id and defined $as->data->{"_utime"} and $now - $as->data->{"_utime"} > $self->conf->{"timeout"} || $self->conf->{"timeoutActivity"} && $as->data->{"_lastSeen"} && $now - $as->data->{"_lastSeen"} > $self->conf->{"timeoutActivity"}) 5 254 259
Condition73%11/15
$id and defined $as->data->{"_utime"} and $now - $as->data->{"_utime"} > $self->conf->{"timeout"} || $self->conf->{"timeoutActivity"} && $as->data->{"_lastSeen"} && $now - $as->data->{"_lastSeen"} > $self->conf->{"timeoutActivity"}
!ll&&!rl&&r
1511080
$id and defined $as->data->{"_utime"}
!ll&&!rl&&r
1438108
$now - $as->data->{"_utime"} > $self->conf->{"timeout"} or $self->conf->{"timeoutActivity"} and $as->data->{"_lastSeen"} and $now - $as->data->{"_lastSeen"} > $self->conf->{"timeoutActivity"}
l!l&&r!l&&!r
50103
$self->conf->{"timeoutActivity"} and $as->data->{"_lastSeen"} and $now - $as->data->{"_lastSeen"} > $self->conf->{"timeoutActivity"}
!ll&&!rl&&r
10030
$self->conf->{"timeoutActivity"} and $as->data->{"_lastSeen"}
!ll&&!rl&&r
10003
MC/DC0%0/6
$id and defined $as->data->{"_utime"} and $now - $as->data->{"_utime"} > $self->conf->{"timeout"} || $self->conf->{"timeoutActivity"} && $as->data->{"_lastSeen"} && $now - $as->data->{"_lastSeen"} > $self->conf->{"timeoutActivity"}
$id defined $as->data->{"_utime"} $now - $as->data->{"_utime"} > $self->conf->{"timeout"} $self->conf->{"timeoutActivity"} $as->data->{"_lastSeen"} $now - $as->data->{"_lastSeen"} > $self->conf->{"timeoutActivity"}
Truth table42%3/7
A and B and C or D and E and F
A= $idB= defined $as->data->{"_utime"}C= $now - $as->data->{"_utime"} > $self->conf->{"timeout"}D= $self->conf->{"timeoutActivity"}E= $as->data->{"_lastSeen"}F= $now - $as->data->{"_lastSeen"} > $self->conf->{"timeoutActivity"}
ABCDEFresult
0XXXXX0
10XXXX0
1100XX0
11010X0
1101100
111XXX1
1101111
593 $id
594 and defined $as->data->{_utime}
595 and (
596 ( ( $now - $as->data->{_utime} ) > $self->conf->{timeout} )
597 or (
598 $self->conf->{timeoutActivity}
599 and $as->data->{_lastSeen}
600 and ( ( $now - $as->data->{_lastSeen} ) >
601 $self->conf->{timeoutActivity} )
602 )
603 )
604 )
605 {
606 5 $self->logger->debug("Session $args{kind} $id expired");
607 5 return;
608 }
609  
610 254 $self->logger->debug( "Return $args{kind} session " . $as->id );
611  
612 254 return $as;
613 }
614  
615 # Try to recover the persistent session corresponding to uid and return session data.
616 sub getPersistentSession {
617 139 ▶ my ( $self, $uid, $info ) = @_;
Subroutine getPersistentSession: called CC=7 SCAR=20.4
Pod undocumented
618  
619 return
620 unless ( defined $uid
621 139 ▶ and !$self->conf->{disablePersistentStorage} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uid and not $self->conf->{"disablePersistentStorage"} 0 139 139
Condition33%1/3
defined $uid and not $self->conf->{"disablePersistentStorage"}
!ll&&!rl&&r
00139
MC/DC0%0/2
defined $uid and not $self->conf->{"disablePersistentStorage"}
defined $uid not $self->conf->{"disablePersistentStorage"}
Truth table33%1/3
A and not(B)
A= defined $uidB= not $self->conf->{"disablePersistentStorage"}
ABresult
0X0
100
111
622  
623 # Compute persistent identifier
624 139 my $pid = getPSessionID($uid);
625  
626 139 $info->{_session_uid} = $uid;
627  
628 my $ps = Lemonldap::NG::Common::Session->new( {
629 storageModule => $self->conf->{persistentStorage},
630 storageModuleOptions => $self->conf->{persistentStorageOptions},
631 139 ▶ id => $pid,
Branch: 1 branch
BranchTrueFalseTotal
$info ? : 139 0 139
632 force => 1,
633 kind => "Persistent",
634 ( $info ? ( info => $info ) : () ),
635 }
636 );
637  
638 139 ▶ if ( $ps->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ps->error) { } 1 138 139
639 1 $self->logger->debug( $ps->error );
640 }
641 else {
642  
643 # Set _session_uid if not already present
644 138 ▶ unless ( defined $ps->data->{_session_uid} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ps->data->{"_session_uid"}) 0 138 138
645 0 $ps->update( { _session_uid => $uid } );
646 }
647  
648 # Set _utime if not already present
649 138 ▶ unless ( defined $ps->data->{_utime} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ps->data->{"_utime"}) 59 79 138
650 59 $ps->update( { _utime => time } );
651 }
652 }
653  
654 139 return $ps;
655 }
656  
657 # Update persistent session.
658 # Call updateSession() and store %$infos in a persistent session.
659 # Note that if the session does not exists, it will be created.
660 # @param infos hash reference of information to update
661 # @param uid optional Unhashed persistent session ID
662 # @param id optional SSO session ID
663 # @return nothing
664 sub updatePersistentSession {
665 0 ▶ my ( $self, $req, $infos, $uid, $id ) = @_;
Subroutine updatePersistentSession: not called CC=8 SCAR=42.8
Pod undocumented
666  
667 # Return if no infos to update
668 return ()
669 unless ( ref $infos eq 'HASH'
670 and %$infos
671 0 ▶ and !$self->conf->{disablePersistentStorage} );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $infos eq "HASH" and %$infos and not $self->conf->{"disablePersistentStorage"} 0 0 0
Condition0%0/6
ref $infos eq "HASH" and %$infos and not $self->conf->{"disablePersistentStorage"}
!ll&&!rl&&r
000
ref $infos eq "HASH" and %$infos
!ll&&!rl&&r
000
MC/DC0%0/3
ref $infos eq "HASH" and %$infos and not $self->conf->{"disablePersistentStorage"}
ref $infos eq "HASH" %$infos not $self->conf->{"disablePersistentStorage"}
Truth table0%0/4
A and B and not(C)
A= ref $infos eq "HASH"B= %$infosC= not $self->conf->{"disablePersistentStorage"}
ABCresult
0XX0
10X0
1100
1111
672  
673 $uid ||= $req->{sessionInfo}->{ $self->conf->{whatToTrace} }
674 0 ▶ || $req->userData->{ $self->conf->{whatToTrace} };
Condition0%0/6
$uid ||= $req->{"sessionInfo"}{$self->conf->{"whatToTrace"}} || $req->userData->{$self->conf->{"whatToTrace"}}
l!l&&r!l&&!r
000
$req->{'sessionInfo'}{$self->conf->{'whatToTrace'}} || $req->userData->{$self->conf->{'whatToTrace'}}
l!l&&r!l&&!r
000
MC/DC0%0/3
$uid ||= $req->{"sessionInfo"}{$self->conf->{"whatToTrace"}} || $req->userData->{$self->conf->{"whatToTrace"}}
$uid $req->{'sessionInfo'}{$self->conf->{'whatToTrace'}} $req->userData->{$self->conf->{'whatToTrace'}}
Truth table0%0/4
A ||= B || C
A= $uidB= $req->{'sessionInfo'}{$self->conf->{'whatToTrace'}}C= $req->userData->{$self->conf->{'whatToTrace'}}
ABCresult
1XX1
01X1
0011
0000
675 0 $self->logger->debug("Found 'whatToTrace' -> $uid");
676 0 ▶ unless ($uid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uid) 0 0 0
677 0 $self->logger->debug('No uid found, skipping updatePersistentSession');
678 0 return ();
679 }
680 0 $self->logger->debug("Update $uid persistent session");
681  
682 # Update current session
683 0 $self->updateSession( $req, $infos, $id );
684  
685 0 my $persistentSession = $self->getPersistentSession( $uid, $infos );
686  
687 0 ▶ if ( $persistentSession->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($persistentSession->error) 0 0 0
688 0 $self->logger->error(
689 "Cannot update persistent session " . getPSessionID($uid) );
690 0 $self->logger->error( $persistentSession->error );
691 }
692 }
693  
694 # Update session stored.
695 # If no id is given, try to get it from cookie.
696 # If the session is available, update data with $info.
697 # Note that outdated session data may remain some time on
698 # server local cache, if there are several LL::NG servers.
699 # @param infos hash reference of information to update
700 # @param id Session ID
701 # @return nothing
702 sub updateSession {
703 16 ▶ my ( $self, $req, $infos, $id ) = @_;
Subroutine updateSession: called CC=11 SCAR=26.8
Pod undocumented
704  
705 # Return if no infos to update
706 16 ▶ return () unless ( ref $infos eq 'HASH' and %$infos );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $infos eq "HASH" and %$infos 0 16 16
Condition33%1/3
ref $infos eq "HASH" and %$infos
!ll&&!rl&&r
0016
MC/DC0%0/2
ref $infos eq "HASH" and %$infos
ref $infos eq "HASH" %$infos
Truth table33%1/3
A and B
A= ref $infos eq "HASH"B= %$infos
ABresult
0X0
100
111
707  
708 # Recover session ID unless given
709 16 ▶ $id ||= $req->id || $req->userData->{_session_id};
Condition50%3/6
$id ||= $req->id || $req->userData->{"_session_id"}
l!l&&r!l&&!r
1330
$req->id || $req->userData->{'_session_id'}
l!l&&r!l&&!r
300
MC/DC0%0/3
$id ||= $req->id || $req->userData->{"_session_id"}
$id $req->id $req->userData->{'_session_id'}
Truth table0%0/4
A ||= B || C
A= $idB= $req->idC= $req->userData->{'_session_id'}
ABCresult
1XX1
01X1
0011
0000
710  
711 16 ▶ if ($id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id) 16 0 16
712  
713 # Update sessionInfo data
714 ## sessionInfo updated if $id defined : quite strange!!
715 ## See https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues/430
716 16 $self->logger->debug("Update session $id");
717 16 foreach ( keys %$infos ) {
718 34 $self->logger->debug("Update sessionInfo $_");
719 34 $self->_dump( $infos->{$_} );
720 34 $req->{sessionInfo}->{$_} = $infos->{$_};
721 34 ▶ if ( $self->HANDLER->data->{_session_id}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->HANDLER->data->{"_session_id"} and $id eq $self->HANDLER->data->{"_session_id"}) 2 32 34
Condition66%2/3
$self->HANDLER->data->{"_session_id"} and $id eq $self->HANDLER->data->{"_session_id"}
!ll&&!rl&&r
3202
MC/DC50%1/2
$self->HANDLER->data->{"_session_id"} and $id eq $self->HANDLER->data->{"_session_id"}
$self->HANDLER->data->{"_session_id"} $id eq $self->HANDLER->data->{"_session_id"}
Truth table66%2/3
A and B
A= $self->HANDLER->data->{"_session_id"}B= $id eq $self->HANDLER->data->{"_session_id"}
ABresult
0X0
100
111
722 && $id eq $self->HANDLER->data->{_session_id} )
723 {
724 2 $self->HANDLER->data->{$_} = $infos->{$_};
725 }
726 }
727  
728 # Update session in global storage with _updateTime
729 16 $infos->{_updateTime} = strftime( "%Y%m%d%H%M%S", localtime() );
730 16 ▶ if ( my $apacheSession =
Branch: 1 branch
BranchTrueFalseTotal
if (my $apacheSession = $self->getApacheSession($id, "info", $infos)) 16 0 16
731 $self->getApacheSession( $id, info => $infos ) )
732 {
733 16 ▶ if ( $apacheSession->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($apacheSession->error) 0 16 16
734 0 $self->logger->error("Cannot update session $id");
735 0 $self->logger->error( $apacheSession->error );
736 }
737 }
738  
739 # remove the corresponding session from handler cache
740 16 HANDLER->publishEvent( $req, { action => 'unlog', id => $id } );
741 }
742 }
743  
744 # Delete an existing session. If "securedCookie" is set to 2, the http session
745 # will also be removed.
746 # @param h tied Apache::Session object
747 # @param preserveCookie do not delete cookie
748 # @return True if session has been deleted
749 sub _deleteSession {
750 50 ▶ my ( $self, $req, $session, $preserveCookie ) = @_;
Subroutine _deleteSession: called CC=10 SCAR=33.0
751  
752 # Invalidate http cookie and session, if set
753 50 ▶ if ( $self->conf->{securedCookie} >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"securedCookie"} >= 2) 0 50 50
754  
755 # Try to find a linked http session (securedCookie == 2)
756 0 ▶ if ( $self->conf->{securedCookie} == 2
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"securedCookie"} == 2 and my $id2 = $session->data->{"_httpSession"}) 0 0 0
Condition0%0/3
$self->conf->{"securedCookie"} == 2 and my $id2 = $session->data->{"_httpSession"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->conf->{"securedCookie"} == 2 and my $id2 = $session->data->{"_httpSession"}
$self->conf->{"securedCookie"} == 2 my $id2 = $session->data->{"_httpSession"}
Truth table0%0/3
A and B
A= $self->conf->{"securedCookie"} == 2B= my $id2 = $session->data->{"_httpSession"}
ABresult
0X0
100
111
757 and my $id2 = $session->data->{_httpSession} )
758 {
759 0 ▶ if ( my $session2 = $self->getApacheSession($id2) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $session2 = $self->getApacheSession($id2)) 0 0 0
760 0 $session2->remove;
761 0 ▶ if ( $session2->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session2->error) 0 0 0
762 0 $self->logger->debug(
763 "Unable to remove linked session $id2");
764 0 $self->logger->debug( $session2->error );
765 }
766 }
767 }
768  
769 # Create an obsolete cookie to remove it
770 $req->addCookie(
771 $self->genDomainCookie(
772 $req,
773 0 ▶ name => $self->conf->{cookieName} . 'http',
Branch: 1 branch
BranchTrueFalseTotal
unless $preserveCookie 0 0 0
774 value => 0,
775 secure => 0,
776 expires => 'Wed, 21 Oct 2015 00:00:00 GMT'
777 )
778 ) unless ($preserveCookie);
779 }
780  
781 # Publishing an "unlog" event will automatically remove the corresponding
782 # session from handler cache. See publishEvent into Handler::Main::Run
783 50 HANDLER->publishEvent( $req, { action => 'unlog', id => $session->id } );
784 50 $session->remove;
785  
786 # Create an obsolete cookie to remove it
787 $req->addCookie(
788 $self->genDomainCookie(
789 $req,
790 name => $self->conf->{cookieName},
791 value => 0,
792 secure => $self->conf->{securedCookie},
793 50 ▶ expires => 'Wed, 21 Oct 2015 00:00:00 GMT'
Branch: 1 branch
BranchTrueFalseTotal
unless $preserveCookie 43 7 50
794 )
795 ) unless ($preserveCookie);
796  
797 # Log
798 50 my $user = $req->{sessionInfo}->{ $self->conf->{whatToTrace} };
799 50 my $mod = $req->{sessionInfo}->{_auth};
800 50 ▶ if ($user) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user) 50 0 50
801 50 $self->auditLog(
802 $req,
803 message => (
804 "User $user has been disconnected from $mod ($req->{sessionInfo}->{ipAddr})"
805 ),
806 code => "LOGOUT",
807 user => $user,
808 auth => $mod,
809 );
810 }
811  
812 50 ▶ return $session->error ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$session->error ? : 1 49 50
813 }
814  
815 # Check if an URL's domain name is declared in LL::NG config or is declared as
816 # trusted domain
817 sub isTrustedUrl {
818 65 ▶ my ( $self, $url ) = @_;
Subroutine isTrustedUrl: called CC=2 SCAR=6.9
Pod undocumented
819 65 ▶ return $url =~ $self->trustedDomainsRe ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$url =~ $self->trustedDomainsRe ? : 52 13 65
820 }
821  
822 sub stamp {
823 3 ▶ my $self = shift;
Subroutine stamp: called CC=2 SCAR=7.0
Pod undocumented
824 my $res =
825 $self->conf->{cipher}
826 3 ▶ ? $self->conf->{cipher}->encrypt( time() )
Branch: 1 branch
BranchTrueFalseTotal
$self->conf->{'cipher'} ? : 3 0 3
827 : 1;
828 3 $res =~ s/\+/%2B/g;
829 3 return $res;
830 }
831  
832 # Transfer POST data with auto submit
833 # @return void
834 sub autoPost {
835 1 ▶ my ( $self, $req ) = @_;
Subroutine autoPost: called CC=7 SCAR=25.2
Pod undocumented
836  
837 # Get URL and Form fields
838 1 $req->{urldc} = $req->postUrl;
839 1 ▶ if ( $req->{urldc} =~ /^\s*((?:java|vb)script|data):/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->{"urldc"} =~ /^\s*((?:java|vb)script|data):/) 0 1 1
840 $self->auditLog(
841 $req,
842 message => "Redirection to $req->{urldc} blocked",
843 code => "UNAUTHORIZED_REDIRECT",
844 url => $req->{urldc},
845 user => $req->sessionInfo->{ $self->conf->{whatToTrace} }
846 0 ▶ || $req->user,
Condition0%0/3
$req->sessionInfo->{$self->conf->{'whatToTrace'}} || $req->user
l!l&&r!l&&!r
000
MC/DC0%0/2
$req->sessionInfo->{$self->conf->{'whatToTrace'}} || $req->user
$req->sessionInfo->{$self->conf->{'whatToTrace'}} $req->user
Truth table0%0/3
A || B
A= $req->sessionInfo->{$self->conf->{'whatToTrace'}}B= $req->user
ABresult
1X1
011
000
847 );
848 0 return PE_BADURL;
849 }
850 1 my $formFields = $req->postFields;
851  
852 1 $self->clearHiddenFormValue($req);
853 1 foreach ( keys %$formFields ) {
854 1 $self->setHiddenFormValue( $req, $_, $formFields->{$_}, "", 0 );
855 }
856  
857 # Display info before redirecting
858 1 ▶ if ( $req->info() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->info) 0 1 1
859 0 ▶ $req->data->{infoFormMethod} = $req->param('method') || "post";
Condition0%0/2
$req->param('method') || 'post'
l!l
00
MC/DC0%0/1
$req->param('method') || 'post'
$req->param('method')
Truth table0%0/2
A
A= $req->param('method')
Aresult
11
00
860 0 return PE_INFO;
861 }
862  
863 1 $req->data->{redirectFormMethod} = "post";
864  
865 # Case 2F wait
866 1 ▶ if ( $req->data->{sfWait} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->data->{"sfWait"}) 1 0 1
867 1 return PE_2FWAIT;
868 }
869  
870 # Default case -> redirect
871 0 return PE_REDIRECT;
872 }
873  
874 # Add element into $self->{portalHiddenFormValues}, those values could be
875 # used to hide values into HTML form.
876 # @param fieldname The field name which will contain the corresponding value
877 # @param value The associated value
878 # @param prefix Prefix of the field key
879 # @param base64 Encode value in base64
880 # @return nothing
881 sub setHiddenFormValue {
882 102 ▶ my ( $self, $req, $key, $val, $prefix, $base64 ) = @_;
Subroutine setHiddenFormValue: called CC=7 SCAR=21.5
Pod undocumented
883  
884 # Default values
885 102 ▶ $prefix = "lmhidden_" unless defined $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefix 0 102 102
886 102 ▶ $base64 = 1 unless defined $base64;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $base64 0 102 102
887 102 ▶ $val = '' unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 102 102
888  
889 # Store value
890 102 ▶ if ( defined $val or !( $val & ~$val ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val or not $val & ~$val) 102 0 102
Condition33%1/3
defined $val or not $val & ~$val
l!l&&r!l&&!r
10200
MC/DC0%0/2
defined $val or not $val & ~$val
defined $val not $val & ~$val
Truth table33%1/3
A or not(B)
A= defined $valB= not $val & ~$val
ABresult
1X1
011
000
891 102 $key = $prefix . $key;
892  
893 102 ▶ $val = encode_base64($val) if $base64;
Branch: 1 branch
BranchTrueFalseTotal
if $base64 0 102 102
894 102 $req->{portalHiddenFormValues}->{$key} = $val;
895 102 $self->logger->debug("Store $val in hidden key $key");
896 }
897 }
898  
899 ## @method public void getHiddenFormValue(string fieldname, string prefix, boolean base64)
900 # Get value into $self->{portalHiddenFormValues}.
901 # @param fieldname The existing field name which contains a value
902 # @param prefix Prefix of the field key
903 # @param base64 Decode value from base64
904 # @return string The associated value
905 sub getHiddenFormValue {
906 103 ▶ my ( $self, $req, $key, $prefix, $base64 ) = @_;
Subroutine getHiddenFormValue: called CC=5 SCAR=19.7
Pod undocumented
907  
908 # Default values
909 103 ▶ $prefix = "lmhidden_" unless defined $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefix 103 0 103
910 103 ▶ $base64 = 1 unless defined $base64;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $base64 103 0 103
911  
912 103 $key = $prefix . $key;
913  
914 # Get value
915 103 my $val = $req->param($key);
916 103 ▶ if ( defined $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 103 103
917 0 ▶ $val = decode_base64($val) if $base64;
Branch: 1 branch
BranchTrueFalseTotal
if $base64 0 0 0
918 0 $self->logger->debug("Hidden value $val found for key $key");
919 0 return $val;
920 }
921  
922 # No value found
923 103 return undef;
924 }
925  
926 ## @method protected void clearHiddenFormValue(arrayref keys)
927 # Clear values form stored hidden fields
928 # Delete all keys if no keys provided
929 # @param keys Array reference of keys
930 # @return nothing
931 sub clearHiddenFormValue {
932 20 ▶ my ( $self, $req, $keys ) = @_;
Subroutine clearHiddenFormValue: called CC=3 SCAR=11.0
Pod undocumented
933  
934 20 ▶ unless ( defined $keys ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $keys) { } 6 14 20
935 14 delete $req->{portalHiddenFormValues};
936 14 $self->logger->debug("Delete all hidden values");
937 }
938 else {
939 6 foreach (@$keys) {
940 6 delete $req->{portalHiddenFormValues}->{$_};
941 6 $self->logger->debug("Delete hidden value for key $_");
942 }
943 }
944  
945 20 return;
946 }
947  
948 # Get the first value of a multivaluated session value
949 sub getFirstValue {
950 0 ▶ my ( $self, $value ) = @_;
Subroutine getFirstValue: not called CC=1 SCAR=6.9
Pod undocumented
951  
952 0 my @values = split /$self->{conf}->{multiValuesSeparator}/, $value;
953  
954 0 return $values[0];
955 }
956  
957 sub info {
958 0 ▶ my ( $self, $req, $info ) = @_;
Subroutine info: not called CC=1 SCAR=6.9
Pod undocumented
959 0 return $req->info($info);
960 }
961  
962 sub fullUrl {
963 49 ▶ my ( $self, $req ) = @_;
Subroutine fullUrl: called CC=1 SCAR=0.0
Pod undocumented
964 49 my $pHost = $req->portal;
965 49 $pHost =~ s#^(https?://[^/]+)(?:/.*)?$#$1#;
966 49 return $pHost . $req->env->{REQUEST_URI};
967 }
968  
969 # Generates a cookie header which can depend on the request
970 # If no domain was explicitely specified, use the default SSO domain
971 sub genDomainCookie {
972 251 ▶ my ( $self, $req, %h ) = @_;
Subroutine genDomainCookie: called CC=2 SCAR=6.9
Pod undocumented
973  
974 251 ▶ $h{domain} ||= $self->getCookieDomain($req);
Condition100%3/3
$h{"domain"} ||= $self->getCookieDomain($req)
l!l&&r!l&&!r
42461
MC/DC100%2/2
$h{"domain"} ||= $self->getCookieDomain($req)
$h{"domain"} $self->getCookieDomain($req)
Truth table100%3/3
A ||= B
A= $h{"domain"}B= $self->getCookieDomain($req)
ABresult
1X1
011
000
975 251 return $self->cookie(%h);
976 }
977  
978 # DEPRECATED, #3228
979 sub genCookie {
980 0 ▶ my ( $self, $req, %h ) = @_;
Subroutine genCookie: not called CC=1 SCAR=6.9
Pod undocumented
981 0 return $self->cookie(%h);
982 }
983  
984 # Generate a cookie header
985 # If no domain was explicitely specified, only the portal will see
986 # this cookie
987 sub cookie {
988 343 ▶ my ( $self, %h ) = @_;
Subroutine cookie: called CC=4 SCAR=15.0
Pod undocumented
989 343 ▶ $h{path} ||= '/';
Condition50%1/2
$h{"path"} ||= "/"
l!l
0343
MC/DC0%0/1
$h{"path"} ||= "/"
$h{"path"}
Truth table50%1/2
A
A= $h{"path"}
Aresult
11
00
990 343 ▶ $h{HttpOnly} //= $self->conf->{httpOnly};
Condition66%2/3
$h{"HttpOnly"} //= $self->conf->{"httpOnly"}
l!l&&r!l&&!r
23410
MC/DC0%0/2
$h{"HttpOnly"} //= $self->conf->{"httpOnly"}
$h{"HttpOnly"} $self->conf->{"httpOnly"}
Truth table66%2/3
A //= B
A= $h{"HttpOnly"}B= $self->conf->{"httpOnly"}
ABresult
1X1
011
000
991 343 ▶ $h{SameSite} ||= $self->cookieSameSite;
Condition33%1/3
$h{"SameSite"} ||= $self->cookieSameSite
l!l&&r!l&&!r
03430
MC/DC0%0/2
$h{"SameSite"} ||= $self->cookieSameSite
$h{"SameSite"} $self->cookieSameSite
Truth table33%1/3
A ||= B
A= $h{"SameSite"}B= $self->cookieSameSite
ABresult
1X1
011
000
992  
993 343 return $self->assemble_cookie(%h);
994 }
995  
996 sub assemble_cookie {
997 343 ▶ my ( $self, %h ) = @_;
Subroutine assemble_cookie: called CC=5 SCAR=16.1
Pod undocumented
998  
999 343 my @res;
1000 343 ▶ $res[0] = "$h{name}" or die("name required");
Branch: 1 branch
BranchTrueFalseTotal
$res[0] = qq[$h{"name"}] or die "name required" 0 343 343
1001 343 $res[0] .= "=$h{value}";
1002  
1003 343 foreach (qw(domain path expires max_age HttpOnly SameSite)) {
1004 2058 my $f = $_;
1005 2058 $f =~ s/_/-/g;
1006 2058 ▶ push @res, "$f=$h{$_}" if ( $h{$_} );
Branch: 1 branch
BranchTrueFalseTotal
if $h{$_} 1405 653 2058
1007 }
1008 343 ▶ push @res, 'secure' if ( $h{secure} );
Branch: 1 branch
BranchTrueFalseTotal
if $h{"secure"} 2 341 343
1009 343 return join( '; ', @res );
1010 }
1011  
1012 sub _dump {
1013 164 ▶ my ( $self, $variable ) = @_;
Subroutine _dump: called CC=2 SCAR=9.9
1014 164 ▶ if ( $self->conf->{logLevel} eq 'debug' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"logLevel"} eq "debug") 0 164 164
1015 0 require Data::Dumper;
1016 0 $Data::Dumper::Indent = 0;
1017 0 $Data::Dumper::Useperl = 1;
1018 0 $self->logger->debug( "Dump: " . Data::Dumper::Dumper($variable) );
1019 }
1020 164 return;
1021 }
1022  
1023 sub getSkinTplDir {
1024 491 ▶ my ( $self, $skin ) = @_;
Subroutine getSkinTplDir: called CC=2 SCAR=7.6
Pod undocumented
1025  
1026 491 ▶ my $base = $self->conf->{skinTemplateDir} || $self->conf->{templateDir};
Condition33%1/3
$self->conf->{'skinTemplateDir'} || $self->conf->{'templateDir'}
l!l&&r!l&&!r
04910
MC/DC0%0/2
$self->conf->{'skinTemplateDir'} || $self->conf->{'templateDir'}
$self->conf->{'skinTemplateDir'} $self->conf->{'templateDir'}
Truth table33%1/3
A || B
A= $self->conf->{'skinTemplateDir'}B= $self->conf->{'templateDir'}
ABresult
1X1
011
000
1027 491 return ( $base . '/' . $skin );
1028 }
1029  
1030 # Warning: this function returns a JSON string
1031 sub getTrOver {
1032 393 ▶ my ( $self, $req, $templateDir ) = @_;
Subroutine getTrOver: called CC=9 SCAR=34.4
Pod undocumented
1033  
1034 393 ▶ $templateDir //= $self->getSkinTplDir( $self->getSkin($req) );
Condition33%1/3
$templateDir //= $self->getSkinTplDir($self->getSkin($req))
l!l&&r!l&&!r
39300
MC/DC0%0/2
$templateDir //= $self->getSkinTplDir($self->getSkin($req))
$templateDir $self->getSkinTplDir($self->getSkin($req))
Truth table33%1/3
A //= B
A= $templateDirB= $self->getSkinTplDir($self->getSkin($req))
ABresult
1X1
011
000
1035  
1036 393 ▶ unless ( $self->trOverCache->{$templateDir} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->trOverCache->{$templateDir}) 165 228 393
1037  
1038 # Override messages
1039 165 my $trOverMessages = JSON::from_json( $self->trOver );
1040  
1041 165 opendir( DIR, $templateDir );
1042 165 my @langfiles = grep( /\.json$/, readdir(DIR) );
1043 165 close(DIR);
1044  
1045 165 foreach my $file (@langfiles) {
1046 0 my ($lang) = ( $file =~ /^(\w+)\.json/ );
1047 0 $self->logger->debug("Use $file to override messages");
1048 0 ▶ if ( open my $json, "<", $templateDir . "/" . $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $json, '<', $templateDir . '/' . $file) { } 0 0 0
1049 0 my $trdata;
1050 0 eval {
1051 0 local $/ = undef;
1052 0 $trdata = JSON::from_json(<$json>);
1053 };
1054 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1055 0 $self->logger->warn("Ignoring $file because of error: $@");
1056 }
1057 0 ▶ if ( ref($trdata) eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $trdata eq "HASH") 0 0 0
1058 0 for my $msg ( keys %$trdata ) {
1059  
1060 # lemonldap-ng.ini has priority
1061 0 ▶ $trOverMessages->{$lang}->{$msg} //= $trdata->{$msg};
Condition0%0/3
$trOverMessages->{$lang}{$msg} //= $trdata->{$msg}
l!l&&r!l&&!r
000
MC/DC0%0/2
$trOverMessages->{$lang}{$msg} //= $trdata->{$msg}
$trOverMessages->{$lang}{$msg} $trdata->{$msg}
Truth table0%0/3
A //= B
A= $trOverMessages->{$lang}{$msg}B= $trdata->{$msg}
ABresult
1X1
011
000
1062 }
1063 }
1064 }
1065 else {
1066 0 $self->logger->error("Unable to read $file");
1067 }
1068 }
1069  
1070 165 $self->trOverCache->{$templateDir} = JSON::to_json($trOverMessages);
1071 }
1072  
1073 393 return $self->trOverCache->{$templateDir};
1074 }
1075  
1076 sub sendHtml {
1077 393 ▶ my ( $self, $req, $template, %args ) = @_;
Subroutine sendHtml: called CC=20 SCAR=32.7
Pod documented
1078 393 ▶ if ( $self->conf->{logParams} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"logParams"}) 0 393 393
1079 0 $self->_dump($req);
1080 0 $self->_dump( \%args );
1081 }
1082  
1083 393 my $skin_template_dir = $self->getSkinTplDir( $self->getSkin($req) );
1084  
1085 # Look for templates in skin subdirectories instead of templateDir
1086 $args{templateDir} =
1087 393 [ $skin_template_dir, $self->conf->{templateDir} . "/bootstrap" ];
1088  
1089 393 $args{params}->{TROVER} = $self->getTrOver( $req, $skin_template_dir );
1090  
1091 393 $self->processHook( $req, 'sendHtml', \$template, \%args );
1092  
1093 393 my $res = $self->SUPER::sendHtml( $req, $template, %args );
1094 393 push @{ $res->[1] },
1095 'X-Content-Type-Options' => 'nosniff',
1096 'Cache-Control' => 'no-cache, no-store, must-revalidate', # HTTP 1.1
1097 'Pragma' => 'no-cache', # HTTP 1.0
1098 'Expires' => '0'; # Proxies
1099  
1100 393 $self->setCorsHeaderFromConfig($res);
1101  
1102 393 ▶ if ( $self->conf->{strictTransportSecurityMax_Age}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"strictTransportSecurityMax_Age"} and $req->portal =~ /^https:/) 0 393 393
Condition66%2/3
$self->conf->{"strictTransportSecurityMax_Age"} and $req->portal =~ /^https:/
!ll&&!rl&&r
39210
MC/DC0%0/2
$self->conf->{"strictTransportSecurityMax_Age"} and $req->portal =~ /^https:/
$self->conf->{"strictTransportSecurityMax_Age"} $req->portal =~ /^https:/
Truth table66%2/3
A and B
A= $self->conf->{"strictTransportSecurityMax_Age"}B= $req->portal =~ /^https:/
ABresult
0X0
100
111
1103 and $req->portal =~ /^https:/ )
1104 {
1105 0 push @{ $res->[1] },
1106 'Strict-Transport-Security' =>
1107 "max-age=$self->{conf}->{strictTransportSecurityMax_Age}";
1108 0 $self->logger->debug(
1109 "Set Strict-Transport-Security with: $self->{conf}->{strictTransportSecurityMax_Age}"
1110 );
1111 }
1112  
1113 # Set authorized URL for POST
1114 393 my $csp = $self->csp . "form-action " . $self->conf->{cspFormAction};
1115 393 ▶ if ( my $url = $req->urldc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $url = $req->urldc) 34 359 393
1116 34 $self->logger->debug("Required urldc: $url");
1117 34 my $host = $self->cspGetHost($url);
1118 34 ▶ if ($host) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host) 34 0 34
1119 34 $self->logger->debug("Set CSP form-action with urldc: $url");
1120 34 $csp .= " $host";
1121 }
1122 }
1123 393 my $url = $args{params}->{URL};
1124 393 ▶ if ( defined $url ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $url) 15 378 393
1125 15 $self->logger->debug("Required Params URL: $url");
1126 15 my $host = $self->cspGetHost($url);
1127 15 ▶ if ($host) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host) 15 0 15
1128 15 $self->logger->debug("Set CSP form-action with Params URL: $url");
1129 15 $csp .= " $host";
1130 }
1131 }
1132 393 ▶ if ( defined $req->data->{cspFormAction}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $req->data->{"cspFormAction"} and ref $req->data->{"cspFormAction"} eq "HASH") 17 376 393
Condition66%2/3
defined $req->data->{"cspFormAction"} and ref $req->data->{"cspFormAction"} eq "HASH"
!ll&&!rl&&r
376017
MC/DC50%1/2
defined $req->data->{"cspFormAction"} and ref $req->data->{"cspFormAction"} eq "HASH"
defined $req->data->{"cspFormAction"} ref $req->data->{"cspFormAction"} eq "HASH"
Truth table66%2/3
A and B
A= defined $req->data->{"cspFormAction"}B= ref $req->data->{"cspFormAction"} eq "HASH"
ABresult
0X0
100
111
1133 and ref( $req->data->{cspFormAction} ) eq "HASH" )
1134 {
1135 my $request_csp_form_action =
1136 17 join( " ", keys %{ $req->data->{cspFormAction} } );
1137 17 $self->logger->debug( "Set CSP form-action with request URL: "
1138 . $request_csp_form_action );
1139 17 $csp .= " " . $request_csp_form_action;
1140 }
1141  
1142 # Set SAML Discovery Protocol in form-action
1143 # See https://github.com/w3c/webappsec-csp/issues/8
1144 393 ▶ if ( $self->conf->{samlDiscoveryProtocolActivation}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"samlDiscoveryProtocolActivation"} and defined $self->conf->{"samlDiscoveryProtocolURL"}) 0 393 393
Condition33%1/3
$self->conf->{"samlDiscoveryProtocolActivation"} and defined $self->conf->{"samlDiscoveryProtocolURL"}
!ll&&!rl&&r
39300
MC/DC0%0/2
$self->conf->{"samlDiscoveryProtocolActivation"} and defined $self->conf->{"samlDiscoveryProtocolURL"}
$self->conf->{"samlDiscoveryProtocolActivation"} defined $self->conf->{"samlDiscoveryProtocolURL"}
Truth table33%1/3
A and B
A= $self->conf->{"samlDiscoveryProtocolActivation"}B= defined $self->conf->{"samlDiscoveryProtocolURL"}
ABresult
0X0
100
111
1145 and defined $self->conf->{samlDiscoveryProtocolURL} )
1146 {
1147 0 $self->logger->debug(
1148 "Add SAML Discovery Protocol URL in CSP form-action");
1149 0 $csp .= " " . $self->conf->{samlDiscoveryProtocolURL};
1150 }
1151 393 $csp .= ';';
1152  
1153 # Deny using portal in frame except if it is required
1154 393 ▶ unless ( $req->frame
Branch: 1 branch
BranchTrueFalseTotal
unless ($req->frame or $self->conf->{"portalAntiFrame"} == 0 or $self->conf->{"cspFrameAncestors"}) 381 12 393
Condition66%4/6
$req->frame or $self->conf->{"portalAntiFrame"} == 0 or $self->conf->{"cspFrameAncestors"}
l!l&&r!l&&!r
120381
$req->frame or $self->conf->{"portalAntiFrame"} == 0
l!l&&r!l&&!r
120381
MC/DC33%1/3
$req->frame or $self->conf->{"portalAntiFrame"} == 0 or $self->conf->{"cspFrameAncestors"}
$req->frame $self->conf->{"portalAntiFrame"} == 0 $self->conf->{"cspFrameAncestors"}
Truth table50%2/4
A or B or C
A= $req->frameB= $self->conf->{"portalAntiFrame"} == 0C= $self->conf->{"cspFrameAncestors"}
ABCresult
1XX1
01X1
0011
0000
1155 or $self->conf->{portalAntiFrame} == 0
1156 or $self->conf->{cspFrameAncestors} )
1157 {
1158 381 push @{ $res->[1] }, 'X-Frame-Options' => 'DENY';
1159 381 $csp .= "frame-ancestors 'none';";
1160 }
1161 393 ▶ if ( $self->conf->{cspFrameAncestors} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"cspFrameAncestors"}) 0 393 393
1162 0 push @{ $res->[1] }, 'X-Frame-Options' => 'ALLOW-FROM '
1163 . "$self->{conf}->{cspFrameAncestors};";
1164 0 $csp .= "frame-ancestors $self->{conf}->{cspFrameAncestors};";
1165 }
1166  
1167 # Check if frames need to be embedded
1168 # FIXME: we should use $req->data->{cspChildSrc} anywhere an iframe is
1169 # created in the code, and remove this
1170 393 my @url;
1171 393 ▶ if ( $req->info ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->info) 14 379 393
1172 14 @url = map { s#https?://([^/]+).*#$1#r }
1173 ( $req->info =~ /<iframe.*?src="(.*?)"/sg );
1174 }
1175  
1176 # Update child-src header from request data
1177 393 ▶ if ( ref( $req->data->{cspChildSrc} ) eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $req->data->{"cspChildSrc"} eq "HASH") 0 393 393
1178 0 push @url, keys %{ $req->data->{cspChildSrc} };
1179 }
1180 393 ▶ if (@url) {
Branch: 1 branch
BranchTrueFalseTotal
if (@url) 1 392 393
1181 1 $csp .= join( ' ', 'child-src', @url, "'self'" ) . ';';
1182 }
1183  
1184 # Set CSP header
1185 393 push @{ $res->[1] }, 'Content-Security-Policy' => $csp
1186 393 ▶ unless $req->data->{dropCsp};
Branch: 1 branch
BranchTrueFalseTotal
unless $req->data->{"dropCsp"} 393 0 393
1187 393 $self->logger->debug("Apply following CSP: $csp");
1188 393 return $res;
1189 }
1190  
1191 sub imgok {
1192 4 ▶ my ( $self, $req, ) = @_;
Subroutine imgok: called CC=1 SCAR=0.0
Pod undocumented
1193 4 return $self->sendImage( $req, 'icons/ok.png' );
1194 }
1195  
1196 sub imgnok {
1197 0 ▶ my ( $self, $req, ) = @_;
Subroutine imgnok: not called CC=1 SCAR=6.9
Pod undocumented
1198 0 return $self->sendImage( $req, 'icons/warning.png' );
1199 }
1200  
1201 sub sendImage {
1202 4 ▶ my ( $self, $req, $img ) = @_;
Subroutine sendImage: called CC=1 SCAR=0.0
Pod undocumented
1203 4 my $u = URI->new_abs( $self->staticPrefix . "/common/$img", $req->portal )
1204 ->as_string;
1205  
1206 4 return $self->sendRedirection( $req, $u );
1207 }
1208  
1209 sub sendCss {
1210 0 ▶ my ( $self, $req ) = @_;
Subroutine sendCss: not called CC=2 SCAR=17.9
Pod undocumented
1211 0 my $s = '/* LL::NG Portal CSS */';
1212 0 ▶ if ( $self->conf->{portalSkinBackground} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"portalSkinBackground"}) 0 0 0
1213 $s .=
1214 'html,body{background:url("'
1215 . $self->staticPrefix
1216 . '/common/backgrounds/'
1217 . $self->conf->{portalSkinBackground}
1218 0 . '") no-repeat center fixed;'
1219 . 'background-size:cover;}';
1220 }
1221 0 return $self->sendTextResponse(
1222 $req, $s,
1223 type => 'text/css',
1224 headers => [ 'Cache-Control' => 'public,max-age=3600' ]
1225 );
1226 }
1227  
1228 sub lmError {
1229 3 ▶ my ( $self, $req, $error ) = @_;
Subroutine lmError: called CC=6 SCAR=19.1
Pod undocumented
1230 3 ▶ my $httpError = $req->param('code') || $error;
Condition33%1/3
$req->param('code') || $error
l!l&&r!l&&!r
300
MC/DC0%0/2
$req->param('code') || $error
$req->param('code') $error
Truth table33%1/3
A || B
A= $req->param('code')B= $error
ABresult
1X1
011
000
1231  
1232 # Check URL
1233 3 $self->controlUrl($req);
1234 3 ▶ $req->pdata( {} ) unless ( $httpError == 404 );
Branch: 1 branch
BranchTrueFalseTotal
unless $httpError == 404 3 0 3
1235  
1236 3 ▶ if ( $req->wantJSON ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->wantJSON) 0 3 3
1237 0 return $self->sendJSONresponse(
1238 $req,
1239 { error => $httpError, result => 0 },
1240 code => $httpError
1241 );
1242 }
1243  
1244 my %templateParams = (
1245 MAIN_LOGO => $self->conf->{portalMainLogo},
1246 LANGS => $self->conf->{showLanguages},
1247 LOGOUT_URL => $self->buildUrl( $req->portal, { logout => 1 } ),
1248 URL => $req->{urldc},
1249 3 );
1250  
1251 # Error code
1252 $templateParams{"ERROR$_"} = ( $httpError == $_ ? 1 : 0 )
1253 3 ▶ foreach ( 403, 404, 500, 502, 503 );
Branch: 1 branch
BranchTrueFalseTotal
$httpError == $_ ? : 3 12 15
1254 3 return $self->sendHtml( $req, 'error', params => \%templateParams );
1255 }
1256  
1257 sub rebuildCookies {
1258 0 ▶ my ( $self, $req ) = @_;
Subroutine rebuildCookies: not called CC=3 SCAR=24.8
Pod undocumented
1259 0 my @tmp;
1260 0 ▶ for ( my $i = 0 ; $i < @{ $req->{respHeaders} } ; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$req->{"respHeaders"};}) 0 0 0
1261 0 ▶ push @tmp, $req->respHeaders->[$i], $req->respHeaders->[ $i + 1 ]
Branch: 1 branch
BranchTrueFalseTotal
unless $req->respHeaders->[$i] eq "Set-Cookie" 0 0 0
1262 unless ( $req->respHeaders->[$i] eq 'Set-Cookie' );
1263 }
1264 0 $req->{respHeaders} = \@tmp;
1265 0 $self->buildCookie($req);
1266 }
1267  
1268 sub tplParams {
1269 415 ▶ my ( $self, $req ) = @_;
Subroutine tplParams: called CC=7 SCAR=19.6
Pod undocumented
1270 415 my %templateParams;
1271  
1272 415 my $portalPath = $req->portal;
1273 415 $portalPath =~ s#^https?://[^/]+/?#/#;
1274 415 $portalPath =~ s#[^/]+\.fcgi$##;
1275  
1276 415 for my $session_key ( keys %{ $req->{sessionInfo} } ) {
1277 $templateParams{ "session_" . $session_key } =
1278 2630 $req->{sessionInfo}->{$session_key};
1279 }
1280  
1281 415 for my $env_key ( keys %{ $req->env } ) {
1282 8867 $templateParams{ "env_" . $env_key } = $req->env->{$env_key};
1283 }
1284  
1285 415 for my $custom_key ( keys %{ $self->conf->{portalCustomTplParams} } ) {
1286 $templateParams{$custom_key} =
1287 0 $self->conf->{portalCustomTplParams}->{$custom_key};
1288 }
1289  
1290 return (
1291 CACHE_TAG => $self->cacheTag,
1292 PORTAL_URL => $req->portal,
1293 PORTAL_BASE => $portalPath,
1294 MAIN_LOGO => $self->conf->{portalMainLogo},
1295 LANGS => $self->conf->{showLanguages},
1296 SCROLL_TOP => $self->conf->{scrollTop},
1297 FLOATING_CAT => $self->conf->{floatingCategoryName},
1298 SKIN => $self->getSkin($req),
1299 SKIN_PATH => $portalPath . "skins",
1300 SAMESITE => getSameSite( $self->conf ),
1301 COOKIESECURE => ( $self->conf->{securedCookie} ? 1 : 0 ),
1302 SKIN_BG => $self->conf->{portalSkinBackground},
1303 FAVICON => $self->conf->{portalFavicon} || 'common/favicon.ico',
1304 LANGUAGE => $self->getLanguage($req),
1305 CUSTOM_CSS => $self->conf->{portalCustomCss},
1306 CUSTOM_JS => $self->conf->{portalCustomJs},
1307 (
1308 $self->customParameters
1309 415 ▶ ? ( %{ $self->customParameters } )
Branch: 2 branches
BranchTrueFalseTotal
$self->conf->{'securedCookie'} ? : 11 404 415
$self->customParameters ? : 415 0 415
Condition50%1/2
$self->conf->{'portalFavicon'} || 'common/favicon.ico'
l!l
4150
MC/DC0%0/1
$self->conf->{'portalFavicon'} || 'common/favicon.ico'
$self->conf->{'portalFavicon'}
Truth table50%1/2
A
A= $self->conf->{'portalFavicon'}
Aresult
11
00
1310 : ()
1311 ),
1312 %templateParams
1313 );
1314 }
1315  
1316 sub getErrorTplParams {
1317 378 ▶ my ( $self, $req ) = @_;
Subroutine getErrorTplParams: called CC=1 SCAR=0.0
Pod undocumented
1318  
1319 378 my $err = $req->error;
1320  
1321 return (
1322 378 AUTH_ERROR => $err,
1323 ( 'AUTH_ERROR_' . $err ) => 1,
1324 AUTH_ERROR_TYPE => $req->error_type,
1325 AUTH_ERROR_ROLE => $req->error_role,
1326 LOCKTIME => $req->lockTime(),
1327 );
1328 }
1329  
1330 sub registerLogin {
1331 0 ▶ my ( $self, $req ) = @_;
Subroutine registerLogin: not called CC=14 SCAR=53.5
Pod undocumented
1332 return
1333 unless ( $self->conf->{loginHistoryEnabled}
1334 0 ▶ and defined $req->authResult );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->conf->{"loginHistoryEnabled"} and defined $req->authResult 0 0 0
Condition0%0/3
$self->conf->{"loginHistoryEnabled"} and defined $req->authResult
!ll&&!rl&&r
000
MC/DC0%0/2
$self->conf->{"loginHistoryEnabled"} and defined $req->authResult
$self->conf->{"loginHistoryEnabled"} defined $req->authResult
Truth table0%0/3
A and B
A= $self->conf->{"loginHistoryEnabled"}B= defined $req->authResult
ABresult
0X0
100
111
1335  
1336 # Do not store login blocked by BruteForceProtection (#3561)
1337 0 ▶ return if $req->authResult == PE_WAIT;
Branch: 1 branch
BranchTrueFalseTotal
if $req->authResult == 86 0 0 0
1338  
1339 # Check old login history
1340 0 ▶ if ( $req->sessionInfo->{loginHistory} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->sessionInfo->{"loginHistory"}) 0 0 0
1341  
1342 0 ▶ if ( !$req->sessionInfo->{_loginHistory} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($req->sessionInfo->{"_loginHistory"}) 0 0 0
1343 0 $self->logger->debug("Restore old login history");
1344  
1345 # Restore success login
1346 $req->sessionInfo->{_loginHistory}->{successLogin} =
1347 0 $req->sessionInfo->{loginHistory}->{successLogin};
1348  
1349 # Restore failed login, with generic error
1350 0 ▶ if ( $req->sessionInfo->{loginHistory}->{failedLogin} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->sessionInfo->{"loginHistory"}{"failedLogin"}) 0 0 0
1351 0 $self->logger->debug("Restore old failed logins");
1352 0 $req->sessionInfo->{_loginHistory}->{failedLogin} = [];
1353 0 foreach (
1354 0 @{ $req->sessionInfo->{loginHistory}->{failedLogin} } )
1355 {
1356 $self->logger->debug(
1357 0 "Replace old failed login error " . $_->{error} );
1358 0 $_->{error} = 5;
1359 0 push @{ $req->sessionInfo->{_loginHistory}->{failedLogin} },
1360 $_;
1361 }
1362 }
1363 }
1364 0 $self->updatePersistentSession( $req, { 'loginHistory' => undef } );
1365 0 delete $req->sessionInfo->{loginHistory};
1366 }
1367  
1368 0 ▶ my $history = $req->sessionInfo->{_loginHistory} ||= {};
Condition0%0/2
$req->sessionInfo->{"_loginHistory"} ||= {}
l!l
00
MC/DC0%0/1
$req->sessionInfo->{"_loginHistory"} ||= {}
$req->sessionInfo->{"_loginHistory"}
Truth table0%0/2
A
A= $req->sessionInfo->{"_loginHistory"}
Aresult
11
00
1369 0 ▶ my $type = ( $req->authResult > 0 ? 'failed' : 'success' ) . 'Login';
Branch: 1 branch
BranchTrueFalseTotal
$req->authResult > 0 ? : 0 0 0
1370 0 ▶ $history->{$type} ||= [];
Condition0%0/2
$history->{$type} ||= []
l!l
00
MC/DC0%0/1
$history->{$type} ||= []
$history->{$type}
Truth table0%0/2
A
A= $history->{$type}
Aresult
11
00
1371 0 $self->logger->debug("Current login saved into $type");
1372  
1373 # Gather current login's parameters and force current time
1374 my $login =
1375 0 $self->_sumUpSession( { %{ $req->sessionInfo }, _utime => time }, 1 );
1376 0 ▶ $login->{error} = $self->error( $req->authResult )
Branch: 1 branch
BranchTrueFalseTotal
if $req->authResult 0 0 0
1377 if ( $req->authResult );
1378  
1379 $self->logger->debug( " Current login -> " . $login->{error} )
1380 0 ▶ if ( $login->{error} );
Branch: 1 branch
BranchTrueFalseTotal
if $login->{"error"} 0 0 0
1381  
1382 # Add current login into history
1383 0 unshift @{ $history->{$type} }, $login;
1384  
1385 # Forget oldest logins
1386 0 splice @{ $history->{$type} }, $self->conf->{ $type . "Number" }
1387 0 ▶ if ( scalar @{ $history->{$type} } > $self->conf->{ $type . "Number" } );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$history->{$type};} > $self->conf->{$type . 'Number'} 0 0 0
1388  
1389 # Save into persistent session
1390 0 $self->updatePersistentSession( $req, { _loginHistory => $history, } );
1391  
1392 0 PE_OK;
1393 }
1394  
1395 # Register access to protected app
1396 # @param Lemonldap::NG::Portal::Main::Request $req The request
1397 # @param string $uid The connected user ident
1398 # @param string $app The application client/RP namespaced with protocol
1399 sub registerProtectedAppAccess {
1400 20 ▶ my ( $self, $req, $uid, $app ) = @_;
Subroutine registerProtectedAppAccess: called CC=3 SCAR=19.6
Pod undocumented
1401  
1402 20 ▶ return unless $self->conf->{appAccessHistoryEnabled};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->conf->{"appAccessHistoryEnabled"} 20 0 20
1403  
1404 0 $self->logger->debug("Registering $app access for user $uid");
1405  
1406 0 my $persistentSession = $self->getPersistentSession($uid);
1407 0 ▶ my $entry = $persistentSession->data->{_appHistory}->{$app} || {};
Condition0%0/2
$persistentSession->data->{'_appHistory'}{$app} || {}
l!l
00
MC/DC0%0/1
$persistentSession->data->{'_appHistory'}{$app} || {}
$persistentSession->data->{'_appHistory'}{$app}
Truth table0%0/2
A
A= $persistentSession->data->{'_appHistory'}{$app}
Aresult
11
00
1408  
1409 0 $entry->{access_time} = time();
1410 0 $entry->{access_count}++;
1411  
1412 $self->updatePersistentSession(
1413 $req,
1414 {
1415 _appHistory => {
1416 0 %{ $persistentSession->data->{_appHistory} }, $app => $entry
1417 }
1418 },
1419 $uid
1420 );
1421 }
1422  
1423 # put main session data into a hash ref
1424 # @param hashref $session The session to sum up
1425 # @return hashref
1426 sub _sumUpSession {
1427 10 ▶ my ( $self, $session, $withoutUser ) = @_;
Subroutine _sumUpSession: called CC=3 SCAR=11.8
1428 my $res =
1429 $withoutUser
1430 ? {}
1431 10 ▶ : { user => $session->{ $self->conf->{whatToTrace} } };
Branch: 1 branch
BranchTrueFalseTotal
$withoutUser ? : 0 10 10
1432 $res->{$_} = $session->{$_}
1433 10 foreach (
1434 "_utime", "ipAddr",
1435 map {
1436 # Modifying key to remove ordering prefix
1437 0 $_ =~ s/(\d+_)?//;
1438 0 $_;
1439 10 } keys %{ $self->conf->{sessionDataToRemember} },
1440 10 keys %{ $self->pluginSessionDataToRemember }
1441 );
1442 10 return $res;
1443 }
1444  
1445 sub corsPreflight {
1446 1 ▶ my ( $self, $req ) = @_;
Subroutine corsPreflight: called CC=1 SCAR=0.0
Pod undocumented
1447 1 my @headers;
1448 1 my $res = $self->sendBinaryResponse( $req, '', code => 204 );
1449  
1450 1 $self->setCorsHeaderFromConfig($res);
1451  
1452 1 return $res;
1453 }
1454  
1455 sub sendJSONresponse {
1456 1087 ▶ my ( $self, $req, $j, %args ) = @_;
Subroutine sendJSONresponse: called CC=5 SCAR=16.6
Pod documented
1457 1087 ▶ $j->{token} = $req->token if $req->token;
Branch: 1 branch
BranchTrueFalseTotal
if $req->token 7 1080 1087
1458 1087 my $res = Lemonldap::NG::Common::PSGI::sendJSONresponse(@_);
1459  
1460 # Handle caching
1461 1087 ▶ if ( $args{ttl} and $args{ttl} =~ /^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'ttl'} and $args{'ttl'} =~ /^\d+$/) { } 0 1087 1087
Condition33%1/3
$args{"ttl"} and $args{"ttl"} =~ /^\d+$/
!ll&&!rl&&r
108700
MC/DC0%0/2
$args{"ttl"} and $args{"ttl"} =~ /^\d+$/
$args{"ttl"} $args{"ttl"} =~ /^\d+$/
Truth table33%1/3
A and B
A= $args{"ttl"}B= $args{"ttl"} =~ /^\d+$/
ABresult
0X0
100
111
1462 0 push @{ $res->[1] }, 'Cache-Control' => 'public, max-age=' . $args{ttl};
1463 }
1464 else {
1465 1087 push @{ $res->[1] },
1466 'Cache-Control' => 'no-cache, no-store, must-revalidate',
1467 'Pragma' => 'no-cache',
1468 'Expires' => '0';
1469 }
1470  
1471 # If this is a cross-domain request from the portal itself
1472 # (Ajax SSL to a different VHost)
1473 # we allow CORS
1474 1087 ▶ if ( $self->_checkSelfCors($req) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_checkSelfCors($req)) { } 2 1085 1087
1475 2 $self->logger->debug('AJAX request from portal, allowing CORS');
1476 2 push @{ $res->[1] },
1477 "Access-Control-Allow-Origin" => $req->origin,
1478 "Access-Control-Allow-Methods" => "*",
1479 "Access-Control-Allow-Credentials" => "true";
1480  
1481 }
1482 else {
1483 1085 $self->setCorsHeaderFromConfig($res);
1484 }
1485 1087 return $res;
1486 }
1487  
1488 sub _checkSelfCors {
1489 1087 ▶ my ( $self, $req ) = @_;
Subroutine _checkSelfCors: called CC=5 SCAR=16.2
1490  
1491 1087 ▶ if ( $req->origin ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->origin) 6 1081 1087
1492 6 my $origin = URI->new( $req->origin );
1493 6 my $portal = URI->new( $req->portal );
1494  
1495 6 ▶ return ( $origin->scheme
Condition77%7/9
$origin->scheme && $portal->scheme eq $origin->scheme && $origin->host_port && $origin->host_port eq $portal->host_port
!ll&&!rl&&r
132
$origin->scheme and $portal->scheme eq $origin->scheme and $origin->host_port
!ll&&!rl&&r
105
$origin->scheme and $portal->scheme eq $origin->scheme
!ll&&!rl&&r
015
MC/DC50%2/4
$origin->scheme && $portal->scheme eq $origin->scheme && $origin->host_port && $origin->host_port eq $portal->host_port
$origin->scheme $portal->scheme eq $origin->scheme $origin->host_port $origin->host_port eq $portal->host_port
Truth table60%3/5
A and B and C && D
A= $origin->schemeB= $portal->scheme eq $origin->schemeC= $origin->host_portD= $origin->host_port eq $portal->host_port
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1496 and $portal->scheme eq $origin->scheme
1497 and $origin->host_port
1498 and $origin->host_port eq $portal->host_port );
1499 }
1500 1081 return;
1501 }
1502  
1503 sub sendRawHtml {
1504 1446 ▶ my ($self) = $_[0];
Subroutine sendRawHtml: called CC=1 SCAR=0.0
Pod undocumented
1505 1446 my $res = Lemonldap::NG::Common::PSGI::sendRawHtml(@_);
1506  
1507 1446 $self->setCorsHeaderFromConfig($res);
1508  
1509 1446 return $res;
1510 }
1511  
1512 sub setCorsHeaderFromConfig {
1513 2930 ▶ my ( $self, $response ) = @_;
Subroutine setCorsHeaderFromConfig: called CC=3 SCAR=11.0
Pod undocumented
1514  
1515 2930 ▶ if ( $self->conf->{corsEnabled} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"corsEnabled"}) 2930 0 2930
1516 2930 my @cors = split /;/, $self->cors;
1517 2930 push @{ $response->[1] }, @cors;
1518 2930 $self->logger->debug('Apply following CORS policy:');
1519 2930 $self->logger->debug(" $_") for @cors;
1520 }
1521 }
1522  
1523 # Temlate loader
1524 sub loadTemplate {
1525 20 ▶ my ( $self, $req, $name, %prm ) = @_;
Subroutine loadTemplate: called CC=5 SCAR=16.6
Pod undocumented
1526  
1527 20 $self->processHook( $req, 'sendHtml', \$name, \%prm );
1528 20 $name .= '.tpl';
1529 my $tpl = HTML::Template->new(
1530 filename => $name,
1531 path => [
1532 $self->getSkinTplDir( $self->getSkin($req) ),
1533 $self->conf->{templateDir} . '/bootstrap/',
1534 $self->conf->{templateDir} . '/common/'
1535 ],
1536 search_path_on_include => 1,
1537 die_on_bad_params => 0,
1538 die_on_missing_include => 1,
1539 cache => ( defined $prm{cache} ? $prm{cache} : 1 ),
1540 global_vars => 0,
1541 20 ▶ ( $prm{filter} ? ( filter => $prm{filter} ) : () ),
Branch: 2 branches
BranchTrueFalseTotal
defined $prm{'cache'} ? : 0 20 20
$prm{'filter'} ? : 0 20 20
1542 );
1543 20 ▶ if ( $self->can('tplParams') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("tplParams")) 20 0 20
1544 20 $tpl->param( $self->tplParams($req) );
1545 }
1546 20 ▶ if ( $prm{params} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prm{"params"}) 20 0 20
1547 20 $tpl->param( %{ $prm{params} } );
1548 }
1549 20 return $tpl->output;
1550 }
1551  
1552 # This method extracts the scheme://host:port part of a URL for use in
1553 # Content-Security-Polity header
1554 sub cspGetHost {
1555 59 ▶ my ( $self, $url ) = @_;
Subroutine cspGetHost: called CC=8 SCAR=20.9
Pod undocumented
1556 59 ▶ return unless $url;
Branch: 1 branch
BranchTrueFalseTotal
unless $url 1 58 59
1557 58 my $uri = $url;
1558 58 ▶ unless ( blessed($uri) && $uri->isa("URI") ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($uri) and $uri->isa("URI")) 58 0 58
Condition33%1/3
builtin::blessed($uri) and $uri->isa("URI")
!ll&&!rl&&r
5800
MC/DC0%0/2
builtin::blessed($uri) and $uri->isa("URI")
builtin::blessed($uri) $uri->isa("URI")
Truth table33%1/3
A and B
A= builtin::blessed($uri)B= $uri->isa("URI")
ABresult
0X0
100
111
1559 58 $uri = URI->new($uri);
1560 }
1561 58 ▶ my $scheme = $uri->scheme || "";
Condition100%2/2
$uri->scheme || ''
l!l
553
MC/DC100%1/1
$uri->scheme || ''
$uri->scheme
Truth table100%2/2
A
A= $uri->scheme
Aresult
11
00
1562 58 ▶ if ( $scheme =~ /^https?/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($scheme =~ /^https?/) { } 54 4 58
elsif ($scheme) { } 1 3 4
1563 return (
1564 54 ▶ $scheme . "://"
Branch: 1 branch
BranchTrueFalseTotal
$uri->port == $uri->default_port ? : 53 1 54
1565 . (
1566 ( $uri->port == $uri->default_port )
1567 ? $uri->host
1568 : $uri->host_port
1569 )
1570 );
1571 }
1572 elsif ($scheme) {
1573 1 return ( $scheme . ":" );
1574 }
1575 3 return;
1576 }
1577  
1578 # Method that triggers back-channel logout notifications to federated
1579 # applications for a session being removed remotely (e.g., by GlobalLogout or
1580 # SingleSession).
1581 #
1582 # @param $req The current request object
1583 # @param $sessionData Hashref of the session data being deleted
1584 # @return PE_OK on success, error code otherwise
1585 sub _triggerBackChannelLogout {
1586 7 ▶ my ( $self, $req, $sessionData ) = @_;
Subroutine _triggerBackChannelLogout: called CC=4 SCAR=14.0
1587  
1588 7 ▶ return PE_OK unless $sessionData && ref($sessionData) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless $sessionData and ref $sessionData eq "HASH" 0 7 7
Condition33%1/3
$sessionData and ref $sessionData eq "HASH"
!ll&&!rl&&r
007
MC/DC0%0/2
$sessionData and ref $sessionData eq "HASH"
$sessionData ref $sessionData eq "HASH"
Truth table33%1/3
A and B
A= $sessionDataB= ref $sessionData eq "HASH"
ABresult
0X0
100
111
1589  
1590 7 $self->logger->debug(
1591 "Triggering back-channel logout for session $sessionData->{_session_id}"
1592 );
1593  
1594 # Save the original context
1595 7 my $originalUserData = $req->userData;
1596 7 my $originalSessionInfo = $req->sessionInfo;
1597 7 my $originalId = $req->id;
1598 7 ▶ my @steps = @{ $req->steps || [] };
Branch: 1 branch
BranchTrueFalseTotal
$req->steps or [] 0 7 7
1599 7 my $noFail = $req->data->{nofail};
1600 7 my $originalError = $req->error;
1601  
1602 # Inject the data of the session being deleted
1603 7 $req->userData($sessionData);
1604 7 $req->sessionInfo($sessionData);
1605 7 $req->id( $sessionData->{_session_id} );
1606 7 $req->steps( [ @{ $self->beforeLogout } ] );
1607  
1608 # Iterate over beforeLogout entry points
1609 7 my $result = $self->process( $req, nofail => 1 );
1610  
1611 # Restore the original context
1612 7 $req->userData($originalUserData);
1613 7 $req->sessionInfo($originalSessionInfo);
1614 7 $req->id($originalId);
1615 7 $req->steps( \@steps );
1616 7 $req->data->{nofail} = $noFail;
1617 7 $req->error($originalError);
1618  
1619 7 return $result;
1620 }
1621  
1622 sub relativeUrl {
1623 6 ▶ my $self = shift;
Subroutine relativeUrl: called CC=1 SCAR=0.0
Pod undocumented
1624 6 my $req = shift;
1625  
1626 6 my $uri = $self->_buildUrlObj( $req->portal, @_ );
1627 6 return $uri->path_query;
1628 }
1629  
1630 sub buildUrl {
1631 462 ▶ my $self = shift;
Subroutine buildUrl: called CC=2 SCAR=6.9
Pod undocumented
1632 462 ▶ return $self->portal unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 3 459 462
1633  
1634 459 return $self->_buildUrlObj(@_)->as_string;
1635 }
1636  
1637 sub _buildUrlObj {
1638 465 ▶ my $self = shift;
Subroutine _buildUrlObj: called CC=6 SCAR=18.2
1639 465 ▶ return URI->new( $self->portal ) unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 465 465
1640  
1641 # URL base is $self->portal unless first arg is an URL
1642 465 ▶ my $uri =
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ m[^https?://] ? : 63 402 465
1643 URI->new( ( $_[0] =~ m#^https?://# ) ? shift(@_) : $self->portal );
1644 465 my @pathSg = grep { $_ ne '' } $uri->path_segments;
1645 465 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 483 465 948
1646 483 my $s = shift;
1647 483 ▶ if ( ref $s ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $s) { } 38 445 483
1648 38 $uri->query_form($s);
1649 38 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 38 38
1650 0 require Carp;
1651 0 Carp::confess('Query must be the last arg of buildUrl');
1652 }
1653 }
1654 else {
1655 445 push @pathSg, $s;
1656 }
1657 }
1658 465 $uri->path_segments(@pathSg);
1659 465 return $uri;
1660 }
1661  
1662 sub rememberBrowser {
1663 126 ▶ my ( $self, $req ) = @_;
Subroutine rememberBrowser: called CC=3 SCAR=13.3
Pod undocumented
1664  
1665 126 ▶ if ( $self->getService('trustedBrowser')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getService('trustedBrowser') and $self->getService('trustedBrowser')->can('newDevice')) { } 0 126 126
Condition33%1/3
$self->getService("trustedBrowser") and $self->getService("trustedBrowser")->can("newDevice")
!ll&&!rl&&r
12600
MC/DC0%0/2
$self->getService("trustedBrowser") and $self->getService("trustedBrowser")->can("newDevice")
$self->getService("trustedBrowser") $self->getService("trustedBrowser")->can("newDevice")
Truth table33%1/3
A and B
A= $self->getService("trustedBrowser")B= $self->getService("trustedBrowser")->can("newDevice")
ABresult
0X0
100
111
1666 and $self->getService('trustedBrowser')->can("newDevice") )
1667 {
1668 0 return $self->getService('trustedBrowser')->newDevice($req);
1669 }
1670 else {
1671 126 return PE_OK;
1672 }
1673 }
1674  
1675 sub rememberBrowserCheck {
1676 129 ▶ my ( $self, $req ) = @_;
Subroutine rememberBrowserCheck: called CC=3 SCAR=13.3
Pod undocumented
1677  
1678 129 ▶ if ( $self->getService('trustedBrowser')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getService('trustedBrowser') and $self->getService('trustedBrowser')->can('check')) { } 0 129 129
Condition33%1/3
$self->getService("trustedBrowser") and $self->getService("trustedBrowser")->can("check")
!ll&&!rl&&r
12900
MC/DC0%0/2
$self->getService("trustedBrowser") and $self->getService("trustedBrowser")->can("check")
$self->getService("trustedBrowser") $self->getService("trustedBrowser")->can("check")
Truth table33%1/3
A and B
A= $self->getService("trustedBrowser")B= $self->getService("trustedBrowser")->can("check")
ABresult
0X0
100
111
1679 and $self->getService('trustedBrowser')->can("check") )
1680 {
1681 0 return $self->getService('trustedBrowser')->check($req);
1682 }
1683 else {
1684 129 return PE_OK;
1685 }
1686 }
1687  
1688 442 1;
« blib/lib/Lemonldap/NG/Portal/Plugins/GrantSession.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/CrowdSec.pm »