blib/lib/Lemonldap/NG/Manager/Api/2F.pm

filter: Statementstmt 94.9% 245 / 258 Branchbran 70.4% 86 / 122 Conditioncond 83.3% 50 / 60 MC/DCmcdc 63.3% 19 / 30 Subroutinesub 96.5% 28 / 29 Podpod 0.0% 0 / 9 totaltotal 84.2% 428 / 508 CC 90 SCAR 18.7
CC
90
Cov
87%
_delete2FFromSessions
15.6
addSecondFactor
9.4
addSecondFactorByType
8.7
CRAP 6.5
« blib/lib/Lemonldap/NG/Manager/Viewer.pm ↑ Summary blib/lib/Lemonldap/NG/Manager/Conf/Zero.pm »
1 package Lemonldap::NG::Manager::Api::2F;
2  
3 28 our $VERSION = '2.21.0';
4  
5 package Lemonldap::NG::Manager::Api;
6  
7 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 28 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 28 ▶use Lemonldap::NG::Common::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 28 ▶use Lemonldap::NG::Common::Util qw/genId2F display2F filterKey2F/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 20 ▶sub getSecondFactors {
Subroutine getSecondFactors: called CC=3 SCAR=11.0
Pod undocumented
16 20 my ( $self, $req ) = @_;
17 20 my ( $uid, $res );
18  
19 20 ▶ $uid = $req->params('_uid')
Branch: 1 branch
BranchTrueFalseTotal
$uid = $req->params("_uid") or return $self->searchSecondFactors($req) 11 9 20
20 or return $self->searchSecondFactors($req);
21  
22 11 $self->logger->debug("[API] 2F for $uid requested");
23  
24 11 $res = $self->_get2F($uid);
25  
26 return $self->sendError( $req, $res->{msg}, $res->{code} )
27 11 ▶ unless ( $res->{res} eq 'ok' );
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"res"} eq "ok" 0 11 11
28  
29 11 return $self->sendJSONresponse( $req, $res->{secondFactors} );
30 }
31  
32 21 ▶sub getSecondFactorsByType {
Subroutine getSecondFactorsByType: called CC=4 SCAR=14.2
Pod undocumented
33 21 my ( $self, $req ) = @_;
34 21 my ( $uid, $type, $res );
35  
36 21 ▶ $uid = $req->params('_uid')
Branch: 1 branch
BranchTrueFalseTotal
$uid = $req->params("_uid") or return $self->sendError($req, "Uid is missing", 400) 21 0 21
37 or return $self->sendError( $req, 'Uid is missing', 400 );
38  
39 21 ▶ $type = $req->params('type')
Branch: 1 branch
BranchTrueFalseTotal
$type = $req->params("type") or return $self->sendError($req, "Type is missing", 400) 21 0 21
40 or return $self->sendError( $req, 'Type is missing', 400 );
41  
42 21 $self->logger->debug("[API] 2F for $uid with type $type requested");
43  
44 21 $res = $self->_get2F( $uid, uc $type );
45  
46 return $self->sendError( $req, $res->{msg}, $res->{code} )
47 21 ▶ unless ( $res->{res} eq 'ok' );
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"res"} eq "ok" 0 21 21
48  
49 21 return $self->sendJSONresponse( $req, $res->{secondFactors} );
50 }
51  
52 12 ▶sub getSecondFactorsById {
Subroutine getSecondFactorsById: called CC=5 SCAR=16.6
Pod undocumented
53 12 my ( $self, $req ) = @_;
54 12 my ( $uid, $id, $res );
55  
56 12 ▶ $uid = $req->params('_uid')
Branch: 1 branch
BranchTrueFalseTotal
$uid = $req->params("_uid") or return $self->sendError($req, "uid is missing", 400) 12 0 12
57 or return $self->sendError( $req, 'uid is missing', 400 );
58  
59 12 ▶ $id = $req->params('id')
Branch: 1 branch
BranchTrueFalseTotal
$id = $req->params("id") or return $self->sendError($req, "id is missing", 400) 12 0 12
60 or return $self->sendError( $req, 'id is missing', 400 );
61  
62 12 $self->logger->debug("[API] 2F for $uid with id $id requested");
63  
64 12 $res = $self->_get2F( $uid, undef, $id );
65  
66 return $self->sendError( $req, $res->{msg}, $res->{code} )
67 12 ▶ unless ( $res->{res} eq 'ok' );
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"res"} eq "ok" 0 12 12
68  
69 return $self->sendError( $req, "2F id '$id' not found for user '$uid'",
70 404 )
71 12 ▶ unless ( scalar @{ $res->{secondFactors} } > 0 );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$res->{"secondFactors"};} > 0 0 12 12
72  
73 12 return $self->sendJSONresponse( $req, @{ $res->{secondFactors} }[0] );
74 }
75  
76 5 ▶sub deleteSecondFactors {
Subroutine deleteSecondFactors: called CC=4 SCAR=14.2
Pod undocumented
77 5 my ( $self, $req, @path ) = @_;
78 5 my ( $uid, $res );
79  
80 # Return an error on
81 @path
82 5 ▶ and return $self->sendError( $req, 'Invalid URI, provide /id/ or /type/',
Branch: 1 branch
BranchTrueFalseTotal
@path and return $self->sendError($req, "Invalid URI, provide /id/ or /type/", 400) 0 5 5
83 400 );
84  
85 5 ▶ $uid = $req->params('uid')
Branch: 1 branch
BranchTrueFalseTotal
$uid = $req->params("uid") or return $self->sendError($req, "uid is missing", 400) 5 0 5
86 or return $self->sendError( $req, 'uid is missing', 400 );
87  
88 5 $self->logger->debug("[API] Delete all 2F for $uid requested");
89  
90 5 $res = $self->_delete2F( $req, $uid );
91  
92 return $self->sendError( $req, $res->{msg}, $res->{code} )
93 5 ▶ unless ( $res->{res} eq 'ok' );
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"res"} eq "ok" 0 5 5
94  
95 5 return $self->sendJSONresponse( $req, { message => $res->{msg} } );
96 }
97  
98 9 ▶sub searchSecondFactors {
Subroutine searchSecondFactors: called CC=6 SCAR=18.1
Pod undocumented
99 9 my ( $self, $req ) = @_;
100  
101 9 ▶ my $uid = $req->param('uid') || '*';
Condition100%2/2
$req->param('uid') || '*'
l!l
54
MC/DC100%1/1
$req->param('uid') || '*'
$req->param('uid')
Truth table100%2/2
A
A= $req->param('uid')
Aresult
11
00
102 9 my @types = $req->parameters->get_all('type');
103 28 ▶ use Hash::MultiValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 9 my $search_params =
105 Hash::MultiValue->from_mixed( { _session_uid => $uid, type => \@types } );
106  
107 9 my $res = $self->search2F( $req, $self->_getPersistentMod, "Persistent",
108 $search_params, 1 );
109  
110 9 ▶ if ( $res->{result} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{'result'}) { } 9 0 9
111 9 my $api_result = [];
112  
113 my @sorted_results =
114 9 ▶ sort { $a->{userId} cmp $b->{userId} } @{ $res->{values} || [] };
Condition50%1/2
$res->{'values'} || []
l!l
90
MC/DC0%0/1
$res->{'values'} || []
$res->{'values'}
Truth table50%1/2
A
A= $res->{'values'}
Aresult
11
00
115 9 for my $result (@sorted_results) {
116 push @$api_result, {
117 uid => $result->{userId},
118 secondFactors => [
119 map {
120 {
121 id => genId2F($_),
122 type => $_->{type},
123 name => $_->{name}
124 }
125 9 ▶ } @{ $result->{_2fDevices} || [] }
Condition50%1/2
$result->{'_2fDevices'} || []
l!l
90
MC/DC0%0/1
$result->{'_2fDevices'} || []
$result->{'_2fDevices'}
Truth table50%1/2
A
A= $result->{'_2fDevices'}
Aresult
11
00
126 ]
127  
128 };
129 }
130 9 return $self->sendJSONresponse( $req, $api_result );
131 }
132 else {
133 0 return $self->sendError( $req, $res->{msg}, $res->{code} );
134 }
135 }
136  
137 8 ▶sub deleteSecondFactorsById {
Subroutine deleteSecondFactorsById: called CC=5 SCAR=16.4
Pod undocumented
138 8 my ( $self, $req ) = @_;
139 8 my ( $uid, $id, $res );
140  
141 8 ▶ $uid = $req->params('uid')
Branch: 1 branch
BranchTrueFalseTotal
$uid = $req->params("uid") or return $self->sendError($req, "uid is missing", 400) 8 0 8
142 or return $self->sendError( $req, 'uid is missing', 400 );
143  
144 8 ▶ $id = $req->params('id')
Branch: 1 branch
BranchTrueFalseTotal
$id = $req->params("id") or return $self->sendError($req, "id is missing", 400) 8 0 8
145 or return $self->sendError( $req, 'id is missing', 400 );
146  
147 8 $self->logger->debug("[API] Delete 2F for $uid with id $id requested");
148  
149 8 $res = $self->_delete2F( $req, $uid, undef, $id );
150  
151 return $self->sendError( $req, $res->{msg}, $res->{code} )
152 8 ▶ unless ( $res->{res} eq 'ok' );
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"res"} eq "ok" 0 8 8
153  
154 return $self->sendError( $req, "2F id '$id' not found for user '$uid'",
155 404 )
156 8 ▶ unless ( $res->{removed} > 0 );
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"removed"} > 0 4 4 8
157  
158 4 return $self->sendJSONresponse( $req, { message => $res->{msg} } );
159 }
160  
161 3 ▶sub deleteSecondFactorsByType {
Subroutine deleteSecondFactorsByType: called CC=4 SCAR=14.2
Pod undocumented
162 3 my ( $self, $req ) = @_;
163 3 my ( $uid, $type, $res );
164  
165 3 ▶ $uid = $req->params('uid')
Branch: 1 branch
BranchTrueFalseTotal
$uid = $req->params("uid") or return $self->sendError($req, "uid is missing", 400) 3 0 3
166 or return $self->sendError( $req, 'uid is missing', 400 );
167  
168 3 ▶ $type = $req->params('type')
Branch: 1 branch
BranchTrueFalseTotal
$type = $req->params("type") or return $self->sendError($req, "type is missing", 400) 3 0 3
169 or return $self->sendError( $req, 'type is missing', 400 );
170  
171 3 $self->logger->debug(
172 "[API] Delete all 2F for $uid with type $type requested");
173  
174 3 $res = $self->_delete2F( $req, $uid, uc $type );
175  
176 return $self->sendError( $req, $res->{msg}, $res->{code} )
177 3 ▶ unless ( $res->{res} eq 'ok' );
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"res"} eq "ok" 0 3 3
178  
179 3 return $self->sendJSONresponse( $req, { message => $res->{msg} } );
180 }
181  
182 55 ▶sub _get2F {
Subroutine _get2F: called CC=7 SCAR=19.5
183 55 my ( $self, $uid, $type, $id ) = @_;
184 55 my ( $res, $psessions, @secondFactors );
185  
186 55 $psessions = $self->_getSessions2F( $self->_getPersistentMod, 'Persistent',
187 '_session_uid', $uid );
188  
189 55 foreach ( keys %$psessions ) {
190 49 my $devices = $self->_getDevicesFromSessionData( $psessions->{$_} );
191 49 foreach my $device ( @{$devices} ) {
192 99 $self->logger->debug(
193 "Check device [epoch=$device->{epoch}, type=$device->{type}, name=$device->{name}]"
194 );
195 99 ▶ if ( ( !defined $type or uc($type) eq uc( $device->{type} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (!defined($type) || uc $type eq uc $device->{"type"} and !defined($id) || $id eq genId2F($device)) 46 53 99
Condition100%9/9
!defined($type) || uc $type eq uc $device->{"type"} and !defined($id) || $id eq genId2F($device)
!ll&&!rl&&r
302346
not defined $type or uc $type eq uc $device->{"type"}
l!l&&r!l&&!r
501930
not defined $id or $id eq genId2F($device)
l!l&&r!l&&!r
341223
MC/DC100%4/4
!defined($type) || uc $type eq uc $device->{"type"} and !defined($id) || $id eq genId2F($device)
not defined $type uc $type eq uc $device->{"type"} not defined $id $id eq genId2F($device)
Truth table71%5/7
not(A) or B and not(C) or D
A= not defined $typeB= uc $type eq uc $device->{"type"}C= not defined $idD= $id eq genId2F($device)
ABCDresult
00XX0
1X000
01000
1X1X1
1X011
011X1
01011
196 and ( !defined $id or $id eq genId2F($device) ) )
197 {
198 push @secondFactors,
199 46 { %{ filterKey2F($device) }, id => genId2F($device), };
200 }
201 }
202 }
203 $self->logger->debug(
204 55 "Found " . scalar @secondFactors . " 2F devices for uid $uid." );
205 55 return { res => 'ok', secondFactors => [@secondFactors] };
206 }
207  
208 103 ▶sub _getSessions2F {
Subroutine _getSessions2F: called CC=3 SCAR=11.0
209 103 my ( $self, $mod, $kind, $key, $uid ) = @_;
210 103 $self->logger->debug("Looking for sessions for uid $uid ...");
211 my $sessions =
212 103 Lemonldap::NG::Common::Apache::Session->searchOn( $mod->{options}, $key,
213 $uid,
214 ( '_session_kind', '_session_uid', '_session_id', '_2fDevices' ) );
215 103 foreach ( keys %$sessions ) {
216 delete $sessions->{$_}
217 176 ▶ unless ( $sessions->{$_}->{_session_kind} eq $kind );
Branch: 1 branch
BranchTrueFalseTotal
unless $sessions->{$_}{"_session_kind"} eq $kind 87 89 176
218 }
219 103 $self->logger->debug( "Found "
220 . scalar( keys %$sessions )
221 . " $kind sessions for uid $uid." );
222  
223 103 return $sessions;
224 }
225  
226 42 ▶sub _getSession2F {
Subroutine _getSession2F: called CC=2 SCAR=7.0
227 42 my ( $self, $sessionId, $mod ) = @_;
228 42 $self->logger->debug("Looking for session with sessionId $sessionId ...");
229 42 my $session = $self->getApacheSession( $mod, $sessionId );
230 42 ▶ $self->logger->debug(
Branch: 1 branch
BranchTrueFalseTotal
defined $session ? : 42 0 42
231 defined $session
232 ? "Session $sessionId found."
233 : " No session found for sessionId $sessionId"
234 );
235  
236 42 return $session;
237 }
238  
239 32 ▶sub _delete2FFromSessions {
Subroutine _delete2FFromSessions: called CC=14 SCAR=27.5
240 32 my ( $self, $uid, $type, $id, $mod, $kind, $key ) = @_;
241 32 my ( $sessions, $session, $devices, @keep, $removed,
242 $total, $module, $localStorage );
243 32 $sessions = $self->_getSessions2F( $mod, $kind, $key, $uid );
244 32 foreach ( keys %$sessions ) {
245  
246 32 ▶ $session = $self->_getSession2F( $_, $mod )
Branch: 1 branch
BranchTrueFalseTotal
$session = $self->_getSession2F($_, $mod) or return {"res", "ko", "code", 500, "msg", $@} 32 0 32
247 or return { res => 'ko', code => 500, msg => $@ };
248  
249 32 $self->logger->debug(
250 "Looking for 2F Device(s) attached to sessionId $_");
251  
252 32 ▶ if ( $session->data->{_2fDevices} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->data->{'_2fDevices'}) { } 32 0 32
253 32 $devices = $self->_getDevicesFromSessionData( $session->data );
254 32 $total = scalar @$devices;
255  
256 32 $self->logger->debug(
257 "Found $total 2F devices attached to sessionId $_");
258  
259 32 @keep = ();
260 32 ▶ while (@$devices) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$devices) 42 32 74
261 42 my $element = shift @$devices;
262 42 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type || defined $id and defined $type && uc $type ne uc $element->{'type'} || defined $id && $id ne genId2F($element)) { } 22 20 42
Condition100%15/15
defined $type || defined $id and defined $type && uc $type ne uc $element->{"type"} || defined $id && $id ne genId2F($element)
!ll&&!rl&&r
41622
defined $type or defined $id
l!l&&r!l&&!r
14244
defined $type and uc $type ne uc $element->{"type"} or defined $id and $id ne genId2F($element)
l!l&&r!l&&!r
61616
defined $type and uc $type ne uc $element->{"type"}
!ll&&!rl&&r
2486
defined $id and $id ne genId2F($element)
!ll&&!rl&&r
8816
MC/DC83%5/6
defined $type || defined $id and defined $type && uc $type ne uc $element->{"type"} || defined $id && $id ne genId2F($element)
defined $type defined $id defined $type uc $type ne uc $element->{"type"} defined $id $id ne genId2F($element)
Truth table33%5/15
A or B and C and D or E and F
A= defined $typeB= defined $idC= defined $typeD= uc $type ne uc $element->{"type"}E= defined $idF= $id ne genId2F($element)
ABCDEFresult
00XXXX0
1X0X0X0
1X0X100
1X100X0
1X10100
010X0X0
010X100
01100X0
0110100
1X11XX1
1X0X111
1X10111
0111XX1
010X111
0110111
263 ( defined $type or defined $id )
264 and ( (
265 defined $type
266 and uc($type) ne uc( $element->{type} )
267 )
268 or ( defined $id and $id ne genId2F($element) )
269 )
270 )
271 {
272 22 push @keep, $element;
273 }
274 else {
275 20 $removed->{ genId2F($element) } = $element;
276 }
277 }
278 32 ▶ if ( ( $total - scalar @keep ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total - scalar(@keep) > 0) { } 16 16 32
279  
280 # Update session
281 16 $self->logger->debug( "Removing "
282 . ( $total - scalar @keep )
283 . " 2F device(s) attached to sessionId $_ ..." );
284 16 $session->data->{_2fDevices} = to_json( \@keep );
285 16 $session->update( $session->data );
286  
287 # Delete from local cache
288 16 ▶ if ( $session->{options}->{localStorage} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->{"options"}{"localStorage"}) 0 16 16
289 0 $module = $session->{options}->{localStorage};
290 0 eval "use $module;";
291 $localStorage =
292 $module->new(
293 0 $session->{options}->{localStorageOptions} );
294 0 ▶ if ( $localStorage->get($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($localStorage->get($_)) 0 0 0
295 0 $self->logger->debug(
296 "Delete local cache for session $_");
297 0 $localStorage->remove($_);
298 }
299 }
300 }
301 else {
302 16 $self->logger->debug(
303 "No matching 2F devices attached to sessionId $_ were selected for removal."
304 );
305 }
306 }
307 else {
308 0 $self->logger->debug(
309 "No 2F devices attached to sessionId $_ were found.");
310 }
311 }
312  
313 32 return { res => 'ok', removed => $removed };
314 }
315  
316 16 ▶sub _delete2F {
Subroutine _delete2F: called CC=7 SCAR=19.5
317 16 my ( $self, $req, $uid, $type, $id ) = @_;
318 16 my ( $res, $removed, $count );
319  
320 16 $res =
321 $self->_delete2FFromSessions( $uid, $type, $id, $self->_getPersistentMod,
322 'Persistent', '_session_uid' );
323 16 ▶ return $res if ( $res->{res} ne 'ok' );
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"res"} ne "ok" 0 16 16
324 16 ▶ $removed = $res->{removed} || {};
Condition100%2/2
$res->{'removed'} || {}
l!l
88
MC/DC100%1/1
$res->{'removed'} || {}
$res->{'removed'}
Truth table100%2/2
A
A= $res->{'removed'}
Aresult
11
00
325  
326 16 my $whatToTrace = Lemonldap::NG::Handler::PSGI::Main->tsv->{whatToTrace};
327 16 $res =
328 $self->_delete2FFromSessions( $uid, $type, $id, $self->_getSSOMod, 'SSO',
329 $whatToTrace );
330 16 ▶ return $res if ( $res->{res} ne 'ok' );
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"res"} ne "ok" 0 16 16
331 16 ▶ $res->{removed} ||= {};
Condition100%2/2
$res->{"removed"} ||= {}
l!l
88
MC/DC100%1/1
$res->{"removed"} ||= {}
$res->{"removed"}
Truth table100%2/2
A
A= $res->{"removed"}
Aresult
11
00
332  
333 # merge results
334 16 $removed = { %$removed, %{ $res->{removed} } };
335 16 $count = scalar( keys %$removed );
336 16 ▶ if ($count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count) 8 8 16
337 8 my @list_log = map { display2F( $removed->{$_} ) } keys %$removed;
338 8 my $list_log = join( ",", @list_log );
339 8 $self->auditLog(
340 $req,
341 code => "API_2FA_DELETED",
342 message => ("[API] 2FA deletion for $uid: $list_log"),
343 user => $uid,
344 removed_2fa => \@list_log,
345 );
346 }
347  
348 return {
349 16 ▶ res => 'ok',
Branch: 1 branch
BranchTrueFalseTotal
$count > 0 ? : 8 8 16
350 removed => $count,
351 msg => $count > 0
352 ? "Successful operation: " . $count . " 2F were removed"
353 : "No operation performed"
354 };
355 }
356  
357 91 ▶sub _getDevicesFromSessionData {
Subroutine _getDevicesFromSessionData: called CC=4 SCAR=14.4
358 91 my ( $self, $sessiondata ) = @_;
359 91 ▶ if ( $sessiondata->{_2fDevices} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sessiondata->{"_2fDevices"}) 86 5 91
360 86 my $devices;
361 86 eval { $devices = from_json( $sessiondata->{_2fDevices} ); };
362 86 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 86 86
363 0 $self->logger->warn("Error deserializing _2fDevices: $@");
364 }
365 else {
366 86 ▶ if ( ref($devices) eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $devices eq 'ARRAY') { } 86 0 86
367 86 return $devices;
368 }
369 else {
370 0 $self->logger->warn(
371 "Error deserializing _2fDevices: not a JSON Array");
372 }
373 }
374 }
375 5 return [];
376 }
377  
378 8 ▶sub addSecondFactor {
Subroutine addSecondFactor: called CC=9 SCAR=22.4
Pod undocumented
379 8 my ( $self, $req ) = @_;
380  
381 8 ▶ my $uid = $req->params('uid')
Branch: 1 branch
BranchTrueFalseTotal
my $uid = $req->params("uid") or return $self->sendError($req, "uid is missing", 400) 8 0 8
382 or return $self->sendError( $req, 'uid is missing', 400 );
383  
384 8 $self->logger->debug("[API] Add 2F for $uid");
385  
386 8 my $add = $req->jsonBodyToObj;
387 8 ▶ return $self->sendError( $req, "Invalid input: " . $req->error, 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless $add and ref $add eq "HASH" 0 8 8
Condition33%1/3
$add and ref $add eq "HASH"
!ll&&!rl&&r
008
MC/DC0%0/2
$add and ref $add eq "HASH"
$add ref $add eq "HASH"
Truth table33%1/3
A and B
A= $addB= ref $add eq "HASH"
ABresult
0X0
100
111
388 unless ( $add and ref($add) eq "HASH" );
389  
390 return $self->sendError( $req, 'Invalid input: type is missing', 400 )
391 8 ▶ unless ( defined $add->{type} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $add->{"type"} 1 7 8
392  
393 return $self->sendError( $req, 'Invalid input: epoch is forbidden', 400 )
394 7 ▶ if ( defined $add->{epoch} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $add->{"epoch"} 1 6 7
395  
396 6 ▶ my $allow_create =
Condition83%5/6
$req->params('create') && $req->params('create') ne 'false' && $req->params('create') ne '0'
!ll&&!rl&&r
501
$req->params("create") and $req->params("create") ne "false"
!ll&&!rl&&r
411
MC/DC66%2/3
$req->params('create') && $req->params('create') ne 'false' && $req->params('create') ne '0'
$req->params("create") $req->params("create") ne "false" $req->params('create') ne '0'
Truth table75%3/4
A and B && C
A= $req->params("create")B= $req->params("create") ne "false"C= $req->params('create') ne '0'
ABCresult
0XX0
10X0
1100
1111
397 ( $req->params('create')
398 && $req->params('create') ne "false"
399 && $req->params('create') ne "0" );
400  
401 6 my $res = $self->_add2F( $uid, undef, $add, $allow_create );
402  
403 return $self->sendError( $req, $res->{msg}, $res->{code} )
404 6 ▶ if ( $res->{res} ne 'ok' );
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"res"} ne "ok" 3 3 6
405 3 return $self->sendJSONresponse(
406 $req,
407 { message => "Successful operation" },
408 code => 201
409 );
410 }
411  
412 10 ▶sub addSecondFactorByType {
Subroutine addSecondFactorByType: called CC=8 SCAR=21.6
Pod undocumented
413 10 my ( $self, $req ) = @_;
414  
415 10 ▶ my $uid = $req->params('uid')
Branch: 1 branch
BranchTrueFalseTotal
my $uid = $req->params("uid") or return $self->sendError($req, "uid is missing", 400) 10 0 10
416 or return $self->sendError( $req, 'uid is missing', 400 );
417  
418 10 ▶ my $type = $req->params('type')
Branch: 1 branch
BranchTrueFalseTotal
my $type = $req->params("type") or return $self->sendError($req, "type is missing", 400) 10 0 10
419 or return $self->sendError( $req, 'type is missing', 400 );
420  
421 10 my $add = $req->jsonBodyToObj;
422 10 ▶ return $self->sendError( $req, "Invalid input: " . $req->error, 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless $add and ref $add eq "HASH" 0 10 10
Condition33%1/3
$add and ref $add eq "HASH"
!ll&&!rl&&r
0010
MC/DC0%0/2
$add and ref $add eq "HASH"
$add ref $add eq "HASH"
Truth table33%1/3
A and B
A= $addB= ref $add eq "HASH"
ABresult
0X0
100
111
423 unless ( $add and ref($add) eq "HASH" );
424  
425 10 $self->logger->debug("[API] Add $type 2F for $uid");
426  
427 10 ▶ my $allow_create =
Condition83%5/6
$req->params('create') && $req->params('create') ne 'false' && $req->params('create') ne '0'
!ll&&!rl&&r
901
$req->params("create") and $req->params("create") ne "false"
!ll&&!rl&&r
811
MC/DC66%2/3
$req->params('create') && $req->params('create') ne 'false' && $req->params('create') ne '0'
$req->params("create") $req->params("create") ne "false" $req->params('create') ne '0'
Truth table75%3/4
A and B && C
A= $req->params("create")B= $req->params("create") ne "false"C= $req->params('create') ne '0'
ABCresult
0XX0
10X0
1100
1111
428 ( $req->params('create')
429 && $req->params('create') ne "false"
430 && $req->params('create') ne "0" );
431  
432 10 my $res = $self->_add2F( $uid, $type, $add, $allow_create );
433  
434 return $self->sendError( $req, $res->{msg}, $res->{code} )
435 10 ▶ unless ( $res->{res} eq 'ok' );
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"res"} eq "ok" 7 3 10
436  
437 3 return $self->sendJSONresponse(
438 $req,
439 { message => "Successful operation" },
440 code => 201
441 );
442 }
443  
444 16 ▶sub _transformNew2f {
Subroutine _transformNew2f: called CC=3 SCAR=11.0
445 16 my ( $self, $type, $add ) = @_;
446  
447 # Generic API, no transformation is done
448 16 ▶ if ( !defined($type) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 6 10 16
449 return {
450 6 res => "ok",
451 device => $add,
452 };
453 }
454  
455 10 ▶ if ( uc($type) eq "TOTP" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $type eq 'TOTP') { } 9 1 10
456 9 return $self->_transformNew2fTotp( $type, $add );
457 }
458  
459 else {
460 return {
461 1 res => "ko",
462 code => 400,
463 msg => "Invalid type: $type",
464 };
465 }
466 }
467  
468 9 ▶sub _transformNew2fTotp {
Subroutine _transformNew2fTotp: called CC=4 SCAR=13.9
469 9 my ( $self, $type, $add ) = @_;
470  
471 9 ▶ if ( !$add->{key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($add->{"key"}) 3 6 9
472 return {
473 3 res => "ko",
474 code => 400,
475 msg => "Invalid input: missing \"key\" parameter",
476 };
477 }
478  
479 6 my $secret = $self->_totpKeyToSecret( $add->{key} );
480 6 ▶ if ( !$secret ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($secret) 1 5 6
481 return {
482 1 res => "ko",
483 code => 400,
484 msg => "Invalid secret: you must provide a base32-encoded key",
485 };
486 }
487  
488 my $newdevice = {
489 type => "TOTP",
490 5 ▶ ( $add->{name} ? ( name => $add->{name} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
$add->{'name'} ? : 3 2 5
491 _secret => $secret,
492 };
493  
494 return {
495 5 res => "ok",
496 device => $newdevice,
497 };
498 }
499  
500 6 ▶sub _totpKeyToSecret {
Subroutine _totpKeyToSecret: called CC=3 SCAR=11.0
501 6 my ( $self, $key ) = @_;
502  
503 6 ▶ return unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 6 6
504  
505 # Make sure only BASE32 characters are present
506 6 ▶ if ( uc($key) =~ /^[ABCDEFGHIJKLMNOPQRSTUVWXYZ234567\s]*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (uc($key) =~ /^[ABCDEFGHIJKLMNOPQRSTUVWXYZ234567\s]*$/) 5 1 6
507  
508 # Trim spaces and normalize to lowercase
509 5 my $newKey = $key =~ s/\s//gr;
510 5 return $self->totp_encrypt->get_storable_secret( lc($newKey) );
511 }
512  
513 1 return undef;
514 }
515  
516 0 ▶sub _get_epoch {
Subroutine _get_epoch: not called CC=1 SCAR=6.9
517 0 my ($self) = @_;
518 0 return time();
519 }
520  
521 16 ▶sub _add2F {
Subroutine _add2F: called CC=4 SCAR=13.9
522 16 my ( $self, $uid, $type, $add, $allow_create ) = @_;
523  
524 16 my $res = $self->_transformNew2f( $type, $add );
525 16 ▶ return $res if ( $res->{res} ne 'ok' );
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"res"} ne "ok" 5 11 16
526  
527 11 my $device = $res->{device};
528  
529 11 $device->{epoch} = $self->_get_epoch();
530  
531 11 $res = $self->_check_duplicates( $uid, $device );
532 11 ▶ return $res if ( $res->{res} ne 'ok' );
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"res"} ne "ok" 1 10 11
533  
534 10 $res =
535 $self->_add2FToSessions( $uid, $device, $self->_getPersistentMod,
536 'Persistent', '_session_uid', $allow_create );
537 10 ▶ return $res if ( $res->{res} ne 'ok' );
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"res"} ne "ok" 4 6 10
538  
539 # Add to existing sessions
540 6 my $whatToTrace = Lemonldap::NG::Handler::PSGI::Main->tsv->{whatToTrace};
541 6 $res =
542 $self->_add2FToSessions( $uid, $device, $self->_getSSOMod, 'SSO',
543 $whatToTrace );
544  
545 6 return { res => "ok" };
546 }
547  
548 11 ▶sub _check_duplicates {
Subroutine _check_duplicates: called CC=5 SCAR=16.4
549 11 my ( $self, $uid, $device ) = @_;
550 11 my $type = $device->{type};
551 11 my $new_id = genId2F($device);
552  
553 11 my $res = $self->_get2F( $uid, uc $type );
554 11 ▶ if ( $res->{res} eq 'ok' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{'res'} eq 'ok') { } 11 0 11
555 11 ▶ my @devices = @{ $res->{secondFactors} // [] };
Condition50%1/2
$res->{"secondFactors"} // []
l!l
110
MC/DC0%0/1
$res->{"secondFactors"} // []
$res->{"secondFactors"}
Truth table50%1/2
A
A= $res->{"secondFactors"}
Aresult
11
00
556 11 ▶ if ( grep { $_->{id} and $_->{id} eq $new_id } @devices ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_->{'id'} and $_->{'id'} eq $new_id;} @devices) { } 1 10 11
Condition66%2/3
$_->{'id'} && $_->{'id'} eq $new_id
!ll&&!rl&&r
011
MC/DC50%1/2
$_->{'id'} && $_->{'id'} eq $new_id
$_->{'id'} $_->{'id'} eq $new_id
Truth table66%2/3
A && B
A= $_->{'id'}B= $_->{'id'} eq $new_id
ABresult
0X0
100
111
557 return {
558 1 res => 'ko',
559 code => 409,
560 msg => "ID $new_id is already used by an existing device"
561 };
562 }
563 else {
564 10 return { res => 'ok' };
565 }
566 }
567 else {
568 0 return { res => 'ok' };
569 }
570 }
571  
572 16 ▶sub _add2FToSessions {
Subroutine _add2FToSessions: called CC=6 SCAR=17.9
573 16 my ( $self, $uid, $add, $mod, $kind, $key, $allow_create ) = @_;
574  
575 16 my ( $sessions, $session, $devices, @keep, $removed,
576 $total, $module, $localStorage );
577 16 $sessions = $self->_getSessions2F( $mod, $kind, $key, $uid );
578 16 my $found = keys %$sessions;
579  
580 16 ▶ if ( !$found and $allow_create ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $found and $allow_create) 2 14 16
Condition100%3/3
not $found and $allow_create
!ll&&!rl&&r
862
MC/DC100%2/2
not $found and $allow_create
not $found $allow_create
Truth table100%3/3
not(A) and B
A= not $foundB= $allow_create
ABresult
0X0
100
111
581 2 my $ps = $self->getPersistentSession( $mod, $uid );
582 2 $sessions = { $ps->id => { _2fDevices => undef } };
583 2 $found = 1;
584 }
585  
586 16 foreach ( keys %$sessions ) {
587 10 ▶ $session = $self->_getSession2F( $_, $mod )
Branch: 1 branch
BranchTrueFalseTotal
$session = $self->_getSession2F($_, $mod) or return {"res", "ko", "code", 500, "msg", $@} 10 0 10
588 or return { res => 'ko', code => 500, msg => $@ };
589  
590 10 $devices = $self->_getDevicesFromSessionData( $session->data );
591 10 push @$devices, $add;
592  
593 # Update session
594 10 $self->logger->debug(
595 "Adding " . display2F($add) . " to sessionId $_" );
596 10 $session->data->{_2fDevices} = to_json($devices);
597 10 $session->update( $session->data );
598 }
599 return {
600 16 ▶ res => "notfound",
Branch: 1 branch
BranchTrueFalseTotal
unless $found 6 10 16
601 message => "User $uid was not found",
602 code => 404
603 }
604 if !$found;
605  
606 10 return { res => 'ok' };
607 }
608  
609 28 1;
« blib/lib/Lemonldap/NG/Manager/Viewer.pm ↑ Summary blib/lib/Lemonldap/NG/Manager/Conf/Zero.pm »