blib/lib/Lemonldap/NG/Portal/Lib/2fDevices.pm

filter: Statementstmt 37.9% 33 / 87 Branchbran 21.4% 6 / 28 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 54.5% 6 / 11 Podpod 100% 7 / 7 totaltotal 33.9% 52 / 153 CC 21 SCAR 37.5
CC
21
Cov
31%
del2fDevices
110.0
update2fDevice
30.0
find2fDevicesByKey
12.0
CRAP 42.6
« blib/lib/Lemonldap/NG/Portal/Plugins/ContextSwitching.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Auth/_WebForm.pm »
1 package Lemonldap::NG::Portal::Lib::2fDevices;
2  
3 =pod
4  
5 =head1 NAME
6  
7 Lemonldap::NG::Portal::Lib::2fDevices - Role for registrable second factors
8  
9 =head1 DESCRIPTION
10  
11 This role provides LemonLDAP::NG modules with a high-level interface to storing
12 information on registrable second factors into the persistent session.
13  
14 It is recommended that _2fDevices is never accessed directly from code outside
15 of this module
16  
17 =head1 METHODS
18  
19 =over
20  
21 =cut
22  
23 342 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 342 ▶use Mouse::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 342 ▶use Lemonldap::NG::Common::Util qw/display2F/;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 342 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 342 requires qw(p conf logger);
29  
30 342 our $VERSION = '2.23.0';
31  
32 =item update2fDevice
33  
34 Updates one field of a registered device
35  
36 $self->update2fDevice($req, $info, $type, $key, $value, $update_key, $update_value);
37  
38 =over 4
39  
40 =item req: Current LemonLDAP::NG request
41  
42 =item info: hashref of current session information
43  
44 =item type: 'type' field of the device to update
45  
46 =item key, value: update the device whose 'key' field equals value
47  
48 =item update_key, update_value: set the matched devices' 'update_key' field to update_value
49  
50 =back
51  
52 Returns true if the update was sucessful
53  
54 =cut
55  
56 0 ▶sub update2fDevice {
Subroutine update2fDevice: not called CC=5 SCAR=34.0
Pod documented
57 0 my ( $self, $req, $info, $type, $key, $value, $update_key, $update_value )
58 = @_;
59  
60 0 my $user = $info->{ $self->conf->{whatToTrace} };
61  
62 0 my $_2fDevices = $self->get2fDevices( $req, $info );
63 0 ▶ return 0 unless $_2fDevices;
Branch: 1 branch
BranchTrueFalseTotal
unless $_2fDevices 0 0 0
64  
65 my @found =
66 0 ▶ grep { $_->{type} eq $type and $_->{$key} eq $value } @{$_2fDevices};
Condition0%0/3
$_->{'type'} eq $type && $_->{$key} eq $value
!ll&&!rl&&r
000
MC/DC0%0/2
$_->{'type'} eq $type && $_->{$key} eq $value
$_->{'type'} eq $type $_->{$key} eq $value
Truth table0%0/3
A && B
A= $_->{'type'} eq $typeB= $_->{$key} eq $value
ABresult
0X0
100
111
67  
68 0 for my $device (@found) {
69 0 $device->{$update_key} = $update_value;
70 }
71  
72 0 ▶ if (@found) {
Branch: 1 branch
BranchTrueFalseTotal
if (@found) 0 0 0
73 0 $self->p->updatePersistentSession( $req,
74 { _2fDevices => to_json($_2fDevices) }, $user );
75 0 return 1;
76 }
77 0 return 0;
78 }
79  
80 =item add2fDevice
81  
82 Store a new device
83  
84 $self->add2fDevice($req, $info, $device);
85  
86 =over 4
87  
88 =item req: Current LemonLDAP::NG request
89  
90 =item info: hashref of current session information
91  
92 =item device: hashref of device details. It must contain at least a 'type',
93 'name' and 'epoch' key
94  
95 =back
96  
97 Returns true if the update was sucessful
98  
99 =cut
100  
101 0 ▶sub add2fDevice {
Subroutine add2fDevice: not called CC=1 SCAR=6.9
Pod documented
102 0 my ( $self, $req, $info, $device ) = @_;
103  
104 0 my $_2fDevices = $self->get2fDevices( $req, $info );
105  
106 0 push @{$_2fDevices}, $device;
107  
108 0 my $uid = $info->{ $self->conf->{whatToTrace} };
109 0 $self->auditLog(
110 $req,
111 message => (
112 "User " . $uid . " registered 2F device: " . display2F($device)
113 ),
114 code => "2FA_DEVICE_REGISTERED",
115 user => $uid,
116 device => display2F($device),
117 );
118 0 $self->p->updatePersistentSession( $req,
119 { _2fDevices => to_json($_2fDevices) } );
120 0 return 1;
121 }
122  
123 =item del2fDevices
124  
125 Delete the devices specified in the @$devices array
126  
127 $self->del2fDevices($req, $info, $devices);
128  
129 =over 4
130  
131 =item req: Current LemonLDAP::NG request
132  
133 =item info: hashref of current session information
134  
135 =item device: arrayref of type+epoch hashrefs
136  
137 [ { type => xxx, epoch => xxx }, { type => xxx, epoch => xxx } ]
138  
139 =back
140  
141 Returns true if the update was sucessful
142  
143 =cut
144  
145 0 ▶sub del2fDevices {
Subroutine del2fDevices: not called CC=10 SCAR=47.0
Pod documented
146 0 my ( $self, $req, $info, $devices ) = @_;
147  
148 0 ▶ return 0 unless ( ref($devices) eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $devices eq "ARRAY" 0 0 0
149  
150 0 my $_2fDevices = $self->get2fDevices( $req, $info );
151 0 ▶ return 0 unless $_2fDevices;
Branch: 1 branch
BranchTrueFalseTotal
unless $_2fDevices 0 0 0
152  
153 0 my @updated_2fDevices = @{$_2fDevices};
154 0 my $need_update = 0;
155  
156 0 for my $device_spec (@$devices) {
157 0 ▶ next unless ( ref($device_spec) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $device_spec eq "HASH" 0 0 0
158 0 my $type = $device_spec->{type};
159 0 my $epoch = $device_spec->{epoch};
160 0 ▶ next unless ( $type and $epoch );
Branch: 1 branch
BranchTrueFalseTotal
unless $type and $epoch 0 0 0
Condition0%0/3
$type and $epoch
!ll&&!rl&&r
000
MC/DC0%0/2
$type and $epoch
$type $epoch
Truth table0%0/3
A and B
A= $typeB= $epoch
ABresult
0X0
100
111
161  
162 0 my $size_before = @updated_2fDevices;
163 @updated_2fDevices =
164 0 ▶ grep { not( $_->{type} eq $type and $_->{epoch} eq $epoch ) }
Condition0%0/3
$_->{'type'} eq $type && $_->{'epoch'} eq $epoch
!ll&&!rl&&r
000
MC/DC0%0/2
$_->{'type'} eq $type && $_->{'epoch'} eq $epoch
$_->{'type'} eq $type $_->{'epoch'} eq $epoch
Truth table0%0/3
A && B
A= $_->{'type'} eq $typeB= $_->{'epoch'} eq $epoch
ABresult
0X0
100
111
165 @updated_2fDevices;
166 0 ▶ if ( @updated_2fDevices < $size_before ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@updated_2fDevices < $size_before) 0 0 0
167 0 $need_update = 1;
168 0 my $uid = $info->{ $self->conf->{whatToTrace} };
169 0 $self->auditLog(
170 $req,
171 message => (
172 "User $uid deleted 2F device: " . display2F($device_spec)
173 ),
174 code => "2FA_DEVICE_DELETED",
175 user => $uid,
176 device => display2F($device_spec),
177 );
178 }
179 }
180  
181 0 ▶ $self->p->updatePersistentSession( $req,
Branch: 1 branch
BranchTrueFalseTotal
if $need_update 0 0 0
182 { _2fDevices => to_json( [@updated_2fDevices] ) } )
183 if $need_update;
184  
185 0 return 1;
186 }
187  
188 =item del2fDevice
189  
190 Delete a single device
191  
192 $self->del2fDevice($req, $info, $type, $epoch);
193  
194 =over 4
195  
196 =item req: Current LemonLDAP::NG request
197  
198 =item info: hashref of current session information
199  
200 =item type: type of the device to remove
201  
202 =item epoch: timestamp of the device to remove
203  
204 =back
205  
206 Returns true if the update was sucessful
207  
208 =cut
209  
210 0 ▶sub del2fDevice {
Subroutine del2fDevice: not called CC=1 SCAR=6.9
Pod documented
211 0 my ( $self, $req, $info, $type, $epoch ) = @_;
212  
213 0 return $self->del2fDevices( $req, $info,
214 [ { type => $type, epoch => $epoch } ] );
215 }
216  
217 =item find2fDevicesByKey
218  
219 Find devices from one of its attributes
220  
221 $self->find2fDevicesByKey($req, $info, $type, $key, $value);
222  
223 =over 4
224  
225 =item req: Current LemonLDAP::NG request
226  
227 =item info: hashref of current session information
228  
229 =item type: device type
230  
231 =item key, value: attribute to search in the device hash and the value to filter on
232  
233 =back
234  
235 Returns an array of devices for which type, key and value match the supplied ones
236  
237 =cut
238  
239 0 ▶sub find2fDevicesByKey {
Subroutine find2fDevicesByKey: not called CC=3 SCAR=24.8
Pod documented
240 0 my ( $self, $req, $info, $type, $key, $value ) = @_;
241  
242 0 my $_2fDevices = $self->get2fDevices( $req, $info );
243 0 ▶ return unless $_2fDevices;
Branch: 1 branch
BranchTrueFalseTotal
unless $_2fDevices 0 0 0
244  
245 my @found =
246 0 ▶ grep { $_->{type} eq $type and $_->{$key} eq $value } @{$_2fDevices};
Condition0%0/3
$_->{'type'} eq $type && $_->{$key} eq $value
!ll&&!rl&&r
000
MC/DC0%0/2
$_->{'type'} eq $type && $_->{$key} eq $value
$_->{'type'} eq $type $_->{$key} eq $value
Truth table0%0/3
A && B
A= $_->{'type'} eq $typeB= $_->{$key} eq $value
ABresult
0X0
100
111
247 0 return @found;
248 }
249  
250 =item get2fDevices
251  
252 Return all registrable devices.
253  
254 $self->get2fDevices($req, $info);
255  
256 =over 4
257  
258 =item req: Current LemonLDAP::NG request
259  
260 =item info: hashref of current session information
261  
262 =back
263  
264 Returns an arrayref of all registrable devices, or undef if an error occured
265  
266 =cut
267  
268 3 ▶sub get2fDevices {
Subroutine get2fDevices: called CC=4 SCAR=14.4
Pod documented
269 3 my ( $self, $req, $info ) = @_;
270 3 my $_2fDevices;
271  
272 3 $self->logger->debug("Looking for 2F devices...");
273 3 ▶ if ( $info->{_2fDevices} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{'_2fDevices'}) { } 2 1 3
274 $_2fDevices =
275 2 eval { from_json( $info->{_2fDevices}, { allow_nonref => 1 } ); };
276 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
277 0 $self->logger->error("Corrupted session (_2fDevices): $@");
278 0 return;
279 }
280 }
281 else {
282 # Return new ArrayRef
283 1 return [];
284 }
285 2 ▶ return ref($_2fDevices) eq 'ARRAY' ? $_2fDevices : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_2fDevices eq 'ARRAY' ? : 2 0 2
286 }
287  
288 =item find2fDevicesByType
289  
290 Return all registrable devices of a certain type. If type is not given, return
291 all registrable devices
292  
293 $self->find2fDevicesByType($req, $info, $type);
294  
295 =over 4
296  
297 =item req: Current LemonLDAP::NG request
298  
299 =item info: hashref of current session information
300  
301 =item type: type of registrable device to return
302  
303 =back
304  
305 Returns an array of all matching devices
306  
307 =cut
308  
309 1 ▶sub find2fDevicesByType {
Subroutine find2fDevicesByType: called CC=3 SCAR=11.3
Pod documented
310 1 my ( $self, $req, $info, $type ) = @_;
311 1 my $_2fDevices = $self->get2fDevices( $req, $info );
312 1 ▶ return unless $_2fDevices;
Branch: 1 branch
BranchTrueFalseTotal
unless $_2fDevices 0 1 1
313 0 ▶ return @{$_2fDevices} unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 1 1
314  
315 1 my @found = grep { $_->{type} eq $type } @{$_2fDevices};
316 1 $self->logger->debug("Return $type");
317 1 return @found;
318 }
319  
320 342 1;
321  
322 =back
323  
« blib/lib/Lemonldap/NG/Portal/Plugins/ContextSwitching.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Auth/_WebForm.pm »