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

filter: Statementstmt 50.7% 275 / 542 Branchbran 33.7% 81 / 240 Conditioncond 14.5% 15 / 103 MC/DCmcdc 1.6% 1 / 59 Subroutinesub 69.7% 30 / 43 Podpod 6.6% 1 / 15 totaltotal 40.2% 403 / 1002 CC 173 SCAR 52.0
CC
173
Cov
42%
_displayRegister
600.0
run
423.9
canUpdateSfa
342.0
CRAP 180.5
« 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 341 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 341 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 341 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 341 ▶use JSON qw(from_json to_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 341 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 341 ▶use Lemonldap::NG::Portal::Main::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
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 341 );
28  
29 341 ▶use Lemonldap::NG::Common::Util qw/display2F filterKey2F/;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 341 our $VERSION = '2.23.0';
32  
33 341 extends 'Lemonldap::NG::Portal::Main::Plugin';
34 341 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 671 ▶has sfModules => ( is => 'rw', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
47 671 ▶has sfRModules => ( is => 'rw', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
48 341 has sfReq => ( is => 'rw' );
49 341 has sfMsgRule => ( is => 'rw' );
50  
51 has ott => (
52 is => 'rw',
53 lazy => 1,
54 8 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
55 my $ott =
56 8 $_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken');
57 8 $ott->timeout( $_[0]->{conf}->{formTimeout} );
58 8 return $ott;
59 }
60 341 );
61  
62 has regOtt => (
63 is => 'rw',
64 lazy => 1,
65 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
66 my $ott =
67 0 $_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken');
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 341 );
74  
75 671 ▶sub init {
Subroutine init: called CC=23 SCAR=32.2
Pod undocumented
76 671 my ($self) = @_;
77  
78 # Load 2F modules
79 671 for my $i ( 0 .. 1 ) {
80 1342 ▶ foreach (
Branch: 1 branch
BranchTrueFalseTotal
$i ? : 671 671 1342
81 split /,\s*/,
82 $self->conf->{
83 $i
84 ? 'available2FSelfRegistration'
85 : 'available2F'
86 }
87 )
88 {
89 8723 ▶ return 0 unless $self->_load_2f_module_by_prefix( $_, $i );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_load_2f_module_by_prefix($_, $i) 0 8723 8723
90 }
91 }
92  
93 # Extra 2F modules
94 671 $self->logger->debug('Processing Extra 2F modules');
95 671 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 6 0 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 4 0 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 671 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->sfReq($self->p->buildRule($self->conf->{"sfRequired"}, "sfRequired"))) 0 671 671
183 $self->sfReq(
184 $self->p->buildRule( $self->conf->{sfRequired}, 'sfRequired' )
185 )
186 )
187 {
188 0 return 0;
189 }
190  
191 671 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->sfMsgRule($self->p->buildRule($self->conf->{"sfRemovedMsgRule"}, "sfRemovedMsg"))) 0 671 671
192 $self->sfMsgRule(
193 $self->p->buildRule(
194 $self->conf->{sfRemovedMsgRule},
195 'sfRemovedMsg'
196 )
197 )
198 )
199 {
200 0 return 0;
201 }
202  
203 671 $self->addAuthRoute( '2fchoice' => '_choice', ['POST'] );
204 671 $self->addAuthRoute( '2fchoice' => '_redirect', ['GET'] );
205 671 $self->addUnauthRoute( '2fchoice' => '_choice', ['POST'] );
206 671 $self->addUnauthRoute( '2fchoice' => '_redirect', ['GET'] );
207  
208 # Registration base
209 671 $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 671 ▶ ) if ( $self->conf->{sfRequired} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{"sfRequired"} 5 666 671
217  
218 671 $self->addEntryPoint(
219 isa => 'Lemonldap::NG::Portal::Main::SecondFactor',
220 service => "secondFactor",
221 method => "_enable_2f_module",
222 );
223  
224 671 $self->addEntryPoint(
225 isa => 'Lemonldap::NG::Portal::2F::Register::Base',
226 service => "secondFactor",
227 method => "_enable_2f_register_module",
228 );
229  
230 671 return 1;
231 }
232  
233 8723 ▶sub _load_2f_module_by_prefix {
Subroutine _load_2f_module_by_prefix: called CC=8 SCAR=20.9
234 8723 my ( $self, $name, $isRegister ) = @_;
235 {
236 8723 my $prefix = lc($name);
237 8723 $prefix =~ s/2f$//i;
238  
239 # Activation parameter
240 8723 ▶ my $ap =
Branch: 1 branch
BranchTrueFalseTotal
$isRegister ? : 2684 6039 8723
241 $prefix . ( $isRegister ? '2fSelfRegistration' : '2fActivation' );
242 8723 $self->logger->debug("Checking $ap");
243  
244 # Unless $rule, skip loading
245 8723 ▶ if ( $self->conf->{$ap} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{$ap}) { } 92 8631 8723
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 92 0 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 28 0 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 64 0 64
257 }
258 }
259 else {
260 8631 $self->logger->debug(' -> not enabled');
261 }
262 }
263 8723 return 1;
264 }
265  
266 28 ▶sub _enable_2f_register_module {
Subroutine _enable_2f_register_module: called CC=1 SCAR=0.0
267 28 my ( $self, $m ) = @_;
268 28 return $self->_enable_module( $m, $self->sfRModules );
269 }
270  
271 64 ▶sub _enable_2f_module {
Subroutine _enable_2f_module: called CC=1 SCAR=0.0
272 64 my ( $self, $m ) = @_;
273  
274 64 $self->_registerRoutes($m);
275 64 return $self->_enable_module( $m, $self->sfModules );
276 }
277  
278 92 ▶sub _enable_module {
Subroutine _enable_module: called CC=3 SCAR=11.3
279 92 my ( $self, $m, $array ) = @_;
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 131 ▶sub run {
Subroutine run: called CC=37 SCAR=60.5
Pod documented
300 131 my ( $self, $req ) = @_;
301 131 my $checkLogins = $req->param('checkLogins');
302 131 my $forceUpgrade = $req->param('forceUpgrade');
303 131 my $stayconnected = $req->param('stayconnected');
304 131 ▶ my $spoofId = $req->param('spoofId') || '';
Condition50%1/2
$req->param('spoofId') || ''
l!l
0131
MC/DC0%0/1
$req->param('spoofId') || ''
$req->param('spoofId')
Truth table50%1/2
A
A= $req->param('spoofId')
Aresult
11
00
305 131 ▶ $self->logger->debug("2F checkLogins is set") if $checkLogins;
Branch: 1 branch
BranchTrueFalseTotal
if $checkLogins 0 131 131
306 131 ▶ $self->logger->debug("2F forceUpgrade is set") if $forceUpgrade;
Branch: 1 branch
BranchTrueFalseTotal
if $forceUpgrade 0 131 131
307  
308 # Skip 2F unless a module has been registered
309 131 ▶ unless ( @{ $self->sfModules } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->sfModules;}) 129 2 131
310 129 ▶ if ( $self->conf->{sfOnlyUpgrade} and $req->data->{doingSfUpgrade} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{'sfOnlyUpgrade'} and $req->data->{'doingSfUpgrade'}) { } 0 129 129
Condition33%1/3
$self->conf->{"sfOnlyUpgrade"} and $req->data->{"doingSfUpgrade"}
!ll&&!rl&&r
12900
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 129 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 2 ▶sub save2faSessionInfo {
Subroutine save2faSessionInfo: called CC=3 SCAR=11.4
Pod undocumented
509 2 my ( $self, $req ) = @_;
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 1 ▶sub restore2faSessionInfo {
Subroutine restore2faSessionInfo: called CC=2 SCAR=6.9
Pod undocumented
529 1 my ( $self, $req, $info ) = @_;
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 12 ▶sub display2fRegisters {
Subroutine display2fRegisters: called CC=3 SCAR=13.1
Pod undocumented
547 12 my ( $self, $req, $session ) = @_;
548 12 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 12 return 0;
552 }
553  
554 20 ▶sub _choice {
Subroutine _choice: called CC=4 SCAR=23.2
555 20 my ( $self, $req ) = @_;
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 0 ▶sub _redirect {
Subroutine _redirect: not called CC=2 SCAR=17.9
614 0 my ( $self, $req ) = @_;
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 0 ▶sub _displayRegister {
Subroutine _displayRegister: not called CC=24 SCAR=64.0
623 0 my ( $self, $req, $prefix ) = @_;
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 0 ▶sub isDelAllowed {
Subroutine isDelAllowed: not called CC=2 SCAR=17.9
Pod undocumented
751 0 my ( $self, $req, $reg_mod_info ) = @_;
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 0 ▶sub isRegistrationEnabled {
Subroutine isRegistrationEnabled: not called CC=3 SCAR=24.8
Pod undocumented
757 0 my ( $self, $req, $reg_mod_info ) = @_;
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 0 ▶sub register {
Subroutine register: not called CC=7 SCAR=40.3
Pod undocumented
775 0 my ( $self, $req, $prefix, @args ) = @_;
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 0 ▶sub _handle_registration {
Subroutine _handle_registration: not called CC=2 SCAR=17.9
815 0 my ( $self, $req, @path ) = @_;
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 27 ▶sub _registration_with_token {
Subroutine _registration_with_token: called CC=7 SCAR=36.6
825 27 my ( $self, $req, @path ) = @_;
826  
827 27 ▶ if ( my $token = $req->pdata->{sfRegToken} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $req->pdata->{"sfRegToken"}) 0 27 27
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 27 return;
859 }
860  
861 0 ▶sub _registration_with_optional_token {
Subroutine _registration_with_optional_token: not called CC=2 SCAR=17.9
862 0 my ( $self, $req, @path ) = @_;
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 27 ▶sub _registration_with_mandatory_token {
Subroutine _registration_with_mandatory_token: called CC=3 SCAR=11.1
873 27 my ( $self, $req, @path ) = @_;
874  
875 27 my $res = $self->_registration_with_token( $req, @path );
876 27 ▶ if ($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) { } 0 27 27
877 0 return $res;
878 }
879 else {
880 27 delete $req->pdata->{sfRegToken};
881 27 $self->logger->warn(
882 "Cannot restore session state during mandatory 2FA registration");
883 27 ▶ if ( $req->wantJSON ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->wantJSON) { } 16 11 27
884 16 return $self->sendError( $req, "PE" . PE_TOKENEXPIRED );
885 }
886 else {
887 11 return $self->p->sendRedirection( $req, $req->portal );
888 }
889 }
890 }
891  
892 # LEGACY
893 341 *restoreSession = \&_registration_with_mandatory_token;
894  
895 0 ▶sub continueLoginAfterRegistration {
Subroutine continueLoginAfterRegistration: not called CC=2 SCAR=17.9
Pod undocumented
896 0 my ( $self, $req ) = @_;
897  
898 # Check if registration was done
899 0 ▶ if ( !$req->sessionInfo->{_2f} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($req->sessionInfo->{"_2f"}) 0 0 0
900 0 $self->logger->error("2FA registration was not completed");
901 0 return $self->p->doPE( $req, PE_ERROR );
902 }
903  
904 # Else restore session
905 0 $req->mustRedirect(1);
906  
907 0 my $prefix = $req->sessionInfo->{_2f};
908 0 my $level = $req->sessionInfo->{newAuthenticationLevel};
909 0 return $self->_continueLogin( $req, $prefix, $level );
910 }
911  
912 1 ▶sub _continueLogin {
Subroutine _continueLogin: called CC=4 SCAR=14.5
913 1 my ( $self, $req, $prefix, $level ) = @_;
914  
915 1 ▶ if ($level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level) 0 1 1
916 0 $self->logger->debug(
917 "Update sessionInfo with new authenticationLevel: $level");
918 0 $req->sessionInfo->{authenticationLevel} = $level;
919 }
920 1 $req->sessionInfo->{_2f} = $prefix;
921  
922 # Compute macros & local groups again with new authenticationLevel and
923 # _2f macro
924 1 $self->logger->debug("Compute macros and local groups...");
925 1 $req->steps( [ 'setMacros', 'setLocalGroups' ] );
926 1 ▶ if ( my $error = $self->p->process($req) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $self->p->process($req)) 0 1 1
927 0 $self->logger->debug("SFA: Process returned error: $error");
928 0 $req->error($error);
929 0 return $self->p->doPE( $req, $error );
930 }
931 1 $self->logger->debug("De-duplicate groups...");
932 $req->sessionInfo->{groups} = join $self->conf->{multiValuesSeparator},
933 1 keys %{ {
934 2 map { $_ => 1 } split $self->conf->{multiValuesSeparator},
935 $req->sessionInfo->{groups}
936 }
937 1 };
938  
939 1 $self->logger->debug("Filter macros...");
940 my %macros = (
941 4 map { $_ => $req->sessionInfo->{$_} }
942 1 keys %{ $self->{conf}->{macros} }
943 );
944  
945 1 $self->logger->debug(
946 "Update session with new authenticationLevel, groups, hGroups and macros"
947 );
948 $self->p->updateSession(
949 $req,
950 {
951 ( $level ? ( authenticationLevel => $level ) : () ),
952 groups => $req->sessionInfo->{groups},
953 hGroups => $req->sessionInfo->{hGroups},
954 1 ▶ _2f => $prefix,
Branch: 1 branch
BranchTrueFalseTotal
$level ? : 0 1 1
955 _lastAuthnUTime => time,
956 %macros
957 }
958 );
959  
960 1 $req->authResult(PE_SENDRESPONSE);
961 return $self->p->do(
962 $req,
963 [
964 1 @{ $self->p->afterData },
965 $self->p->validSession,
966 1 @{ $self->p->endAuth },
967 [ returnPE => PE_OK ],
968 ]
969 );
970 }
971  
972 85 ▶sub _verify {
Subroutine _verify: called CC=17 SCAR=38.8
973 85 my ( $self, $prefix, $req ) = @_;
974  
975 # Locate the module which handles this prefix
976 my ($m) =
977 85 grep { $_->{m}->prefix eq $prefix } @{ $self->sfModules };
978 85 ▶ return $self->p->sendError( $req, 'Unknown 2F module', 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless $m 0 85 85
979 unless $m;
980 85 my $module = $m->{m};
981  
982 85 my ( $tokres, $session ) = $self->_check_token( $req, $module->ott );
983 85 ▶ if ( $tokres != PE_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokres != 0) 84 1 85
984 $self->auditLog(
985 $req,
986 message => (
987 "Token validation failed during ${prefix}2f verification ("
988 . portalConsts->{$tokres} . ")"
989 ),
990 code => "2FA_FAILED",
991 reason => "Token validation failed",
992 type => $prefix,
993 84 portal_error => portalConsts->{$tokres},
994 );
995 84 return $self->p->doPE( $req, $tokres );
996 }
997  
998 1 my $user = $session->{ $self->conf->{whatToTrace} };
999  
1000 # Evaluate hook
1001 {
1002 1 my $h =
1003 $self->p->processHook( $req, 'sfBeforeVerify', $module, $session );
1004 1 ▶ if ( $h != PE_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h != 0) 0 1 1
1005 0 $req->noLoginDisplay(1);
1006 0 return $self->p->doPE( $req, $h );
1007 }
1008 }
1009  
1010 # Launch second factor verification
1011 my $verify_result = {
1012 result => $module->verify( $req, $session ),
1013 authenticationLevel => $module->authnLevel,
1014 retries => ( $session->{_2fRetries} || 0 ),
1015 (
1016 $req->data->{_2fDevice} ? ( device => $req->data->{_2fDevice} ) : ()
1017 ),
1018 (
1019 $req->data->{_2fLogInfo}
1020 ? ( logInfo => $req->data->{_2fLogInfo} )
1021 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
1022 ),
1023 };
1024  
1025 # Evaluate hook
1026 {
1027 1 my $h = $self->p->processHook( $req, 'sfAfterVerify', $module, $session,
1028 $verify_result );
1029 1 ▶ if ( $h != PE_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h != 0) 0 1 1
1030 0 $req->noLoginDisplay(1);
1031 0 return $self->p->doPE( $req, $h );
1032 }
1033 }
1034  
1035 # May be modified by sfAfterVerify
1036 1 my $res = $verify_result->{result};
1037 1 my $authnLevel = $verify_result->{authenticationLevel};
1038 1 my $retries = $verify_result->{retries};
1039  
1040 1 $self->restore2faSessionInfo( $req, $session );
1041  
1042 # Case error
1043 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
1044 0 $req->noLoginDisplay(1);
1045 0 $req->authResult(PE_BADCREDENTIALS);
1046  
1047 $self->auditLog(
1048 $req,
1049 message => (
1050 "Second factor ${prefix}2f failed for $user ("
1051 . portalConsts->{$res} . ")"
1052 ),
1053 code => "2FA_FAILED",
1054 user => $user,
1055 type => $prefix,
1056 0 portal_error => portalConsts->{$res},
1057 );
1058  
1059 # Set the failed 2f type in case you want to store it in history
1060 # through sessionDataToRemember
1061 0 $req->sessionInfo->{'_2f'} = $prefix;
1062 0 return $self->p->do( $req, [ 'storeHistory', [ returnPE => $res ] ] );
1063 }
1064 elsif ($res) {
1065 $req->data->{_2fRetries} = (
1066 0 ▶ ( $retries > 0 )
Branch: 1 branch
BranchTrueFalseTotal
$retries > 0 ? : 0 0 0
1067 ? $retries - 1
1068 : 0
1069 );
1070  
1071 # Evaluate hook
1072 {
1073 0 my $h = $self->p->processHook( $req, 'sfBeforeRetry', $module );
1074 0 ▶ if ( $h != PE_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h != 0) 0 0 0
1075 0 $req->noLoginDisplay(1);
1076 0 return $self->p->doPE( $req, $h );
1077 }
1078 }
1079  
1080 $self->logger->debug(
1081 0 "Retrying 2FA: " . $req->data->{_2fRetries} . " retries left" );
1082  
1083 0 $req->error(PE_RETRY_2FA);
1084 0 my $token =
1085 $module->ott->createToken( $self->save2faSessionInfo($req) );
1086 0 my $res = $module->run( $req, $token );
1087 0 $req->authResult($res);
1088  
1089 0 return $self->p->doPE( $req, $res );
1090 }
1091  
1092 # Else restore session
1093 1 $req->mustRedirect(1);
1094 1 my $device = $verify_result->{device};
1095 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
1096 my $logInfoStr =
1097 0 join( ', ', map { "$_: " . $logInfo->{$_} } keys %$logInfo );
1098  
1099 1 ▶ $self->auditLog(
Branch: 3 branches
BranchTrueFalseTotal
$device ? : 0 1 1
$logInfoStr ? : 0 1 1
$device ? : 0 1 1
1100 $req,
1101 message => (
1102 "Second factor ${prefix}2f verification succeeded for $user"
1103 . ( $device ? ( " with device " . display2F($device) ) : "" )
1104 . ( $logInfoStr ? " ($logInfoStr)" : "" )
1105 ),
1106 code => "2FA_SUCCESS",
1107 user => $user,
1108 type => $prefix,
1109 ( $device ? ( device => display2F($device) ) : () ),
1110 %$logInfo,
1111 );
1112  
1113 1 return $self->_continueLogin( $req, $prefix, $authnLevel, );
1114 }
1115  
1116 133 ▶sub searchForAuthorized2Fmodules {
Subroutine searchForAuthorized2Fmodules: called CC=5 SCAR=16.5
Pod undocumented
1117 133 my ( $self, $req, $session ) = @_;
1118 133 ▶ $session ||= $req->sessionInfo;
Condition33%1/3
$session ||= $req->sessionInfo
l!l&&r!l&&!r
01330
MC/DC0%0/2
$session ||= $req->sessionInfo
$session $req->sessionInfo
Truth table33%1/3
A ||= B
A= $sessionB= $req->sessionInfo
ABresult
1X1
011
000
1119 133 my @am;
1120 133 foreach ( @{ $self->sfModules } ) {
1121 4 my $authModuleName = $_->{m}->prefix;
1122 4 $self->logger->debug( 'Looking for ' . $authModuleName . ' 2f' );
1123  
1124 # Adding targetAuthnLevel from req to session;
1125 4 my $modifiedSession = {%$session};
1126 $modifiedSession->{targetAuthnLevel} = $req->{pdata}->{targetAuthnLevel}
1127 4 ▶ if $req->{pdata}->{targetAuthnLevel};
Branch: 1 branch
BranchTrueFalseTotal
if $req->{"pdata"}{"targetAuthnLevel"} 0 4 4
1128  
1129 4 ▶ if ( $_->{r}->( $req, $modifiedSession ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{"r"}->($req, $modifiedSession)) 4 0 4
1130 4 $self->logger->debug(
1131 ' -> ' . $authModuleName . ' 2f is available' );
1132 4 push @am, $_->{m};
1133 }
1134 }
1135 133 return @am;
1136 }
1137  
1138 0 ▶sub canUpdateSfa {
Subroutine canUpdateSfa: not called CC=18 SCAR=58.3
Pod undocumented
1139 0 my ( $self, $req, $module, $action ) = @_;
1140 0 my $user = $req->userData->{ $self->conf->{whatToTrace} };
1141 0 my $prefix = '';
1142 0 my $msg;
1143  
1144 # Test actions
1145 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
1146 0 my $requiredLevel = $module->authnLevel;
1147 0 $prefix = $module->prefix;
1148 0 $self->logger->debug("$user request to $action ${prefix}2f device");
1149  
1150 0 ▶ if ( $action eq 'delete' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "delete") 0 0 0
1151 $msg = 'notAuthorizedAuthLevel'
1152 if ( $requiredLevel
1153 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
1154 }
1155 0 ▶ if ( $action =~ /^(regist|verify)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action =~ /^(regist|verify)/) 0 0 0
1156 0 my $_2fDevices = $self->get2fDevices( $req, $req->userData );
1157 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
1158 0 $msg = 'maxNumberOf2FDevicesReached';
1159 }
1160 elsif (
1161 !$self->checkMaxAvailableAuthenticationLevel(
1162 $req, $_2fDevices
1163 )
1164 )
1165 {
1166 0 $msg = 'notAuthorizedAuthLevel';
1167 }
1168 else {
1169 0 $self->logger->debug(
1170 "$user is allowed to $action ${prefix}2f device");
1171 }
1172 }
1173 }
1174  
1175 0 ▶ unless ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg) 0 0 0
1176 0 my $module;
1177 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
1178  
1179 # Test if Impersonation is in progress
1180 0 ▶ if ( $self->conf->{impersonationRule} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"impersonationRule"}) 0 0 0
1181 0 $self->logger->debug('Impersonation plugin is enabled');
1182 0 $module = 'Impersonation';
1183 $msg = 'notAuthorized'
1184 if ( $req->userData->{"$self->{conf}->{impersonationPrefix}_user"}
1185 && $req->userData->{"$self->{conf}->{impersonationPrefix}_user"}
1186 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
1187 }
1188  
1189 # Test if ContextSwitching is in progress
1190 0 ▶ if ( $self->conf->{contextSwitchingRule} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"contextSwitchingRule"}) 0 0 0
1191 0 $self->logger->debug('ContextSwitching plugin is enabled');
1192 0 $module = 'ContextSwitching';
1193 $msg = 'notAuthorized'
1194 if (
1195 $req->userData->{
1196 "$self->{conf}->{contextSwitchingPrefix}_session_id"}
1197 && !$self->conf->{contextSwitchingAllowed2fModifications}
1198 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
1199  
1200 }
1201 0 ▶ if ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) { } 0 0 0
1202 0 $self->userLogger->warn(
1203 "$module in progress! $user is not allowed to $action ${prefix}2f device"
1204 );
1205 0 $self->logger->debug(
1206 "$user is NOT allowed to $action ${prefix}2f device because $module is in progress"
1207 );
1208 }
1209 else {
1210 0 $self->logger->debug(
1211 "$user is allowed to $action ${prefix}2f device");
1212 }
1213 }
1214  
1215 0 return $msg;
1216 }
1217  
1218 4 ▶sub findRegisterModuleFor {
Subroutine findRegisterModuleFor: called CC=2 SCAR=7.2
Pod undocumented
1219 4 my ( $self, $mod ) = @_;
1220  
1221 4 ▶ return $mod->isa("Lemonldap::NG::Portal::Lib::Code2F")
Branch: 1 branch
BranchTrueFalseTotal
$mod->isa('Lemonldap::NG::Portal::Lib::Code2F') ? : 4 0 4
1222 ? "::2F::Register::Generic"
1223 : undef;
1224 }
1225  
1226 # Check if user can register one more SFA
1227 0 ▶sub checkMaximumSfaCount {
Subroutine checkMaximumSfaCount: not called CC=2 SCAR=17.9
Pod undocumented
1228 0 my ( $self, $req, $_2fDevices ) = @_;
1229 0 my $user = $req->userData->{ $self->conf->{whatToTrace} };
1230 0 my $maxSize = $self->conf->{max2FDevices};
1231 0 my $size = @$_2fDevices;
1232 0 $self->logger->debug("Registered 2F device(s) for $user: $size / $maxSize");
1233 0 ▶ if ( $size >= $maxSize ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size >= $maxSize) 0 0 0
1234 0 $self->userLogger->warn(
1235 "Max number of 2F devices is reached for $user");
1236 0 return 0;
1237 }
1238 0 return 1;
1239 }
1240  
1241 # Check if user's authentication is sufficient to register a SFA
1242 0 ▶sub checkMaxAvailableAuthenticationLevel {
Subroutine checkMaxAvailableAuthenticationLevel: not called CC=5 SCAR=34.0
Pod undocumented
1243 0 my ( $self, $req, $_2fDevices ) = @_;
1244 0 my $user = $req->userData->{ $self->conf->{whatToTrace} };
1245 0 my $requiredLevel = my $authnLevel = $req->userData->{authenticationLevel};
1246 0 my @am = $self->searchForAuthorized2Fmodules( $req, $req->userData );
1247 0 my %authnLevel = map { $_->type => $_->authnLevel } @am;
1248 0 foreach (@$_2fDevices) {
1249 $requiredLevel = $authnLevel{ $_->{type} }
1250 if ( $authnLevel{ $_->{type} }
1251 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
1252 }
1253 0 ▶ if ( $requiredLevel > $authnLevel ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($requiredLevel > $authnLevel) 0 0 0
1254 0 $self->userLogger->warn(
1255 "$user request rejected due to insufficient authentication level!");
1256 0 $self->logger->debug(
1257 "authnLevel: $authnLevel < requiredLevel: $requiredLevel");
1258 0 return 0;
1259 }
1260 0 return 1;
1261 }
1262  
1263 70 ▶sub _registerRoutes {
Subroutine _registerRoutes: called CC=2 SCAR=7.1
1264 70 my ( $self, $m ) = @_;
1265 70 ▶ unless ( $m->noRoute ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($m->noRoute) 70 0 70
1266 70 my $prefix = $m->prefix;
1267 70 $self->logger->debug( 'Adding ' . $prefix . '2fcheck routes' );
1268 $self->addAuthRoute(
1269 0 ▶ $prefix . '2fcheck' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1270 0 my $self = shift;
1271 0 return $self->_verify( $prefix, @_ );
1272 },
1273 70 ['POST']
1274 );
1275 70 $self->addAuthRoute(
1276 $prefix . '2fcheck' => '_redirect',
1277 ['GET']
1278 );
1279 $self->addUnauthRoute(
1280 85 ▶ $prefix . '2fcheck' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1281 85 my $self = shift;
1282 85 return $self->_verify( $prefix, @_ );
1283 },
1284 70 ['POST']
1285 );
1286 70 $self->addUnauthRoute(
1287 $prefix . '2fcheck' => '_redirect',
1288 ['GET']
1289 );
1290 }
1291 }
1292  
1293 2 ▶sub get2fTplParams {
Subroutine get2fTplParams: called CC=3 SCAR=11.1
Pod undocumented
1294 2 my ( $self, $req, $module ) = @_;
1295 2 my %param = $self->p->getErrorTplParams($req);
1296 2 my $checkLogins = $req->param('checkLogins');
1297 2 ▶ $self->logger->debug( $module->prefix . '2f: checkLogins set' )
Branch: 1 branch
BranchTrueFalseTotal
if $checkLogins 0 2 2
1298 if $checkLogins;
1299  
1300 2 my $stayConnected = $req->param('stayconnected');
1301 2 ▶ $self->logger->debug( $module->prefix . '2f: stayConnected set' )
Branch: 1 branch
BranchTrueFalseTotal
if $stayConnected 0 2 2
1302 if $stayConnected;
1303  
1304 2 $param{CHECKLOGINS} = $checkLogins;
1305 2 $param{STAYCONNECTED} = $stayConnected;
1306  
1307 2 return %param;
1308 }
1309  
1310 105 ▶sub _check_token {
Subroutine _check_token: called CC=4 SCAR=15.6
1311 105 my ( $self, $req, $ott ) = @_;
1312  
1313 105 my $token;
1314 105 ▶ unless ( $token = $req->param('token') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($token = $req->param("token")) 104 1 105
1315 104 $self->logger->error('2F choice requested without token');
1316 104 $req->mustRedirect(1);
1317 104 return PE_NOTOKEN;
1318 }
1319  
1320 1 my $session;
1321 1 ▶ unless ( $session = $ott->getToken($token) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session = $ott->getToken($token)) 0 1 1
1322 0 $self->logger->error('Invalid 2F choice form token');
1323 0 $req->noLoginDisplay(1);
1324 0 return PE_TOKENEXPIRED;
1325 }
1326  
1327 1 ▶ unless ( $session->{_2fRealSession} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session->{"_2fRealSession"}) 0 1 1
1328 0 $self->logger->error("Invalid 2FA session token");
1329 0 $req->noLoginDisplay(1);
1330 0 return PE_ERROR;
1331 }
1332 1 return ( PE_OK, $session );
1333 }
1334  
1335 341 1;
« blib/lib/Lemonldap/NG/Portal/Plugins/CheckState.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/UserDB/SAML.pm »