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

filter: Statementstmt 21.7% 32 / 147 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 38.8% 7 / 18 Podpod 0.0% 0 / 2 totaltotal 15.4% 39 / 252 CC 36 SCAR 44.3
CC
36
Cov
14%
_registration
240.0
_verification
56.0
_registrationchallenge
42.0
CRAP 83.8
« blib/lib/Lemonldap/NG/Portal/Auth/GitHub.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/Upgrade.pm »
1 # Self WebAuthn registration
2 package Lemonldap::NG::Portal::2F::Register::WebAuthn;
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Lemonldap::NG::Portal::Main::Constants 'PE_OK';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use JSON qw(from_json to_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use MIME::Base64 qw(encode_base64url decode_base64url);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Crypt::URandom;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '2.21.0';
12  
13 1 extends 'Lemonldap::NG::Portal::2F::Register::Base';
14 1 with 'Lemonldap::NG::Portal::Lib::WebAuthn';
15  
16 # INITIALIZATION
17  
18 1 has logo => ( is => 'rw', default => 'webauthn.png' );
19 1 has prefix => ( is => 'rw', default => 'webauthn' );
20 1 has template => ( is => 'ro', default => 'webauthn2fregister' );
21 1 has welcome => ( is => 'ro', default => 'webauthn2fWelcome' );
22 has ott => (
23 is => 'ro',
24 lazy => 1,
25 default => sub {
26 my $ott =
27 0 ▶ $_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken');
Subroutine __ANON__: not called CC=2 SCAR=17.9
28 my $timeout = $_[0]->{conf}->{sfRegisterTimeout}
29 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
30 0 $ott->timeout($timeout);
31 0 return $ott;
32 }
33 1 );
34  
35 has displayname_attr => (
36 is => 'rw',
37 lazy => 1,
38 default => sub {
39 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
40 $self->conf->{webauthnDisplayNameAttr}
41 || $self->conf->{portalUserAttr}
42 || $self->conf->{whatToTrace}
43 0 ▶ || '_user';
Branch: 1 branch
BranchTrueFalseTotal
$self->conf->{"webauthnDisplayNameAttr"} or $self->conf->{"portalUserAttr"} or $self->conf->{"whatToTrace"} or "_user" 0 0 0
Condition0%0/6
$self->conf->{"webauthnDisplayNameAttr"} or $self->conf->{"portalUserAttr"} or $self->conf->{"whatToTrace"}
l!l&&r!l&&!r
000
$self->conf->{"webauthnDisplayNameAttr"} or $self->conf->{"portalUserAttr"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->conf->{"webauthnDisplayNameAttr"} or $self->conf->{"portalUserAttr"} or $self->conf->{"whatToTrace"}
$self->conf->{"webauthnDisplayNameAttr"} $self->conf->{"portalUserAttr"} $self->conf->{"whatToTrace"}
Truth table0%0/4
A or B or C
A= $self->conf->{"webauthnDisplayNameAttr"}B= $self->conf->{"portalUserAttr"}C= $self->conf->{"whatToTrace"}
ABCresult
1XX1
01X1
0011
0000
44 }
45 1 );
46  
47 has rpName => (
48 is => 'rw',
49 lazy => 1,
50 default => sub {
51 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
52 0 ▶ $self->conf->{webauthnRpName} || "LemonLDAP::NG";
Branch: 1 branch
BranchTrueFalseTotal
$self->conf->{"webauthnRpName"} or "LemonLDAP::NG" 0 0 0
53 }
54 1 );
55  
56 1 use constant supportedActions => {
57 registrationchallenge => "_registrationchallenge",
58 registration => "_registration",
59 verificationchallenge => "_verificationchallenge",
60 verification => "_verification",
61 delete => "delete",
62 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 sub initDisplay {
65 0 ▶ my ( $self, $req ) = @_;
Subroutine initDisplay: not called CC=1 SCAR=6.9
Pod undocumented
66  
67 0 my $cacheTag = $self->p->cacheTag;
68 0 $req->data->{customScript} .= <<"EOF";
69 <script type="text/javascript" src="$self->{p}->{staticPrefix}/common/js/webauthn-json.browser-global.min.js?v=$cacheTag"></script>
70 <script type="text/javascript" src="$self->{p}->{staticPrefix}/common/js/webauthnregistration.min.js?v=$cacheTag"></script>
71 EOF
72  
73 }
74  
75 # Split content of webauthn2fAttestationTrust into an array ref of PEM certificates
76 sub _build_trust_anchors {
77 0 ▶ my ($self) = shift;
Subroutine _build_trust_anchors: not called CC=3 SCAR=24.8
78 0 my $pem_data = $self->conf->{webauthn2fAttestationTrust};
79 0 ▶ if ($pem_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pem_data) 0 0 0
80 0 my @split_certs = $pem_data =~
81 /(-----BEGIN CERTIFICATE-----.*?-----END CERTIFICATE-----)/sg;
82 0 ▶ if ( @split_certs > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@split_certs > 0) 0 0 0
83 0 return \@split_certs;
84 }
85 }
86 0 return [];
87 }
88  
89 # RUNNING METHODS
90  
91 # Return a Base64url encoded user handle
92 sub getRegistrationUserHandle {
93 0 ▶ my ( $self, $req ) = @_;
Subroutine getRegistrationUserHandle: not called CC=2 SCAR=17.9
Pod undocumented
94  
95 0 my $current_user_handle = $self->getUserHandle( $req, $req->userData );
96 0 ▶ if ($current_user_handle) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_user_handle) { } 0 0 0
97 0 return $current_user_handle;
98 }
99 else {
100 0 my $new_user_handle = $self->_generate_user_handle;
101 0 $self->setUserHandle( $req, $new_user_handle );
102 0 return $new_user_handle;
103 }
104 }
105  
106 # https://www.w3.org/TR/webauthn-2/#sctn-user-handle-privacy
107 # It is RECOMMENDED to let the user handle be 64 random bytes, and store this
108 # value in the user’s account.
109 sub _generate_user_handle {
110 0 ▶ my ($self) = @_;
Subroutine _generate_user_handle: not called CC=1 SCAR=6.9
111 0 return encode_base64url( Crypt::URandom::urandom(64) );
112 }
113  
114 sub _registrationchallenge {
115 0 ▶ my ( $self, $req ) = @_;
Subroutine _registrationchallenge: not called CC=6 SCAR=37.4
116  
117 0 ▶ return $self->failResponse( $req, 'csrfError', 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->checkCsrf($req) 0 0 0
118 unless $self->checkCsrf($req);
119  
120 0 my $user = $req->userData->{ $self->conf->{whatToTrace} };
121 0 my @alldevices = $self->find2fDevicesByType( $req, $req->userData );
122 0 my $challenge_base64 = encode_base64url( Crypt::URandom::urandom(32) );
123  
124 0 ▶ my $displayName = $req->userData->{ $self->displayname_attr } || $user;
Condition0%0/3
$req->userData->{$self->displayname_attr} || $user
l!l&&r!l&&!r
000
MC/DC0%0/2
$req->userData->{$self->displayname_attr} || $user
$req->userData->{$self->displayname_attr} $user
Truth table0%0/3
A || B
A= $req->userData->{$self->displayname_attr}B= $user
ABresult
1X1
011
000
125 0 my $userVerification = $self->conf->{webauthn2fUserVerification};
126 0 my $residentKey = $self->conf->{webauthn2fResidentKey};
127 0 ▶ my $attestation = $self->conf->{webauthn2fAttestation} || "none";
Condition0%0/2
$self->conf->{'webauthn2fAttestation'} || 'none'
l!l
00
MC/DC0%0/1
$self->conf->{'webauthn2fAttestation'} || 'none'
$self->conf->{'webauthn2fAttestation'}
Truth table0%0/2
A
A= $self->conf->{'webauthn2fAttestation'}
Aresult
11
00
128 0 ▶ my $request = {
Branch: 2 branches
BranchTrueFalseTotal
$userVerification ? : 0 0 0
$residentKey ? : 0 0 0
129 rp => {
130 name => $self->rpName,
131 id => $self->rp_id($req),
132 },
133 user => {
134 name => $user,
135 id => $self->getRegistrationUserHandle($req),
136 displayName => $displayName,
137 },
138 challenge => $challenge_base64,
139 attestation => $attestation,
140 pubKeyCredParams => [],
141 authenticatorSelection => { (
142 $userVerification ? ( userVerification => $userVerification )
143 : ()
144 ),
145 (
146 $residentKey ? ( residentKey => $residentKey )
147 : ()
148 )
149 }
150 };
151  
152 # Challenge is persisted on the server
153 0 my $token = $self->ott->createToken( {
154 registration_options => $request,
155 }
156 );
157  
158 0 $self->logger->debug(
159 "WebAuthn registration parameters " . to_json($request) );
160 0 return $self->successResponse( $req,
161 { request => $request, state_id => $token } );
162 }
163  
164 sub _registration {
165 0 ▶ my ( $self, $req ) = @_;
Subroutine _registration: not called CC=15 SCAR=54.8
166 0 my $user = $req->userData->{ $self->conf->{whatToTrace} };
167  
168 0 ▶ return $self->failResponse( $req, 'csrfError', 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->checkCsrf($req) 0 0 0
169 unless $self->checkCsrf($req);
170  
171 # Recover creation parameters, including challenge
172 0 my $state_id = $req->param('state_id');
173 0 ▶ unless ($state_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state_id) 0 0 0
174 0 $self->logger->error(
175 $self->prefix . "2f: could not find state ID in response" );
176 0 return $self->failResponse( $req, 'webAuthnRegisterFailed', 400 );
177 }
178 0 my $state_data;
179 0 ▶ unless ( $state_data = $self->ott->getToken($state_id) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state_data = $self->ott->getToken($state_id)) 0 0 0
180 0 $self->logger->error( $self->prefix
181 . "2f: expired or invalid state ID in response: $state_id" );
182 0 return $self->failResponse( $req, 'PE82', 400 );
183 }
184 0 my $registration_options = ( $state_data->{registration_options} );
185 0 ▶ unless ($registration_options) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($registration_options) 0 0 0
186 0 $self->logger->error( $self->prefix
187 . '2f: registration options missing from state data' );
188 0 return $self->failResponse( $req, 'webAuthnRegisterFailed', 400 );
189 }
190  
191 # Data required for WebAuthn verification
192 0 my $credential_json = $req->param('credential');
193 0 $self->logger->debug(
194 $self->prefix . "2f: get registered credential data $credential_json" );
195  
196 0 ▶ unless ($credential_json) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($credential_json) 0 0 0
197 0 $self->logger->error(
198 $self->prefix . '2f: missing credential parameter' );
199 0 return $self->failResponse( $req, 'webAuthnRegisterFailed', 400 );
200 }
201  
202 0 my $validation = eval {
203 0 $self->validateCredential( $req, $registration_options,
204 $credential_json );
205 };
206 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
207 0 $self->logger->error(
208 $self->prefix . "2f: Credential validation error: $@" );
209 0 return $self->failResponse( $req, "webAuthnRegisterFailed", 400 );
210 }
211  
212 0 my $credential_id = $validation->{credential_id};
213 0 my $credential_pubkey = $validation->{credential_pubkey};
214 0 my $signature_count = $validation->{signature_count};
215 0 my $aaguid = $validation->{attestation_result}->{aaguid};
216 0 ▶ $self->logger->debug(
Branch: 1 branch
BranchTrueFalseTotal
$aaguid ? : 0 0 0
217 $self->prefix
218 . "2f: registering new credential: \n"
219 . join( "\n",
220 "ID: $credential_id",
221 "Public key: $credential_pubkey",
222 "Signature count: $signature_count",
223 ( $aaguid ? "AAGUID: $aaguid" : () ) )
224 );
225  
226 0 ▶ return $self->failResponse( $req, 'webauthnAlreadyRegistered', 400 )
Branch: 1 branch
BranchTrueFalseTotal
if $self->find2fDevicesByKey($req, $req->userData, $self->type, "_credentialId", $credential_id) 0 0 0
227 if $self->find2fDevicesByKey( $req, $req->userData, $self->type,
228 "_credentialId", $credential_id );
229  
230 0 my $keyName =
231 $self->checkNameSfa( $req, $self->type, $req->param('keyName') );
232 0 ▶ return $self->failResponse( $req, 'badName', 200 ) unless $keyName;
Branch: 1 branch
BranchTrueFalseTotal
unless $keyName 0 0 0
233  
234 my $is_resident = (
235 $registration_options->{authenticatorSelection}->{residentKey}
236 and
237 ( $registration_options->{authenticatorSelection}->{residentKey} eq
238 0 ▶ "required" )
Condition0%0/3
$registration_options->{'authenticatorSelection'}{'residentKey'} && $registration_options->{'authenticatorSelection'}{'residentKey'} eq 'required'
!ll&&!rl&&r
000
MC/DC0%0/2
$registration_options->{'authenticatorSelection'}{'residentKey'} && $registration_options->{'authenticatorSelection'}{'residentKey'} eq 'required'
$registration_options->{'authenticatorSelection'}{'residentKey'} $registration_options->{'authenticatorSelection'}{'residentKey'} eq 'required'
Truth table0%0/3
A && B
A= $registration_options->{'authenticatorSelection'}{'residentKey'}B= $registration_options->{'authenticatorSelection'}{'residentKey'} eq 'required'
ABresult
0X0
100
111
239 );
240  
241 0 my $serialized_transports =
242 $self->_serializeTransportsFromJsonResponse($credential_json);
243 0 ▶ my $res = $self->registerDevice(
Branch: 3 branches
BranchTrueFalseTotal
$serialized_transports ? : 0 0 0
$aaguid ? : 0 0 0
$is_resident ? : 0 0 0
244 $req,
245 $req->userData,
246 {
247 _credentialId => $credential_id,
248 _credentialPublicKey => $credential_pubkey,
249 _signCount => $signature_count,
250 (
251 $serialized_transports
252 ? ( _transports => $serialized_transports )
253 : ()
254 ),
255 ( $aaguid ? ( _aaguid => $aaguid ) : () ),
256 type => $self->type,
257 name => $keyName,
258 epoch => time(),
259 ( $is_resident ? ( resident => 1 ) : () ),
260 }
261 );
262  
263 0 ▶ if ( $res == PE_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res == 0) { } 0 0 0
264 0 return $self->successResponse( $req, { result => 1 } );
265 }
266 else {
267 0 $self->logger->error( $self->prefix . '2f: unable to add device' );
268 0 return $self->failResponse( $req, "PE$res" );
269 }
270 }
271  
272 sub _verificationchallenge {
273 0 ▶ my ( $self, $req ) = @_;
Subroutine _verificationchallenge: not called CC=3 SCAR=24.8
274 0 my $user = $req->userData->{ $self->conf->{whatToTrace} };
275  
276 0 ▶ return $self->failResponse( $req, 'csrfError', 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->checkCsrf($req) 0 0 0
277 unless $self->checkCsrf($req);
278  
279 0 $self->logger->debug( $self->prefix . '2f: verification challenge req' );
280  
281 0 my $request = $self->generateChallenge( $req, $req->userData );
282  
283 0 ▶ unless ($request) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($request) 0 0 0
284 0 $self->logger->error( $self->prefix . '2f: no registered device' );
285 0 return $self->failResponse( $req, 'webAuthnNoDevice', 500 );
286 }
287  
288 # Request is persisted on the server
289 0 my $token = $self->ott->createToken( {
290 authentication_options => $request,
291 }
292 );
293  
294 0 $self->logger->debug(
295 $self->prefix . "2f: authentication parameters: " . to_json($request) );
296 0 return $self->successResponse( $req,
297 { request => $request, state_id => $token } );
298 }
299  
300 sub _verification {
301 0 ▶ my ( $self, $req ) = @_;
Subroutine _verification: not called CC=7 SCAR=40.3
302 0 my $user = $req->userData->{ $self->conf->{whatToTrace} };
303  
304 0 ▶ return $self->failResponse( $req, 'csrfError', 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->checkCsrf($req) 0 0 0
305 unless $self->checkCsrf($req);
306  
307 0 my $credential_json = $req->param('credential');
308  
309 0 ▶ unless ($credential_json) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($credential_json) 0 0 0
310 0 $self->logger->error(
311 $self->prefix . '2f: missing credential parameter' );
312 0 return $self->failResponse( $req, 'webAuthnFailed', 400 );
313 }
314  
315 0 my $state_id = $req->param('state_id');
316 0 ▶ unless ($state_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state_id) 0 0 0
317 0 $self->logger->error( $self->prefix
318 . "2f: could not find state ID in response ($credential_json)" );
319 0 return $self->failResponse( $req, 'webAuthnFailed', 400 );
320 }
321  
322 # Recover challenge
323 0 my $state_data;
324 0 ▶ unless ( $state_data = $self->ott->getToken($state_id) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state_data = $self->ott->getToken($state_id)) 0 0 0
325 0 $self->logger->error( $self->prefix
326 . "2f: expired or invalid state ID in response ($state_id)" );
327 0 return $self->failResponse( $req, 'PE82', 400 );
328 }
329  
330 0 my $signature_options = ( $state_data->{authentication_options} );
331 0 my $validation_result = eval {
332 0 $self->validateAssertion( $req, $req->userData, $signature_options,
333 $credential_json );
334 };
335 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
336 0 $self->logger->error(
337 $self->prefix . "2f: validation error for $user: $@" );
338 0 return $self->successResponse( $req, { result => 0 } );
339 }
340  
341 0 ▶ if ( $validation_result->{success} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($validation_result->{'success'} == 1) { } 0 0 0
342 0 return $self->successResponse( $req, { result => 1 } );
343 }
344 else {
345 0 return $self->successResponse( $req, { result => 0 } );
346 }
347 }
348  
349 1 1;
« blib/lib/Lemonldap/NG/Portal/Auth/GitHub.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/Upgrade.pm »