blib/lib/Lemonldap/NG/Portal/2F/Register/Base.pm

filter: Statementstmt 22.7% 30 / 132 Branchbran 12.5% 6 / 48 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 35.0% 7 / 20 Podpod 7.6% 1 / 13 totaltotal 18.1% 44 / 243 CC 33 SCAR 29.8
CC
33
Cov
18%
delete
56.0
run
30.0
modify
30.0
CRAP 19.6
« blib/lib/Lemonldap/NG/Portal/Plugins/AdminLogout.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Lib/REST.pm »
1 # Base package for 2F Register modules
2 package Lemonldap::NG::Portal::2F::Register::Base;
3  
4 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 27 ▶use Lemonldap::NG::Portal::Main::Constants ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 27 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 27 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 27 our $VERSION = '2.23.0';
10  
11 27 extends 'Lemonldap::NG::Portal::Main::Plugin';
12  
13 27 has logo => ( is => 'rw', default => '2f.png' );
14 27 has label => ( is => 'rw' );
15 27 has authnLevel => ( is => 'rw' );
16 27 has userCanRemove => ( is => 'rw' );
17  
18 # 'type' field of stored _2fDevices
19 # Defaults to the last component of the package name
20 # But can be overriden by sfExtra
21 has type => (
22 is => 'rw',
23 28 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
24 28 ( split( '::', ref( $_[0] ) ) )[-1];
25 }
26 27 );
27  
28 has rule => (
29 is => 'rw',
30 lazy => 1,
31 28 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
32 28 return $_[0]->conf->{ $_[0]->prefix . '2fSelfRegistration' };
33 }
34 27 );
35  
36 has ott => (
37 is => 'rw',
38 lazy => 1,
39 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
40 my $ott =
41 0 $_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken');
42 $ott->timeout( $_[0]->{conf}->{sfLoginTimeout}
43 0 ▶ || $_[0]->{conf}->{formTimeout} );
Condition0%0/3
$_[0]{'conf'}{'sfLoginTimeout'} || $_[0]{'conf'}{'formTimeout'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{'conf'}{'sfLoginTimeout'} || $_[0]{'conf'}{'formTimeout'}
$_[0]{'conf'}{'sfLoginTimeout'} $_[0]{'conf'}{'formTimeout'}
Truth table0%0/3
A || B
A= $_[0]{'conf'}{'sfLoginTimeout'}B= $_[0]{'conf'}{'formTimeout'}
ABresult
1X1
011
000
44 0 return $ott;
45 }
46 27 );
47  
48 32 ▶sub init {
Subroutine init: called CC=5 SCAR=16.2
Pod undocumented
49 32 my ($self) = @_;
50  
51 # Set logo if overridden
52 $self->logo( $self->conf->{ $self->prefix . "2fLogo" } )
53 32 ▶ if $self->conf->{ $self->prefix . "2fLogo" };
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{$self->prefix . '2fLogo'} 0 32 32
54  
55 # Set label if provided, translation files will be used otherwise
56 $self->label( $self->conf->{ $self->prefix . "2fLabel" } )
57 32 ▶ if $self->conf->{ $self->prefix . "2fLabel" };
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{$self->prefix . '2fLabel'} 0 32 32
58  
59 $self->authnLevel( $self->conf->{ $self->prefix . "2fAuthnLevel" } )
60 32 ▶ if $self->conf->{ $self->prefix . "2fAuthnLevel" };
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{$self->prefix . '2fAuthnLevel'} 10 22 32
61  
62 # Set whether the user can remove this registration
63 $self->userCanRemove(
64 $self->conf->{ $self->prefix . '2fUserCanRemoveKey' } )
65 32 ▶ if $self->conf->{ $self->prefix . '2fUserCanRemoveKey' };
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{$self->prefix . '2fUserCanRemoveKey'} 28 4 32
66  
67 32 return 1;
68 }
69  
70 # Legacy
71 0 ▶sub canUpdateSfa {
Subroutine canUpdateSfa: not called CC=1 SCAR=6.9
Pod undocumented
72 0 my ( $self, $req ) = @_;
73 0 $self->logger->warn(
74 ref($self)
75 . "::canUpdateSfa is deprecated,"
76 . " this check is now performed by the 2F Engine,"
77 . " you can remove it from your custom module" );
78  
79 0 return;
80 }
81  
82 # Check characters and truncate SFA name if too long
83 0 ▶sub checkNameSfa {
Subroutine checkNameSfa: not called CC=3 SCAR=24.8
Pod undocumented
84 0 my ( $self, $req, $type, $name ) = @_;
85  
86 0 ▶ $name ||= "My$type";
Condition0%0/2
$name ||= "My$type"
l!l
00
MC/DC0%0/1
$name ||= "My$type"
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
87 0 ▶ unless ( $name =~ /^[\w\s-]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name =~ /^[\w\s-]+$/) 0 0 0
88 0 $self->userLogger->error("$type name with bad character(s)");
89 0 return;
90 }
91 0 $name = substr( $name, 0, $self->conf->{max2FDevicesNameLength} );
92 0 $self->logger->debug("Return $type name: $name");
93  
94 0 return $name;
95 }
96  
97 # Update request with newly registered device
98 0 ▶sub markRegistered {
Subroutine markRegistered: not called CC=2 SCAR=17.9
Pod undocumented
99 0 my ( $self, $req, $authnLevel ) = @_;
100 0 $req->data->{_2fRegistered} = 1;
101 0 $req->userData->{_2f} = $self->prefix;
102 0 ▶ $req->userData->{registeredAuthenticationLevel} = $authnLevel
Condition0%0/3
$authnLevel // $self->authnLevel
l!l&&r!l&&!r
000
MC/DC0%0/2
$authnLevel // $self->authnLevel
$authnLevel $self->authnLevel
Truth table0%0/3
A // B
A= $authnLevelB= $self->authnLevel
ABresult
1X1
011
000
103 // $self->authnLevel;
104 }
105  
106 # Default run method, for modules that don't want to implement it themselves
107 0 ▶sub run {
Subroutine run: not called CC=5 SCAR=34.0
Pod documented
108 0 my ( $self, $req, $action ) = @_;
109 0 my $user = $req->userData->{ $self->conf->{whatToTrace} };
110 0 ▶ return $self->errorResponse( $req, pe_status => PE_TOKENEXPIRED )
Branch: 1 branch
BranchTrueFalseTotal
unless $user 0 0 0
111 unless $user;
112  
113 0 ▶ if ( $self->can('supportedActions')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("supportedActions") and my $actions = $self->supportedActions($req)) 0 0 0
Condition0%0/3
$self->can("supportedActions") and my $actions = $self->supportedActions($req)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->can("supportedActions") and my $actions = $self->supportedActions($req)
$self->can("supportedActions") my $actions = $self->supportedActions($req)
Truth table0%0/3
A and B
A= $self->can("supportedActions")B= my $actions = $self->supportedActions($req)
ABresult
0X0
100
111
114 and my $actions = $self->supportedActions($req) )
115 {
116 0 ▶ if ( my $sub = $actions->{$action} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $actions->{$action}) 0 0 0
117 0 return $self->$sub($req);
118 }
119 }
120 0 $self->logger->error( $self->prefix . "2f: unknown action ($action)" );
121 0 return $self->errorResponse( $req, label => 'unknownAction' );
122 }
123  
124 0 ▶sub delete {
Subroutine delete: not called CC=7 SCAR=40.3
Pod undocumented
125 0 my ( $self, $req ) = @_;
126 0 my $user = $req->userData->{ $self->conf->{whatToTrace} };
127  
128 # Check if unregistration is allowed
129 0 ▶ return $self->errorResponse( $req, label => 'notAuthorized' )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->userCanRemove 0 0 0
130 unless $self->userCanRemove;
131  
132 0 ▶ $self->checkCsrf($req)
Branch: 1 branch
BranchTrueFalseTotal
$self->checkCsrf($req) or return $self->errorResponse($req, "label", "csrfError") 0 0 0
133 or return $self->errorResponse( $req, label => 'csrfError' );
134  
135 0 my $epoch = $req->param('epoch');
136 0 ▶ if ( !$epoch ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($epoch) 0 0 0
137 0 $self->logger->error(
138 $self->prefix . '2f: "epoch" parameter is missing' );
139 0 return $self->errorResponse($req);
140 }
141  
142 0 my $registration_state = { module => $self };
143  
144 0 my $h = $self->p->processHook( $req, 'sfDeleteDevice', $req->userData,
145 $self->type, $epoch, $registration_state );
146  
147 0 ▶ if ( $h != PE_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h != 0) 0 0 0
148  
149 # Return custom error label if defined by sfDeleteDevice hook
150 my $errorLabel = $registration_state->{errorLabel}
151 0 ▶ // 'sfDeleteDeviceHookError';
Condition0%0/2
$registration_state->{"errorLabel"} // "sfDeleteDeviceHookError"
l!l
00
MC/DC0%0/1
$registration_state->{"errorLabel"} // "sfDeleteDeviceHookError"
$registration_state->{"errorLabel"}
Truth table0%0/2
A
A= $registration_state->{"errorLabel"}
Aresult
11
00
152 0 $self->logger->error( $self->prefix
153 . "2f: error in sfDeleteDevice hook: "
154 . $errorLabel );
155 0 return $self->errorResponse( $req, label => $errorLabel );
156 }
157  
158 0 ▶ if ( $self->del2fDevice( $req, $req->userData, $self->type, $epoch ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->del2fDevice($req, $req->userData, $self->type, $epoch)) { } 0 0 0
159 0 return $self->p->sendJSONresponse( $req, { result => 1 } );
160 }
161 else {
162 0 $self->logger->error(
163 $self->prefix . "2f: unable to delete device: 2FDeviceNotFound" );
164 0 return $self->errorResponse( $req, label => '2FDeviceNotFound' );
165 }
166 }
167  
168 0 ▶sub modify {
Subroutine modify: not called CC=5 SCAR=34.0
Pod undocumented
169 0 my ( $self, $req ) = @_;
170 0 my $user = $req->userData->{ $self->conf->{whatToTrace} };
171  
172 0 ▶ $self->checkCsrf($req)
Branch: 1 branch
BranchTrueFalseTotal
$self->checkCsrf($req) or return $self->errorResponse($req, "label", "csrfError") 0 0 0
173 or return $self->errorResponse( $req, label => 'csrfError' );
174  
175 0 my $epoch = $req->param('epoch');
176 0 ▶ if ( !$epoch ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($epoch) 0 0 0
177 0 $self->logger->error(
178 $self->prefix . '2f: "epoch" parameter is missing' );
179 0 return $self->errorResponse($req);
180 }
181 0 my $label = $req->param('label');
182 0 ▶ if ( !$label ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($label) 0 0 0
183 0 $self->logger->error(
184 $self->prefix . '2f: "label" parameter is missing', 400 );
185 0 return $self->errorResponse($req);
186 }
187 0 $self->logger->debug( "Modifying 2FA device with the label " . $label );
188 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->update2fDevice($req, $req->userData, $self->type, "epoch", $epoch, "name", $label)) 0 0 0
189 $self->update2fDevice(
190 $req, $req->userData, $self->type, "epoch",
191 $epoch, "name", $label
192 )
193 )
194 {
195 0 return $self->p->sendJSONresponse( $req, { result => 1 } );
196 }
197 0 $self->logger->error( $self->prefix . "2f: device not found" );
198 0 return $self->errorResponse( $req, label => '2FDeviceNotFound' );
199 }
200  
201 0 ▶sub failHtmlResponse {
Subroutine failHtmlResponse: not called CC=3 SCAR=24.8
Pod undocumented
202 0 my ( $self, $req, $error ) = @_;
203 0 my $uid = $req->userData->{ $self->conf->{whatToTrace} };
204  
205 0 my $pe;
206 0 my $loglabel = $error;
207 0 my $label = $error;
208 0 ▶ if ( looks_like_number($error) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($error)) 0 0 0
209 0 $pe = portalConsts->{$error};
210 0 $loglabel = $pe;
211 0 $label = "PE" . $error;
212 }
213  
214 $self->auditLog(
215 0 ▶ $req,
Branch: 1 branch
BranchTrueFalseTotal
$pe ? : 0 0 0
216 message => (
217 $self->type . " 2F device registration failed for $uid : $loglabel"
218 ),
219 code => "2FA_DEVICE_REGISTRATION_FAILED",
220 type => $self->prefix,
221 ( $pe ? ( portal_error => $pe ) : ( error => $error ) ),
222 user => $uid,
223 );
224  
225 0 return $self->p->sendHtml(
226 $req, 'error',
227 params => {
228 RAW_ERROR => $label,
229 AUTH_ERROR_TYPE => 'error',
230 }
231 );
232 }
233  
234 0 ▶sub failResponse {
Subroutine failResponse: not called CC=4 SCAR=30.0
Pod undocumented
235 0 my ( $self, $req, $error, $code ) = @_;
236 0 my $uid = $req->userData->{ $self->conf->{whatToTrace} };
237  
238 0 my $pe;
239 0 my $loglabel = $error;
240 0 my $label = $error;
241 0 ▶ if ( looks_like_number($error) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($error)) 0 0 0
242 0 $pe = portalConsts->{$error};
243 0 $loglabel = $pe;
244 0 $label = "PE" . $error;
245 }
246  
247 $self->auditLog(
248 0 ▶ $req,
Branch: 1 branch
BranchTrueFalseTotal
$pe ? : 0 0 0
249 message => (
250 $self->type . " 2F device registration failed for $uid : $loglabel"
251 ),
252 code => "2FA_DEVICE_REGISTRATION_FAILED",
253 type => $self->prefix,
254 ( $pe ? ( portal_error => $pe ) : ( error => $error ) ),
255 user => $uid,
256 );
257  
258 0 ▶ $code ||= 500;
Condition0%0/2
$code ||= 500
l!l
00
MC/DC0%0/1
$code ||= 500
$code
Truth table0%0/2
A
A= $code
Aresult
11
00
259 0 return $self->sendJSONresponse( $req, { error => $label }, code => $code );
260 }
261  
262 0 ▶sub successResponse {
Subroutine successResponse: not called CC=1 SCAR=6.9
Pod undocumented
263 0 my ( $self, $req, $info ) = @_;
264 0 return $self->p->sendJSONresponse( $req, $info );
265 }
266  
267 0 ▶sub errorResponse {
Subroutine errorResponse: not called CC=4 SCAR=30.0
Pod undocumented
268 0 my ( $self, $req, %args ) = @_;
269  
270 0 ▶ my $code = $args{code} || 400;
Condition0%0/2
$args{'code'} || 400
l!l
00
MC/DC0%0/1
$args{'code'} || 400
$args{'code'}
Truth table0%0/2
A
A= $args{'code'}
Aresult
11
00
271 0 ▶ my $status = $args{pe_status} // PE_ERROR;
Condition0%0/2
$args{"pe_status"} // 24
l!l
00
MC/DC0%0/1
$args{"pe_status"} // 24
$args{"pe_status"}
Truth table0%0/2
A
A= $args{"pe_status"}
Aresult
11
00
272 0 my $err = "PE" . $status;
273 0 ▶ if ( $args{label} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"label"}) 0 0 0
274 0 $err = $args{label};
275 }
276 0 return $self->sendJSONresponse( $req, { error => $err }, code => $code );
277 }
278  
279 0 ▶sub registerDevice {
Subroutine registerDevice: not called CC=3 SCAR=24.8
Pod undocumented
280 0 my ( $self, $req, $info, $device ) = @_;
281  
282 0 my $registration_state = {
283 authenticationLevel => $self->authnLevel,
284 module => $self
285 };
286  
287 0 my $h = $self->p->processHook( $req, 'sfRegisterDevice', $info, $device,
288 $registration_state );
289 0 ▶ return $h if ( $h != PE_OK );
Branch: 1 branch
BranchTrueFalseTotal
if $h != 0 0 0 0
290  
291 0 ▶ if ( $self->add2fDevice( $req, $info, $device ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->add2fDevice($req, $info, $device)) { } 0 0 0
292 $self->markRegistered( $req,
293 0 $registration_state->{authenticationLevel} );
294 0 return PE_OK;
295 }
296 else {
297 0 return PE_ERROR;
298 }
299 }
300  
301 0 ▶sub checkCsrf {
Subroutine checkCsrf: not called CC=1 SCAR=6.9
Pod undocumented
302 0 my ( $self, $req ) = @_;
303 0 return $req->headers->header('X-CSRF-Check');
304 }
305  
306 27 1;
« blib/lib/Lemonldap/NG/Portal/Plugins/AdminLogout.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Lib/REST.pm »