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

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