blib/lib/Lemonldap/NG/Portal/2F/Engines/Default.pm

filter: Statementstmt 27.4% 144 / 525 Branchbran 19.1% 44 / 230 Conditioncond 3.9% 4 / 101 MC/DCmcdc 1.7% 1 / 58 Subroutinesub 55.0% 22 / 40 Podpod 6.2% 1 / 16 totaltotal 22.2% 216 / 970 CC 167 SCAR 58.3
CC
167
Cov
22%
run
1260.0
_displayRegister
600.0
canUpdateSfa
342.0
CRAP 340.4
« blib/lib/Lemonldap/NG/Portal/Password/Base.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/CertificateResetByMail.pm »
1 # Default 2FA engine
2 #
3 # 2FA engine provides 3 functions and 1 interface:
4 # - init()
5 # - run($req): called during auth process after session populating
6 # - display2fRegisters($req, $session): indicates if a 2F registration is
7 # available for this user
8 # - /2fregisters: the URL path that displays 2F registration menu
9  
10 package Lemonldap::NG::Portal::2F::Engines::Default;
11  
12 321 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 321 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 321 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 321 ▶use JSON qw(from_json to_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 321 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 321 use Lemonldap::NG::Portal::Main::Constants qw(
18 PE_OK
19 PE_ERROR
20 PE_NOTOKEN
21 PE_SENDRESPONSE
22 PE_BADCREDENTIALS
23 PE_TOKENEXPIRED
24 PE_NO_SECOND_FACTORS
25 PE_RETRY_2FA
26 portalConsts
27 321 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 321 ▶use Lemonldap::NG::Common::Util qw/display2F filterKey2F/;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 321 our $VERSION = '2.22.0';
32  
33 321 extends 'Lemonldap::NG::Portal::Main::Plugin';
34 321 with qw(
35 Lemonldap::NG::Portal::Lib::OverConf
36 Lemonldap::NG::Portal::Lib::2fDevices
37 );
38  
39 # INITIALIZATION
40  
41 # Arrayref of objects
42 # p => module prefix
43 # m => module instance
44 # r => compiled rule
45 # t => type of record in _2fDevices
46 490 ▶has sfModules => ( is => 'rw', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
47 490 ▶has sfRModules => ( is => 'rw', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
48 321 has sfReq => ( is => 'rw' );
49 321 has sfMsgRule => ( is => 'rw' );
50  
51 has ott => (
52 is => 'rw',
53 lazy => 1,
54 default => sub {
55 my $ott =
56 5 ▶ $_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken');
Subroutine __ANON__: called CC=1 SCAR=0.0
57 5 $ott->timeout( $_[0]->{conf}->{formTimeout} );
58 5 return $ott;
59 }
60 321 );
61  
62 has regOtt => (
63 is => 'rw',
64 lazy => 1,
65 default => sub {
66 my $ott =
67 0 ▶ $_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken');
Subroutine __ANON__: not called CC=2 SCAR=17.9
68 my $timeout = $_[0]->{conf}->{sfRegisterTimeout}
69 0 ▶ // $_[0]->{conf}->{formTimeout};
Condition0%0/3
$_[0]{"conf"}{"sfRegisterTimeout"} // $_[0]{"conf"}{"formTimeout"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{"conf"}{"sfRegisterTimeout"} // $_[0]{"conf"}{"formTimeout"}
$_[0]{"conf"}{"sfRegisterTimeout"} $_[0]{"conf"}{"formTimeout"}
Truth table0%0/3
A // B
A= $_[0]{"conf"}{"sfRegisterTimeout"}B= $_[0]{"conf"}{"formTimeout"}
ABresult
1X1
011
000
70 0 $ott->timeout($timeout);
71 0 return $ott;
72 }
73 321 );
74  
75 sub init {
76 490 ▶ my ($self) = @_;
Subroutine init: called CC=23 SCAR=32.2
Pod undocumented
77  
78 # Load 2F modules
79 490 for my $i ( 0 .. 1 ) {
80 980 ▶ foreach (
Branch: 1 branch
BranchTrueFalseTotal
$i ? : 490 490 980
81 split /,\s*/,
82 $self->conf->{
83 $i
84 ? 'available2FSelfRegistration'
85 : 'available2F'
86 }
87 )
88 {
89 6370 ▶ return 0 unless $self->_load_2f_module_by_prefix( $_, $i );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_load_2f_module_by_prefix($_, $i) 0 6370 6370
90 }
91 }
92  
93 # Extra 2F modules
94 490 $self->logger->debug('Processing Extra 2F modules');
95 490 foreach my $extraKey ( sort keys %{ $self->conf->{sfExtra} } ) {
96  
97 6 my $moduleType = $self->conf->{sfExtra}->{$extraKey}->{type};
98 6 ▶ next unless ($moduleType);
Branch: 1 branch
BranchTrueFalseTotal
unless $moduleType 0 6 6
99 6 my $over = $self->conf->{sfExtra}->{$extraKey}->{over};
100  
101 6 $self->logger->debug(
102 "Loading extra 2F module $extraKey of type $moduleType");
103 my $m = $self->loadModule(
104 "::2F::$moduleType",
105 $over,
106 prefix => $extraKey,
107 (
108 $self->conf->{sfExtra}->{$extraKey}->{register}
109 6 ▶ ? ( is_registrable => 1 )
Branch: 2 branches
BranchTrueFalseTotal
my $m = $self->loadModule("::2F::$moduleType", $over, "prefix", $extraKey, $self->conf->{"sfExtra"}{$extraKey}{"register"} ? ("is_registrable", 1) : ()) or return 0 0 6 6
$self->conf->{'sfExtra'}{$extraKey}{'register'} ? : 4 2 6
110 : ()
111 ),
112 ) or return 0;
113  
114 6 $self->_registerRoutes($m);
115  
116 # Rule and prefix may be modified by 2F module, reread them
117 6 ▶ my $rule = $self->conf->{sfExtra}->{$extraKey}->{rule} || 1;
Condition100%2/2
$self->conf->{'sfExtra'}{$extraKey}{'rule'} || 1
l!l
42
MC/DC100%1/1
$self->conf->{'sfExtra'}{$extraKey}{'rule'} || 1
$self->conf->{'sfExtra'}{$extraKey}{'rule'}
Truth table100%2/2
A
A= $self->conf->{'sfExtra'}{$extraKey}{'rule'}
Aresult
11
00
118 6 my $reg_rule;
119 6 my $prefix = $m->prefix;
120 6 ▶ if ( $self->conf->{sfExtra}->{$extraKey}->{register} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"sfExtra"}{$extraKey}{"register"}) 4 2 6
121 4 ▶ $reg_rule = $self->conf->{sfExtra}->{$extraKey}->{regrule} || $rule;
Condition66%2/3
$self->conf->{'sfExtra'}{$extraKey}{'regrule'} || $rule
l!l&&r!l&&!r
130
MC/DC0%0/2
$self->conf->{'sfExtra'}{$extraKey}{'regrule'} || $rule
$self->conf->{'sfExtra'}{$extraKey}{'regrule'} $rule
Truth table66%2/3
A || B
A= $self->conf->{'sfExtra'}{$extraKey}{'regrule'}B= $rule
ABresult
1X1
011
000
122 4 $rule = "( $rule ) and has2f('$prefix')";
123 }
124  
125 # Overwrite logo, label, level from user configuration
126 $m->logo( $self->conf->{sfExtra}->{$extraKey}->{logo} )
127 6 ▶ if $self->conf->{sfExtra}->{$extraKey}->{logo};
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{"sfExtra"}{$extraKey}{"logo"} 6 0 6
128 $m->label( $self->conf->{sfExtra}->{$extraKey}->{label} )
129 6 ▶ if $self->conf->{sfExtra}->{$extraKey}->{label};
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{"sfExtra"}{$extraKey}{"label"} 5 1 6
130 $m->authnLevel( $self->conf->{sfExtra}->{$extraKey}->{level} )
131 6 ▶ if $self->conf->{sfExtra}->{$extraKey}->{level};
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{"sfExtra"}{$extraKey}{"level"} 5 1 6
132  
133 # Compile rule
134 6 $rule = $self->p->buildRule( $rule, "extra 2F activation for $prefix" );
135 6 ▶ return 0 unless $rule;
Branch: 1 branch
BranchTrueFalseTotal
unless $rule 0 6 6
136  
137 # Store module
138 6 push @{ $self->{'sfModules'} },
139 {
140 p => $prefix,
141 m => $m,
142 r => $rule,
143 t => $prefix
144 };
145  
146 # Push register module
147 6 ▶ if ( $self->conf->{sfExtra}->{$extraKey}->{register} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"sfExtra"}{$extraKey}{"register"}) 4 2 6
148 4 $self->logger->debug(
149 "Loading register module for $extraKey of type $moduleType");
150 4 my $register_module_name = $self->findRegisterModuleFor($m);
151 4 ▶ if ($register_module_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($register_module_name) { } 4 0 4
152 my $rm = $self->loadModule(
153 "::2F::Register::Generic",
154 $over,
155 prefix => $extraKey,
156 type => $extraKey,
157 logo => $m->logo,
158 label => $m->label,
159 userCanRemove => 1,
160 verificationModule => $m,
161 authnLevel => $self->conf->{sfExtra}->{$extraKey}->{level},
162 4 ▶ ) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $rm = $self->loadModule("::2F::Register::Generic", $over, "prefix", $extraKey, "type", $extraKey, "logo", $m->logo, "label", $m->label, "userCanRemove", 1, "verificationModule", $m, "authnLevel", $self->conf->{"sfExtra"}{$extraKey}{"level"}) or return 0 0 4 4
163  
164 4 my $reg_rule = $self->p->buildRule( $reg_rule,
165 "extra 2F registration for $prefix" );
166 4 ▶ return 0 unless $reg_rule;
Branch: 1 branch
BranchTrueFalseTotal
unless $reg_rule 0 4 4
167 4 push @{ $self->{'sfRModules'} },
168 {
169 p => $prefix,
170 m => $rm,
171 r => $reg_rule,
172 t => $prefix,
173 };
174 }
175 else {
176 0 $self->logger->warn(
177 "Could not find a proper register module for $moduleType");
178 }
179 }
180 }
181  
182 490 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->sfReq($self->p->buildRule($self->conf->{"sfRequired"}, "sfRequired"))) 0 490 490
183 $self->sfReq(
184 $self->p->buildRule( $self->conf->{sfRequired}, 'sfRequired' )
185 )
186 )
187 {
188 0 return 0;
189 }
190  
191 490 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->sfMsgRule($self->p->buildRule($self->conf->{"sfRemovedMsgRule"}, "sfRemovedMsg"))) 0 490 490
192 $self->sfMsgRule(
193 $self->p->buildRule(
194 $self->conf->{sfRemovedMsgRule},
195 'sfRemovedMsg'
196 )
197 )
198 )
199 {
200 0 return 0;
201 }
202  
203 490 $self->addAuthRoute( '2fchoice' => '_choice', ['POST'] );
204 490 $self->addAuthRoute( '2fchoice' => '_redirect', ['GET'] );
205 490 $self->addUnauthRoute( '2fchoice' => '_choice', ['POST'] );
206 490 $self->addUnauthRoute( '2fchoice' => '_redirect', ['GET'] );
207  
208 # Registration base
209 490 $self->addAuthRoute( '2fregisters' => '_displayRegister', ['GET'] );
210 490 $self->addAuthRoute( '2fregisters' => 'register', ['POST'] );
211 $self->addUnauthRoute(
212 '2fregisters' => 'restoreSession',
213 [ 'GET', 'POST' ]
214 490 ▶ ) if ( $self->conf->{sfRequired} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{"sfRequired"} 3 487 490
215  
216 490 $self->addEntryPoint(
217 isa => 'Lemonldap::NG::Portal::Main::SecondFactor',
218 service => "secondFactor",
219 method => "_enable_2f_module",
220 );
221  
222 490 $self->addEntryPoint(
223 isa => 'Lemonldap::NG::Portal::2F::Register::Base',
224 service => "secondFactor",
225 method => "_enable_2f_register_module",
226 );
227  
228 490 return 1;
229 }
230  
231 sub _load_2f_module_by_prefix {
232 6370 ▶ my ( $self, $name, $isRegister ) = @_;
Subroutine _load_2f_module_by_prefix: called CC=8 SCAR=20.9
233 {
234 6370 my $prefix = lc($name);
235 6370 $prefix =~ s/2f$//i;
236  
237 # Activation parameter
238 6370 ▶ my $ap =
Branch: 1 branch
BranchTrueFalseTotal
$isRegister ? : 1960 4410 6370
239 $prefix . ( $isRegister ? '2fSelfRegistration' : '2fActivation' );
240 6370 $self->logger->debug("Checking $ap");
241  
242 # Unless $rule, skip loading
243 6370 ▶ if ( $self->conf->{$ap} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{$ap}) { } 86 6284 6370
244 86 $self->logger->debug("Trying to load $name 2F");
245 86 ▶ my $m =
Branch: 2 branches
BranchTrueFalseTotal
my $m = $self->p->loadModule($isRegister ? "::2F::Register::$name" : "::2F::$name") or return 0 0 86 86
$isRegister ? : 25 61 86
246 $self->p->loadModule(
247 $isRegister ? "::2F::Register::$name" : "::2F::$name" )
248 or return 0;
249  
250 86 ▶ if ($isRegister) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isRegister) { } 25 61 86
251 25 ▶ $self->_enable_2f_register_module($m) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_enable_2f_register_module($m) or return 0 0 25 25
252 }
253 else {
254 61 ▶ $self->_enable_2f_module($m) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_enable_2f_module($m) or return 0 0 61 61
255 }
256 }
257 else {
258 6284 $self->logger->debug(' -> not enabled');
259 }
260 }
261 6370 return 1;
262 }
263  
264 sub _enable_2f_register_module {
265 25 ▶ my ( $self, $m ) = @_;
Subroutine _enable_2f_register_module: called CC=1 SCAR=0.0
266 25 return $self->_enable_module( $m, $self->sfRModules );
267 }
268  
269 sub _enable_2f_module {
270 61 ▶ my ( $self, $m ) = @_;
Subroutine _enable_2f_module: called CC=1 SCAR=0.0
271  
272 61 $self->_registerRoutes($m);
273 61 return $self->_enable_module( $m, $self->sfModules );
274 }
275  
276 sub _enable_module {
277 86 ▶ my ( $self, $m, $array ) = @_;
Subroutine _enable_module: called CC=3 SCAR=11.3
278  
279 86 my $rule = $self->p->buildRule( $m->rule );
280 86 ▶ return 0 unless $rule;
Branch: 1 branch
BranchTrueFalseTotal
unless $rule 0 86 86
281  
282 86 ▶ push @$array,
Branch: 1 branch
BranchTrueFalseTotal
$m->can('type') ? : 86 0 86
283 {
284 p => $m->prefix,
285 m => $m,
286 r => $rule,
287 t => ( $m->can('type') ? $m->type : $m->prefix ),
288 };
289 86 return 1;
290 }
291  
292 # RUNNING METHODS
293  
294 # public PE_CODE run($req)
295 #
296 # run() is called at each authentication, just after sessionInfo populated
297 sub run {
298 0 ▶ my ( $self, $req ) = @_;
Subroutine run: not called CC=35 SCAR=71.4
Pod documented
299 0 my $checkLogins = $req->param('checkLogins');
300 0 my $forceUpgrade = $req->param('forceUpgrade');
301 0 my $stayconnected = $req->param('stayconnected');
302 0 ▶ my $spoofId = $req->param('spoofId') || '';
Condition0%0/2
$req->param('spoofId') || ''
l!l
00
MC/DC0%0/1
$req->param('spoofId') || ''
$req->param('spoofId')
Truth table0%0/2
A
A= $req->param('spoofId')
Aresult
11
00
303 0 ▶ $self->logger->debug("2F checkLogins is set") if $checkLogins;
Branch: 1 branch
BranchTrueFalseTotal
if $checkLogins 0 0 0
304 0 ▶ $self->logger->debug("2F forceUpgrade is set") if $forceUpgrade;
Branch: 1 branch
BranchTrueFalseTotal
if $forceUpgrade 0 0 0
305  
306 # Skip 2F unless a module has been registered
307 0 ▶ unless ( @{ $self->sfModules } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->sfModules;}) 0 0 0
308 0 ▶ if ( $self->conf->{sfOnlyUpgrade} and $req->data->{doingSfUpgrade} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{'sfOnlyUpgrade'} and $req->data->{'doingSfUpgrade'}) { } 0 0 0
Condition0%0/3
$self->conf->{"sfOnlyUpgrade"} and $req->data->{"doingSfUpgrade"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->conf->{"sfOnlyUpgrade"} and $req->data->{"doingSfUpgrade"}
$self->conf->{"sfOnlyUpgrade"} $req->data->{"doingSfUpgrade"}
Truth table0%0/3
A and B
A= $self->conf->{"sfOnlyUpgrade"}B= $req->data->{"doingSfUpgrade"}
ABresult
0X0
100
111
309 0 $self->logger->error(
310 "Trying to perform 2FA session upgrade but no "
311 . "second factor modules are configured" );
312 0 return PE_ERROR;
313 }
314 else {
315 0 return PE_OK;
316 }
317 }
318  
319 # Skip 2F if authnLevel is already high enough
320 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"sfOnlyUpgrade"} and not $forceUpgrade and ($req->pdata->{"targetAuthnLevel"} || 0) <= ($req->sessionInfo->{"authenticationLevel"} || 0)) 0 0 0
Condition0%0/10
$self->conf->{"sfOnlyUpgrade"} and not $forceUpgrade and ($req->pdata->{"targetAuthnLevel"} || 0) <= ($req->sessionInfo->{"authenticationLevel"} || 0)
!ll&&!rl&&r
000
$self->conf->{"sfOnlyUpgrade"} and not $forceUpgrade
!ll&&!rl&&r
000
$req->pdata->{'targetAuthnLevel'} || 0
l!l
00
$req->sessionInfo->{'authenticationLevel'} || 0
l!l
00
MC/DC0%0/3
$self->conf->{"sfOnlyUpgrade"} and not $forceUpgrade and ($req->pdata->{"targetAuthnLevel"} || 0) <= ($req->sessionInfo->{"authenticationLevel"} || 0)
$self->conf->{"sfOnlyUpgrade"} not $forceUpgrade $req->pdata->{'targetAuthnLevel'}
MC/DC0%0/1
$req->sessionInfo->{'authenticationLevel'} || 0
$req->sessionInfo->{'authenticationLevel'}
Truth table0%0/4
A and not(B) and C
A= $self->conf->{"sfOnlyUpgrade"}B= not $forceUpgradeC= $req->pdata->{'targetAuthnLevel'}
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/2
A
A= $req->sessionInfo->{'authenticationLevel'}
Aresult
11
00
321 $self->conf->{sfOnlyUpgrade}
322 and !$forceUpgrade
323 and ( ( $req->pdata->{targetAuthnLevel} || 0 ) <=
324 ( $req->sessionInfo->{authenticationLevel} || 0 ) )
325 )
326 {
327 0 $self->logger->debug(
328 "Current authentication level satisfied target service,"
329 . " skipping 2FA" );
330 0 return PE_OK;
331 }
332  
333 # Remove expired 2F devices
334 0 my $session = $req->sessionInfo;
335 0 my $_2fDevices = $self->get2fDevices( $req, $session );
336 0 ▶ if ( scalar @$_2fDevices ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$_2fDevices) 0 0 0
337 0 my ( $removed, $name, $now, @expired2fDevices );
338  
339 0 $self->logger->debug("Looking for expired 2F device(s)...");
340 0 $now = time();
341 0 foreach my $device (@$_2fDevices) {
342 0 my $type = lc( $device->{type} );
343 0 $type =~ s/2f$//i;
344 0 ▶ $type = 'yubikey' if $type eq 'ubk';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "ubk" 0 0 0
345 0 my $ttl = $self->conf->{ $type . '2fTTL' };
346 0 ▶ if ( $ttl and $ttl > 0 and $now - $device->{epoch} > $ttl ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ttl and $ttl > 0 and $now - $device->{"epoch"} > $ttl) 0 0 0
Condition0%0/6
$ttl and $ttl > 0 and $now - $device->{"epoch"} > $ttl
!ll&&!rl&&r
000
$ttl and $ttl > 0
!ll&&!rl&&r
000
MC/DC0%0/3
$ttl and $ttl > 0 and $now - $device->{"epoch"} > $ttl
$ttl $ttl > 0 $now - $device->{"epoch"} > $ttl
Truth table0%0/4
A and B and C
A= $ttlB= $ttl > 0C= $now - $device->{"epoch"} > $ttl
ABCresult
0XX0
10X0
1100
1111
347 0 $self->logger->debug(
348 "Remove $device->{type} -> $device->{name} / $device->{epoch}"
349 );
350 0 $self->auditLog(
351 $req,
352 message => ("Remove expired $device->{type}"),
353 code => "2FA_DEVICE_EXPIRED",
354 device => display2F($device),
355 user => $req->user,
356 );
357 0 push @expired2fDevices, $device;
358 0 $name .= "$device->{name}; ";
359 0 $removed++;
360 }
361 }
362  
363 0 ▶ if ($removed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($removed) 0 0 0
364 0 $name =~ s/;\s$//;
365 0 $self->logger->debug(
366 "Found $removed EXPIRED 2F device(s) => Update persistent session"
367 );
368 0 $self->userLogger->notice(
369 " -> $removed expired 2F device(s) will be removed ($name)");
370  
371 # Display message if required
372 0 ▶ if ( $self->del2fDevices( $req, $session, \@expired2fDevices )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->del2fDevices($req, $session, \@expired2fDevices) and $self->sfMsgRule->($req, $req->sessionInfo)) 0 0 0
Condition0%0/3
$self->del2fDevices($req, $session, \@expired2fDevices) and $self->sfMsgRule->($req, $req->sessionInfo)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->del2fDevices($req, $session, \@expired2fDevices) and $self->sfMsgRule->($req, $req->sessionInfo)
$self->del2fDevices($req, $session, \@expired2fDevices) $self->sfMsgRule->($req, $req->sessionInfo)
Truth table0%0/3
A and B
A= $self->del2fDevices($req, $session, \@expired2fDevices)B= $self->sfMsgRule->($req, $req->sessionInfo)
ABresult
0X0
100
111
373 && $self->sfMsgRule->( $req, $req->sessionInfo ) )
374 {
375 0 my $uid = $req->user;
376 0 my $date = strftime "%Y-%m-%d", localtime;
377 0 ▶ my $ref = $self->conf->{sfRemovedNotifRef} || 'RemoveSF';
Condition0%0/2
$self->conf->{'sfRemovedNotifRef'} || 'RemoveSF'
l!l
00
MC/DC0%0/1
$self->conf->{'sfRemovedNotifRef'} || 'RemoveSF'
$self->conf->{'sfRemovedNotifRef'}
Truth table0%0/2
A
A= $self->conf->{'sfRemovedNotifRef'}
Aresult
11
00
378 0 $ref .= '-' . time();
379 my $title = $self->conf->{sfRemovedNotifTitle}
380 0 ▶ || 'Second factor notification';
Condition0%0/2
$self->conf->{'sfRemovedNotifTitle'} || 'Second factor notification'
l!l
00
MC/DC0%0/1
$self->conf->{'sfRemovedNotifTitle'} || 'Second factor notification'
$self->conf->{'sfRemovedNotifTitle'}
Truth table0%0/2
A
A= $self->conf->{'sfRemovedNotifTitle'}
Aresult
11
00
381 my $msg = $self->conf->{sfRemovedNotifMsg}
382 0 ▶ || "$removed expired second factor(s) has/have been removed ($name)!";
Condition0%0/2
$self->conf->{'sfRemovedNotifMsg'} || "$removed expired second factor(s) has/have been removed ($name)!"
l!l
00
MC/DC0%0/1
$self->conf->{'sfRemovedNotifMsg'} || "$removed expired second factor(s) has/have been removed ($name)!"
$self->conf->{'sfRemovedNotifMsg'}
Truth table0%0/2
A
A= $self->conf->{'sfRemovedNotifMsg'}
Aresult
11
00
383 0 $msg =~ s/\b_removedSF_\b/$removed/;
384 0 $msg =~ s/\b_nameSF_\b/$name/;
385 0 ▶ my $params =
Branch: 1 branch
BranchTrueFalseTotal
$removed > 1 ? : 0 0 0
386 $removed > 1
387 ? { trspan => "expired2Fremoved, $removed, $name" }
388 : { trspan => "oneExpired2Fremoved, $name" };
389  
390 my $notifEngine = $self->p->loadedModules->{
391 0 'Lemonldap::NG::Portal::Plugins::Notifications'};
392  
393 my $res =
394 0 ▶ ( $self->conf->{sfRemovedUseNotif} && $notifEngine )
Branch: 1 branch
BranchTrueFalseTotal
$self->conf->{'sfRemovedUseNotif'} && $notifEngine ? : 0 0 0
Condition0%0/3
$self->conf->{"sfRemovedUseNotif"} and $notifEngine
!ll&&!rl&&r
000
MC/DC0%0/2
$self->conf->{"sfRemovedUseNotif"} and $notifEngine
$self->conf->{"sfRemovedUseNotif"} $notifEngine
Truth table0%0/3
A and B
A= $self->conf->{"sfRemovedUseNotif"}B= $notifEngine
ABresult
0X0
100
111
395 ? $self->createNotification( $req, $uid, $date, $ref, $title,
396 $msg )
397 : $self->displayTemplate( $req, 'simpleInfo', $params );
398 0 ▶ return $res if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 0 0 0
399 }
400 }
401 }
402  
403 # Search for authorized modules for this user
404 0 my @am = $self->searchForAuthorized2Fmodules($req);
405  
406 # If no 2F module is authorized, skipping 2F
407 # Note that a rule may forbid access after (GrantSession plugin)
408 0 ▶ unless (@am) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@am) 0 0 0
409  
410 # Except if 2FA is required, move to registration
411 0 ▶ if ( $self->sfReq->( $req, $req->sessionInfo ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sfReq->($req, $req->sessionInfo)) { } 0 0 0
412 0 $self->logger->debug("2F is required...");
413 0 $self->logger->debug(" -> Register 2F");
414 $req->pdata->{sfRegToken} =
415 0 $self->regOtt->createToken( $self->save2faSessionInfo($req) );
416 0 $self->logger->debug("Just one 2F is enabled");
417 0 $self->logger->debug(" -> Redirect to 2fregisters/");
418 0 $req->response( [
419 302,
420 [
421 Location =>
422 $self->p->buildUrl( $req->portal, '2fregisters' )
423 ],
424 []
425 ]
426 );
427 0 return PE_SENDRESPONSE;
428 }
429 else {
430 0 ▶ if ( $self->conf->{sfOnlyUpgrade} and $req->data->{doingSfUpgrade} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{'sfOnlyUpgrade'} and $req->data->{'doingSfUpgrade'}) { } 0 0 0
Condition0%0/3
$self->conf->{"sfOnlyUpgrade"} and $req->data->{"doingSfUpgrade"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->conf->{"sfOnlyUpgrade"} and $req->data->{"doingSfUpgrade"}
$self->conf->{"sfOnlyUpgrade"} $req->data->{"doingSfUpgrade"}
Truth table0%0/3
A and B
A= $self->conf->{"sfOnlyUpgrade"}B= $req->data->{"doingSfUpgrade"}
ABresult
0X0
100
111
431 {
432  
433 # cancel redirection to issuer/vhost
434 0 delete $req->pdata->{_url};
435 0 return PE_NO_SECOND_FACTORS;
436 }
437 else {
438 0 return PE_OK;
439 }
440 }
441 }
442  
443 $self->auditLog(
444 $req,
445 message => (
446 'Second factor required for '
447 . $req->sessionInfo->{ $self->conf->{whatToTrace} }
448 ),
449 code => "2FA_REQUIRED",
450 user => $req->sessionInfo->{ $self->conf->{whatToTrace} },
451 0 );
452  
453 0 delete $req->{authResult};
454  
455 0 $req->data->{_2fRetries} = $self->conf->{sfRetries};
456  
457 # If only one 2F is authorized, display it
458 0 ▶ unless ($#am) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($#am) 0 0 0
459 0 my $token = $am[0]->ott->createToken( $self->save2faSessionInfo($req) );
460 $self->auditLog(
461 $req,
462 message => (
463 'Second factor '
464 . $am[0]->prefix
465 . '2f selected for '
466 . $req->sessionInfo->{ $self->conf->{whatToTrace} }
467 ),
468 code => "2FA_SELECTED",
469 user => $req->sessionInfo->{ $self->conf->{whatToTrace} },
470 0 type => $am[0]->prefix,
471 );
472  
473 0 my $res = $am[0]->run( $req, $token );
474 0 $req->authResult($res);
475 0 return $res;
476 }
477  
478 # More than 1 2F has been found, display choice
479 0 my $token = $self->ott->createToken( $self->save2faSessionInfo($req) );
480 0 $self->logger->debug("Prepare 2F choice");
481 my $res = $self->p->sendHtml(
482 $req,
483 '2fchoice',
484 params => {
485 TARGET => $self->p->relativeUrl( $req, '2fchoice' ),
486 CHECKLOGINS => $checkLogins,
487 STAYCONNECTED => $stayconnected,
488 TOKEN => $token,
489 MSG => $self->canUpdateSfa($req) || 'choose2f',
490 ALERT => ( $self->canUpdateSfa($req) ? 'warning' : 'positive' ),
491 MODULES => [
492 map {
493 0 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
$self->canUpdateSfa($req) ? : 0 0 0
Condition0%0/2
$self->canUpdateSfa($req) || 'choose2f'
l!l
00
MC/DC0%0/1
$self->canUpdateSfa($req) || 'choose2f'
$self->canUpdateSfa($req)
Truth table0%0/2
A
A= $self->canUpdateSfa($req)
Aresult
11
00
494 0 CODE => $_->prefix,
495 LOGO => $_->logo,
496 LABEL => $_->label
497 }
498 } @am
499 ],
500 }
501 );
502 0 $req->response($res);
503 0 return PE_SENDRESPONSE;
504 }
505  
506 sub save2faSessionInfo {
507 0 ▶ my ( $self, $req ) = @_;
Subroutine save2faSessionInfo: not called CC=3 SCAR=24.8
Pod undocumented
508 0 ▶ my $spoofId = $req->param('spoofId') || '';
Condition0%0/2
$req->param('spoofId') || ''
l!l
00
MC/DC0%0/1
$req->param('spoofId') || ''
$req->param('spoofId')
Truth table0%0/2
A
A= $req->param('spoofId')
Aresult
11
00
509  
510 0 my $info = { %{ $req->sessionInfo } };
511  
512 # Store user data in a token
513 0 $info->{_2fRetries} = $req->data->{_2fRetries};
514 0 $info->{_2fRealSession} = $req->id;
515 0 $info->{_2fUrldc} = $req->urldc;
516 0 $info->{_2fInfo} = $req->info;
517 0 $info->{_2fUtime} = $req->sessionInfo->{_utime};
518 0 ▶ if ( $self->conf->{impersonationRule} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"impersonationRule"}) 0 0 0
519 0 $info->{_impSpoofId} = $spoofId;
520 0 $info->{_impUser} = $req->user;
521 }
522  
523 0 return $info;
524 }
525  
526 sub restore2faSessionInfo {
527 0 ▶ my ( $self, $req, $info ) = @_;
Subroutine restore2faSessionInfo: not called CC=2 SCAR=17.9
Pod undocumented
528  
529 # Update sessionInfo
530 delete $info->{$_}
531 0 foreach (qw(tokenSessionStartTimestamp tokenTimeoutTimestamp _type));
532 0 $req->data->{_2fRetries} = delete $info->{_2fRetries};
533 0 $req->sessionInfo($info);
534 0 $req->id( delete $req->sessionInfo->{_2fRealSession} );
535 0 $req->urldc( delete $req->sessionInfo->{_2fUrldc} );
536 0 $req->info( delete $req->sessionInfo->{_2fInfo} );
537 0 $req->{sessionInfo}->{_utime} = delete $req->{sessionInfo}->{_2fUtime};
538 }
539  
540 # bool public display2fRegisters($req, $session)
541 #
542 # Return true if at least 1 register module is available for this user.
543 # Used by Menu for displaying or not /2fregisters page
544 sub display2fRegisters {
545 0 ▶ my ( $self, $req, $session ) = @_;
Subroutine display2fRegisters: not called CC=3 SCAR=24.8
Pod undocumented
546 0 foreach my $m ( @{ $self->sfRModules } ) {
547 0 ▶ return 1 if $m->{r}->( $req, $session );
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"r"}->($req, $session) 0 0 0
548 }
549 0 return 0;
550 }
551  
552 sub _choice {
553 7 ▶ my ( $self, $req ) = @_;
Subroutine _choice: called CC=4 SCAR=23.2
554  
555 7 my ( $tokres, $session ) = $self->_check_token( $req, $self->ott );
556 7 ▶ if ( $tokres != PE_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokres != 0) 7 0 7
557 $self->auditLog(
558 $req,
559 message => "Token validation failed during 2FA type selection",
560 code => "2FA_SELECTION_FAILED",
561 user => $req->sessionInfo->{ $self->conf->{whatToTrace} },
562 reason => "Token validation failed",
563 7 portal_error => portalConsts->{$tokres},
564 );
565 7 return $self->p->doPE( $req, $tokres );
566 }
567  
568 0 $req->sessionInfo($session);
569  
570 0 my $ch = $req->param('sf');
571 0 foreach my $m ( @{ $self->sfModules } ) {
572 0 ▶ if ( $m->{m}->prefix eq $ch ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m->{"m"}->prefix eq $ch) 0 0 0
573 $self->auditLog(
574 $req,
575 message => (
576 'Second factor '
577 . $m->{m}->prefix
578 . '2f selected for '
579 . $req->sessionInfo->{ $self->conf->{whatToTrace} }
580 ),
581 code => "2FA_SELECTED",
582 user => $req->sessionInfo->{ $self->conf->{whatToTrace} },
583 type => $m->{m}->prefix,
584 0 );
585  
586 # New token
587 0 my $token = $m->{m}->ott->createToken( $req->sessionInfo );
588 0 my $res = $m->{m}->run( $req, $token );
589 0 $req->authResult($res);
590 return $self->p->do(
591 $req,
592 [
593 [ returnPE => $res ], 'controlUrl',
594 0 'buildCookie', @{ $self->p->endAuth },
595 ]
596 );
597 }
598 }
599 $self->auditLog(
600 $req,
601 message => ('Bad 2F choice'),
602 code => "2FA_SELECTION_FAILED",
603 reason => "Unknown type",
604 user => $req->sessionInfo->{ $self->conf->{whatToTrace} },
605 portal_error => portalConsts->{PE_ERROR},
606 0 type => $ch,
607 );
608 0 return $self->p->doPE( $req, PE_ERROR );
609 }
610  
611 sub _redirect {
612 0 ▶ my ( $self, $req ) = @_;
Subroutine _redirect: not called CC=2 SCAR=17.9
613 0 my $arg = $req->env->{QUERY_STRING};
614  
615 0 $self->logger->debug('Call sfEngine _redirect method');
616 0 ▶ return [ 302, [ Location => $req->portal . ( $arg ? "?$arg" : '' ) ], [] ];
Branch: 1 branch
BranchTrueFalseTotal
$arg ? : 0 0 0
617 }
618  
619 sub _displayRegister {
620 0 ▶ my ( $self, $req, $prefix ) = @_;
Subroutine _displayRegister: not called CC=24 SCAR=64.0
621 0 my $_2fDevices = [];
622 0 my @am;
623 my %tplParams;
624  
625 0 ▶ if ( $req->params("continue") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->params("continue")) 0 0 0
626 0 %tplParams = (
627 %tplParams,
628 MSG => 'your2faIsRegistered',
629 ALERT => "positive",
630 );
631 }
632  
633 0 $self->p->importHandlerData($req);
634  
635 # After verifying rule:
636 # - display template if $prefix
637 # - else display choice template
638 0 ▶ if ($prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix) 0 0 0
639 my ($m) =
640 0 grep { $_->{m}->prefix eq $prefix } @{ $self->sfRModules };
641 0 ▶ return $self->p->sendError( $req, 'Inexistent register module', 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless $m 0 0 0
642 unless $m;
643 return $self->p->sendError( $req, 'Registration not authorized', 403 )
644 0 ▶ unless $m->{r}->( $req, $req->userData );
Branch: 1 branch
BranchTrueFalseTotal
unless $m->{"r"}->($req, $req->userData) 0 0 0
645 0 my $mod = $m->{m};
646  
647 0 ▶ if ( $mod->can('initDisplay') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod->can("initDisplay")) 0 0 0
648 0 $mod->initDisplay($req);
649 }
650 return $self->p->sendHtml(
651 $req,
652 $mod->template,
653 params => {
654 CUSTOM_SCRIPT => $req->data->{customScript},
655 0 ▶ PREFIX => $prefix,
Branch: 1 branch
BranchTrueFalseTotal
$self->canUpdateSfa($req) ? : 0 0 0
Condition0%0/3
$self->canUpdateSfa($req) || $mod->welcome
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->canUpdateSfa($req) || $mod->welcome
$self->canUpdateSfa($req) $mod->welcome
Truth table0%0/3
A || B
A= $self->canUpdateSfa($req)B= $mod->welcome
ABresult
1X1
011
000
656 "PREFIX_$prefix" => 1,
657 MSG => $self->canUpdateSfa($req) || $mod->welcome,
658 ALERT => ( $self->canUpdateSfa($req) ? 'warning' : 'positive' ),
659 }
660 );
661 }
662  
663 0 foreach my $m ( @{ $self->sfRModules } ) {
664 $self->logger->debug(
665 0 'Looking if ' . $m->{m}->prefix . '2f register is available' );
666 0 ▶ if ( $m->{r}->( $req, $req->userData ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m->{"r"}->($req, $req->userData)) 0 0 0
667 push @am,
668 {
669 CODE => $m->{m}->prefix,
670 URL =>
671 $self->p->relativeUrl( $req, '2fregisters', $m->{m}->prefix ),
672 LOGO => $m->{m}->logo,
673 LABEL => $m->{m}->label
674 0 };
675 }
676 }
677  
678 # Retrieve user all second factors
679 0 ▶ if ( $self->canUpdateSfa($req) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->canUpdateSfa($req)) { } 0 0 0
680 0 $self->userLogger->warn("Do not display 2F devices!");
681 }
682 else {
683 0 $_2fDevices = $self->get2fDevices( $req, $req->userData );
684 }
685  
686 # If only one 2F is available, redirect to it
687 return [
688 302,
689 [
690 Location =>
691 $self->p->buildUrl( $req->portal, '2fregisters', $am[0]->{CODE} )
692 ],
693 []
694 ]
695 if (
696 @am == 1
697 and not( @$_2fDevices
698 or $req->data->{sfRegRequired} )
699 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
if @am == 1 and not @$_2fDevices || $req->data->{"sfRegRequired"} 0 0 0
Condition0%0/6
@am == 1 and not @$_2fDevices || $req->data->{"sfRegRequired"}
!ll&&!rl&&r
000
@$_2fDevices || $req->data->{'sfRegRequired'}
l!l&&r!l&&!r
000
MC/DC0%0/3
@am == 1 and not @$_2fDevices || $req->data->{"sfRegRequired"}
@am == 1 @$_2fDevices $req->data->{'sfRegRequired'}
Truth table0%0/4
A and not(B || C)
A= @am == 1B= @$_2fDevicesC= $req->data->{'sfRegRequired'}
ABCresult
0XX0
11X0
1010
1001
700  
701 # Parse second factors to display delete button if allowed and upgrade button
702 0 my ( $displayUpgBtn, $action ) = ( 0, '' );
703 0 foreach my $reg_mod_info ( @{ $self->sfRModules } ) {
704 0 my $type = $reg_mod_info->{t};
705 0 my $prefix = $reg_mod_info->{p};
706 0 my $module = $reg_mod_info->{m};
707 0 foreach (@$_2fDevices) {
708  
709 0 ▶ if ( $_->{type} eq $type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{"type"} eq $type) 0 0 0
710  
711 # Set k_v template variables for non_private values
712 # and also not for epoch/name
713 0 my $extrakeys = filterKey2F( $_, "epoch", "name" );
714 0 ▶ while ( my ( $k, $v ) = each %$extrakeys ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$extrakeys) 0 0 0
715 0 $_->{"${k}_${v}"} = 1;
716 }
717  
718 # Populate additional info for template engine
719 0 $_->{prefix} = $prefix;
720 0 $_->{label} = $module->label;
721 0 $_->{delAllowed} = $self->isDelAllowed( $req, $reg_mod_info );
722  
723 # Display upgrade button
724 $displayUpgBtn ||= $module->authnLevel
725 && $module->authnLevel >
726 0 ▶ $req->userData->{authenticationLevel};
Condition0%0/6
$displayUpgBtn ||= $module->authnLevel && $module->authnLevel > $req->userData->{"authenticationLevel"}
l!l&&r!l&&!r
000
$module->authnLevel && $module->authnLevel > $req->userData->{'authenticationLevel'}
!ll&&!rl&&r
000
MC/DC0%0/3
$displayUpgBtn ||= $module->authnLevel && $module->authnLevel > $req->userData->{"authenticationLevel"}
$displayUpgBtn $module->authnLevel $module->authnLevel > $req->userData->{'authenticationLevel'}
Truth table0%0/4
A ||= B && C
A= $displayUpgBtnB= $module->authnLevelC= $module->authnLevel > $req->userData->{'authenticationLevel'}
ABCresult
1XX1
0111
00X0
0100
727 }
728 0 ▶ $action ||= $_->{delAllowed};
Condition0%0/3
$action ||= $_->{"delAllowed"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$action ||= $_->{"delAllowed"}
$action $_->{"delAllowed"}
Truth table0%0/3
A ||= B
A= $actionB= $_->{"delAllowed"}
ABresult
1X1
011
000
729 }
730 }
731 0 ▶ $displayUpgBtn = 0 unless $self->conf->{upgradeSession};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->conf->{"upgradeSession"} 0 0 0
732  
733 # Display template
734 return $self->p->sendHtml(
735 $req,
736 '2fregisters',
737 params => {
738 MODULES => \@am,
739 SFDEVICES => $_2fDevices,
740 ACTION => $action,
741 REG_REQUIRED => $req->data->{sfRegRequired},
742 0 ▶ DISPLAY_UPG => $displayUpgBtn,
Branch: 1 branch
BranchTrueFalseTotal
$self->canUpdateSfa($req) ? : 0 0 0
Condition0%0/2
$self->canUpdateSfa($req) || 'choose2f'
l!l
00
MC/DC0%0/1
$self->canUpdateSfa($req) || 'choose2f'
$self->canUpdateSfa($req)
Truth table0%0/2
A
A= $self->canUpdateSfa($req)
Aresult
11
00
743 MSG => $self->canUpdateSfa($req) || 'choose2f',
744 ALERT => ( $self->canUpdateSfa($req) ? 'warning' : 'positive' ),
745 SFREGISTERS_URL => encode_base64(
746 $self->p->buildUrl( $req->portal, '2fregisters' ), ''
747 ),
748 %tplParams,
749 }
750 );
751 }
752  
753 sub isDelAllowed {
754 0 ▶ my ( $self, $req, $reg_mod_info ) = @_;
Subroutine isDelAllowed: not called CC=2 SCAR=17.9
Pod undocumented
755 return ( $self->isRegistrationEnabled( $req, $reg_mod_info )
756 0 ▶ && $reg_mod_info->{m}->userCanRemove );
Condition0%0/3
$self->isRegistrationEnabled($req, $reg_mod_info) && $reg_mod_info->{'m'}->userCanRemove
!ll&&!rl&&r
000
MC/DC0%0/2
$self->isRegistrationEnabled($req, $reg_mod_info) && $reg_mod_info->{'m'}->userCanRemove
$self->isRegistrationEnabled($req, $reg_mod_info) $reg_mod_info->{'m'}->userCanRemove
Truth table0%0/3
A && B
A= $self->isRegistrationEnabled($req, $reg_mod_info)B= $reg_mod_info->{'m'}->userCanRemove
ABresult
0X0
100
111
757 }
758  
759 sub isRegistrationEnabled {
760 0 ▶ my ( $self, $req, $reg_mod_info ) = @_;
Subroutine isRegistrationEnabled: not called CC=3 SCAR=24.8
Pod undocumented
761 0 my $module = $reg_mod_info->{m};
762 0 my $prefix = $module->prefix;
763  
764 0 my $rule = $reg_mod_info->{r};
765  
766 # Extra modules require special processing
767 0 ▶ if ( $self->conf->{sfExtra}->{$prefix} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{'sfExtra'}{$prefix}) { } 0 0 0
768 0 return $rule->( $req, $req->userData );
769 }
770 else {
771 0 ▶ return $self->conf->{ $prefix . '2fActivation' }
Condition0%0/3
$self->conf->{$prefix . '2fActivation'} && &$rule($req, $req->userData)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->conf->{$prefix . '2fActivation'} && &$rule($req, $req->userData)
$self->conf->{$prefix . '2fActivation'} &$rule($req, $req->userData)
Truth table0%0/3
A && B
A= $self->conf->{$prefix . '2fActivation'}B= &$rule($req, $req->userData)
ABresult
0X0
100
111
772 && $rule->( $req, $req->userData );
773 }
774 }
775  
776 # Check rule and display
777 sub register {
778 0 ▶ my ( $self, $req, $prefix, @args ) = @_;
Subroutine register: not called CC=7 SCAR=40.3
Pod undocumented
779 0 my @am;
780  
781 # After verifying rule:
782 # - call register run method if $prefix
783 # - else give JSON list of available registers for this user
784 0 ▶ if ($prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix) 0 0 0
785 my ($m) =
786 0 grep { $_->{m}->prefix eq $prefix } @{ $self->sfRModules };
787 0 ▶ return $self->p->sendError( $req, 'Unknown register module', 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless $m 0 0 0
788 unless $m;
789 0 ▶ unless ( $m->{r}->( $req, $req->userData ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($m->{"r"}->($req, $req->userData)) 0 0 0
790 0 $self->userLogger->error("${prefix}2F registration not allowed");
791 0 return $self->p->sendError( $req, "notAuthorized", 403 );
792 }
793  
794 0 my $can_update_error = $self->canUpdateSfa( $req, $m->{m}, @args );
795 return $can_update_error
796 ? $self->sendError( $req, $can_update_error, 400 )
797 0 ▶ : $m->{m}->run( $req, @args );
Branch: 1 branch
BranchTrueFalseTotal
$can_update_error ? : 0 0 0
798 }
799  
800 0 foreach ( @{ $self->sfRModules } ) {
801 $self->logger->debug(
802 0 'Looking if ' . $_->{m}->prefix . '2F register is available' );
803 0 ▶ if ( $_->{r}->( $req, $req->userData ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{"r"}->($req, $req->userData)) 0 0 0
804 0 $self->logger->debug(' -> OK');
805 0 my $name = $_->{m}->prefix;
806 push @am,
807 {
808 name => $name,
809 logo => $_->{m}->logo,
810 0 url => "/2fregisters/$name"
811 };
812 }
813 }
814 0 return $self->p->sendJSONresponse( $req, \@am );
815 }
816  
817 sub restoreSession {
818 7 ▶ my ( $self, $req, @path ) = @_;
Subroutine restoreSession: called CC=8 SCAR=38.8
Pod undocumented
819  
820 7 ▶ if ( my $token = $req->pdata->{sfRegToken} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $req->pdata->{"sfRegToken"}) 0 7 7
821 0 ▶ if ( my $sessionData = $self->regOtt->getToken( $token, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sessionData = $self->regOtt->getToken($token, 1)) 0 0 0
822  
823 0 ▶ unless ( $sessionData->{_2fRealSession} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sessionData->{"_2fRealSession"}) 0 0 0
824 0 $self->logger->error("Invalid 2FA registration token");
825 0 return [ 302, [ Location => $req->portal ], [] ];
826 }
827 0 $self->restore2faSessionInfo( $req, $sessionData );
828  
829 0 ▶ if ( $req->params("continue") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->params("continue")) 0 0 0
830 0 return $self->continueLoginAfterRegistration($req);
831 }
832  
833 0 $req->userData( $req->sessionInfo );
834 0 $req->data->{sfRegRequired} = 1;
835 0 ▶ if ( $req->method eq 'POST' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->method eq 'POST') { } 0 0 0
836 0 my $res = $self->register( $req, @path );
837  
838 0 ▶ if ( $req->data->{_2fRegistered} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->data->{"_2fRegistered"}) 0 0 0
839 $self->regOtt->updateToken( $token,
840 0 "_2f" => $req->userData->{'_2f'} );
841 0 ▶ if ( my $authnLevel =
Branch: 1 branch
BranchTrueFalseTotal
if (my $authnLevel = $req->userData->{"registeredAuthenticationLevel"}) 0 0 0
842 $req->userData->{registeredAuthenticationLevel} )
843 {
844 0 $self->regOtt->updateToken( $token,
845 "newAuthenticationLevel" => $authnLevel );
846 }
847 }
848 0 return $res;
849 }
850 else {
851 0 return $self->_displayRegister( $req, @path );
852 }
853 }
854 }
855 $self->logger->warn(
856 7 "Cannot restore session state during mandatory 2FA registration");
857 7 return [ 302, [ Location => $req->portal ], [] ];
858 }
859  
860 sub continueLoginAfterRegistration {
861 0 ▶ my ( $self, $req ) = @_;
Subroutine continueLoginAfterRegistration: not called CC=2 SCAR=17.9
Pod undocumented
862  
863 # Check if registration was done
864 0 ▶ if ( !$req->sessionInfo->{_2f} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($req->sessionInfo->{"_2f"}) 0 0 0
865 0 $self->logger->error("2FA registration was not completed");
866 0 return $self->p->doPE( $req, PE_ERROR );
867 }
868  
869 # Else restore session
870 0 $req->mustRedirect(1);
871  
872 0 my $prefix = $req->sessionInfo->{_2f};
873 0 my $level = $req->sessionInfo->{newAuthenticationLevel};
874 0 return $self->_continueLogin( $req, $prefix, $level );
875 }
876  
877 sub _continueLogin {
878 0 ▶ my ( $self, $req, $prefix, $level ) = @_;
Subroutine _continueLogin: not called CC=3 SCAR=24.8
879  
880 0 ▶ if ($level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level) { } 0 0 0
881 0 $self->logger->debug(
882 "Update sessionInfo with new authenticationLevel: $level");
883 0 $req->sessionInfo->{authenticationLevel} = $level;
884 0 $req->sessionInfo->{_2f} = $prefix;
885  
886 # Compute macros & local groups again with new authenticationLevel
887 0 $self->logger->debug("Compute macros and local groups...");
888 0 $req->steps( [ 'setMacros', 'setLocalGroups' ] );
889 0 ▶ if ( my $error = $self->p->process($req) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $self->p->process($req)) 0 0 0
890 0 $self->logger->debug("SFA: Process returned error: $error");
891 0 $req->error($error);
892 0 return $self->p->doPE( $req, $error );
893 }
894 0 $self->logger->debug("De-duplicate groups...");
895 $req->sessionInfo->{groups} = join $self->conf->{multiValuesSeparator},
896 0 keys %{ {
897 0 map { $_ => 1 } split $self->conf->{multiValuesSeparator},
898 $req->sessionInfo->{groups}
899 }
900 0 };
901  
902 0 $self->logger->debug("Filter macros...");
903 my %macros = (
904 0 map { $_ => $req->sessionInfo->{$_} }
905 0 keys %{ $self->{conf}->{macros} }
906 );
907  
908 0 $self->logger->debug(
909 "Update session with new authenticationLevel, groups, hGroups and macros"
910 );
911 $self->p->updateSession(
912 $req,
913 {
914 authenticationLevel => $level,
915 groups => $req->sessionInfo->{groups},
916 hGroups => $req->sessionInfo->{hGroups},
917 0 _2f => $prefix,
918 _lastAuthnUTime => time,
919 %macros
920 }
921 );
922 }
923 else {
924 # Only update _2f session key
925 0 $self->p->updateSession(
926 $req,
927 {
928 _lastAuthnUTime => time,
929 _2f => $prefix,
930 }
931 );
932 }
933  
934 0 $req->authResult(PE_SENDRESPONSE);
935 return $self->p->do(
936 $req,
937 [
938 0 @{ $self->p->afterData },
939 $self->p->validSession,
940 0 @{ $self->p->endAuth },
941 [ returnPE => PE_OK ],
942 ]
943 );
944 }
945  
946 sub _verify {
947 47 ▶ my ( $self, $prefix, $req ) = @_;
Subroutine _verify: called CC=17 SCAR=52.9
948  
949 # Locate the module which handles this prefix
950 my ($m) =
951 47 grep { $_->{m}->prefix eq $prefix } @{ $self->sfModules };
952 47 ▶ return $self->p->sendError( $req, 'Unknown 2F module', 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless $m 0 47 47
953 unless $m;
954 47 my $module = $m->{m};
955  
956 47 my ( $tokres, $session ) = $self->_check_token( $req, $module->ott );
957 47 ▶ if ( $tokres != PE_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokres != 0) 47 0 47
958 $self->auditLog(
959 $req,
960 message => (
961 "Token validation failed during ${prefix}2f verification ("
962 . portalConsts->{$tokres} . ")"
963 ),
964 code => "2FA_FAILED",
965 reason => "Token validation failed",
966 type => $prefix,
967 47 portal_error => portalConsts->{$tokres},
968 );
969 47 return $self->p->doPE( $req, $tokres );
970 }
971  
972 0 my $user = $session->{ $self->conf->{whatToTrace} };
973  
974 # Evaluate hook
975 {
976 0 my $h =
977 $self->p->processHook( $req, 'sfBeforeVerify', $module, $session );
978 0 ▶ if ( $h != PE_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h != 0) 0 0 0
979 0 $req->noLoginDisplay(1);
980 0 return $self->p->doPE( $req, $h );
981 }
982 }
983  
984 # Launch second factor verification
985 my $verify_result = {
986 result => $module->verify( $req, $session ),
987 authenticationLevel => $module->authnLevel,
988 retries => ( $session->{_2fRetries} || 0 ),
989 (
990 $req->data->{_2fDevice} ? ( device => $req->data->{_2fDevice} ) : ()
991 ),
992 (
993 $req->data->{_2fLogInfo}
994 ? ( logInfo => $req->data->{_2fLogInfo} )
995 0 ▶ : ()
Branch: 2 branches
BranchTrueFalseTotal
$req->data->{'_2fDevice'} ? : 0 0 0
$req->data->{'_2fLogInfo'} ? : 0 0 0
Condition0%0/2
$session->{'_2fRetries'} || 0
l!l
00
MC/DC0%0/1
$session->{'_2fRetries'} || 0
$session->{'_2fRetries'}
Truth table0%0/2
A
A= $session->{'_2fRetries'}
Aresult
11
00
996 ),
997 };
998  
999 # Evaluate hook
1000 {
1001 0 my $h = $self->p->processHook( $req, 'sfAfterVerify', $module, $session,
1002 $verify_result );
1003 0 ▶ if ( $h != PE_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h != 0) 0 0 0
1004 0 $req->noLoginDisplay(1);
1005 0 return $self->p->doPE( $req, $h );
1006 }
1007 }
1008  
1009 # May be modified by sfAfterVerify
1010 0 my $res = $verify_result->{result};
1011 0 my $authnLevel = $verify_result->{authenticationLevel};
1012 0 my $retries = $verify_result->{retries};
1013  
1014 0 $self->restore2faSessionInfo( $req, $session );
1015  
1016 # Case error
1017 0 ▶ if ( $res and $retries == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res and $retries == 0) { } 0 0 0
elsif ($res) { } 0 0 0
Condition0%0/3
$res and $retries == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$res and $retries == 0
$res $retries == 0
Truth table0%0/3
A and B
A= $resB= $retries == 0
ABresult
0X0
100
111
1018 0 $req->noLoginDisplay(1);
1019 0 $req->authResult(PE_BADCREDENTIALS);
1020  
1021 $self->auditLog(
1022 $req,
1023 message => (
1024 "Second factor ${prefix}2f failed for $user ("
1025 . portalConsts->{$res} . ")"
1026 ),
1027 code => "2FA_FAILED",
1028 user => $user,
1029 type => $prefix,
1030 0 portal_error => portalConsts->{$res},
1031 );
1032  
1033 # Set the failed 2f type in case you want to store it in history
1034 # through sessionDataToRemember
1035 0 $req->sessionInfo->{'_2f'} = $prefix;
1036 0 return $self->p->do( $req, [ 'storeHistory', [ returnPE => $res ] ] );
1037 }
1038 elsif ($res) {
1039 $req->data->{_2fRetries} = (
1040 0 ▶ ( $retries > 0 )
Branch: 1 branch
BranchTrueFalseTotal
$retries > 0 ? : 0 0 0
1041 ? $retries - 1
1042 : 0
1043 );
1044  
1045 # Evaluate hook
1046 {
1047 0 my $h = $self->p->processHook( $req, 'sfBeforeRetry', $module );
1048 0 ▶ if ( $h != PE_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h != 0) 0 0 0
1049 0 $req->noLoginDisplay(1);
1050 0 return $self->p->doPE( $req, $h );
1051 }
1052 }
1053  
1054 $self->logger->debug(
1055 0 "Retrying 2FA: " . $req->data->{_2fRetries} . " retries left" );
1056  
1057 0 $req->error(PE_RETRY_2FA);
1058 0 my $token =
1059 $module->ott->createToken( $self->save2faSessionInfo($req) );
1060 0 my $res = $module->run( $req, $token );
1061 0 $req->authResult($res);
1062  
1063 0 return $self->p->doPE( $req, $res );
1064 }
1065  
1066 # Else restore session
1067 0 $req->mustRedirect(1);
1068 0 my $device = $verify_result->{device};
1069 0 ▶ my $logInfo = $verify_result->{logInfo} || {};
Condition0%0/2
$verify_result->{'logInfo'} || {}
l!l
00
MC/DC0%0/1
$verify_result->{'logInfo'} || {}
$verify_result->{'logInfo'}
Truth table0%0/2
A
A= $verify_result->{'logInfo'}
Aresult
11
00
1070 my $logInfoStr =
1071 0 join( ', ', map { "$_: " . $logInfo->{$_} } keys %$logInfo );
1072  
1073 0 ▶ $self->auditLog(
Branch: 3 branches
BranchTrueFalseTotal
$device ? : 0 0 0
$logInfoStr ? : 0 0 0
$device ? : 0 0 0
1074 $req,
1075 message => (
1076 "Second factor ${prefix}2f verification succeeded for $user"
1077 . ( $device ? ( " with device " . display2F($device) ) : "" )
1078 . ( $logInfoStr ? " ($logInfoStr)" : "" )
1079 ),
1080 code => "2FA_SUCCESS",
1081 user => $user,
1082 type => $prefix,
1083 ( $device ? ( device => display2F($device) ) : () ),
1084 %$logInfo,
1085 );
1086  
1087 0 return $self->_continueLogin( $req, $prefix, $authnLevel, );
1088 }
1089  
1090 sub searchForAuthorized2Fmodules {
1091 0 ▶ my ( $self, $req, $session ) = @_;
Subroutine searchForAuthorized2Fmodules: not called CC=5 SCAR=34.0
Pod undocumented
1092 0 ▶ $session ||= $req->sessionInfo;
Condition0%0/3
$session ||= $req->sessionInfo
l!l&&r!l&&!r
000
MC/DC0%0/2
$session ||= $req->sessionInfo
$session $req->sessionInfo
Truth table0%0/3
A ||= B
A= $sessionB= $req->sessionInfo
ABresult
1X1
011
000
1093 0 my @am;
1094 0 foreach ( @{ $self->sfModules } ) {
1095 0 my $authModuleName = $_->{m}->prefix;
1096 0 $self->logger->debug( 'Looking for ' . $authModuleName . ' 2f' );
1097  
1098 # Adding targetAuthnLevel from req to session;
1099 0 my $modifiedSession = {%$session};
1100 $modifiedSession->{targetAuthnLevel} = $req->{pdata}->{targetAuthnLevel}
1101 0 ▶ if $req->{pdata}->{targetAuthnLevel};
Branch: 1 branch
BranchTrueFalseTotal
if $req->{"pdata"}{"targetAuthnLevel"} 0 0 0
1102  
1103 0 ▶ if ( $_->{r}->( $req, $modifiedSession ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{"r"}->($req, $modifiedSession)) 0 0 0
1104 0 $self->logger->debug(
1105 ' -> ' . $authModuleName . ' 2f is available' );
1106 0 push @am, $_->{m};
1107 }
1108 }
1109 0 return @am;
1110 }
1111  
1112 sub canUpdateSfa {
1113 0 ▶ my ( $self, $req, $module, $action ) = @_;
Subroutine canUpdateSfa: not called CC=18 SCAR=58.3
Pod undocumented
1114 0 my $user = $req->userData->{ $self->conf->{whatToTrace} };
1115 0 my $prefix = '';
1116 0 my $msg;
1117  
1118 # Test actions
1119 0 ▶ if ( $module && $action ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module and $action) 0 0 0
Condition0%0/3
$module and $action
!ll&&!rl&&r
000
MC/DC0%0/2
$module and $action
$module $action
Truth table0%0/3
A and B
A= $moduleB= $action
ABresult
0X0
100
111
1120 0 my $requiredLevel = $module->authnLevel;
1121 0 $prefix = $module->prefix;
1122 0 $self->logger->debug("$user request to $action ${prefix}2f device");
1123  
1124 0 ▶ if ( $action eq 'delete' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "delete") 0 0 0
1125 $msg = 'notAuthorizedAuthLevel'
1126 if ( $requiredLevel
1127 0 ▶ && $req->userData->{authenticationLevel} < $requiredLevel );
Branch: 1 branch
BranchTrueFalseTotal
if $requiredLevel and $req->userData->{"authenticationLevel"} < $requiredLevel 0 0 0
Condition0%0/3
$requiredLevel and $req->userData->{"authenticationLevel"} < $requiredLevel
!ll&&!rl&&r
000
MC/DC0%0/2
$requiredLevel and $req->userData->{"authenticationLevel"} < $requiredLevel
$requiredLevel $req->userData->{"authenticationLevel"} < $requiredLevel
Truth table0%0/3
A and B
A= $requiredLevelB= $req->userData->{"authenticationLevel"} < $requiredLevel
ABresult
0X0
100
111
1128 }
1129 0 ▶ if ( $action =~ /^(regist|verify)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action =~ /^(regist|verify)/) 0 0 0
1130 0 my $_2fDevices = $self->get2fDevices( $req, $req->userData );
1131 0 ▶ if ( !$self->checkMaximumSfaCount( $req, $_2fDevices ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $self->checkMaximumSfaCount($req, $_2fDevices)) { } 0 0 0
elsif (not $self->checkMaxAvailableAuthenticationLevel($req, $_2fDevices)) { } 0 0 0
1132 0 $msg = 'maxNumberOf2FDevicesReached';
1133 }
1134 elsif (
1135 !$self->checkMaxAvailableAuthenticationLevel(
1136 $req, $_2fDevices
1137 )
1138 )
1139 {
1140 0 $msg = 'notAuthorizedAuthLevel';
1141 }
1142 else {
1143 0 $self->logger->debug(
1144 "$user is allowed to $action ${prefix}2f device");
1145 }
1146 }
1147 }
1148  
1149 0 ▶ unless ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg) 0 0 0
1150 0 my $module;
1151 0 ▶ $action ||= 'update';
Condition0%0/2
$action ||= "update"
l!l
00
MC/DC0%0/1
$action ||= "update"
$action
Truth table0%0/2
A
A= $action
Aresult
11
00
1152  
1153 # Test if Impersonation is in progress
1154 0 ▶ if ( $self->conf->{impersonationRule} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"impersonationRule"}) 0 0 0
1155 0 $self->logger->debug('Impersonation plugin is enabled');
1156 0 $module = 'Impersonation';
1157 $msg = 'notAuthorized'
1158 if ( $req->userData->{"$self->{conf}->{impersonationPrefix}_user"}
1159 && $req->userData->{"$self->{conf}->{impersonationPrefix}_user"}
1160 0 ▶ ne $req->userData->{_user} );
Branch: 1 branch
BranchTrueFalseTotal
if $req->userData->{qq[$self->{"conf"}{"impersonationPrefix"}_user]} and $req->userData->{qq[$self->{"conf"}{"impersonationPrefix"}_user]} ne $req->userData->{"_user"} 0 0 0
Condition0%0/3
$req->userData->{qq[$self->{"conf"}{"impersonationPrefix"}_user]} and $req->userData->{qq[$self->{"conf"}{"impersonationPrefix"}_user]} ne $req->userData->{"_user"}
!ll&&!rl&&r
000
MC/DC0%0/2
$req->userData->{qq[$self->{"conf"}{"impersonationPrefix"}_user]} and $req->userData->{qq[$self->{"conf"}{"impersonationPrefix"}_user]} ne $req->userData->{"_user"}
$req->userData->{qq[$self->{"conf"}{"impersonationPrefix"}_user]} $req->userData->{qq[$self->{"conf"}{"impersonationPrefix"}_user]} ne $req->userData->{"_user"}
Truth table0%0/3
A and B
A= $req->userData->{qq[$self->{"conf"}{"impersonationPrefix"}_user]}B= $req->userData->{qq[$self->{"conf"}{"impersonationPrefix"}_user]} ne $req->userData->{"_user"}
ABresult
0X0
100
111
1161 }
1162  
1163 # Test if ContextSwitching is in progress
1164 0 ▶ if ( $self->conf->{contextSwitchingRule} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"contextSwitchingRule"}) 0 0 0
1165 0 $self->logger->debug('ContextSwitching plugin is enabled');
1166 0 $module = 'ContextSwitching';
1167 $msg = 'notAuthorized'
1168 if (
1169 $req->userData->{
1170 "$self->{conf}->{contextSwitchingPrefix}_session_id"}
1171 && !$self->conf->{contextSwitchingAllowed2fModifications}
1172 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
if $req->userData->{qq[$self->{"conf"}{"contextSwitchingPrefix"}_session_id]} and not $self->conf->{"contextSwitchingAllowed2fModifications"} 0 0 0
Condition0%0/3
$req->userData->{qq[$self->{"conf"}{"contextSwitchingPrefix"}_session_id]} and not $self->conf->{"contextSwitchingAllowed2fModifications"}
!ll&&!rl&&r
000
MC/DC0%0/2
$req->userData->{qq[$self->{"conf"}{"contextSwitchingPrefix"}_session_id]} and not $self->conf->{"contextSwitchingAllowed2fModifications"}
$req->userData->{qq[$self->{"conf"}{"contextSwitchingPrefix"}_session_id]} not $self->conf->{"contextSwitchingAllowed2fModifications"}
Truth table0%0/3
A and not(B)
A= $req->userData->{qq[$self->{"conf"}{"contextSwitchingPrefix"}_session_id]}B= not $self->conf->{"contextSwitchingAllowed2fModifications"}
ABresult
0X0
100
111
1173  
1174 }
1175 0 ▶ if ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) { } 0 0 0
1176 0 $self->userLogger->warn(
1177 "$module in progress! $user is not allowed to $action ${prefix}2f device"
1178 );
1179 0 $self->logger->debug(
1180 "$user is NOT allowed to $action ${prefix}2f device because $module is in progress"
1181 );
1182 }
1183 else {
1184 0 $self->logger->debug(
1185 "$user is allowed to $action ${prefix}2f device");
1186 }
1187 }
1188  
1189 0 return $msg;
1190 }
1191  
1192 sub findRegisterModuleFor {
1193 4 ▶ my ( $self, $mod ) = @_;
Subroutine findRegisterModuleFor: called CC=2 SCAR=7.2
Pod undocumented
1194  
1195 4 ▶ return $mod->isa("Lemonldap::NG::Portal::Lib::Code2F")
Branch: 1 branch
BranchTrueFalseTotal
$mod->isa('Lemonldap::NG::Portal::Lib::Code2F') ? : 4 0 4
1196 ? "::2F::Register::Generic"
1197 : undef;
1198 }
1199  
1200 # Check if user can register one more SFA
1201 sub checkMaximumSfaCount {
1202 0 ▶ my ( $self, $req, $_2fDevices ) = @_;
Subroutine checkMaximumSfaCount: not called CC=2 SCAR=17.9
Pod undocumented
1203 0 my $user = $req->userData->{ $self->conf->{whatToTrace} };
1204 0 my $maxSize = $self->conf->{max2FDevices};
1205 0 my $size = @$_2fDevices;
1206 0 $self->logger->debug("Registered 2F device(s) for $user: $size / $maxSize");
1207 0 ▶ if ( $size >= $maxSize ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size >= $maxSize) 0 0 0
1208 0 $self->userLogger->warn(
1209 "Max number of 2F devices is reached for $user");
1210 0 return 0;
1211 }
1212 0 return 1;
1213 }
1214  
1215 # Check if user's authentication is sufficient to register a SFA
1216 sub checkMaxAvailableAuthenticationLevel {
1217 0 ▶ my ( $self, $req, $_2fDevices ) = @_;
Subroutine checkMaxAvailableAuthenticationLevel: not called CC=5 SCAR=34.0
Pod undocumented
1218 0 my $user = $req->userData->{ $self->conf->{whatToTrace} };
1219 0 my $requiredLevel = my $authnLevel = $req->userData->{authenticationLevel};
1220 0 my @am = $self->searchForAuthorized2Fmodules( $req, $req->userData );
1221 0 my %authnLevel = map { $_->type => $_->authnLevel } @am;
1222 0 foreach (@$_2fDevices) {
1223 $requiredLevel = $authnLevel{ $_->{type} }
1224 if ( $authnLevel{ $_->{type} }
1225 0 ▶ && $authnLevel{ $_->{type} } > $requiredLevel );
Branch: 1 branch
BranchTrueFalseTotal
if $authnLevel{$_->{"type"}} and $authnLevel{$_->{"type"}} > $requiredLevel 0 0 0
Condition0%0/3
$authnLevel{$_->{"type"}} and $authnLevel{$_->{"type"}} > $requiredLevel
!ll&&!rl&&r
000
MC/DC0%0/2
$authnLevel{$_->{"type"}} and $authnLevel{$_->{"type"}} > $requiredLevel
$authnLevel{$_->{"type"}} $authnLevel{$_->{"type"}} > $requiredLevel
Truth table0%0/3
A and B
A= $authnLevel{$_->{"type"}}B= $authnLevel{$_->{"type"}} > $requiredLevel
ABresult
0X0
100
111
1226 }
1227 0 ▶ if ( $requiredLevel > $authnLevel ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($requiredLevel > $authnLevel) 0 0 0
1228 0 $self->userLogger->warn(
1229 "$user request rejected due to insufficient authentication level!");
1230 0 $self->logger->debug(
1231 "authnLevel: $authnLevel < requiredLevel: $requiredLevel");
1232 0 return 0;
1233 }
1234 0 return 1;
1235 }
1236  
1237 sub _registerRoutes {
1238 67 ▶ my ( $self, $m ) = @_;
Subroutine _registerRoutes: called CC=2 SCAR=7.1
1239 67 ▶ unless ( $m->noRoute ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($m->noRoute) 67 0 67
1240 67 my $prefix = $m->prefix;
1241 67 $self->logger->debug( 'Adding ' . $prefix . '2fcheck routes' );
1242 $self->addAuthRoute(
1243 $prefix . '2fcheck' => sub {
1244 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1245 0 return $self->_verify( $prefix, @_ );
1246 },
1247 67 ['POST']
1248 );
1249 67 $self->addAuthRoute(
1250 $prefix . '2fcheck' => '_redirect',
1251 ['GET']
1252 );
1253 $self->addUnauthRoute(
1254 $prefix . '2fcheck' => sub {
1255 47 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
1256 47 return $self->_verify( $prefix, @_ );
1257 },
1258 67 ['POST']
1259 );
1260 67 $self->addUnauthRoute(
1261 $prefix . '2fcheck' => '_redirect',
1262 ['GET']
1263 );
1264 }
1265 }
1266  
1267 sub get2fTplParams {
1268 0 ▶ my ( $self, $req, $module ) = @_;
Subroutine get2fTplParams: not called CC=3 SCAR=24.8
Pod undocumented
1269 0 my %param = $self->p->getErrorTplParams($req);
1270 0 my $checkLogins = $req->param('checkLogins');
1271 0 ▶ $self->logger->debug( $module->prefix . '2f: checkLogins set' )
Branch: 1 branch
BranchTrueFalseTotal
if $checkLogins 0 0 0
1272 if $checkLogins;
1273  
1274 0 my $stayConnected = $req->param('stayconnected');
1275 0 ▶ $self->logger->debug( $module->prefix . '2f: stayConnected set' )
Branch: 1 branch
BranchTrueFalseTotal
if $stayConnected 0 0 0
1276 if $stayConnected;
1277  
1278 0 $param{CHECKLOGINS} = $checkLogins;
1279 0 $param{STAYCONNECTED} = $stayConnected;
1280  
1281 0 return %param;
1282 }
1283  
1284 sub _check_token {
1285 54 ▶ my ( $self, $req, $ott ) = @_;
Subroutine _check_token: called CC=4 SCAR=22.1
1286  
1287 54 my $token;
1288 54 ▶ unless ( $token = $req->param('token') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($token = $req->param("token")) 54 0 54
1289 54 $self->logger->error('2F choice requested without token');
1290 54 $req->mustRedirect(1);
1291 54 return PE_NOTOKEN;
1292 }
1293  
1294 0 my $session;
1295 0 ▶ unless ( $session = $ott->getToken($token) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session = $ott->getToken($token)) 0 0 0
1296 0 $self->logger->error('Invalid 2F choice form token');
1297 0 $req->noLoginDisplay(1);
1298 0 return PE_TOKENEXPIRED;
1299 }
1300  
1301 0 ▶ unless ( $session->{_2fRealSession} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session->{"_2fRealSession"}) 0 0 0
1302 0 $self->logger->error("Invalid 2FA session token");
1303 0 $req->noLoginDisplay(1);
1304 0 return PE_ERROR;
1305 }
1306 0 return ( PE_OK, $session );
1307 }
1308  
1309 321 1;
« blib/lib/Lemonldap/NG/Portal/Password/Base.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/CertificateResetByMail.pm »