blib/lib/Lemonldap/NG/Common/CliSessions.pm

filter: Statementstmt 80.9% 328 / 405 Branchbran 57.5% 91 / 158 Conditioncond 45.9% 28 / 61 MC/DCmcdc 27.0% 10 / 37 Subroutinesub 91.6% 44 / 48 Podpod 0.0% 0 / 17 totaltotal 69.0% 501 / 726 CC 119 SCAR 25.9
CC
119
Cov
72%
_migrateu2f
72.0
_search
44.8
__ANON__
20.0
CRAP 13.3
« blib/lib/Lemonldap/NG/Common/IPv6.pm ↑ Summary blib/lib/Lemonldap/NG/Common/MessageBroker/MQTT.pm »
1 package Lemonldap::NG::Common::CliSessions;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Lemonldap::NG::Common::Conf;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Lemonldap::NG::Common::Logger::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Lemonldap::NG::Common::Apache::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Lemonldap::NG::Common::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Lemonldap::NG::Common::Util qw/getPSessionID genId2F/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use constant BACKENDS => qw(global persistent oidc saml cas);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use constant ISREFRESHTOKEN => '_type=refresh_token';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 our $VERSION = '2.23.0';
16  
17 3 has opts => ( is => 'rw' );
18  
19 3 has stdout => (
20 is => 'ro',
21 default => *STDOUT,
22 );
23  
24 3 has stderr => (
25 is => 'ro',
26 default => *STDERR,
27 );
28  
29 has conf => (
30 is => 'ro',
31 0 ▶ default => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
32 my $ca = Lemonldap::NG::Common::Conf->new( { (
33 ref $_[0] && $_[0]->{iniFile}
34 ? ( confFile => $_[0]->{iniFile} )
35 0 ▶ : ()
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && $_[0]{'iniFile'} ? : 0 0 0
Condition0%0/3
ref $_[0] and $_[0]{"iniFile"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and $_[0]{"iniFile"}
ref $_[0] $_[0]{"iniFile"}
Truth table0%0/3
A and B
A= ref $_[0]B= $_[0]{"iniFile"}
ABresult
0X0
100
111
36 )
37 }
38 );
39 0 ▶ die $Lemonldap::NG::Common::Conf::msg unless ($ca);
Branch: 1 branch
BranchTrueFalseTotal
unless $ca 0 0 0
40 0 my $res = $ca->getConf('all');
41 0 my $localConf = $ca->getLocalConf( 'all', $_[0]->{iniFile} );
42 0 return { %$res, %$localConf };
43 },
44 3 );
45  
46 28 ▶sub _to_json {
Subroutine _to_json: called CC=1 SCAR=0.0
47 28 my $self = shift;
48 28 my $obj = shift;
49 28 return to_json( $obj, { pretty => 1, canonical => 1 } );
50 }
51  
52 21 ▶sub _search {
Subroutine _search: called CC=18 SCAR=38.0
53 21 my ($self) = shift;
54 my $backendStorage =
55 21 ▶ ( lc( $self->opts->{backend} || 'global' ) ) . "Storage";
Condition100%2/2
$self->opts->{'backend'} || 'global'
l!l
615
MC/DC100%1/1
$self->opts->{'backend'} || 'global'
$self->opts->{'backend'}
Truth table100%2/2
A
A= $self->opts->{'backend'}
Aresult
11
00
56  
57 # Handle --persistent
58 21 ▶ if ( $self->opts->{persistent} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->opts->{'persistent'}) { } 4 17 21
elsif ($self->opts->{'refreshTokens'}) { } 0 17 17
59 4 $backendStorage = "persistentStorage";
60 }
61 elsif ( $self->opts->{refreshTokens} ) {
62 0 $backendStorage = "oidcStorage";
63 0 ▶ $self->opts->{where} ||= ISREFRESHTOKEN;
Condition0%0/2
$self->opts->{"where"} ||= "_type=refresh_token"
l!l
00
MC/DC0%0/1
$self->opts->{"where"} ||= "_type=refresh_token"
$self->opts->{"where"}
Truth table0%0/2
A
A= $self->opts->{"where"}
Aresult
11
00
64 }
65  
66 21 ▶ unless ( $self->conf->{$backendStorage} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->conf->{$backendStorage}) 0 21 21
67 print STDERR "Backend "
68 . $self->opts->{backend}
69 0 . " was not found, falling back to global\n";
70 0 $backendStorage = "globalStorage";
71 }
72  
73 21 my $args = $self->conf->{"${backendStorage}Options"};
74 21 $args->{backend} = $self->conf->{$backendStorage};
75  
76 21 ▶ my @fields = @{ $self->opts->{select} || [] };
Condition100%2/2
$self->opts->{'select'} || []
l!l
219
MC/DC100%1/1
$self->opts->{'select'} || []
$self->opts->{'select'}
Truth table100%2/2
A
A= $self->opts->{'select'}
Aresult
11
00
77 21 ▶ if ( $self->opts->{idonly} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opts->{"idonly"}) 2 19 21
78 2 @fields = ('_session_id');
79 }
80  
81 21 my $res;
82 21 ▶ if ( $self->opts->{where} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opts->{'where'}) { } 10 11 21
83  
84 # TODO fix regexp?
85 10 ▶ if ( $self->opts->{where} =~ /^(\w+)\s*=\s*(.*)/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->opts->{'where'} =~ /^(\w+)\s*=\s*(.*)/) { } 10 0 10
elsif ($self->opts->{'where'} =~ /^(\w+)\s*<\s*(.*)/) { } 0 0 0
elsif ($self->opts->{'where'} =~ /^(\w+)\s*>\s*(.*)/) { } 0 0 0
86 10 my ( $selectField, $value ) = ( $1, $2 );
87 10 $res = Lemonldap::NG::Common::Apache::Session->searchOn( $args,
88 $selectField, $value, @fields );
89 }
90 elsif ( $self->opts->{where} =~ /^(\w+)\s*<\s*(.*)/ ) {
91 0 $res =
92 Lemonldap::NG::Common::Apache::Session->searchLt( $args, $1, $2,
93 @fields );
94 }
95 elsif ( $self->opts->{where} =~ /^(\w+)\s*>\s*(.*)/ ) {
96 0 $res =
97 Lemonldap::NG::Common::Apache::Session->searchGt( $args, $1, $2,
98 @fields );
99 }
100 else {
101 0 die "Invalid --where option : " . $self->opts->{where};
102 }
103 }
104 else {
105 11 ▶ $res =
Branch: 1 branch
BranchTrueFalseTotal
@fields ? : 0 11 11
106 Lemonldap::NG::Common::Apache::Session->get_key_from_all_sessions(
107 $args, ( @fields ? [@fields] : () ) );
108 }
109  
110 21 ▶ if ( $self->opts->{refreshTokens}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opts->{"refreshTokens"} and $self->opts->{"where"} ne "_type=refresh_token") 0 21 21
Condition33%1/3
$self->opts->{"refreshTokens"} and $self->opts->{"where"} ne "_type=refresh_token"
!ll&&!rl&&r
2100
MC/DC0%0/2
$self->opts->{"refreshTokens"} and $self->opts->{"where"} ne "_type=refresh_token"
$self->opts->{"refreshTokens"} $self->opts->{"where"} ne "_type=refresh_token"
Truth table33%1/3
A and B
A= $self->opts->{"refreshTokens"}B= $self->opts->{"where"} ne "_type=refresh_token"
ABresult
0X0
100
111
111 and $self->opts->{where} ne ISREFRESHTOKEN )
112 {
113 0 foreach my $key (%$res) {
114 delete $res->{$key}
115 unless $res->{$key}->{_type}
116 0 ▶ and $res->{$key}->{_type} eq 'refresh_token';
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{$key}{"_type"} and $res->{$key}{"_type"} eq "refresh_token" 0 0 0
Condition0%0/3
$res->{$key}{"_type"} and $res->{$key}{"_type"} eq "refresh_token"
!ll&&!rl&&r
000
MC/DC0%0/2
$res->{$key}{"_type"} and $res->{$key}{"_type"} eq "refresh_token"
$res->{$key}{"_type"} $res->{$key}{"_type"} eq "refresh_token"
Truth table0%0/3
A and B
A= $res->{$key}{"_type"}B= $res->{$key}{"_type"} eq "refresh_token"
ABresult
0X0
100
111
117 }
118 }
119  
120 21 return $res;
121 }
122  
123 16 ▶sub search {
Subroutine search: called CC=3 SCAR=11.0
Pod undocumented
124 16 my ($self) = shift;
125 16 my $res = $self->_search();
126 16 my $o = $self->stdout;
127 16 ▶ if ( $self->opts->{count} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opts->{'count'}) { } 3 13 16
128 3 my @tmp = keys %$res;
129 3 print $o scalar(@tmp) . "\n";
130 }
131 else {
132 13 ▶ if ( $self->opts->{idonly} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opts->{'idonly'}) { } 2 11 13
133 2 print $o map { $res->{$_}->{_session_id} . "\n" } keys %{$res};
134 }
135 else {
136 11 print $o $self->_to_json( [ values %{$res} ] );
137 }
138 }
139 16 return 0;
140  
141 }
142  
143 1 ▶sub backup {
Subroutine backup: called CC=5 SCAR=16.6
Pod undocumented
144 1 my $self = shift;
145 1 my $res = {};
146 1 ▶ if ( $self->opts->{persistent} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->opts->{'persistent'}) { } 0 1 1
elsif ($self->opts->{'refreshTokens'}) { } 0 1 1
147 0 $res->{persistent} = $self->_search();
148 }
149 elsif ( $self->opts->{refreshTokens} ) {
150 0 $res->{oidc} = $self->_search();
151 }
152 else {
153 1 foreach my $backend (BACKENDS) {
154 5 ▶ next unless $self->conf->{"${backend}Storage"};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->conf->{"${backend}Storage"} 3 2 5
155 2 $self->opts->{backend} = $backend;
156 2 my $_res = $self->_search();
157 2 $res->{$backend} = $_res;
158 }
159 }
160 1 my $o = $self->stdout;
161 1 print $o to_json($res);
162 1 return 0;
163 }
164  
165 1 ▶sub restore {
Subroutine restore: called CC=8 SCAR=21.3
Pod undocumented
166 1 my $self = shift;
167 1 my $obj;
168 {
169 1 local $/ = undef;
170 1 my $i = *STDIN;
171 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
172 1 ▶ open( $i, '<', "$_[0]" ) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open $i, "<", "$_[0]" or die $! 1 0 1
173 }
174 1 ▶ while (<$i>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $i)) 1 1 2
175 1 $obj .= $_;
176 }
177 }
178 1 eval {
179 1 $obj = from_json($obj);
180 3 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
181 1 %$obj;
182 };
183 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
184 0 print STDERR "Given backup file looks bad: $@\n";
185 0 return 1;
186 }
187 1 foreach my $backend ( keys %$obj ) {
188 2 ▶ unless ( grep { $backend eq $_ } BACKENDS ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$backend eq $_;} ("global", "persistent", "oidc", "saml", "cas")) 0 2 2
189 0 print STDERR
190 "Given backup file looks bad: unknpwn backend $backend\n";
191 }
192 2 $self->opts->{backend} = $backend;
193 2 foreach my $_session_id ( keys %{ $obj->{$backend} } ) {
194 7 $self->_insert( $_session_id, $obj->{$backend}->{$_session_id} );
195 }
196 }
197 1 return 0;
198 }
199  
200 7 ▶sub _insert {
Subroutine _insert: called CC=5 SCAR=17.8
201 7 my ( $self, $id, $content ) = @_;
202 7 ▶ $id = id2storage($id) if $id and $self->opts->{hash};
Branch: 1 branch
BranchTrueFalseTotal
if $id and $self->opts->{"hash"} 0 7 7
Condition33%1/3
$id and $self->opts->{"hash"}
!ll&&!rl&&r
070
MC/DC0%0/2
$id and $self->opts->{"hash"}
$id $self->opts->{"hash"}
Truth table33%1/3
A and B
A= $idB= $self->opts->{"hash"}
ABresult
0X0
100
111
203  
204 # Lookup backend storage from CLI options
205 my $backendStorage =
206 7 ▶ ( lc( $self->opts->{backend} || 'global' ) ) . "Storage";
Condition50%1/2
$self->opts->{'backend'} || 'global'
l!l
70
MC/DC0%0/1
$self->opts->{'backend'} || 'global'
$self->opts->{'backend'}
Truth table50%1/2
A
A= $self->opts->{'backend'}
Aresult
11
00
207  
208 my $as = Lemonldap::NG::Common::Session->new( {
209 hashStore => 0,
210 storageModule => $self->conf->{$backendStorage},
211 7 storageModuleOptions => $self->conf->{"${backendStorage}Options"},
212 id => $id,
213 force => 1,
214 info => $content,
215 }
216 );
217 print STDERR "Unable to restore session $id in backend "
218 7 ▶ . $self->opts->{backend} . "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $as 0 7 7
219 unless $as;
220 7 return $as;
221 }
222  
223 # Returns the session object, so we can modify it
224 32 ▶sub _get_one_session {
Subroutine _get_one_session: called CC=9 SCAR=23.1
225 32 my ( $self, $id, $backend ) = @_;
226  
227 32 ▶ $id = id2storage($id) if $id and $self->opts->{hash};
Branch: 1 branch
BranchTrueFalseTotal
if $id and $self->opts->{"hash"} 0 32 32
Condition33%1/3
$id and $self->opts->{"hash"}
!ll&&!rl&&r
0320
MC/DC0%0/2
$id and $self->opts->{"hash"}
$id $self->opts->{"hash"}
Truth table33%1/3
A and B
A= $idB= $self->opts->{"hash"}
ABresult
0X0
100
111
228  
229 # Lookup backend storage from CLI options
230 my $backendStorage =
231 32 ▶ ( lc( $self->opts->{backend} || 'global' ) ) . "Storage";
Condition100%2/2
$self->opts->{'backend'} || 'global'
l!l
230
MC/DC100%1/1
$self->opts->{'backend'} || 'global'
$self->opts->{'backend'}
Truth table100%2/2
A
A= $self->opts->{'backend'}
Aresult
11
00
232  
233 # allow argument to overwrite the backend
234 32 ▶ if ($backend) {
Branch: 3 branches
BranchTrueFalseTotal
if ($backend) { } 18 14 32
elsif ($self->opts->{'persistent'}) { } 2 12 14
elsif ($self->opts->{'refreshTokens'}) { } 0 12 12
235 18 $backendStorage = $backend . "Storage";
236 }
237  
238 # Handle --persistent
239 elsif ( $self->opts->{persistent} ) {
240 2 $backendStorage = "persistentStorage";
241 2 $id = getPSessionID($id);
242 }
243 elsif ( $self->opts->{refreshTokens} ) {
244 0 $backendStorage = "oidcStorage";
245 }
246  
247 # In any case, fall back to global storage if we couldn't find the backend
248 32 ▶ unless ( $self->conf->{$backendStorage} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->conf->{$backendStorage}) 0 32 32
249 print STDERR "Backend "
250 . $self->opts->{backend}
251 0 . " was not found, falling back to global\n";
252 0 $backendStorage = "globalStorage";
253 }
254  
255 my $as = Lemonldap::NG::Common::Session->new( {
256 hashStore => 0,
257 storageModule => $self->conf->{$backendStorage},
258 32 storageModuleOptions => $self->conf->{"${backendStorage}Options"},
259 id => $id,
260 }
261 );
262  
263 32 ▶ if ( $as->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($as->error) { } 2 30 32
264 2 my $e = $self->stderr;
265 2 print $e $as->error;
266 2 return undef;
267 }
268 else {
269 30 return $as;
270 }
271 }
272  
273 # Returns only session data
274 8 ▶sub _get_one_data {
Subroutine _get_one_data: called CC=6 SCAR=18.0
275 8 my ( $self, $id ) = @_;
276 8 my $as = $self->_get_one_session($id);
277 8 ▶ if ($as) {
Branch: 1 branch
BranchTrueFalseTotal
if ($as) 6 2 8
278 6 my $new;
279  
280 # Filter
281 6 ▶ if ( ( ref( $self->opts->{select} ) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->opts->{'select'} eq 'ARRAY' and scalar @{$self->opts->{'select'};} > 0) { } 1 5 6
Condition66%2/3
ref $self->opts->{"select"} eq "ARRAY" and scalar @{$self->opts->{"select"};} > 0
!ll&&!rl&&r
501
MC/DC50%1/2
ref $self->opts->{"select"} eq "ARRAY" and scalar @{$self->opts->{"select"};} > 0
ref $self->opts->{"select"} eq "ARRAY" scalar @{$self->opts->{"select"};} > 0
Truth table66%2/3
A and B
A= ref $self->opts->{"select"} eq "ARRAY"B= scalar @{$self->opts->{"select"};} > 0
ABresult
0X0
100
111
282 1 and ( scalar @{ $self->opts->{select} } > 0 ) )
283 {
284 1 for ( @{ $self->opts->{select} } ) {
285 2 ▶ $new->{$_} = $as->data->{$_} if defined $as->data->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if defined $as->data->{$_} 2 0 2
286 }
287 }
288 else {
289 5 $new = $as->data;
290 }
291 6 return $new;
292 }
293 2 return undef;
294 }
295  
296 3 ▶sub delete {
Subroutine delete: called CC=5 SCAR=16.6
Pod undocumented
297 3 my ($self) = shift;
298 3 my $result = 0;
299 3 my @sessions;
300  
301 # Run search if a where option was provided
302 3 ▶ if ( $self->opts->{where} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opts->{'where'}) { } 1 2 3
303 1 my $res = $self->_search();
304 1 @sessions = keys %{$res};
305 }
306 else {
307 2 @sessions = @_;
308 }
309  
310 3 my @result;
311 3 for my $id (@sessions) {
312 4 my $as = $self->_get_one_session($id);
313 4 ▶ if ($as) {
Branch: 1 branch
BranchTrueFalseTotal
if ($as) 4 0 4
314 4 ▶ unless ( $as->remove ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($as->remove) 0 4 4
315 0 my $e = $self->stderr;
316 0 print $e $as->error;
317 0 $result = 1;
318 }
319 }
320 }
321 3 return $result;
322 }
323  
324 8 ▶sub get {
Subroutine get: called CC=3 SCAR=11.0
Pod undocumented
325 8 my $self = shift;
326 8 my $o = $self->stdout;
327  
328 8 my @result;
329 8 for my $id (@_) {
330 8 my $new = $self->_get_one_data($id);
331 8 ▶ push @result, $new if $new;
Branch: 1 branch
BranchTrueFalseTotal
if $new 6 2 8
332 }
333 8 print $o $self->_to_json( \@result );
334 8 return 0;
335 }
336  
337 14 ▶sub _get_psession {
Subroutine _get_psession: called CC=2 SCAR=7.0
338 14 my ( $self, $uid ) = @_;
339 14 my $psession_id = getPSessionID($uid);
340 14 my $res = $self->_get_one_session( $psession_id, 'persistent' );
341 14 ▶ die "Could not get psession for user $uid" unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 14 14
342 14 return $res;
343 }
344  
345 8 ▶sub _get_psession_data {
Subroutine _get_psession_data: called CC=1 SCAR=0.0
346 8 my ( $self, $uid ) = @_;
347 8 my $ps = $self->_get_psession($uid);
348 8 return $ps->data;
349 }
350  
351 # This method takes a special psession key (oidcConsents, 2fDevices..)
352 # and returns the expected JSON object
353 # idBuilder is a sub that gets applied to every object in the array, yielding the key of
354 # this object in the resulting hash
355 8 ▶sub _get_psession_special {
Subroutine _get_psession_special: called CC=4 SCAR=14.0
356 8 my ( $self, $target, $keyName, $idBuilder ) = @_;
357 8 my $psession = $self->_get_psession_data($target);
358 8 my $res = {};
359 8 ▶ my $special = $psession->{$keyName} || "[]";
Condition50%1/2
$psession->{$keyName} || '[]'
l!l
80
MC/DC0%0/1
$psession->{$keyName} || '[]'
$psession->{$keyName}
Truth table50%1/2
A
A= $psession->{$keyName}
Aresult
11
00
360 8 $special = from_json($special);
361 8 ▶ die "Expecting JSON array in $keyName" unless ref($special) eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $special eq "ARRAY" 0 8 8
362 8 for my $item ( @{$special} ) {
363 14 my $id = $idBuilder->($item);
364 14 $res->{$id} = $item;
365 }
366 8 return $res;
367 }
368  
369 # This method deletes all matching items from an array psession key (oidcConsents, 2fDevices..)
370 # keyBuilder is a sub that gets applied to every object in the array, yielding the value to be
371 # compared against
372 6 ▶sub _del_psession_special {
Subroutine _del_psession_special: called CC=7 SCAR=19.6
373 6 my ( $self, $target, $specialKeyName, $itemKeyBuilder, @todelete ) = @_;
374 6 my $psession = $self->_get_psession($target);
375 6 my $data = $psession->data;
376 6 my $deleted = 0;
377  
378 6 ▶ my $special = $data->{$specialKeyName} || "[]";
Condition50%1/2
$data->{$specialKeyName} || '[]'
l!l
60
MC/DC0%0/1
$data->{$specialKeyName} || '[]'
$data->{$specialKeyName}
Truth table50%1/2
A
A= $data->{$specialKeyName}
Aresult
11
00
379 6 $special = from_json($special);
380  
381 6 ▶ die "Expecting JSON array in $specialKeyName"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $special eq "ARRAY" 0 6 6
382 unless ref($special) eq "ARRAY";
383  
384 6 my @new;
385 6 for my $item ( @{$special} ) {
386 15 my $id = $itemKeyBuilder->($item);
387 15 ▶ if ( $id and grep { $_ eq $id } @todelete ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id and grep {$_ eq $id;} @todelete) { } 6 9 15
Condition66%2/3
$id and grep {$_ eq $id;} @todelete
!ll&&!rl&&r
096
MC/DC50%1/2
$id and grep {$_ eq $id;} @todelete
$id grep {$_ eq $id;} @todelete
Truth table66%2/3
A and B
A= $idB= grep {$_ eq $id;} @todelete
ABresult
0X0
100
111
388 6 $deleted = $deleted + 1;
389 }
390 else {
391 9 push @new, $item;
392 }
393 }
394 6 ▶ if ($deleted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deleted) 6 0 6
395 6 $data->{$specialKeyName} = to_json( [@new] );
396 }
397  
398 # TODO should this be in the if???
399 6 $psession->update($data);
400 }
401  
402 0 ▶sub _migrateu2f_device {
Subroutine _migrateu2f_device: not called CC=2 SCAR=17.9
403 0 my ( $self, $device ) = @_;
404  
405 0 my $credential_id = $device->{_keyHandle};
406 0 my $_userKey = $device->{_userKey};
407  
408 0 eval { require Authen::WebAuthn };
409 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
410 0 die "Missing Authen::WebAuthn dependency: $@";
411 }
412  
413 0 my $credential_pubkey =
414 Authen::WebAuthn::convert_raw_ecc_to_cose($_userKey);
415  
416 return {
417 0 type => "WebAuthn",
418 name => "$device->{name}",
419 _credentialId => "$credential_id",
420 _credentialPublicKey => "$credential_pubkey",
421 _signCount => 0,
422 epoch => "$device->{epoch}",
423 };
424 }
425  
426 0 ▶sub _migrateu2f {
Subroutine _migrateu2f: not called CC=8 SCAR=42.8
427 0 my $self = shift;
428 0 my $target = shift;
429  
430 0 my $psession = $self->_get_psession($target);
431 0 my $data = $psession->data;
432 0 my $migrated = 0;
433  
434 0 ▶ my $_2fDevices = $data->{_2fDevices} || "[]";
Condition0%0/2
$data->{'_2fDevices'} || '[]'
l!l
00
MC/DC0%0/1
$data->{'_2fDevices'} || '[]'
$data->{'_2fDevices'}
Truth table0%0/2
A
A= $data->{'_2fDevices'}
Aresult
11
00
435 0 $_2fDevices = from_json($_2fDevices);
436  
437 0 ▶ die "Expecting JSON array in _2fDevices"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_2fDevices eq "ARRAY" 0 0 0
438 unless ref($_2fDevices) eq "ARRAY";
439  
440 0 my @new_2fDevices = @{$_2fDevices};
441 0 my @u2f_devices = grep { $_->{type} eq "U2F" } @{$_2fDevices};
442  
443 0 my %migrated_devices;
444 0 for my $u2f_device (@u2f_devices) {
445 0 my $migrated_device = $self->_migrateu2f_device($u2f_device);
446 $migrated_devices{ $migrated_device->{_credentialId} } =
447 0 $migrated_device;
448 }
449  
450 0 for my $migrated_device ( keys %migrated_devices ) {
451  
452 # If credentialId is not already present
453 0 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_->{"type"} eq "WebAuthn" and $_->{"_credentialId"} eq $migrated_device;} @new_2fDevices) 0 0 0
454 grep {
455 $_->{type} eq "WebAuthn"
456 0 ▶ and $_->{_credentialId} eq $migrated_device
Condition0%0/3
$_->{'type'} eq 'WebAuthn' && $_->{'_credentialId'} eq $migrated_device
!ll&&!rl&&r
000
MC/DC0%0/2
$_->{'type'} eq 'WebAuthn' && $_->{'_credentialId'} eq $migrated_device
$_->{'type'} eq 'WebAuthn' $_->{'_credentialId'} eq $migrated_device
Truth table0%0/3
A && B
A= $_->{'type'} eq 'WebAuthn'B= $_->{'_credentialId'} eq $migrated_device
ABresult
0X0
100
111
457 } @new_2fDevices
458 )
459 {
460 0 push @new_2fDevices, $migrated_devices{$migrated_device};
461 0 $migrated = 1;
462 }
463 }
464  
465 0 ▶ if ($migrated) {
Branch: 1 branch
BranchTrueFalseTotal
if ($migrated) 0 0 0
466 0 $data->{_2fDevices} = to_json( [@new_2fDevices] );
467 0 $psession->update($data);
468 }
469  
470 }
471  
472 2 ▶sub consents_get {
Subroutine consents_get: called CC=2 SCAR=7.0
Pod undocumented
473 2 my $self = shift;
474 2 my $target = shift;
475 2 ▶ return 0 unless $target;
Branch: 1 branch
BranchTrueFalseTotal
unless $target 0 2 2
476  
477 2 my $o = $self->stdout;
478 my $consents = $self->_get_psession_special( $target, '_oidcConsents',
479 2 ▶ sub { $_[0]->{rp} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
480 2 print $o $self->_to_json($consents);
481 2 return 0;
482 }
483  
484 6 ▶sub secondfactors_get {
Subroutine secondfactors_get: called CC=2 SCAR=7.0
Pod undocumented
485 6 my $self = shift;
486 6 my $target = shift;
487 6 ▶ return 0 unless $target;
Branch: 1 branch
BranchTrueFalseTotal
unless $target 0 6 6
488  
489 6 my $o = $self->stdout;
490 my $consents = $self->_get_psession_special( $target, '_2fDevices',
491 6 ▶ sub { genId2F( $_[0] ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
492 6 print $o $self->_to_json($consents);
493 6 return 0;
494 }
495  
496 1 ▶sub consents_delete {
Subroutine consents_delete: called CC=3 SCAR=11.1
Pod undocumented
497 1 my $self = shift;
498 1 my $target = shift;
499 1 ▶ return 0 unless $target;
Branch: 1 branch
BranchTrueFalseTotal
unless $target 0 1 1
500  
501 1 my @ids = @_;
502 1 ▶ return 0 unless @ids;
Branch: 1 branch
BranchTrueFalseTotal
unless @ids 0 1 1
503  
504 $self->_del_psession_special( $target, '_oidcConsents',
505 1 ▶ sub { $_[0]->{rp} }, @ids );
Subroutine __ANON__: called CC=1 SCAR=0.0
506 1 return 0;
507 }
508  
509 1 ▶sub secondfactors_delete {
Subroutine secondfactors_delete: called CC=3 SCAR=11.1
Pod undocumented
510 1 my $self = shift;
511 1 my $target = shift;
512 1 ▶ return 0 unless $target;
Branch: 1 branch
BranchTrueFalseTotal
unless $target 0 1 1
513  
514 1 my @ids = @_;
515 1 ▶ return 0 unless @ids;
Branch: 1 branch
BranchTrueFalseTotal
unless @ids 0 1 1
516 $self->_del_psession_special( $target, '_2fDevices',
517 1 ▶ sub { genId2F( $_[0] ) }, @ids );
Subroutine __ANON__: called CC=1 SCAR=0.0
518 1 return 0;
519 }
520  
521 3 ▶sub _get_psession_targets {
Subroutine _get_psession_targets: called CC=4 SCAR=13.9
522 3 my ( $self, @args ) = @_;
523  
524 3 ▶ if ( $self->opts->{where} or $self->opts->{all} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opts->{'where'} or $self->opts->{'all'}) { } 2 1 3
Condition100%3/3
$self->opts->{"where"} or $self->opts->{"all"}
l!l&&r!l&&!r
111
MC/DC100%2/2
$self->opts->{"where"} or $self->opts->{"all"}
$self->opts->{"where"} $self->opts->{"all"}
Truth table100%3/3
A or B
A= $self->opts->{"where"}B= $self->opts->{"all"}
ABresult
1X1
011
000
525 2 $self->opts->{persistent} = 1;
526  
527 2 ▶ if ( $self->opts->{all} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opts->{"all"}) 1 1 2
528 1 delete $self->opts->{where};
529 }
530  
531 2 my $res = $self->_search();
532 2 return ( map { $res->{$_}->{_session_uid} } keys %{$res} );
533 }
534 else {
535 1 return @args;
536 }
537 }
538  
539 3 ▶sub secondfactors_delType {
Subroutine secondfactors_delType: called CC=5 SCAR=16.1
Pod undocumented
540 3 my $self = shift;
541  
542 3 my $target;
543 3 ▶ unless ( $self->opts->{where} or $self->opts->{all} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->opts->{"where"} or $self->opts->{"all"}) 1 2 3
Condition100%3/3
$self->opts->{"where"} or $self->opts->{"all"}
l!l&&r!l&&!r
111
MC/DC100%2/2
$self->opts->{"where"} or $self->opts->{"all"}
$self->opts->{"where"} $self->opts->{"all"}
Truth table100%3/3
A or B
A= $self->opts->{"where"}B= $self->opts->{"all"}
ABresult
1X1
011
000
544 1 $target = shift;
545 }
546  
547 3 my @types = @_;
548 3 ▶ return 0 unless @types;
Branch: 1 branch
BranchTrueFalseTotal
unless @types 0 3 3
549  
550 3 my @targets = $self->_get_psession_targets($target);
551 3 for my $target (@targets) {
552 $self->_del_psession_special( $target, '_2fDevices',
553 4 ▶ sub { $_[0]->{type} }, @types );
Subroutine __ANON__: called CC=1 SCAR=0.0
554 }
555  
556 3 return 0;
557 }
558  
559 0 ▶sub secondfactors_migrateu2f {
Subroutine secondfactors_migrateu2f: not called CC=3 SCAR=24.8
Pod undocumented
560 0 my ( $self, @ids ) = @_;
561 0 my $result = 0;
562 0 my @sessions = $self->_get_psession_targets(@ids);
563  
564 0 for my $id (@sessions) {
565 0 ▶ if ( !$self->_migrateu2f($id) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_migrateu2f($id)) 0 0 0
566 0 $result = 1;
567 }
568 }
569 0 return $result;
570 }
571  
572 1 ▶sub setKey {
Subroutine setKey: called CC=6 SCAR=18.7
Pod undocumented
573 1 my $self = shift;
574 1 my $id = shift;
575 1 ▶ die "Unever number of arguments" unless ( @_ % 2 ) == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 == 0 0 1 1
576 1 my %newvalues = (@_);
577 1 ▶ die "Session ID and new values not provided" unless $id and %newvalues;
Branch: 1 branch
BranchTrueFalseTotal
unless $id and %newvalues 0 1 1
Condition33%1/3
$id and %newvalues
!ll&&!rl&&r
001
MC/DC0%0/2
$id and %newvalues
$id %newvalues
Truth table33%1/3
A and B
A= $idB= %newvalues
ABresult
0X0
100
111
578  
579 1 my $as = $self->_get_one_session($id);
580 1 ▶ die unless $as;
Branch: 1 branch
BranchTrueFalseTotal
unless $as 0 1 1
581  
582 1 my $data = $as->data;
583  
584 1 for ( keys %newvalues ) {
585 2 $data->{$_} = $newvalues{$_};
586 }
587  
588 1 $as->update($data);
589 1 return 0;
590 }
591  
592 1 ▶sub delKey {
Subroutine delKey: called CC=5 SCAR=16.6
Pod undocumented
593 1 my $self = shift;
594 1 my $id = shift;
595 1 my @todelete = @_;
596 1 ▶ die "Session ID and key names not provided" unless $id and @todelete;
Branch: 1 branch
BranchTrueFalseTotal
unless $id and @todelete 0 1 1
Condition33%1/3
$id and @todelete
!ll&&!rl&&r
001
MC/DC0%0/2
$id and @todelete
$id @todelete
Truth table33%1/3
A and B
A= $idB= @todelete
ABresult
0X0
100
111
597  
598 1 my $as = $self->_get_one_session($id);
599 1 ▶ die unless $as;
Branch: 1 branch
BranchTrueFalseTotal
unless $as 0 1 1
600  
601 1 my $data = $as->data;
602  
603 1 for (@todelete) {
604  
605 # Weird, I know. But that's how
606 # Lemonldap::NG::Common::Session::update works
607 3 $data->{$_} = undef;
608 }
609  
610 1 $as->update($data);
611 1 return 0;
612 }
613  
614 4 ▶sub _get_login_history {
Subroutine _get_login_history: called CC=4 SCAR=14.0
615 4 my ( $self, $uid ) = @_;
616  
617 4 my $psession_id = getPSessionID($uid);
618 4 my $as = $self->_get_one_session( $psession_id, 'persistent' );
619 4 ▶ return undef unless $as;
Branch: 1 branch
BranchTrueFalseTotal
unless $as 0 4 4
620  
621 4 my $data = $as->data;
622 4 my $login_history = $data->{_loginHistory};
623 4 ▶ return $login_history
Branch: 1 branch
BranchTrueFalseTotal
if $login_history and ref $login_history eq "HASH" 3 1 4
Condition66%2/3
$login_history and ref $login_history eq "HASH"
!ll&&!rl&&r
103
MC/DC50%1/2
$login_history and ref $login_history eq "HASH"
$login_history ref $login_history eq "HASH"
Truth table66%2/3
A and B
A= $login_historyB= ref $login_history eq "HASH"
ABresult
0X0
100
111
624 if $login_history && ref($login_history) eq 'HASH';
625 1 return undef;
626 }
627  
628 3 ▶sub _last_login_event {
Subroutine _last_login_event: called CC=8 SCAR=22.7
629 3 my ( $self, $uid, $key, $label ) = @_;
630 3 ▶ die "User ID required" unless $uid;
Branch: 1 branch
BranchTrueFalseTotal
unless $uid 0 3 3
631  
632 3 my $o = $self->stdout;
633 3 my $e = $self->stderr;
634 3 my $login_history = $self->_get_login_history($uid);
635  
636 3 ▶ unless ($login_history) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($login_history) 1 2 3
637 1 print $e "No login history found for user $uid\n";
638 1 return 1;
639 }
640  
641 2 my $events = $login_history->{$key};
642 2 ▶ unless ( $events && ref($events) eq 'ARRAY' && @$events ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($events and ref $events eq "ARRAY" and @$events) 0 2 2
Condition33%2/6
$events and ref $events eq "ARRAY" and @$events
!ll&&!rl&&r
002
$events and ref $events eq "ARRAY"
!ll&&!rl&&r
002
MC/DC0%0/3
$events and ref $events eq "ARRAY" and @$events
$events ref $events eq "ARRAY" @$events
Truth table25%1/4
A and B and C
A= $eventsB= ref $events eq "ARRAY"C= @$events
ABCresult
0XX0
10X0
1100
1111
643 0 print $e "No $label found for user $uid\n";
644 0 return 1;
645 }
646  
647 2 my $last = $events->[0];
648 2 my $utime = $last->{_utime};
649  
650 2 ▶ unless ($utime) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($utime) 0 2 2
651 0 print $e "No timestamp found in last $label\n";
652 0 return 1;
653 }
654  
655 2 my $msg = scalar( localtime($utime) );
656 2 ▶ $msg .= " ($last->{error})" if $last->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $last->{"error"} 1 1 2
657 2 print $o "$msg\n";
658 2 return 0;
659 }
660  
661 2 ▶sub history_lastsuccessdate {
Subroutine history_lastsuccessdate: called CC=1 SCAR=0.0
Pod undocumented
662 2 my ( $self, $uid ) = @_;
663 2 return $self->_last_login_event( $uid, 'successLogin', 'successful login' );
664 }
665  
666 1 ▶sub history_lastfailuredate {
Subroutine history_lastfailuredate: called CC=1 SCAR=0.0
Pod undocumented
667 1 my ( $self, $uid ) = @_;
668 1 return $self->_last_login_event( $uid, 'failedLogin', 'failed login' );
669 }
670  
671 1 ▶sub history_get {
Subroutine history_get: called CC=5 SCAR=16.7
Pod undocumented
672 1 my ( $self, $uid ) = @_;
673 1 ▶ die "User ID required" unless $uid;
Branch: 1 branch
BranchTrueFalseTotal
unless $uid 0 1 1
674  
675 1 my $o = $self->stdout;
676 1 my $e = $self->stderr;
677 1 my $login_history = $self->_get_login_history($uid);
678  
679 1 ▶ unless ($login_history) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($login_history) 0 1 1
680 0 print $e "No login history found for user $uid\n";
681 0 return 1;
682 }
683  
684 1 my $result = {};
685  
686 1 for my $key (qw(successLogin failedLogin)) {
687 2 ▶ next unless my $events = $login_history->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless my $events = $login_history->{$key} 0 2 2
688 $result->{$key} = [
689 map {
690 2 {
691 %$_,
692 3 date => scalar( localtime( $_->{_utime} ) ),
693 }
694 } @$events
695 ];
696 }
697  
698 1 print $o $self->_to_json($result);
699 1 return 0;
700 }
701  
702 48 ▶sub run {
Subroutine run: called CC=7 SCAR=21.8
Pod undocumented
703 48 my $self = shift;
704 48 my $action = shift;
705 48 my $opts = shift;
706  
707 48 ▶ unless ( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 48 48
708 0 $self = $self->new;
709 }
710 48 ▶ unless ($action) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($action) 0 48 48
711 0 die 'nothing to do, aborting';
712 }
713 48 $self->opts($opts);
714  
715 # Simple commands
716 48 ▶ if ( $action =~ /^(?:get|search|delete|setKey|delKey|backup|restore)$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($action =~ /^(?:get|search|delete|setKey|delKey|backup|restore)$/) { } 31 17 48
elsif ($action =~ /^(?:secondfactors|consents|history)$/) { } 17 0 17
717 31 return $self->$action(@_);
718 }
719  
720 # Subcommands and target
721 elsif ( $action =~ /^(?:secondfactors|consents|history)$/ ) {
722 17 my $subcommand = shift;
723 17 ▶ unless ($subcommand) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subcommand) 0 17 17
724 0 die "Missing subcommand $action";
725 }
726 17 my $func = "${action}_${subcommand}";
727 17 ▶ if ( $self->can($func) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($func)) { } 17 0 17
728 17 return $self->$func(@_);
729 }
730 else {
731 0 die "Unknown subcommand $subcommand for action $action";
732 }
733 }
734 else {
735 0 die "unknown action $action. Only get or search are allowed";
736 }
737 }
738  
739 3 1;
« blib/lib/Lemonldap/NG/Common/IPv6.pm ↑ Summary blib/lib/Lemonldap/NG/Common/MessageBroker/MQTT.pm »