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

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