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/Plugins/RESTServer.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 ':all';
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.23.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 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
26 my $ott =
27 0 $_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken');
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 0 ▶ default => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
39 0 my $self = shift;
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 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
51 0 my $self = shift;
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 => {
Subroutine BEGIN: called CC=1 SCAR=0.0
57 registrationchallenge => "_registrationchallenge",
58 registration => "_registration",
59 verificationchallenge => "_verificationchallenge",
60 verification => "_verification",
61 modify => "modify",
62 delete => "delete",
63 1 };
64  
65 0 ▶sub initDisplay {
Subroutine initDisplay: not called CC=1 SCAR=6.9
Pod undocumented
66 0 my ( $self, $req ) = @_;
67  
68 0 my $cacheTag = $self->p->cacheTag;
69 0 $req->data->{customScript} .= <<"EOF";
70 <script type="text/javascript" src="$self->{p}->{staticPrefix}/common/js/webauthn-json.browser-global.min.js?v=$cacheTag"></script>
71 <script type="text/javascript" src="$self->{p}->{staticPrefix}/common/js/webauthnregistration.min.js?v=$cacheTag"></script>
72 EOF
73  
74 }
75  
76 # Split content of webauthn2fAttestationTrust into an array ref of PEM certificates
77 0 ▶sub _build_trust_anchors {
Subroutine _build_trust_anchors: not called CC=3 SCAR=24.8
78 0 my ($self) = shift;
79 0 my $pem_data = $self->conf->{webauthn2fAttestationTrust};
80 0 ▶ if ($pem_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pem_data) 0 0 0
81 0 my @split_certs = $pem_data =~
82 /(-----BEGIN CERTIFICATE-----.*?-----END CERTIFICATE-----)/sg;
83 0 ▶ if ( @split_certs > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@split_certs > 0) 0 0 0
84 0 return \@split_certs;
85 }
86 }
87 0 return [];
88 }
89  
90 # RUNNING METHODS
91  
92 # Return a Base64url encoded user handle
93 0 ▶sub getRegistrationUserHandle {
Subroutine getRegistrationUserHandle: not called CC=2 SCAR=17.9
Pod undocumented
94 0 my ( $self, $req ) = @_;
95  
96 0 my $current_user_handle = $self->getUserHandle( $req, $req->userData );
97 0 ▶ if ($current_user_handle) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_user_handle) { } 0 0 0
98 0 return $current_user_handle;
99 }
100 else {
101 0 my $new_user_handle = $self->_generate_user_handle;
102 0 $self->setUserHandle( $req, $new_user_handle );
103 0 return $new_user_handle;
104 }
105 }
106  
107 # https://www.w3.org/TR/webauthn-2/#sctn-user-handle-privacy
108 # It is RECOMMENDED to let the user handle be 64 random bytes, and store this
109 # value in the user’s account.
110 0 ▶sub _generate_user_handle {
Subroutine _generate_user_handle: not called CC=1 SCAR=6.9
111 0 my ($self) = @_;
112 0 return encode_base64url( Crypt::URandom::urandom(64) );
113 }
114  
115 0 ▶sub _registrationchallenge {
Subroutine _registrationchallenge: not called CC=6 SCAR=37.4
116 0 my ( $self, $req ) = @_;
117  
118 0 ▶ return $self->failResponse( $req, 'csrfError', 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->checkCsrf($req) 0 0 0
119 unless $self->checkCsrf($req);
120  
121 0 my $user = $req->userData->{ $self->conf->{whatToTrace} };
122 0 my @alldevices = $self->find2fDevicesByType( $req, $req->userData );
123 0 my $challenge_base64 = encode_base64url( Crypt::URandom::urandom(32) );
124  
125 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
126 0 my $userVerification = $self->conf->{webauthn2fUserVerification};
127 0 my $residentKey = $self->conf->{webauthn2fResidentKey};
128 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
129 0 ▶ my $request = {
Branch: 2 branches
BranchTrueFalseTotal
$userVerification ? : 0 0 0
$residentKey ? : 0 0 0
130 rp => {
131 name => $self->rpName,
132 id => $self->rp_id($req),
133 },
134 user => {
135 name => $user,
136 id => $self->getRegistrationUserHandle($req),
137 displayName => $displayName,
138 },
139 challenge => $challenge_base64,
140 attestation => $attestation,
141 pubKeyCredParams => [
142 { type => 'public-key', alg => -7 }, # ES256
143 { type => 'public-key', alg => -257 }, # RS256
144 { type => 'public-key', alg => -37 }, # PS256
145 ],
146 authenticatorSelection => { (
147 $userVerification ? ( userVerification => $userVerification )
148 : ()
149 ),
150 (
151 $residentKey ? ( residentKey => $residentKey )
152 : ()
153 )
154 }
155 };
156  
157 # Challenge is persisted on the server
158 0 my $token = $self->ott->createToken( {
159 registration_options => $request,
160 }
161 );
162  
163 0 $self->logger->debug(
164 "WebAuthn registration parameters " . to_json($request) );
165 0 return $self->successResponse( $req,
166 { request => $request, state_id => $token } );
167 }
168  
169 0 ▶sub _registration {
Subroutine _registration: not called CC=15 SCAR=54.8
170 0 my ( $self, $req ) = @_;
171 0 my $user = $req->userData->{ $self->conf->{whatToTrace} };
172  
173 0 ▶ return $self->failResponse( $req, 'csrfError', 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->checkCsrf($req) 0 0 0
174 unless $self->checkCsrf($req);
175  
176 # Recover creation parameters, including challenge
177 0 my $state_id = $req->param('state_id');
178 0 ▶ unless ($state_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state_id) 0 0 0
179 0 $self->logger->error(
180 $self->prefix . "2f: could not find state ID in response" );
181 0 return $self->failResponse( $req, 'webAuthnRegisterFailed', 400 );
182 }
183 0 my $state_data;
184 0 ▶ unless ( $state_data = $self->ott->getToken($state_id) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state_data = $self->ott->getToken($state_id)) 0 0 0
185 0 $self->logger->error( $self->prefix
186 . "2f: expired or invalid state ID in response: $state_id" );
187 0 return $self->failResponse( $req, PE_TOKENEXPIRED, 400 );
188 }
189 0 my $registration_options = ( $state_data->{registration_options} );
190 0 ▶ unless ($registration_options) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($registration_options) 0 0 0
191 0 $self->logger->error( $self->prefix
192 . '2f: registration options missing from state data' );
193 0 return $self->failResponse( $req, 'webAuthnRegisterFailed', 400 );
194 }
195  
196 # Data required for WebAuthn verification
197 0 my $credential_json = $req->param('credential');
198 0 $self->logger->debug(
199 $self->prefix . "2f: get registered credential data $credential_json" );
200  
201 0 ▶ unless ($credential_json) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($credential_json) 0 0 0
202 0 $self->logger->error(
203 $self->prefix . '2f: missing credential parameter' );
204 0 return $self->failResponse( $req, 'webAuthnRegisterFailed', 400 );
205 }
206  
207 0 my $validation = eval {
208 0 $self->validateCredential( $req, $registration_options,
209 $credential_json );
210 };
211 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
212 0 $self->logger->error(
213 $self->prefix . "2f: Credential validation error: $@" );
214 0 return $self->failResponse( $req, "webAuthnRegisterFailed", 400 );
215 }
216  
217 0 my $credential_id = $validation->{credential_id};
218 0 my $credential_pubkey = $validation->{credential_pubkey};
219 0 my $signature_count = $validation->{signature_count};
220 0 my $aaguid = $validation->{attestation_result}->{aaguid};
221 0 ▶ $self->logger->debug(
Branch: 1 branch
BranchTrueFalseTotal
$aaguid ? : 0 0 0
222 $self->prefix
223 . "2f: registering new credential: \n"
224 . join( "\n",
225 "ID: $credential_id",
226 "Public key: $credential_pubkey",
227 "Signature count: $signature_count",
228 ( $aaguid ? "AAGUID: $aaguid" : () ) )
229 );
230  
231 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
232 if $self->find2fDevicesByKey( $req, $req->userData, $self->type,
233 "_credentialId", $credential_id );
234  
235 0 my $keyName =
236 $self->checkNameSfa( $req, $self->type, $req->param('keyName') );
237 0 ▶ return $self->failResponse( $req, 'badName', 200 ) unless $keyName;
Branch: 1 branch
BranchTrueFalseTotal
unless $keyName 0 0 0
238  
239 my $is_resident = (
240 $registration_options->{authenticatorSelection}->{residentKey}
241 and
242 ( $registration_options->{authenticatorSelection}->{residentKey} eq
243 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
244 );
245  
246 0 my $serialized_transports =
247 $self->_serializeTransportsFromJsonResponse($credential_json);
248 0 ▶ my $res = $self->registerDevice(
Branch: 3 branches
BranchTrueFalseTotal
$serialized_transports ? : 0 0 0
$aaguid ? : 0 0 0
$is_resident ? : 0 0 0
249 $req,
250 $req->userData,
251 {
252 _credentialId => $credential_id,
253 _credentialPublicKey => $credential_pubkey,
254 _signCount => $signature_count,
255 (
256 $serialized_transports
257 ? ( _transports => $serialized_transports )
258 : ()
259 ),
260 ( $aaguid ? ( _aaguid => $aaguid ) : () ),
261 type => $self->type,
262 name => $keyName,
263 epoch => time(),
264 ( $is_resident ? ( resident => 1 ) : () ),
265 }
266 );
267  
268 0 ▶ if ( $res == PE_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res == 0) { } 0 0 0
269 0 return $self->successResponse( $req, { result => 1 } );
270 }
271 else {
272 0 $self->logger->error( $self->prefix . '2f: unable to add device' );
273 0 return $self->failResponse( $req, $res );
274 }
275 }
276  
277 0 ▶sub _verificationchallenge {
Subroutine _verificationchallenge: not called CC=3 SCAR=24.8
278 0 my ( $self, $req ) = @_;
279 0 my $user = $req->userData->{ $self->conf->{whatToTrace} };
280  
281 0 ▶ return $self->failResponse( $req, 'csrfError', 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->checkCsrf($req) 0 0 0
282 unless $self->checkCsrf($req);
283  
284 0 $self->logger->debug( $self->prefix . '2f: verification challenge req' );
285  
286 0 my $request = $self->generateChallenge( $req, $req->userData );
287  
288 0 ▶ unless ($request) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($request) 0 0 0
289 0 $self->logger->error( $self->prefix . '2f: no registered device' );
290 0 return $self->failResponse( $req, 'webAuthnNoDevice', 500 );
291 }
292  
293 # Request is persisted on the server
294 0 my $token = $self->ott->createToken( {
295 authentication_options => $request,
296 }
297 );
298  
299 0 $self->logger->debug(
300 $self->prefix . "2f: authentication parameters: " . to_json($request) );
301 0 return $self->successResponse( $req,
302 { request => $request, state_id => $token } );
303 }
304  
305 0 ▶sub _verification {
Subroutine _verification: not called CC=7 SCAR=40.3
306 0 my ( $self, $req ) = @_;
307 0 my $user = $req->userData->{ $self->conf->{whatToTrace} };
308  
309 0 ▶ return $self->failResponse( $req, 'csrfError', 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->checkCsrf($req) 0 0 0
310 unless $self->checkCsrf($req);
311  
312 0 my $credential_json = $req->param('credential');
313  
314 0 ▶ unless ($credential_json) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($credential_json) 0 0 0
315 0 $self->logger->error(
316 $self->prefix . '2f: missing credential parameter' );
317 0 return $self->failResponse( $req, 'webAuthnFailed', 400 );
318 }
319  
320 0 my $state_id = $req->param('state_id');
321 0 ▶ unless ($state_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state_id) 0 0 0
322 0 $self->logger->error( $self->prefix
323 . "2f: could not find state ID in response ($credential_json)" );
324 0 return $self->failResponse( $req, 'webAuthnFailed', 400 );
325 }
326  
327 # Recover challenge
328 0 my $state_data;
329 0 ▶ unless ( $state_data = $self->ott->getToken($state_id) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state_data = $self->ott->getToken($state_id)) 0 0 0
330 0 $self->logger->error( $self->prefix
331 . "2f: expired or invalid state ID in response ($state_id)" );
332 0 return $self->failResponse( $req, PE_TOKENEXPIRED, 400 );
333 }
334  
335 0 my $signature_options = ( $state_data->{authentication_options} );
336 0 my $validation_result = eval {
337 0 $self->validateAssertion( $req, $req->userData, $signature_options,
338 $credential_json );
339 };
340 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
341 0 $self->logger->error(
342 $self->prefix . "2f: validation error for $user: $@" );
343 0 return $self->successResponse( $req, { result => 0 } );
344 }
345  
346 0 ▶ if ( $validation_result->{success} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($validation_result->{'success'} == 1) { } 0 0 0
347 0 return $self->successResponse( $req, { result => 1 } );
348 }
349 else {
350 0 return $self->successResponse( $req, { result => 0 } );
351 }
352 }
353  
354 1 1;
« blib/lib/Lemonldap/NG/Portal/Plugins/RESTServer.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/Upgrade.pm »