blib/lib/Lemonldap/NG/Portal/Plugins/GlobalLogout.pm

filter: Statementstmt 71.6% 111 / 155 Branchbran 43.7% 28 / 64 Conditioncond 33.3% 5 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 92.8% 13 / 14 Podpod 20.0% 1 / 5 totaltotal 60.5% 158 / 261 CC 42 SCAR 31.4
CC
42
Cov
62%
removeOtherActiveSessions
56.0
globalLogout
31.1
run
23.3
CRAP 23.1
« blib/lib/Lemonldap/NG/Portal/Plugins/GrantSession.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/CrowdSec.pm »
1 package Lemonldap::NG::Portal::Plugins::GlobalLogout;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use JSON qw(from_json to_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Time::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Lemonldap::NG::Common::Session 'id2storage';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Lemonldap::NG::Portal::Main::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
9 PE_OK
10 PE_ERROR
11 PE_NOTOKEN
12 PE_TOKENEXPIRED
13 PE_SENDRESPONSE
14 PE_SLO_ERROR
15 1 );
16  
17 1 our $VERSION = '2.23.0';
18  
19 1 extends qw(
20 Lemonldap::NG::Portal::Main::Plugin
21 Lemonldap::NG::Portal::Lib::OtherSessions
22 );
23  
24 # INTERFACE
25 1 ▶use constant beforeLogout => 'run';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # INITIALIZATION
28 1 ▶has rule => ( is => 'rw', default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
29 has ott => (
30 is => 'rw',
31 lazy => 1,
32 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
33 my $ott =
34 1 $_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken');
35 1 $ott->timeout( $_[0]->conf->{formTimeout} );
36 1 return $ott;
37 }
38 1 );
39  
40 1 ▶sub init {
Subroutine init: called CC=2 SCAR=7.0
Pod undocumented
41 1 my ($self) = @_;
42 1 $self->addAuthRoute( globallogout => 'globalLogout', [ 'POST', 'GET' ] );
43  
44 # Parse activation rule
45 $self->rule(
46 1 $self->p->buildRule( $self->conf->{globalLogoutRule}, 'globalLogout' )
47 );
48 1 ▶ return 0 unless $self->rule;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->rule 0 1 1
49  
50 1 return 1;
51 }
52  
53 # RUNNING METHODS
54 # Look for user active SSO sessions and suggest to close them
55 2 ▶sub run {
Subroutine run: called CC=11 SCAR=31.5
Pod documented
56 2 my ( $self, $req ) = @_;
57  
58 # Skip if called from _triggerBackChannelLogout to avoid recursion
59 2 ▶ return PE_OK if $req->data->{_globalLogoutBCL};
Branch: 1 branch
BranchTrueFalseTotal
if $req->data->{"_globalLogoutBCL"} 1 1 2
60  
61 1 my $user = $req->userData->{ $self->conf->{whatToTrace} };
62  
63 # Check activation rule
64 1 ▶ unless ( $self->rule->( $req, $req->userData ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->rule->($req, $req->userData)) 0 1 1
65 0 $self->userLogger->info("GlobaLogout not allowed for $user");
66 0 return PE_OK;
67 }
68  
69 # Looking for active sessions
70 1 my $sessions = $self->activeSessions($req);
71 1 my $nbr = @{$sessions};
72 1 ▶ $self->logger->debug("GlobalLogout: $nbr session(s) found") if $nbr;
Branch: 1 branch
BranchTrueFalseTotal
if $nbr 1 0 1
73 1 ▶ return PE_OK unless ( $nbr > 1 );
Branch: 1 branch
BranchTrueFalseTotal
unless $nbr > 1 0 1 1
74  
75 # Force GlobalLogout if timer is disabled
76 1 ▶ if ( $req->param('confirm') && $req->param('confirm') == 1
Branch: 1 branch
BranchTrueFalseTotal
if ($req->param("confirm") and $req->param("confirm") == 1 or not $self->conf->{"globalLogoutTimer"}) 0 1 1
Condition33%2/6
$req->param("confirm") and $req->param("confirm") == 1 or not $self->conf->{"globalLogoutTimer"}
l!l&&r!l&&!r
001
$req->param("confirm") and $req->param("confirm") == 1
!ll&&!rl&&r
100
MC/DC0%0/3
$req->param("confirm") and $req->param("confirm") == 1 or not $self->conf->{"globalLogoutTimer"}
$req->param("confirm") $req->param("confirm") == 1 not $self->conf->{"globalLogoutTimer"}
Truth table20%1/5
A and B or not(C)
A= $req->param("confirm")B= $req->param("confirm") == 1C= not $self->conf->{"globalLogoutTimer"}
ABCresult
11X1
0X11
1011
0X00
1000
77 || !$self->conf->{globalLogoutTimer} )
78 {
79 my $msg =
80 $self->conf->{globalLogoutTimer}
81 0 ▶ ? 'GlobalLogout: confirm parameter detected'
Branch: 1 branch
BranchTrueFalseTotal
$self->conf->{'globalLogoutTimer'} ? : 0 0 0
82 : 'GlobalLogout: timer disabled';
83 0 $self->logger->debug($msg);
84 0 $self->userLogger->info("GlobalLogout: force global logout for $user");
85 0 $nbr = $self->removeOtherActiveSessions( $req, $sessions );
86 0 $self->userLogger->info("$nbr remaining session(s) removed");
87  
88 # Return PE_SLO_ERROR if BCL failed, PE_OK otherwise
89 0 ▶ return $req->error || PE_OK;
Condition0%0/2
$req->error || 0
l!l
00
MC/DC0%0/1
$req->error || 0
$req->error
Truth table0%0/2
A
A= $req->error
Aresult
11
00
90 }
91  
92 # Prepare token
93 1 my $token = $self->ott->createToken( {
94 user => $user,
95 sessions => to_json($sessions)
96 }
97 );
98  
99 # Prepare form
100 1 $self->logger->debug("Prepare global logout confirmation");
101 my $tmp = $self->p->sendHtml(
102 $req,
103 'globallogout',
104 params => {
105 FORM_ACTION =>
106 $self->p->relativeUrl( $req, 'globallogout', { all => 1 } ),
107 SESSIONS => $sessions,
108 TOKEN => $token,
109 LOGIN => $user,
110 CUSTOMPRM => $self->conf->{globalLogoutCustomParam}
111 }
112 1 );
113  
114 1 ▶ $req->response(
Branch: 1 branch
BranchTrueFalseTotal
$req->wantJSON ? : 0 1 1
115 $req->wantJSON
116 ? $self->sendJSONresponse( $req,
117 { globalLogout => $nbr, confirmationRequired => 1 } )
118 : $tmp
119 );
120  
121 1 return PE_SENDRESPONSE;
122 }
123  
124 1 ▶sub globalLogout {
Subroutine globalLogout: called CC=16 SCAR=34.4
Pod undocumented
125 1 my ( $self, $req ) = @_;
126 1 my $res = PE_OK;
127 1 my $count = 0;
128  
129 1 ▶ if ( $req->param('all') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->param("all")) 1 0 1
130 1 ▶ if ( my $token = $req->param('token') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $req->param('token')) { } 1 0 1
131 1 ▶ if ( $token = $self->ott->getToken($token) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $self->ott->getToken($token)) { } 1 0 1
132 my $storeId =
133 $self->conf->{hashedSessionStore}
134 ? id2storage( $req->userData->{_session_id} )
135 1 ▶ : $req->userData->{_session_id};
Branch: 1 branch
BranchTrueFalseTotal
$self->conf->{'hashedSessionStore'} ? : 1 0 1
136  
137 # Read active sessions from token
138 1 my $sessions = eval { from_json( $token->{sessions} ) };
139 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
140 0 $self->logger->error(
141 "GlobalLogout: bad encoding in OTT ($@)");
142 0 $res = PE_ERROR;
143 }
144 1 my $as;
145 1 my $user = $token->{user};
146 my $req_user =
147 1 $req->userData->{ $self->{conf}->{whatToTrace} };
148 1 ▶ if ( $req_user eq $user ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req_user eq $user) { } 1 0 1
149 1 my $bclFailed = 0;
150 1 foreach (@$sessions) {
151 2 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($as = $self->p->getApacheSession($_->{"id"}, "hashStore", 0)) 0 2 2
152  
153 # searchOn() returns sessions indexed by their
154 # storage ID, then it is required to set hashStore
155 # to 0
156 $as = $self->p->getApacheSession(
157 $_->{id}, hashStore => 0,
158 )
159 )
160 {
161 0 $self->userLogger->info(
162 "GlobalLogout: session $_->{id} expired");
163 0 next;
164 }
165 2 ▶ unless ( $storeId eq $_->{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($storeId eq $_->{"id"}) 1 1 2
166 1 $self->userLogger->info(
167 "Remove \"$user\" session: $_->{id}");
168  
169 # Trigger back-channel logout before removal
170 1 $req->data->{_globalLogoutBCL} = 1;
171 1 my $bclRes =
172 $self->p->_triggerBackChannelLogout( $req,
173 $as->data );
174 1 delete $req->data->{_globalLogoutBCL};
175 1 ▶ $bclFailed = 1 if $bclRes != PE_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $bclRes != 0 0 1 1
176  
177 1 $as->remove;
178 1 $count++;
179 }
180 }
181 1 ▶ $res = PE_SLO_ERROR if $bclFailed;
Branch: 1 branch
BranchTrueFalseTotal
if $bclFailed 0 1 1
182 }
183 else {
184 0 $self->userLogger->warn(
185 "GlobalLogout called with an invalid token: $req_user is NOT $user"
186 );
187 0 $res = PE_TOKENEXPIRED;
188 }
189 }
190 else {
191 0 $self->userLogger->error(
192 "GlobalLogout called with an expired token");
193 0 $res = PE_TOKENEXPIRED;
194 }
195 }
196 else {
197 0 $self->userLogger->error('GlobalLogout called without token');
198 0 $res = PE_NOTOKEN;
199 }
200 }
201  
202 # Return error for blocking errors (not PE_SLO_ERROR)
203 1 ▶ return $self->p->doPE( $req, $res )
Branch: 1 branch
BranchTrueFalseTotal
if $res and $res != 56 0 1 1
Condition33%1/3
$res and $res != 56
!ll&&!rl&&r
100
MC/DC0%0/2
$res and $res != 56
$res $res != 56
Truth table33%1/3
A and B
A= $resB= $res != 56
ABresult
0X0
100
111
204 if $res && $res != PE_SLO_ERROR;
205  
206 1 $self->userLogger->info("$count remaining session(s) removed");
207  
208 # Set error for display if BCL failed (non-blocking warning)
209  
210 1 my $ret = $self->p->do( $req, [ 'authLogout', 'deleteSession' ] );
211 1 ▶ return $ret if $ret;
Branch: 1 branch
BranchTrueFalseTotal
if $ret 1 0 1
212 0 ▶ $ret = PE_SLO_ERROR if $res == PE_SLO_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if $res == 56 0 0 0
213 0 $req->error($ret);
214 0 return $ret;
215 }
216  
217 1 ▶sub activeSessions {
Subroutine activeSessions: called CC=10 SCAR=24.0
Pod undocumented
218 1 my ( $self, $req ) = @_;
219 1 my $activeSessions = [];
220 1 my $sessions = {};
221 1 my $user = $req->userData->{ $self->conf->{whatToTrace} };
222 1 ▶ my $customParam = $self->conf->{globalLogoutCustomParam} || '';
Condition50%1/2
$self->conf->{'globalLogoutCustomParam'} || ''
l!l
01
MC/DC0%0/1
$self->conf->{'globalLogoutCustomParam'} || ''
$self->conf->{'globalLogoutCustomParam'}
Truth table50%1/2
A
A= $self->conf->{'globalLogoutCustomParam'}
Aresult
11
00
223  
224 # Try to retrieve sessions from sessions DB
225 1 ▶ if ($user) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user) 1 0 1
226 1 $self->logger->debug('Try to retrieve sessions from DB');
227 1 ▶ my $moduleOptions = $self->conf->{globalStorageOptions} || {};
Condition50%1/2
$self->conf->{'globalStorageOptions'} || {}
l!l
10
MC/DC0%0/1
$self->conf->{'globalStorageOptions'} || {}
$self->conf->{'globalStorageOptions'}
Truth table50%1/2
A
A= $self->conf->{'globalStorageOptions'}
Aresult
11
00
228 1 $moduleOptions->{backend} = $self->conf->{globalStorage};
229 1 $self->logger->debug("Looking for \"$user\" sessions...");
230 $sessions =
231 $self->module->searchOn( $moduleOptions, $self->conf->{whatToTrace},
232 1 $user );
233  
234 1 $self->logger->debug('Skip non-SSO session(s)...');
235 1 my $other = 0;
236 1 foreach ( keys %$sessions ) {
237 2 ▶ unless ( $sessions->{$_}->{_session_kind} eq 'SSO' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sessions->{$_}{"_session_kind"} eq "SSO") 0 2 2
238 0 delete $sessions->{$_};
239 0 $other++;
240 }
241 }
242 1 ▶ $self->logger->info("$other non-SSO session(s) skipped")
Branch: 1 branch
BranchTrueFalseTotal
if $other 0 1 1
243 if $other;
244  
245 1 $self->logger->debug('Build an array ref with sessions info...');
246 @$activeSessions =
247 map {
248 2 my $epoch;
249 2 my $regex = '^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$';
250 2 ▶ if ( $_->{startTime} =~ /$regex/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{'startTime'} =~ /$regex/) { } 2 0 2
251 2 $epoch = timelocal( $6, $5, $4, $3, $2 - 1, $1 );
252 2 $_->{startTime} = $epoch;
253 }
254 else {
255 0 delete $_->{startTime};
256 }
257 2 ▶ if ( $_->{updateTime} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{"updateTime"}) 1 1 2
258 1 ▶ if ( $_->{updateTime} =~ /$regex/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{'updateTime'} =~ /$regex/) { } 1 0 1
259  
260 1 $epoch = timelocal( $6, $5, $4, $3, $2 - 1, $1 );
261 1 $_->{updateTime} = $epoch;
262 }
263 else {
264 0 delete $_->{updateTime};
265 }
266 }
267 2 $_;
268 }
269 1 sort { $b->{startTime} cmp $a->{startTime} } map {
270 1 {
271 id => $_,
272 customParam => $sessions->{$_}->{$customParam},
273 ipAddr => $sessions->{$_}->{ipAddr},
274 authLevel => $sessions->{$_}->{authenticationLevel},
275 startTime => $sessions->{$_}->{_startTime},
276 updateTime => $sessions->{$_}->{_updateTime}
277 2 };
278 } keys %$sessions;
279 }
280  
281 1 return $activeSessions;
282 }
283  
284 0 ▶sub removeOtherActiveSessions {
Subroutine removeOtherActiveSessions: not called CC=7 SCAR=40.3
Pod undocumented
285 0 my ( $self, $req, $sessions ) = @_;
286 0 my $count = 0;
287 0 my $bclFailed = 0;
288 0 my $as;
289  
290 my $storeId =
291 $self->conf->{hashedSessionStore}
292 ? id2storage( $req->userData->{_session_id} )
293 0 ▶ : $req->userData->{_session_id};
Branch: 1 branch
BranchTrueFalseTotal
$self->conf->{'hashedSessionStore'} ? : 0 0 0
294 0 foreach (@$sessions) {
295  
296 # searchOn() returns sessions indexed by their storage ID, then
297 # it is required to set hashStore to 0
298 0 ▶ unless ( $as = $self->p->getApacheSession( $_->{id}, hashStore => 0, ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($as = $self->p->getApacheSession($_->{"id"}, "hashStore", 0)) 0 0 0
299 {
300 0 $self->userLogger->info("GlobalLogout: session $_->{id} expired");
301 0 next;
302 }
303 0 ▶ unless ( $storeId eq $_->{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($storeId eq $_->{"id"}) 0 0 0
304 0 $self->userLogger->info(
305 "Remove \"$req->userData->{ $self->conf->{whatToTrace} }\" session: $_->{id}"
306 );
307  
308 # Trigger back-channel logout before removal
309 0 $req->data->{_globalLogoutBCL} = 1;
310 0 my $bclRes = $self->p->_triggerBackChannelLogout( $req, $as->data );
311 0 delete $req->data->{_globalLogoutBCL};
312 0 ▶ $bclFailed = 1 if $bclRes != PE_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $bclRes != 0 0 0 0
313  
314 0 $as->remove;
315 0 $count++;
316 }
317 }
318  
319 # Set error if BCL failed (non-blocking warning)
320 0 ▶ $req->error(PE_SLO_ERROR) if $bclFailed;
Branch: 1 branch
BranchTrueFalseTotal
if $bclFailed 0 0 0
321  
322 0 return $count;
323 }
324  
325 1 1;
« blib/lib/Lemonldap/NG/Portal/Plugins/GrantSession.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/CrowdSec.pm »