blib/lib/Lemonldap/NG/Manager/Sessions.pm

filter: Statementstmt 83.0% 226 / 272 Branchbran 55.0% 76 / 138 Conditioncond 39.6% 23 / 58 MC/DCmcdc 15.1% 5 / 33 Subroutinesub 95.0% 19 / 20 Podpod 0.0% 0 / 7 totaltotal 66.1% 349 / 528 CC 106 SCAR 47.0
CC
106
Cov
69%
sessions
174.1
userLogout
51.3
delOIDCConsent
30.0
CRAP 110.2
« blib/lib/Lemonldap/NG/Manager/Conf/Parser.pm ↑ Summary blib/lib/Lemonldap/NG/Manager/Cli.pm »
1 package Lemonldap::NG::Manager::Sessions;
2  
3 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 28 ▶use Lemonldap::NG::Common::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 ▶use Lemonldap::NG::Common::PSGI::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 28 ▶use Lemonldap::NG::Common::Conf::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶use Lemonldap::NG::Common::Conf::ReConstants;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶use Lemonldap::NG::Common::FormEncode;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 28 ▶use Lemonldap::NG::Common::IPv6;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 28 extends qw(
15 Lemonldap::NG::Manager::Plugin
16 Lemonldap::NG::Common::Session::REST
17 Lemonldap::NG::Common::Conf::AccessLib
18 );
19  
20 28 our $VERSION = '2.23.0';
21  
22 #############################
23 # I. INITIALIZATION METHODS #
24 #############################
25  
26 28 ▶use constant defaultRoute => 'sessions.html';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 28 ▶use constant icon => 'duplicate';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 45 ▶sub init {
Subroutine init: called CC=8 SCAR=22.9
Pod undocumented
30 45 my ( $self, $conf ) = @_;
31  
32 45 $self->ua( Lemonldap::NG::Common::UserAgent->new($conf) );
33  
34 # HTML template
35 45 $self->addRoute( 'sessions.html', undef, ['GET'] )
36  
37 # READ
38 ->addRoute( sessions => { ':sessionType' => 'sessions' }, ['GET'] )
39  
40 # DELETE
41 ->addRoute(
42 sessions => { ':sessionType' => { ':sessionId' => 'delSession' } },
43 ['DELETE']
44 )
45  
46 # DELETE ALL SESSIONS FOR A USER
47 ->addRoute(
48 sessions =>
49 { glogout => { ':sessionType' => { ':sessionId' => 'userLogout' } } },
50 ['POST']
51 )
52  
53 # DELETE OIDC CONSENT
54 ->addRoute(
55 sessions => {
56 OIDCConsent =>
57 { ':sessionType' => { ':sessionId' => 'delOIDCConsent' } }
58 },
59 ['DELETE']
60 );
61  
62 45 $self->setTypes($conf);
63  
64 45 ▶ $self->{ipField} ||= 'ipAddr';
Condition50%1/2
$self->{"ipField"} ||= "ipAddr"
l!l
045
MC/DC0%0/1
$self->{"ipField"} ||= "ipAddr"
$self->{"ipField"}
Truth table50%1/2
A
A= $self->{"ipField"}
Aresult
11
00
65 45 ▶ $self->{portal} ||= $conf->{portal};
Condition33%1/3
$self->{"portal"} ||= $conf->{"portal"}
l!l&&r!l&&!r
0450
MC/DC0%0/2
$self->{"portal"} ||= $conf->{"portal"}
$self->{"portal"} $conf->{"portal"}
Truth table33%1/3
A ||= B
A= $self->{"portal"}B= $conf->{"portal"}
ABresult
1X1
011
000
66 45 ▶ $self->{cookieName} ||= $conf->{cookieName};
Condition33%1/3
$self->{"cookieName"} ||= $conf->{"cookieName"}
l!l&&r!l&&!r
0450
MC/DC0%0/2
$self->{"cookieName"} ||= $conf->{"cookieName"}
$self->{"cookieName"} $conf->{"cookieName"}
Truth table33%1/3
A ||= B
A= $self->{"cookieName"}B= $conf->{"cookieName"}
ABresult
1X1
011
000
67 45 ▶ $self->{multiValuesSeparator} ||= '; ';
Condition50%1/2
$self->{"multiValuesSeparator"} ||= "; "
l!l
045
MC/DC0%0/1
$self->{"multiValuesSeparator"} ||= "; "
$self->{"multiValuesSeparator"}
Truth table50%1/2
A
A= $self->{"multiValuesSeparator"}
Aresult
11
00
68 45 ▶ $self->{impersonationPrefix} = $conf->{impersonationPrefix} || 'real_';
Condition50%1/2
$conf->{'impersonationPrefix'} || 'real_'
l!l
450
MC/DC0%0/1
$conf->{'impersonationPrefix'} || 'real_'
$conf->{'impersonationPrefix'}
Truth table50%1/2
A
A= $conf->{'impersonationPrefix'}
Aresult
11
00
69 45 ▶ $self->{hiddenAttributes} //= '_password';
Condition50%1/2
$self->{"hiddenAttributes"} //= "_password"
l!l
045
MC/DC0%0/1
$self->{"hiddenAttributes"} //= "_password"
$self->{"hiddenAttributes"}
Truth table50%1/2
A
A= $self->{"hiddenAttributes"}
Aresult
11
00
70 $self->{hiddenAttributes} .= ' _session_id'
71 45 ▶ unless $conf->{displaySessionId};
Branch: 1 branch
BranchTrueFalseTotal
unless $conf->{"displaySessionId"} 0 45 45
72 45 $self->{tokenRevokeSecret} = $conf->{adminLogoutServerSecret};
73 45 return 1;
74 }
75  
76 #
77 # User logout
78 #
79  
80 2 ▶sub userLogout {
Subroutine userLogout: called CC=19 SCAR=39.4
Pod undocumented
81 2 my ( $self, $req ) = @_;
82  
83 2 ▶ my $mod = $self->getMod($req)
Branch: 1 branch
BranchTrueFalseTotal
my $mod = $self->getMod($req) or return $self->sendError($req, undef, 400) 2 0 2
84 or return $self->sendError( $req, undef, 400 );
85 2 ▶ my $id = $req->params('sessionId')
Branch: 1 branch
BranchTrueFalseTotal
my $id = $req->params("sessionId") or return $self->sendError($req, "sessionId is missing", 400) 2 0 2
86 or return $self->sendError( $req, 'sessionId is missing', 400 );
87 2 ▶ my $session = $self->getApacheSession( $mod, $id )
Branch: 1 branch
BranchTrueFalseTotal
my $session = $self->getApacheSession($mod, $id) or return $self->sendError($req, "session $id not found") 2 0 2
88 or return $self->sendError( $req, "session $id not found" );
89  
90 2 my $uidKey = Lemonldap::NG::Handler::Main->tsv->{whatToTrace};
91 2 ▶ my $uid = $session->data->{$uidKey} || $session->data->{_session_uid};
Condition33%1/3
$session->data->{$uidKey} || $session->data->{'_session_uid'}
l!l&&r!l&&!r
200
MC/DC0%0/2
$session->data->{$uidKey} || $session->data->{'_session_uid'}
$session->data->{$uidKey} $session->data->{'_session_uid'}
Truth table33%1/3
A || B
A= $session->data->{$uidKey}B= $session->data->{'_session_uid'}
ABresult
1X1
011
000
92  
93 2 my $count = 0;
94 2 foreach my $storage (qw(oidcStorage sessionStorage)) {
95 my $storageModule =
96 4 Lemonldap::NG::Handler::Main->tsv->{"${storage}Module"};
97 4 ▶ if ( defined $storageModule ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $storageModule) 4 0 4
98 4 ▶ next if ( $storageModule eq "Apache::Session::Memcached" );
Branch: 1 branch
BranchTrueFalseTotal
if $storageModule eq "Apache::Session::Memcached" 0 4 4
99 4 my $opts = Lemonldap::NG::Handler::Main->tsv->{"${storage}Options"};
100 4 $opts->{backend} = $storageModule;
101 4 my $sessions =
102 Lemonldap::NG::Common::Apache::Session->searchOn( $opts, $uidKey,
103 $uid );
104 4 my @keys;
105 4 ▶ if ( $sessions and %$sessions ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sessions and %$sessions) 2 2 4
Condition66%2/3
$sessions and %$sessions
!ll&&!rl&&r
022
MC/DC50%1/2
$sessions and %$sessions
$sessions %$sessions
Truth table66%2/3
A and B
A= $sessionsB= %$sessions
ABresult
0X0
100
111
106 2 @keys = keys %$sessions;
107 2 foreach my $sid (@keys) {
108 4 my $kind = $sessions->{$sid}->{_session_kind};
109 4 my $deletedByPortal = 0;
110 4 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tokenRevokeSecret"} and $kind eq "SSO" || $kind eq "OIDCI" && $sessions->{$sid}{"_type"} eq "refresh_token") 0 4 4
Condition11%1/9
$self->{"tokenRevokeSecret"} and $kind eq "SSO" || $kind eq "OIDCI" && $sessions->{$sid}{"_type"} eq "refresh_token"
!ll&&!rl&&r
400
$kind eq "SSO" or $kind eq "OIDCI" and $sessions->{$sid}{"_type"} eq "refresh_token"
l!l&&r!l&&!r
000
$kind eq "OIDCI" and $sessions->{$sid}{"_type"} eq "refresh_token"
!ll&&!rl&&r
000
MC/DC0%0/4
$self->{"tokenRevokeSecret"} and $kind eq "SSO" || $kind eq "OIDCI" && $sessions->{$sid}{"_type"} eq "refresh_token"
$self->{"tokenRevokeSecret"} $kind eq "SSO" $kind eq "OIDCI" $sessions->{$sid}{"_type"} eq "refresh_token"
Truth table20%1/5
A and B or C and D
A= $self->{"tokenRevokeSecret"}B= $kind eq "SSO"C= $kind eq "OIDCI"D= $sessions->{$sid}{"_type"} eq "refresh_token"
ABCDresult
0XXX0
100X0
10100
11XX1
10111
111 $self->{tokenRevokeSecret}
112 and (
113 $kind eq 'SSO'
114 or ( $kind eq 'OIDCI'
115 and $sessions->{$sid}->{_type} eq
116 'refresh_token' )
117 )
118 )
119 {
120 my $type =
121 0 ▶ $kind eq 'SSO' ? 'SSO' : $sessions->{$sid}->{_type};
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'SSO' ? : 0 0 0
122 0 my $s = build_urlencoded(
123 token_hint => $type,
124 token => $sid,
125 raw => 1,
126 );
127 0 my $portal = $self->{portal};
128 0 $portal =~ s#/+$##;
129 0 my $lreq = HTTP::Request->new(
130 POST => "$portal/admintokenrevoke",
131 [
132 Authorization =>
133 "Bearer $self->{tokenRevokeSecret}",
134 'Content-Length' => length($s),
135 'Content-Type' =>
136 'application/x-www-form-urlencoded',
137 Accept => 'application/json',
138 ],
139 $s
140 );
141 0 my $resp = $self->ua->request($lreq);
142 0 ▶ if ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->is_success) { } 0 0 0
143 0 $self->logger->info(
144 "Session $type/$sid deleted by portal");
145 0 $deletedByPortal = 1;
146 0 $count++;
147 }
148 else {
149 0 $self->logger->error(
150 "Error when calling portal to logout: "
151 . $resp->status_line );
152 }
153 }
154  
155 # Skip manual removal if already deleted by portal
156 4 ▶ next if $deletedByPortal;
Branch: 1 branch
BranchTrueFalseTotal
if $deletedByPortal 0 4 4
157  
158 my $session = Lemonldap::NG::Common::Session->new(
159 storageModule => $storageModule,
160 storageModuleOptions => $opts,
161 cacheModule => Lemonldap::NG::Handler::Main->tsv
162 ->{sessionCacheModule},
163 cacheModuleOptions => Lemonldap::NG::Handler::Main->tsv
164 ->{sessionCacheOptions},
165 4 id => $sid,
166 );
167 4 ▶ if ( $session->data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->data) 4 0 4
168 4 $session->remove;
169 4 $count++;
170 }
171 }
172 }
173 }
174 }
175 2 Lemonldap::NG::Handler::PSGI::Main->localUnlog( $req, $id );
176 2 return $self->sendJSONresponse( $req, { result => 1, count => $count } );
177 }
178  
179 #######################
180 # II. CONSENT METHODS #
181 #######################
182  
183 0 ▶sub delOIDCConsent {
Subroutine delOIDCConsent: not called CC=5 SCAR=34.0
Pod undocumented
184  
185 0 my ( $self, $req ) = @_;
186  
187 0 ▶ my $mod = $self->getMod($req)
Branch: 1 branch
BranchTrueFalseTotal
my $mod = $self->getMod($req) or return $self->sendError($req, undef, 400) 0 0 0
188 or return $self->sendError( $req, undef, 400 );
189  
190 0 my $params = $req->parameters();
191 0 my $epoch = $params->{epoch};
192 0 my $rp = $params->{rp};
193  
194 0 ▶ my $id = $req->params('sessionId')
Branch: 1 branch
BranchTrueFalseTotal
my $id = $req->params("sessionId") or return $self->sendError($req, "sessionId is missing", 400) 0 0 0
195 or return $self->sendError( $req, 'sessionId is missing', 400 );
196  
197 0 $req->parameters->set( 'sessionId', $self->_maybeDecryptSessionId($id) );
198  
199 0 ▶ if ( $rp =~ /\b[\w-]+\b/ and defined $epoch ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rp =~ /\b[\w-]+\b/ and defined $epoch) { } 0 0 0
Condition0%0/3
$rp =~ /\b[\w-]+\b/ and defined $epoch
!ll&&!rl&&r
000
MC/DC0%0/2
$rp =~ /\b[\w-]+\b/ and defined $epoch
$rp =~ /\b[\w-]+\b/ defined $epoch
Truth table0%0/3
A and B
A= $rp =~ /\b[\w-]+\b/B= defined $epoch
ABresult
0X0
100
111
200 0 $self->logger->debug(
201 "Call procedure deleteOIDCConsent with RP=$rp and epoch=$epoch");
202 0 return $self->deleteOIDCConsent($req);
203 }
204 else {
205 0 return $self->sendError( $req, undef, 400 );
206 }
207 }
208  
209 ########################
210 # III. DISPLAY METHODS #
211 ########################
212  
213 18 ▶sub sessions {
Subroutine sessions: called CC=72 SCAR=51.6
Pod undocumented
214 18 my ( $self, $req, $session, $skey ) = @_;
215  
216 # Case 1: only one session is required
217 18 ▶ if ($session) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session) 7 11 18
218 7 return $self->session( $req, $session, $skey );
219 }
220  
221 11 ▶ my $mod = $self->getMod($req)
Branch: 1 branch
BranchTrueFalseTotal
my $mod = $self->getMod($req) or return $self->sendError($req, undef, 400) 11 0 11
222 or return $self->sendError( $req, undef, 400 );
223 11 my $params = $req->parameters();
224 11 my $type = delete $params->{sessionType};
225 11 ▶ $type = $type eq 'global' ? 'SSO' : ucfirst($type);
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'global' ? : 11 0 11
226 11 ▶ $type = $type eq 'Offline' ? 'OIDCI' : ucfirst($type);
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'Offline' ? : 0 11 11
227  
228 11 my $res;
229  
230 # Case 2: list of sessions
231  
232 11 my $whatToTrace = Lemonldap::NG::Handler::Main->tsv->{whatToTrace};
233  
234 # 2.1 Get fields to require
235 11 my @fields = ( '_httpSessionType', $self->{ipField}, $whatToTrace );
236 11 ▶ if ( my $groupBy = $params->{groupBy} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $groupBy = $params->{'groupBy'}) { } 5 6 11
elsif (my $order = $params->{'orderBy'}) { } 2 4 6
237 5 ▶ $groupBy =~ s/^substr\((\w+)(?:,\d+(?:,\d+)?)?\)$/$1/
Branch: 1 branch
BranchTrueFalseTotal
$groupBy =~ s/^substr\((\w+)(?:,\d+(?:,\d+)?)?\)$/$1/ or $groupBy =~ s/^net(?:4|6|)\(([\w:]+),\d+(?:,\d+)?\)$/$1/ 2 3 5
238 or $groupBy =~ s/^net(?:4|6|)\(([\w:]+),\d+(?:,\d+)?\)$/$1/;
239 5 ▶ $groupBy =~ s/^_whatToTrace$/$whatToTrace/o
Branch: 1 branch
BranchTrueFalseTotal
$groupBy =~ s/^_whatToTrace$/$whatToTrace/o or push @fields, $groupBy 0 5 5
240 or push @fields, $groupBy;
241 }
242 elsif ( my $order = $params->{orderBy} ) {
243 2 $order =~ s/^net(?:4|6|)\(([\w:]+)\)$/$1/;
244 2 ▶ $order =~ s/^_whatToTrace$/$whatToTrace/o
Branch: 1 branch
BranchTrueFalseTotal
$order =~ s/^_whatToTrace$/$whatToTrace/o or push @fields, split(/, /, $order, 0) 0 2 2
245 or push @fields, split( /, /, $order );
246 }
247 else {
248 4 push @fields, '_utime';
249 }
250  
251 # 2.2 Restrict query if possible: search for filters (any query arg that is
252 # not a keyword)
253 11 my $moduleOptions = $mod->{options};
254 11 $moduleOptions->{backend} = $mod->{module};
255 my %filters = map {
256 11 my $s = $_;
257 10 $s =~ s/\b_whatToTrace\b/$whatToTrace/o;
258 /^(?:(?:group|order)By|doubleIp)$/
259 ? ()
260 10 ▶ : ( $s => $params->{$_} );
Branch: 1 branch
BranchTrueFalseTotal
/^(?:(?:group|order)By|doubleIp)$/ ? : 8 2 10
261 } keys %$params;
262 11 $filters{_session_kind} = $type;
263 11 push @fields, keys(%filters);
264 {
265 11 my %seen;
266 11 @fields = grep { !$seen{$_}++ } @fields;
267 }
268  
269 # For now, only one argument can be passed to
270 # Lemonldap::NG::Common::Apache::Session so just the first filter is
271 # used
272 my ($firstFilter) = sort {
273 11 $filters{$a} =~ m#^[\w:]+/\d+\*?$# ? 1
274 2 ▶ : $filters{$b} =~ m#^[\w:]+/\d+\*?$# ? -1
Branch: 4 branches
BranchTrueFalseTotal
$filters{$a} =~ m[^[\w:]+/\d+\*?$] ? : 0 2 2
$filters{$b} =~ m[^[\w:]+/\d+\*?$] ? : 0 2 2
$a eq '_session_kind' ? : 2 0 2
$b eq '_session_kind' ? : 0 0 0
275 : $a eq '_session_kind' ? 1
276 : $b eq '_session_kind' ? -1
277 : $a cmp $b
278 } keys %filters;
279  
280 # Check if a '*' is required
281 11 my $function = 'searchOn';
282 $function = 'searchOnExpr'
283 11 ▶ if ( grep { /\*/ and not m#^[\w:]+/\d+\*?$# }
Condition66%2/3
/\*/ && !m[^[\w:]+/\d+\*?$]
!ll&&!rl&&r
902
MC/DC50%1/2
/\*/ && !m[^[\w:]+/\d+\*?$]
/\*/ !m[^[\w:]+/\d+\*?$]
Truth table66%2/3
A && not(B)
A= /\*/B= !m[^[\w:]+/\d+\*?$]
ABresult
0X0
100
111
284 11 ▶ ( $filters{$firstFilter} ) );
Branch: 1 branch
BranchTrueFalseTotal
if grep {/\*/ and not m[^[\w:]+/\d+\*?$];} $filters{$firstFilter} 2 9 11
285 11 $self->logger->debug(
286 "First filter: $firstFilter = $filters{$firstFilter} ($function)");
287  
288 $res =
289 Lemonldap::NG::Common::Apache::Session->$function( $moduleOptions,
290 11 $firstFilter, $filters{$firstFilter}, @fields );
291  
292 11 ▶ return $self->sendJSONresponse(
Branch: 1 branch
BranchTrueFalseTotal
unless $res and %$res 0 11 11
Condition33%1/3
$res and %$res
!ll&&!rl&&r
0011
MC/DC0%0/2
$res and %$res
$res %$res
Truth table33%1/3
A and B
A= $resB= %$res
ABresult
0X0
100
111
293 $req,
294 {
295 result => 1,
296 count => 0,
297 total => 0,
298 values => []
299 }
300 ) unless ( $res and %$res );
301  
302 delete $filters{$firstFilter}
303 11 ▶ unless ( grep { /\*/ and not m#^[\w:]+/\d+\*?$# }
Condition66%2/3
/\*/ && !m[^[\w:]+/\d+\*?$]
!ll&&!rl&&r
902
MC/DC50%1/2
/\*/ && !m[^[\w:]+/\d+\*?$]
/\*/ !m[^[\w:]+/\d+\*?$]
Truth table66%2/3
A && not(B)
A= /\*/B= !m[^[\w:]+/\d+\*?$]
ABresult
0X0
100
111
304 11 ▶ ( $filters{$firstFilter} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless grep {/\*/ and not m[^[\w:]+/\d+\*?$];} $filters{$firstFilter} 9 2 11
305 11 foreach my $k ( keys %filters ) {
306 4 $self->logger->debug("Removing unless $k =~ /^$filters{$k}\$/");
307 4 ▶ if ( $filters{$k} =~ m#^([\w:]+)/(\d+)\*?$# ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filters{$k} =~ m[^([\w:]+)/(\d+)\*?$]) { } 0 4 4
308 0 my ( $net, $bits ) = ( $1, $2 );
309 0 foreach my $session ( keys %$res ) {
310 delete $res->{$session}
311 0 ▶ unless ( net6( $res->{$session}->{$k}, $bits ) eq $net );
Branch: 1 branch
BranchTrueFalseTotal
unless net6($res->{$session}{$k}, $bits) eq $net 0 0 0
312 }
313 }
314 else {
315 4 $filters{$k} =~ s/\./\\./g;
316 4 $filters{$k} =~ s/\*/\.\*/g;
317 4 foreach my $session ( keys %$res ) {
318 6 ▶ if ( $res->{$session}->{$k} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{$session}{$k}) 6 0 6
319 delete $res->{$session}
320 6 ▶ unless ( $res->{$session}->{$k} =~ /^$filters{$k}$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{$session}{$k} =~ /^$filters{$k}$/ 0 6 6
321 }
322 }
323 }
324 }
325  
326 11 my $total = ( keys %$res );
327  
328 # 2.4 Special case doubleIp (users connected from more than 1 IP)
329 11 ▶ if ( defined $params->{doubleIp} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $params->{'doubleIp'}) { } 1 10 11
elsif (my $group = $req->params('groupBy')) { } 5 5 10
elsif (my $f = $req->params('orderBy')) { } 2 3 5
330 1 my %r;
331  
332 # 2.4.1 Store user IP addresses in %r
333 1 foreach my $id ( keys %$res ) {
334 4 my $entry = $res->{$id};
335 4 ▶ next if ( $entry->{_httpSessionType} );
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"_httpSessionType"} 0 4 4
336 4 $r{ $entry->{$whatToTrace} }->{ $entry->{ $self->{ipField} } }++;
337 }
338  
339 # 2.4.2 Store sessions owned by users that has more than one IP address in $r
340 1 my $r;
341 1 $total = 0;
342 1 foreach my $k ( keys %$res ) {
343 4 my @tmp = keys %{ $r{ $res->{$k}->{$whatToTrace} } };
344 4 ▶ if ( @tmp > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tmp > 1) 2 2 4
345 2 $total += 1;
346 2 $res->{$k}->{_sessionId} = $k;
347 2 push @{ $r->{ $res->{$k}->{$whatToTrace} } }, $res->{$k};
348 }
349 }
350  
351 # 2.4.3 Store these session in an array. Array elements are :
352 # {
353 # uid => whatToTraceFieldValue,
354 # sessions => [
355 # { session => <session-id-1>, date => <_utime> },
356 # { session => <session-id-2>, date => <_utime> },
357 # ]
358 # }
359 1 $res = [];
360 1 foreach my $uid ( sort keys %$r ) {
361 push @$res, {
362 value => $uid,
363 1 count => scalar( @{ $r->{$uid} } ),
364 sessions => [
365 map {
366 {
367 session =>
368 $self->_maybeEncryptSessionId( $_->{_sessionId} ),
369 date => $_->{_utime}
370 }
371 1 } @{ $r->{$uid} }
372 ]
373 };
374 }
375 }
376  
377 # 2.4 Order and group by
378 # $res will become an array ref here (except for doubleIp, already done below).
379  
380 # If "groupBy" is asked, elements will be like:
381 # { uid => 'foo.bar', count => 3 }
382 elsif ( my $group = $req->params('groupBy') ) {
383 5 my $r;
384 5 $group =~ s/\b_whatToTrace\b/$whatToTrace/o;
385  
386 # Substrings
387 5 ▶ if ( $group =~ /^substr\((\w+)(?:,(\d+)(?:,(\d+))?)?\)$/ ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($group =~ /^substr\((\w+)(?:,(\d+)(?:,(\d+))?)?\)$/) { } 2 3 5
elsif ($group =~ /^net4\((\w+),(\d)\)$/) { } 2 1 3
elsif ($group =~ /^net6\(([\w:]+),(\d)\)$/) { } 0 1 1
elsif ($group =~ /^net\(([\w:]+),(\d+),(\d+)\)$/) { } 0 1 1
elsif ($group =~ /^\w+$/) { } 1 0 1
388 2 my ( $field, $length, $start ) = ( $1, $2, $3 );
389  
390 # "next" is a try with $length+1, if only one account, $length++
391 # This permits UI to jump directly to the uid
392 # -d (300)
393 # -> dwho (300)
394 # ... sessions
395 #
396 2 ▶ $start ||= 0;
Condition50%1/2
$start ||= 0
l!l
02
MC/DC0%0/1
$start ||= 0
$start
Truth table50%1/2
A
A= $start
Aresult
11
00
397 2 ▶ $length = 1 if ( $length < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $length < 1 0 2 2
398 2 while (1) {
399 5 my $next = {};
400 5 my $stop;
401 5 foreach my $k ( keys %$res ) {
402 11 my $subk = substr $res->{$k}->{$field}, $start, $length + 1;
403 $next->{$subk}++
404 11 ▶ if ( $res->{$k}->{$field} );
Branch: 1 branch
BranchTrueFalseTotal
if $res->{$k}{$field} 11 0 11
405  
406 # End of the loop if we reached the length of uid
407 $stop++
408 11 ▶ if $start + $length >= length( $res->{$k}->{$field} );
Branch: 1 branch
BranchTrueFalseTotal
if $start + $length >= length $res->{$k}{$field} 1 10 11
409 }
410  
411 # Loop if only one result
412 5 my @k = keys %$next;
413 5 ▶ if ( @k == 1 and !$stop ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@k == 1 and not $stop) 3 2 5
Condition66%2/3
@k == 1 and not $stop
!ll&&!rl&&r
203
MC/DC50%1/2
@k == 1 and not $stop
@k == 1 not $stop
Truth table66%2/3
A and not(B)
A= @k == 1B= not $stop
ABresult
0X0
100
111
414 3 $length++;
415 3 next;
416 }
417  
418 # Result calculation
419 2 foreach my $k ( keys %$res ) {
420 $r->{ substr $res->{$k}->{$field}, $start, $length }++
421 5 ▶ if ( $res->{$k}->{$field} );
Branch: 1 branch
BranchTrueFalseTotal
if $res->{$k}{$field} 5 0 5
422 }
423 2 $group = $field;
424 2 last;
425 }
426 }
427  
428 # Subnets IPv4
429 elsif ( $group =~ /^net4\((\w+),(\d)\)$/ ) {
430 2 my $field = $1;
431 2 my $nb = $2 - 1;
432 2 foreach my $k ( keys %$res ) {
433 8 ▶ if ( $res->{$k}->{$field} =~ /^((((\d+)\.\d+)\.\d+)\.\d+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{$k}{$field} =~ /^((((\d+)\.\d+)\.\d+)\.\d+)$/) 8 0 8
434 8 my @d = ( $4, $3, $2, $1 );
435 8 $r->{ $d[$nb] }++;
436 }
437 }
438 2 $group = $field;
439 }
440  
441 # Subnets IPv6
442 elsif ( $group =~ /^net6\(([\w:]+),(\d)\)$/ ) {
443 0 my $field = $1;
444 0 my $bits = $2;
445 0 foreach my $k ( keys %$res ) {
446 $r->{ net6( $res->{$k}->{$field}, $bits ) . "/$bits" }++
447 0 ▶ if ( isIPv6( $res->{$k}->{$field} ) );
Branch: 1 branch
BranchTrueFalseTotal
if isIPv6($res->{$k}{$field}) 0 0 0
448 }
449 }
450  
451 # Both IPv4 and IPv6
452 elsif ( $group =~ /^net\(([\w:]+),(\d+),(\d+)\)$/ ) {
453 0 my $field = $1;
454 0 my $bits = $2;
455 0 my $nb = $3 - 1;
456 0 foreach my $k ( keys %$res ) {
457 0 ▶ if ( isIPv6( $res->{$k}->{$field} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (isIPv6($res->{$k}{$field})) { } 0 0 0
elsif ($res->{$k}{$field} =~ /^((((\d+)\.\d+)\.\d+)\.\d+)$/) { } 0 0 0
458 0 $r->{ net6( $res->{$k}->{$field}, $bits ) . "/$bits" }++;
459 }
460 elsif ( $res->{$k}->{$field} =~ /^((((\d+)\.\d+)\.\d+)\.\d+)$/ )
461 {
462 0 my @d = ( $4, $3, $2, $1 );
463 0 $r->{ $d[$nb] }++;
464 }
465 }
466 }
467  
468 # Simple field groupBy query
469 elsif ( $group =~ /^\w+$/ ) {
470 1 eval {
471 1 foreach my $k ( keys %$res ) {
472 4 $r->{ $res->{$k}->{$group} }++;
473 }
474 };
475 1 ▶ return $self->sendError(
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
476 $req,
477 qq{Use of an uninitialized attribute "$group" to group sessions},
478 400
479 ) if ($@);
480 }
481 else {
482 0 return $self->sendError( $req, 'Syntax error in groupBy', 400 );
483 }
484  
485 # Build result
486 5 $total = 0;
487 $res = [
488 sort {
489 7 my @a = ( $a->{value} =~ /^(\d+)(?:\.(\d+))*$/ );
490 7 my @b = ( $b->{value} =~ /^(\d+)(?:\.(\d+))*$/ );
491 ( @a and @b )
492 ? ( $a[0] <=> $b[0]
493 or $a[1] <=> $b[1]
494 or $a[2] <=> $b[2]
495 or $a[3] <=> $b[3] )
496 : $a->{value} cmp $b->{value}
497 7 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
@a && @b ? : 3 4 7
Condition41%5/12
@a and @b
!ll&&!rl&&r
403
$a[0] <=> $b[0] or $a[1] <=> $b[1] or $a[2] <=> $b[2] or $a[3] <=> $b[3]
l!l&&r!l&&!r
300
$a[0] <=> $b[0] or $a[1] <=> $b[1] or $a[2] <=> $b[2]
l!l&&r!l&&!r
300
$a[0] <=> $b[0] or $a[1] <=> $b[1]
l!l&&r!l&&!r
030
MC/DC50%1/2
@a and @b
@a @b
MC/DC0%0/4
$a[0] <=> $b[0] or $a[1] <=> $b[1] or $a[2] <=> $b[2] or $a[3] <=> $b[3]
$a[0] <=> $b[0] $a[1] <=> $b[1] $a[2] <=> $b[2] $a[3] <=> $b[3]
Truth table66%2/3
A and B
A= @aB= @b
ABresult
0X0
100
111
Truth table20%1/5
A or B or C or D
A= $a[0] <=> $b[0]B= $a[1] <=> $b[1]C= $a[2] <=> $b[2]D= $a[3] <=> $b[3]
ABCDresult
1XXX1
01XX1
001X1
00011
00000
498 5 map { $total += $r->{$_}; { value => $_, count => $r->{$_} } }
499 keys %$r
500 ];
501 }
502  
503 # Else if "orderBy" is asked, $res elements will be like:
504 # { uid => 'foo.bar', session => <sessionId> }
505 elsif ( my $f = $req->params('orderBy') ) {
506 2 my @fields = split /,/, $f;
507 my @r = map {
508 2 my $tmp = { session => $self->_maybeEncryptSessionId($_) };
509 8 foreach my $f (@fields) {
510 8 my $s = $f;
511 8 $s =~ s/^net(?:4|6|)\(([\w:]+)\)$/$1/;
512 8 $tmp->{$s} = $res->{$_}->{$s};
513 }
514 $tmp
515 8 } keys %$res;
516 2 ▶ while ( my $f = pop @fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = pop @fields) 2 2 4
517 2 ▶ if ( $f =~ s/^net4\((\w+)\)$/$1/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($f =~ s/^net4\((\w+)\)$/$1/) { } 1 1 2
elsif ($f =~ s/^net6\(([:\w]+)\)$/$1/) { } 0 1 1
elsif ($f =~ s/^net\(([:\w]+)\)$/$1/) { } 0 1 1
518 1 @r = sort { cmpIPv4( $a->{$f}, $b->{$f} ); } @r;
519 }
520 elsif ( $f =~ s/^net6\(([:\w]+)\)$/$1/ ) {
521 0 @r = sort { expand6( $a->{$f} ) cmp expand6( $b->{$f} ); } @r;
522 }
523 elsif ( $f =~ s/^net\(([:\w]+)\)$/$1/ ) {
524 @r = sort {
525 0 my $ip1 = $a->{$f};
526 0 my $ip2 = $b->{$f};
527 0 ▶ isIPv6($ip1)
Branch: 3 branches
BranchTrueFalseTotal
isIPv6($ip1) ? : 0 0 0
isIPv6($ip2) ? : 0 0 0
isIPv6($ip2) ? : 0 0 0
528 ? (
529 isIPv6($ip2)
530 ? expand6($ip1) cmp expand6($ip2)
531 : -1
532 )
533 : isIPv6($ip2) ? 1
534 : cmpIPv4( $ip1, $ip2 );
535 } @r;
536 }
537 else {
538 1 @r = sort { $a->{$f} cmp $b->{$f} } @r;
539 }
540 }
541 2 $res = [@r];
542 }
543  
544 # Else, $res elements will be like:
545 # { session => <sessionId>, date => <timestamp> }
546 else {
547 $res = [
548 2 sort { $a->{date} <=> $b->{date} }
549 map {
550 3 {
551 session => $self->_maybeEncryptSessionId($_),
552 date => $res->{$_}->{_utime}
553 }
554 5 }
555 keys %$res
556 ];
557 }
558  
559 11 return $self->sendJSONresponse(
560 $req,
561 {
562 result => 1,
563 count => scalar(@$res),
564 total => $total,
565 values => $res
566 }
567 );
568 }
569  
570 7 ▶sub session {
Subroutine session: called CC=1 SCAR=0.0
Pod undocumented
571 7 my ( $self, $req, $session, $skey ) = @_;
572  
573 7 $session = $self->_maybeDecryptSessionId($session);
574 7 return $self->SUPER::session( $req, $session, $skey );
575 }
576  
577 11 ▶sub _maybeDecryptSessionId {
Subroutine _maybeDecryptSessionId: called CC=2 SCAR=7.6
578 11 my ( $self, $session ) = @_;
579  
580 11 ▶ if ( $self->{hiddenAttributes} =~ /\b_session_id\b/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"hiddenAttributes"} =~ /\b_session_id\b/) 0 11 11
581 $session =
582 0 Lemonldap::NG::Handler::Main->tsv->{cipher}->decryptHex($session);
583 }
584  
585 11 return $session;
586 }
587  
588 15 ▶sub _maybeEncryptSessionId {
Subroutine _maybeEncryptSessionId: called CC=2 SCAR=7.6
589 15 my ( $self, $session ) = @_;
590  
591 15 ▶ if ( $self->{hiddenAttributes} =~ /\b_session_id\b/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"hiddenAttributes"} =~ /\b_session_id\b/) 0 15 15
592 $session =
593 0 Lemonldap::NG::Handler::Main->tsv->{cipher}->encryptHex($session);
594 }
595  
596 15 return $session;
597 }
598  
599 4 ▶sub delSession {
Subroutine delSession: called CC=2 SCAR=7.0
Pod undocumented
600 4 my ( $self, $req ) = @_;
601 4 ▶ my $id = $req->params('sessionId')
Branch: 1 branch
BranchTrueFalseTotal
my $id = $req->params("sessionId") or return $self->sendError($req, "sessionId is missing", 400) 4 0 4
602 or return $self->sendError( $req, 'sessionId is missing', 400 );
603  
604 4 $req->parameters->set( 'sessionId', $self->_maybeDecryptSessionId($id) );
605  
606 4 return $self->SUPER::delSession($req);
607 }
608  
609 5 ▶sub cmpIPv4 {
Subroutine cmpIPv4: called CC=3 SCAR=11.0
Pod undocumented
610 5 my @a = split /\./, $_[0];
611 5 my @b = split /\./, $_[1];
612 5 my $cmp = 0;
613 5 ▶ F: for ( my $i = 0 ; $i < 4 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 4) 12 0 12
614 12 ▶ if ( $a[$i] != $b[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a[$i] != $b[$i]) 5 7 12
615 5 $cmp = $a[$i] <=> $b[$i];
616 5 last F;
617 }
618 }
619 5 $cmp;
620 }
621  
622 28 1;
« blib/lib/Lemonldap/NG/Manager/Conf/Parser.pm ↑ Summary blib/lib/Lemonldap/NG/Manager/Cli.pm »