blib/lib/Lemonldap/NG/Portal/Lib/Code2F.pm

filter: Statementstmt 35.4% 34 / 96 Branchbran 10.0% 3 / 30 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 46.6% 7 / 15 Podpod 12.5% 1 / 8 totaltotal 27.6% 45 / 163 CC 19 SCAR 27.0
CC
19
Cov
27%
_resend
42.0
verify_internal
12.0
populateDestAttribute
12.0
CRAP 14.8
« blib/lib/Lemonldap/NG/Portal/Plugins/InitializePasswordReset.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Password/REST.pm »
1 package Lemonldap::NG::Portal::Lib::Code2F;
2  
3 # Base class for 2F methods that work by
4 # * optionally generating a code
5 # * delivering the code through an external system (mail/rest/shell...)
6 # * validating that the input code is the correct one
7  
8 # Subclasses must define a 'conf_type' field that contains the configuration prefix
9 # to use to lookup common configuration keys
10  
11 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 23 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 23 ▶use Lemonldap::NG::Common::Util qw/display2F/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 23 ▶use Lemonldap::NG::Portal::Main::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
16 PE_OK
17 PE_NOTOKEN
18 PE_TOKENEXPIRED
19 PE_ERROR
20 PE_BADOTP
21 PE_FORMEMPTY
22 PE_SENDRESPONSE
23 portalConsts
24 23 );
25  
26 23 our $VERSION = '2.23.0';
27  
28 has resend_interval => (
29 is => 'rw',
30 lazy => 1,
31 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
32 0 ▶ $_[0]->{conf}->{ $_[0]->conf_type . '2fResendInterval' } || 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"conf"}{$_[0]->conf_type . '2fResendInterval'} or 0 0 0 0
33 }
34 23 );
35  
36 has code_activation => (
37 is => 'rw',
38 lazy => 1,
39 19 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
40 19 $_[0]->{conf}->{ $_[0]->conf_type . '2fCodeActivation' };
41 }
42 23 );
43  
44 23 has is_registrable => (
45 is => 'rw',
46 default => 0,
47 );
48  
49 23 has random => ( is => 'rw' );
50  
51 23 extends 'Lemonldap::NG::Portal::Main::SecondFactor';
52 23 with 'Lemonldap::NG::Portal::Lib::2fDevices';
53  
54 33 ▶sub init {
Subroutine init: called CC=2 SCAR=6.9
Pod undocumented
55 33 my ($self) = @_;
56 33 ▶ if ( $self->code_activation ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->code_activation) 24 9 33
57 24 $self->random( Lemonldap::NG::Common::Crypto::srandom() );
58 }
59  
60 $self->addUnauthRoute(
61 33 $self->prefix . '2fresend' => '_resend',
62 ['POST']
63 );
64  
65 33 $self->addAuthRoute(
66 $self->prefix . '2fresend' => '_resend',
67 ['POST']
68 );
69  
70 33 return $self->SUPER::init();
71 }
72  
73 0 ▶sub run {
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
74 0 my ( $self, $req, $token ) = @_;
75 0 $self->populateDestAttribute( $req, $req->sessionInfo );
76  
77 0 ▶ return PE_ERROR unless $self->challenge( $req, $req->sessionInfo, $token );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->challenge($req, $req->sessionInfo, $token) 0 0 0
78  
79 0 $self->logger->debug("Prepare external 2F verification");
80 0 my $tmp =
81 $self->sendCodeForm( $req, TOKEN => $token, LEGEND => $self->legend );
82  
83 0 $req->response($tmp);
84 0 return PE_SENDRESPONSE;
85 }
86  
87 0 ▶sub challenge {
Subroutine challenge: not called CC=2 SCAR=17.9
Pod undocumented
88 0 my ( $self, $req, $sessionInfo, $token ) = @_;
89  
90 # Generate Code to send
91 0 my $code;
92 0 ▶ if ( $self->code_activation ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->code_activation) 0 0 0
93 0 $code = $self->random->randregex( $self->code_activation );
94 0 $self->logger->debug(
95 "Generated " . $self->prefix . "2f code : $code" );
96 0 $self->ott->updateToken( $token,
97 '__' . $self->prefix . '2fcode' => $code );
98 }
99  
100 0 return $self->sendCode( $req, $sessionInfo, $code );
101 }
102  
103 10 ▶sub _resend {
Subroutine _resend: called CC=8 SCAR=37.4
104 10 my ( $self, $req ) = @_;
105  
106 10 $self->logger->debug("Request to re-send code");
107  
108 # Check token
109 10 my $token;
110 10 ▶ unless ( $token = $req->param('token') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($token = $req->param("token")) 10 0 10
111 $self->auditLog(
112 $req,
113 message => ( $self->prefix . '2f access without token' ),
114 code => "2FA_RESEND_FAILED",
115 reason => "Missing token",
116 portal_error => portalConsts->{PE_NOTOKEN},
117 10 type => $self->prefix,
118 );
119  
120 10 $req->noLoginDisplay(1);
121 10 return $self->p->doPE( $req, PE_NOTOKEN );
122 }
123  
124 0 my $session;
125  
126 # Do not invalidate the token while getting it
127 0 ▶ unless ( $session = $self->ott->getToken( $token, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session = $self->ott->getToken($token, 1)) 0 0 0
128 $self->auditLog(
129 $req,
130 message =>
131 ( 'Invalid token during ' . $self->prefix . '2f resend' ),
132 code => "2FA_RESEND_FAILED",
133 reason => "Token expired",
134 portal_error => portalConsts->{PE_TOKENEXPIRED},
135 0 type => $self->prefix,
136 );
137 0 $req->noLoginDisplay(1);
138 0 return $self->p->doPE( $req, PE_TOKENEXPIRED );
139 }
140  
141 0 my $code = $session->{ '__' . $self->prefix . '2fcode' };
142  
143 0 my $legend = $self->legend;
144  
145 # Timer
146 my $lastretry =
147 0 ▶ $session->{__lastRetry} || $session->{tokenSessionStartTimestamp} || time;
Condition0%0/6
$session->{'__lastRetry'} || $session->{'tokenSessionStartTimestamp'} || &CORE::GLOBAL::time()
l!l&&r!l&&!r
000
$session->{"__lastRetry"} or $session->{"tokenSessionStartTimestamp"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$session->{'__lastRetry'} || $session->{'tokenSessionStartTimestamp'} || &CORE::GLOBAL::time()
$session->{"__lastRetry"} $session->{"tokenSessionStartTimestamp"} &CORE::GLOBAL::time()
Truth table0%0/4
A or B || C
A= $session->{"__lastRetry"}B= $session->{"tokenSessionStartTimestamp"}C= &CORE::GLOBAL::time()
ABCresult
1XX1
01X1
0011
0000
148  
149 0 $self->p->_sfEngine->restore2faSessionInfo( $req, $session );
150  
151 0 ▶ if ( $self->resend_interval
Branch: 1 branch
BranchTrueFalseTotal
if ($self->resend_interval and $lastretry + $self->resend_interval < &CORE::GLOBAL::time()) { } 0 0 0
Condition0%0/3
$self->resend_interval and $lastretry + $self->resend_interval < &CORE::GLOBAL::time()
!ll&&!rl&&r
000
MC/DC0%0/2
$self->resend_interval and $lastretry + $self->resend_interval < &CORE::GLOBAL::time()
$self->resend_interval $lastretry + $self->resend_interval < &CORE::GLOBAL::time()
Truth table0%0/3
A and B
A= $self->resend_intervalB= $lastretry + $self->resend_interval < &CORE::GLOBAL::time()
ABresult
0X0
100
111
152 and ( $lastretry + $self->resend_interval < time ) )
153 {
154  
155 0 $self->logger->debug("Re-sending code");
156 0 $self->populateDestAttribute( $req, $req->sessionInfo );
157  
158 # Resend code and update last retry
159 0 ▶ unless ( $self->sendCode( $req, $req->sessionInfo, $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->sendCode($req, $req->sessionInfo, $code)) 0 0 0
160 0 return $self->p->doPE( $req, PE_ERROR );
161 }
162 0 $self->ott->updateToken( $token, __lastRetry => time );
163 }
164 else {
165 0 $self->logger->debug("Resend request arrived too soon");
166 0 $legend = "resendTooSoon";
167 }
168  
169 0 return $self->sendCodeForm( $req, TOKEN => $token, LEGEND => $legend );
170 }
171  
172 0 ▶sub verify {
Subroutine verify: not called CC=2 SCAR=17.9
Pod undocumented
173 0 my ( $self, $req, $session ) = @_;
174 0 my $usercode;
175 0 ▶ unless ( $usercode = $req->param('code') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($usercode = $req->param("code")) 0 0 0
176 0 return PE_FORMEMPTY;
177 }
178  
179 0 $self->populateDestAttribute( $req, $session );
180  
181 0 my $res = $self->verify_supplied_code( $req, $session, $usercode );
182 0 return $res;
183 }
184  
185 0 ▶sub verify_supplied_code {
Subroutine verify_supplied_code: not called CC=2 SCAR=17.9
Pod undocumented
186 0 my ( $self, $req, $session, $usercode ) = @_;
187  
188 0 ▶ if ( $self->code_activation ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->code_activation) { } 0 0 0
189 0 return $self->verify_internal( $req, $session, $usercode );
190 }
191 else {
192 0 return $self->verify_external( $req, $session, $usercode );
193 }
194 }
195  
196 0 ▶sub verify_internal {
Subroutine verify_internal: not called CC=3 SCAR=24.8
Pod undocumented
197 0 my ( $self, $req, $session, $code ) = @_;
198  
199 0 my $savedcode = $session->{ '__' . $self->prefix . '2fcode' };
200 0 ▶ unless ($savedcode) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($savedcode) 0 0 0
201 0 $self->logger->error(
202 'Unable to find generated 2F code in token session');
203 0 return PE_ERROR;
204 }
205  
206 $self->logger->debug(
207 0 "Verifying " . $self->prefix . "2f code: $code VS $savedcode" );
208 0 ▶ if ( $code eq $savedcode ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code eq $savedcode) { } 0 0 0
209 0 return PE_OK;
210 }
211 else {
212 0 return PE_BADOTP;
213 }
214 }
215  
216 0 ▶sub sendCodeForm {
Subroutine sendCodeForm: not called CC=2 SCAR=17.9
Pod undocumented
217 0 my ( $self, $req, %params ) = @_;
218  
219 # Prepare form
220 0 my $prefix = $self->prefix;
221 return $self->p->sendHtml(
222 $req,
223 'ext2fcheck',
224 params => {
225 MAIN_LOGO => $self->conf->{portalMainLogo},
226 0 ▶ SKIN => $self->p->getSkin($req),
Branch: 1 branch
BranchTrueFalseTotal
$self->resend_interval ? : 0 0 0
227 "PREFIX_$prefix" => 1,
228 PREFIX => $prefix,
229 (
230 $self->resend_interval
231 ? (
232 RESENDTARGET => $self->p->relativeUrl(
233 $req,
234 $self->prefix . '2fresend',
235 { skin => $self->p->getSkin($req) }
236 )
237 )
238 : ()
239 ),
240 TARGET => $self->p->relativeUrl(
241 $req,
242 $self->prefix . '2fcheck',
243 { skin => $self->p->getSkin($req) }
244 ),
245 $self->get2fTplParams($req),
246 %params,
247 }
248 );
249 }
250  
251 0 ▶sub populateDestAttribute {
Subroutine populateDestAttribute: not called CC=3 SCAR=24.8
Pod undocumented
252 0 my ( $self, $req, $sessionInfo ) = @_;
253 0 ▶ if ( $self->is_registrable ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_registrable) 0 0 0
254 0 my @registered_devices =
255 $self->find2fDevicesByType( $req, $sessionInfo, $self->prefix );
256 0 ▶ if (@registered_devices) {
Branch: 1 branch
BranchTrueFalseTotal
if (@registered_devices) 0 0 0
257  
258 0 $sessionInfo->{destination} = $registered_devices[0]->{_generic};
259 0 $req->data->{_2fDevice} = $registered_devices[0];
260 }
261 }
262 }
263  
264 23 1;
« blib/lib/Lemonldap/NG/Portal/Plugins/InitializePasswordReset.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Password/REST.pm »