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

filter: Statementstmt 32.9% 27 / 82 Branchbran 21.4% 6 / 28 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 35.2% 6 / 17 Podpod 9.0% 1 / 11 totaltotal 25.6% 40 / 156 CC 19 SCAR 25.2
CC
19
Cov
27%
run
30.0
delete
30.0
checkNameSfa
12.0
CRAP 12.5
« blib/lib/Lemonldap/NG/Portal/Plugins/NewLocationWarning.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/AdminLogout.pm »
1 # Base package for 2F Register modules
2 package Lemonldap::NG::Portal::2F::Register::Base;
3  
4 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 ▶use Lemonldap::NG::Portal::Main::Constants qw/PE_OK PE_ERROR/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 24 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 24 our $VERSION = '2.21.0';
9  
10 24 extends 'Lemonldap::NG::Portal::Main::Plugin';
11  
12 24 has logo => ( is => 'rw', default => '2f.png' );
13 24 has label => ( is => 'rw' );
14 24 has authnLevel => ( is => 'rw' );
15 24 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 25 ▶ ( split( '::', ref( $_[0] ) ) )[-1];
Subroutine __ANON__: called CC=1 SCAR=0.0
24 }
25 24 );
26  
27 has rule => (
28 is => 'rw',
29 lazy => 1,
30 default => sub {
31 25 ▶ return $_[0]->conf->{ $_[0]->prefix . '2fSelfRegistration' };
Subroutine __ANON__: called CC=1 SCAR=0.0
32 }
33 24 );
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 24 );
46  
47 sub init {
48 29 ▶ 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 29 ▶ if $self->conf->{ $self->prefix . "2fLogo" };
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{$self->prefix . '2fLogo'} 0 29 29
53  
54 # Set label if provided, translation files will be used otherwise
55 $self->label( $self->conf->{ $self->prefix . "2fLabel" } )
56 29 ▶ if $self->conf->{ $self->prefix . "2fLabel" };
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{$self->prefix . '2fLabel'} 0 29 29
57  
58 $self->authnLevel( $self->conf->{ $self->prefix . "2fAuthnLevel" } )
59 29 ▶ if $self->conf->{ $self->prefix . "2fAuthnLevel" };
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{$self->prefix . '2fAuthnLevel'} 8 21 29
60  
61 # Set whether the user can remove this registration
62 $self->userCanRemove(
63 $self->conf->{ $self->prefix . '2fUserCanRemoveKey' } )
64 29 ▶ if $self->conf->{ $self->prefix . '2fUserCanRemoveKey' };
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{$self->prefix . '2fUserCanRemoveKey'} 25 4 29
65  
66 29 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=5 SCAR=34.0
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 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
138 0 return $self->p->sendJSONresponse( $req, { result => 1 } );
139 }
140 0 $self->logger->error( $self->prefix . "2f: device not found" );
141 0 return $self->p->sendError( $req, '2FDeviceNotFound', 400 );
142 }
143  
144 sub failHtmlResponse {
145 0 ▶ my ( $self, $req, $error ) = @_;
Subroutine failHtmlResponse: not called CC=1 SCAR=6.9
Pod undocumented
146 0 my $uid = $req->userData->{ $self->conf->{whatToTrace} };
147  
148 0 $self->auditLog(
149 $req,
150 message => (
151 $self->type . " 2F device registration failed for $uid : $error"
152 ),
153 code => "2FA_DEVICE_REGISTRATION_FAILED",
154 type => $self->prefix,
155 portal_error => $error,
156 user => $uid,
157 );
158  
159 0 return $self->p->sendHtml(
160 $req, 'error',
161 params => {
162 RAW_ERROR => $error,
163 AUTH_ERROR_TYPE => 'error',
164 }
165 );
166 }
167  
168 sub failResponse {
169 0 ▶ my ( $self, $req, $error, $code ) = @_;
Subroutine failResponse: not called CC=1 SCAR=6.9
Pod undocumented
170 0 my $uid = $req->userData->{ $self->conf->{whatToTrace} };
171  
172 0 $self->auditLog(
173 $req,
174 message => (
175 $self->type . " 2F device registration failed for $uid : $error"
176 ),
177 code => "2FA_DEVICE_REGISTRATION_FAILED",
178 type => $self->prefix,
179 portal_error => $error,
180 user => $uid,
181 );
182  
183 0 return $self->p->sendError( $req, $error, $code );
184 }
185  
186 sub successResponse {
187 0 ▶ my ( $self, $req, $info ) = @_;
Subroutine successResponse: not called CC=1 SCAR=6.9
Pod undocumented
188 0 return $self->p->sendJSONresponse( $req, $info );
189 }
190  
191 sub registerDevice {
192 0 ▶ my ( $self, $req, $info, $device ) = @_;
Subroutine registerDevice: not called CC=3 SCAR=24.8
Pod undocumented
193  
194 0 my $registration_state = { authenticationLevel => $self->authnLevel, };
195  
196 0 my $h = $self->p->processHook( $req, 'sfRegisterDevice', $info, $device,
197 $registration_state );
198 0 ▶ return $h if ( $h != PE_OK );
Branch: 1 branch
BranchTrueFalseTotal
if $h != 0 0 0 0
199  
200 0 ▶ if ( $self->add2fDevice( $req, $info, $device ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->add2fDevice($req, $info, $device)) { } 0 0 0
201 $self->markRegistered( $req,
202 0 $registration_state->{authenticationLevel} );
203 0 return PE_OK;
204 }
205 else {
206 0 return PE_ERROR;
207 }
208 }
209  
210 sub checkCsrf {
211 0 ▶ my ( $self, $req ) = @_;
Subroutine checkCsrf: not called CC=1 SCAR=6.9
Pod undocumented
212 0 return $req->headers->header('X-CSRF-Check');
213 }
214  
215 24 1;
« blib/lib/Lemonldap/NG/Portal/Plugins/NewLocationWarning.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/AdminLogout.pm »