1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
#[macro_use]
extern crate nom;

use crate::error::WebauthnCError;
use webauthn_rs::base64_data::Base64UrlSafeData;

use serde_cbor::value::Value;
use std::collections::BTreeMap;
use std::convert::TryFrom;
use std::iter;
use url::Url;
use webauthn_rs::crypto::compute_sha256;
use webauthn_rs::proto::{
    AllowCredentials,
    // AttestationConveyancePreference,
    AuthenticatorAssertionResponseRaw,
    AuthenticatorAttachment,
    AuthenticatorAttestationResponseRaw,
    // AttestationObject
    // AuthenticatorData,
    CollectedClientData,
    CreationChallengeResponse,
    PublicKeyCredential,
    RegisterPublicKeyCredential,
    RequestChallengeResponse,
    UserVerificationPolicy,
};

#[derive(Debug)]
pub struct U2FRegistrationData {
    public_key_x: Vec<u8>,
    public_key_y: Vec<u8>,
    key_handle: Vec<u8>,
    att_cert: Vec<u8>,
    signature: Vec<u8>,
}

#[derive(Debug)]
pub struct U2FSignData {
    appid: Vec<u8>,
    key_handle: Vec<u8>,
    counter: u32,
    signature: Vec<u8>,
    user_present: u8,
}

pub mod error;
pub mod softtok;
pub mod u2fhid;

pub struct WebauthnAuthenticator<T>
where
    T: U2FToken,
{
    token: T,
}

pub trait U2FToken {
    fn perform_u2f_register(
        &mut self,
        // This is rp.id_hash
        app_bytes: Vec<u8>,
        // This is client_data_json_hash
        chal_bytes: Vec<u8>,
        // timeout from options
        timeout_ms: u64,
        platform_attached: bool,
        resident_key: bool,
        user_verification: bool,
    ) -> Result<U2FRegistrationData, WebauthnCError>;

    fn perform_u2f_sign(
        &mut self,
        // This is rp.id_hash
        app_bytes: Vec<u8>,
        // This is client_data_json_hash
        chal_bytes: Vec<u8>,
        // timeout from options
        timeout_ms: u64,
        // list of creds
        allowed_credentials: &[AllowCredentials],
        user_verification: bool,
    ) -> Result<U2FSignData, WebauthnCError>;
}

impl<T> WebauthnAuthenticator<T>
where
    T: U2FToken,
{
    pub fn new(token: T) -> Self {
        WebauthnAuthenticator { token }
    }
}

impl<T> WebauthnAuthenticator<T>
where
    T: U2FToken,
{
    /// 5.1.3. Create a New Credential - PublicKeyCredential’s [[Create]](origin, options, sameOriginWithAncestors) Method
    /// https://www.w3.org/TR/webauthn/#createCredential
    ///
    /// 6.3.2. The authenticatorMakeCredential Operation
    /// https://www.w3.org/TR/webauthn/#op-make-cred
    pub fn do_registration(
        &mut self,
        origin: &str,
        options: CreationChallengeResponse,
        // _same_origin_with_ancestors: bool,
    ) -> Result<RegisterPublicKeyCredential, WebauthnCError> {
        // Assert: options.publicKey is present.
        // This is asserted through rust types.

        // If sameOriginWithAncestors is false, return a "NotAllowedError" DOMException.
        // We just don't take this value.

        // Let options be the value of options.publicKey.
        let options = &options.public_key;

        // If the timeout member of options is present, check if its value lies within a reasonable range as defined by the client and if not, correct it to the closest value lying within that range. Set a timer lifetimeTimer to this adjusted value. If the timeout member of options is not present, then set lifetimeTimer to a client-specific default.
        let timeout = options
            .timeout
            .map(|t| if t > 60000 { 60000 } else { t })
            .unwrap_or(60000);

        // Let callerOrigin be origin. If callerOrigin is an opaque origin, return a DOMException whose name is "NotAllowedError", and terminate this algorithm.
        // This is a bit unclear - see https://github.com/w3c/wpub/issues/321.
        // It may be a browser specific quirk.
        // https://html.spec.whatwg.org/multipage/origin.html
        // As a result we don't need to check for our needs.

        // Let effectiveDomain be the callerOrigin’s effective domain. If effective domain is not a valid domain, then return a DOMException whose name is "Security" and terminate this algorithm.
        let caller_origin = Url::parse(origin).map_err(|pe| {
            log::error!("url parse failure -> {:x?}", pe);
            WebauthnCError::Security
        })?;

        let effective_domain = caller_origin
            .domain()
            // Checking by IP today muddies things. We'd need a check for rp.id about suffixes
            // to be different for this.
            // .or_else(|| caller_origin.host_str())
            .ok_or(WebauthnCError::Security)
            .map_err(|e| {
                log::error!("origin has no domain or host_str");
                e
            })?;

        log::debug!("effective domain -> {:x?}", effective_domain);
        log::debug!("relying party id -> {:x?}", options.rp.id);

        // If options.rp.id
        //      Is present
        //          If options.rp.id is not a registrable domain suffix of and is not equal to effectiveDomain, return a DOMException whose name is "Security", and terminate this algorithm.
        //      Is not present
        //          Set options.rp.id to effectiveDomain.

        if !effective_domain.ends_with(&options.rp.id) {
            log::error!("relying party id domain is not suffix of effective domain.");
            return Err(WebauthnCError::Security);
        }

        // Check origin is https:// if effectiveDomain != localhost.
        if !(effective_domain == "localhost" || caller_origin.scheme() == "https") {
            log::error!("An insecure domain or scheme in origin. Must be localhost or https://");
            return Err(WebauthnCError::Security);
        }

        // Let credTypesAndPubKeyAlgs be a new list whose items are pairs of PublicKeyCredentialType and a COSEAlgorithmIdentifier.
        // Done in rust types.

        // For each current of options.pubKeyCredParams:
        //     If current.type does not contain a PublicKeyCredentialType supported by this implementation, then continue.
        //     Let alg be current.alg.
        //     Append the pair of current.type and alg to credTypesAndPubKeyAlgs.
        let cred_types_and_pub_key_algs: Vec<_> = options
            .pub_key_cred_params
            .iter()
            .filter_map(|param| {
                if param.type_ != "public-key" {
                    None
                } else {
                    Some((param.type_.clone(), param.alg))
                }
            })
            .collect();

        log::debug!("Found -> {:x?}", cred_types_and_pub_key_algs);

        // If credTypesAndPubKeyAlgs is empty and options.pubKeyCredParams is not empty, return a DOMException whose name is "NotSupportedError", and terminate this algorithm.
        if cred_types_and_pub_key_algs.is_empty() {
            return Err(WebauthnCError::NotSupported);
        }

        // Webauthn-rs doesn't support this yet.
        /*
            // Let clientExtensions be a new map and let authenticatorExtensions be a new map.

            // If the extensions member of options is present, then for each extensionId → clientExtensionInput of options.extensions:
            //     If extensionId is not supported by this client platform or is not a registration extension, then continue.
            //     Set clientExtensions[extensionId] to clientExtensionInput.
            //     If extensionId is not an authenticator extension, then continue.
            //     Let authenticatorExtensionInput be the (CBOR) result of running extensionId’s client extension processing algorithm on clientExtensionInput. If the algorithm returned an error, continue.
            //     Set authenticatorExtensions[extensionId] to the base64url encoding of authenticatorExtensionInput.
        */

        // Let collectedClientData be a new CollectedClientData instance whose fields are:
        //    type
        //        The string "webauthn.create".
        //    challenge
        //        The base64url encoding of options.challenge.
        //    origin
        //        The serialization of callerOrigin.

        //    Not Supported Yet.
        //    tokenBinding
        //        The status of Token Binding between the client and the callerOrigin, as well as the Token Binding ID associated with callerOrigin, if one is available.
        let collected_client_data = CollectedClientData {
            type_: "webauthn.create".to_string(),
            challenge: options.challenge.clone(),
            origin: caller_origin.as_str().trim_end_matches("/").to_string(),
            token_binding: None,
        };

        //  Let clientDataJSON be the JSON-serialized client data constructed from collectedClientData.
        let client_data_json =
            serde_json::to_string(&collected_client_data).map_err(|_| WebauthnCError::JSON)?;

        // Let clientDataHash be the hash of the serialized client data represented by clientDataJSON.
        let client_data_json_hash = compute_sha256(client_data_json.as_bytes());

        log::debug!("client_data_json -> {:x?}", client_data_json);
        log::debug!("client_data_json_hash -> {:x?}", client_data_json_hash);

        // Not required.
        // If the options.signal is present and its aborted flag is set to true, return a DOMException whose name is "AbortError" and terminate this algorithm.

        // Let issuedRequests be a new ordered set.

        // Let authenticators represent a value which at any given instant is a set of client platform-specific handles, where each item identifies an authenticator presently available on this client platform at that instant.

        // Start lifetimeTimer.

        // While lifetimeTimer has not expired, perform the following actions depending upon lifetimeTimer, and the state and response for each authenticator in authenticators:

        //    If lifetimeTimer expires,
        //        For each authenticator in issuedRequests invoke the authenticatorCancel operation on authenticator and remove authenticator from issuedRequests.

        //    If the user exercises a user agent user-interface option to cancel the process,
        //        For each authenticator in issuedRequests invoke the authenticatorCancel operation on authenticator and remove authenticator from issuedRequests. Return a DOMException whose name is "NotAllowedError".

        //    If the options.signal is present and its aborted flag is set to true,
        //        For each authenticator in issuedRequests invoke the authenticatorCancel operation on authenticator and remove authenticator from issuedRequests. Then return a DOMException whose name is "AbortError" and terminate this algorithm.

        //    If an authenticator becomes available on this client device,
        //         If options.authenticatorSelection is present:
        //             If options.authenticatorSelection.authenticatorAttachment is present and its value is not equal to authenticator’s authenticator attachment modality, continue.
        //             If options.authenticatorSelection.requireResidentKey is set to true and the authenticator is not capable of storing a client-side-resident public key credential source, continue.
        //             If options.authenticatorSelection.userVerification is set to required and the authenticator is not capable of performing user verification, continue.
        //          Let userVerification be the effective user verification requirement for credential creation, a Boolean value, as follows. If options.authenticatorSelection.userVerification
        //              is set to required -> Let userVerification be true.
        //              is set to preferred
        //                  If the authenticator
        //                      is capable of user verification -> Let userVerification be true.
        //                      is not capable of user verification -> Let userVerification be false.
        //              is set to discouraged -> Let userVerification be false.
        //          Let userPresence be a Boolean value set to the inverse of userVerification.
        //          Let excludeCredentialDescriptorList be a new list.
        //          For each credential descriptor C in options.excludeCredentials:
        //              If C.transports is not empty, and authenticator is connected over a transport not mentioned in C.transports, the client MAY continue.
        //              Otherwise, Append C to excludeCredentialDescriptorList.
        //          Invoke the authenticatorMakeCredential operation on authenticator with clientDataHash, options.rp, options.user, options.authenticatorSelection.requireResidentKey, userPresence, userVerification, credTypesAndPubKeyAlgs, excludeCredentialDescriptorList, and authenticatorExtensions as parameters.

        //          Append authenticator to issuedRequests.

        //    If an authenticator ceases to be available on this client device,
        //         Remove authenticator from issuedRequests.

        //    If any authenticator returns a status indicating that the user cancelled the operation,
        //         Remove authenticator from issuedRequests.
        //         For each remaining authenticator in issuedRequests invoke the authenticatorCancel operation on authenticator and remove it from issuedRequests.

        //    If any authenticator returns an error status equivalent to "InvalidStateError",
        //         Remove authenticator from issuedRequests.
        //         For each remaining authenticator in issuedRequests invoke the authenticatorCancel operation on authenticator and remove it from issuedRequests.
        //         Return a DOMException whose name is "InvalidStateError" and terminate this algorithm.

        //    If any authenticator returns an error status not equivalent to "InvalidStateError",
        //         Remove authenticator from issuedRequests.

        //    If any authenticator indicates success,
        //         Remove authenticator from issuedRequests.
        //         Let credentialCreationData be a struct whose items are:
        //         Let constructCredentialAlg be an algorithm that takes a global object global, and whose steps are:

        //         Let attestationObject be a new ArrayBuffer, created using global’s %ArrayBuffer%, containing the bytes of credentialCreationData.attestationObjectResult’s value.

        //         Let id be attestationObject.authData.attestedCredentialData.credentialId.
        //         Let pubKeyCred be a new PublicKeyCredential object associated with global whose fields are:
        //         For each remaining authenticator in issuedRequests invoke the authenticatorCancel operation on authenticator and remove it from issuedRequests.
        //         Return constructCredentialAlg and terminate this algorithm.

        // For our needs, we let the u2f auth library handle the above, but currently it can't accept
        // verified devices for u2f with ctap1/2. We may need to change u2f/authenticator library in the future.
        // As a result this really limits our usage to certain device classes. This is why we implement
        // this section in a seperate function call.

        let (platform_attached, resident_key, user_verification) =
            match &options.authenticator_selection {
                Some(auth_sel) => {
                    let pa = auth_sel
                        .authenticator_attachment
                        .as_ref()
                        .map(|v| v == &AuthenticatorAttachment::Platform)
                        .unwrap_or(false);
                    let uv = &auth_sel.user_verification == &UserVerificationPolicy::Required;
                    (pa, auth_sel.require_resident_key, uv)
                }
                None => (false, false, false),
            };

        let rp_id_hash = compute_sha256(options.rp.id.as_bytes());

        let u2rd = self.token.perform_u2f_register(
            rp_id_hash.clone(),
            client_data_json_hash,
            timeout.into(),
            platform_attached,
            resident_key,
            user_verification,
        )?;

        // From the u2f response, we now need to assemble the attestation object now.

        // cbor encode the public key. We already decomposed this, so just create
        // the correct bytes.
        let mut map = BTreeMap::new();
        // KeyType -> EC2
        map.insert(Value::Integer(1), Value::Integer(2));
        // Alg -> ES256
        map.insert(Value::Integer(3), Value::Integer(-7));

        // Curve -> P-256
        map.insert(Value::Integer(-1), Value::Integer(1));
        // EC X coord
        map.insert(Value::Integer(-2), Value::Bytes(u2rd.public_key_x));
        // EC Y coord
        map.insert(Value::Integer(-3), Value::Bytes(u2rd.public_key_y));

        let pk_cbor = Value::Map(map);
        let pk_cbor_bytes = serde_cbor::to_vec(&pk_cbor).map_err(|e| {
            log::error!("PK CBOR -> {:x?}", e);
            WebauthnCError::CBOR
        })?;

        let key_handle_len: u16 = u16::try_from(u2rd.key_handle.len()).map_err(|e| {
            log::error!("CBOR kh len is not u16 -> {:x?}", e);
            WebauthnCError::CBOR
        })?;

        // combine aaGuid, KeyHandle, CborPubKey into a AttestedCredentialData. (acd)
        let aaguid: [u8; 16] = [0; 16];

        // make a 00 aaguid
        let acd: Vec<u8> = aaguid
            .iter()
            .chain(key_handle_len.to_be_bytes().iter())
            .map(|v| *v)
            .chain(u2rd.key_handle.iter().map(|v| *v))
            .chain(pk_cbor_bytes.iter().map(|v| *v))
            .collect();

        // set counter to 0 during create
        // Combine rp_id_hash, flags, counter, acd, into authenticator data.
        // The flags are always user_present, att present
        let flags = 0b01000001;

        let authdata: Vec<u8> = rp_id_hash
            .iter()
            .map(|v| *v)
            .chain(iter::once(flags))
            .chain(
                // A 0 u32 counter
                iter::repeat(0).take(4),
            )
            .chain(acd.into_iter())
            .collect();

        let mut attest_map = BTreeMap::new();

        match options.attestation {
            // None | Some(AttestationConveyancePreference::None) => {
            _ => {
                attest_map.insert(
                    Value::Text("fmt".to_string()),
                    Value::Text("none".to_string()),
                );
                attest_map.insert(Value::Text("attStmt".to_string()), Value::Null);
                attest_map.insert(Value::Text("authData".to_string()), Value::Bytes(authdata));
            } /*
              _ => {
              //    create a u2f attestation from authData, attest cert, a signature,)
                  unimplemented!();
              }
              */
        }

        let ao = Value::Map(attest_map);

        let ao_bytes = serde_cbor::to_vec(&ao).map_err(|e| {
            log::error!("AO CBOR -> {:x?}", e);
            WebauthnCError::CBOR
        })?;

        // Return a DOMException whose name is "NotAllowedError". In order to prevent information leak that could identify the user without consent, this step MUST NOT be executed before lifetimeTimer has expired. See §14.5 Registration Ceremony Privacy for details.

        let id: String = Base64UrlSafeData(u2rd.key_handle.clone()).to_string();

        let rego = RegisterPublicKeyCredential {
            id,
            raw_id: Base64UrlSafeData(u2rd.key_handle.clone()),
            response: AuthenticatorAttestationResponseRaw {
                attestation_object: Base64UrlSafeData(ao_bytes),
                client_data_json: Base64UrlSafeData(client_data_json.as_bytes().to_vec()),
            },
            type_: "public-key".to_string(),
        };

        log::debug!("rego  -> {:x?}", rego);
        Ok(rego)
    }

    /// https://www.w3.org/TR/webauthn/#getAssertion
    pub fn do_authentication(
        &mut self,
        origin: &str,
        options: RequestChallengeResponse,
    ) -> Result<PublicKeyCredential, WebauthnCError> {
        // Assert: options.publicKey is present.
        // This is asserted through rust types.

        // If sameOriginWithAncestors is false, return a "NotAllowedError" DOMException.
        // We just don't take this value.

        // Let options be the value of options.publicKey.
        let options = &options.public_key;

        // If the timeout member of options is present, check if its value lies within a reasonable range as defined by the client and if not, correct it to the closest value lying within that range. Set a timer lifetimeTimer to this adjusted value. If the timeout member of options is not present, then set lifetimeTimer to a client-specific default.
        let timeout = options
            .timeout
            .map(|t| if t > 60000 { 60000 } else { t })
            .unwrap_or(60000);

        // Let callerOrigin be origin. If callerOrigin is an opaque origin, return a DOMException whose name is "NotAllowedError", and terminate this algorithm.
        // This is a bit unclear - see https://github.com/w3c/wpub/issues/321.
        // It may be a browser specific quirk.
        // https://html.spec.whatwg.org/multipage/origin.html
        // As a result we don't need to check for our needs.

        // Let effectiveDomain be the callerOrigin’s effective domain. If effective domain is not a valid domain, then return a DOMException whose name is "Security" and terminate this algorithm.
        let caller_origin = Url::parse(origin).map_err(|pe| {
            log::error!("url parse failure -> {:x?}", pe);
            WebauthnCError::Security
        })?;

        let effective_domain = caller_origin
            .domain()
            // Checking by IP today muddies things. We'd need a check for rp.id about suffixes
            // to be different for this.
            // .or_else(|| caller_origin.host_str())
            .ok_or(WebauthnCError::Security)
            .map_err(|e| {
                log::error!("origin has no domain or host_str");
                e
            })?;

        log::debug!("effective domain -> {:x?}", effective_domain);
        log::debug!("relying party id -> {:x?}", options.rp_id);

        // If options.rp.id
        //      Is present
        //          If options.rp.id is not a registrable domain suffix of and is not equal to effectiveDomain, return a DOMException whose name is "Security", and terminate this algorithm.
        //      Is not present
        //          Set options.rp.id to effectiveDomain.

        if !effective_domain.ends_with(&options.rp_id) {
            log::error!("relying party id domain is not suffix of effective domain.");
            return Err(WebauthnCError::Security);
        }

        // Check origin is https:// if effectiveDomain != localhost.
        if !(effective_domain == "localhost" || caller_origin.scheme() == "https") {
            log::error!("An insecure domain or scheme in origin. Must be localhost or https://");
            return Err(WebauthnCError::Security);
        }

        // Let clientExtensions be a new map and let authenticatorExtensions be a new map.

        // If the extensions member of options is present, then for each extensionId → clientExtensionInput of options.extensions:
        // ...

        // Let collectedClientData be a new CollectedClientData instance whose fields are:
        let collected_client_data = CollectedClientData {
            type_: "webauthn.get".to_string(),
            challenge: options.challenge.clone(),
            origin: caller_origin.as_str().trim_end_matches("/").to_string(),
            token_binding: None,
        };

        // Let clientDataJSON be the JSON-serialized client data constructed from collectedClientData.
        let client_data_json =
            serde_json::to_string(&collected_client_data).map_err(|_| WebauthnCError::JSON)?;

        // Let clientDataHash be the hash of the serialized client data represented by clientDataJSON.
        let client_data_json_hash = compute_sha256(client_data_json.as_bytes());

        log::debug!("client_data_json -> {:x?}", client_data_json);
        log::debug!("client_data_json_hash -> {:x?}", client_data_json_hash);

        // This is where we deviate from the spec, since we aren't a browser.

        let user_verification = &options.user_verification == &UserVerificationPolicy::Required;

        let rp_id_hash = compute_sha256(options.rp_id.as_bytes());

        let u2sd = self.token.perform_u2f_sign(
            rp_id_hash.clone(),
            client_data_json_hash,
            timeout.into(),
            options.allow_credentials.as_slice(),
            user_verification,
        )?;

        log::debug!("u2sd -> {:x?}", u2sd);
        // Transform the result to webauthn

        // The flags are set from the device.

        let authdata: Vec<u8> = rp_id_hash
            .iter()
            .map(|v| *v)
            .chain(iter::once(u2sd.user_present))
            .chain(
                // A 0 u32 counter
                u2sd.counter.to_be_bytes().iter().map(|v| *v),
            )
            .collect();

        let id: String = Base64UrlSafeData(u2sd.key_handle.clone()).to_string();

        Ok(PublicKeyCredential {
            id,
            raw_id: Base64UrlSafeData(u2sd.key_handle.clone()),
            response: AuthenticatorAssertionResponseRaw {
                authenticator_data: Base64UrlSafeData(authdata),
                client_data_json: Base64UrlSafeData(client_data_json.as_bytes().to_vec()),
                signature: Base64UrlSafeData(u2sd.signature.clone()),
                user_handle: None,
            },
            type_: "public-key".to_string(),
        })
    }
}

#[cfg(test)]
mod tests {
    use crate::softtok::U2FSoft;
    use crate::u2fhid::U2FHid;
    use crate::WebauthnAuthenticator;
    // use webauthn_rs::base64_data::Base64UrlSafeData;
    use webauthn_rs::ephemeral::WebauthnEphemeralConfig;
    use webauthn_rs::proto::*;
    use webauthn_rs::Webauthn;
    pub const CHALLENGE_SIZE_BYTES: usize = 32;

    /*
    #[test]
    fn webauthn_authenticator_basic_registration() {
        let _ = env_logger::builder().is_test(true).try_init();
        let chal = CreationChallengeResponse {
            public_key: PublicKeyCredentialCreationOptions {
                rp: RelyingParty {
                    name: "WebauthenAuthticatorRs".to_string(),
                    id: "localhost".to_string(),
                },
                user: User {
                    id: Base64UrlSafeData(
                        base64::decode("d2lsbGlhbQ==").unwrap()
                    ),
                    name: "william".to_string(),
                    display_name: "William".to_string(),
                },
                challenge: Base64UrlSafeData(
                    (0..CHALLENGE_SIZE_BYTES).map(|_| 0).collect::<Vec<u8>>()
                ),
                pub_key_cred_params: vec![
                    PubKeyCredParams {
                        type_: "public-key".to_string(),
                        alg: -7,
                    }
                ],
                timeout: Some(60000),
                attestation: Some(AttestationConveyancePreference::None),
                exclude_credentials: None,
                authenticator_selection: Some(AuthenticatorSelectionCriteria{
                    authenticator_attachment: None,
                    require_resident_key: false,
                    user_verification: UserVerificationPolicy::Discouraged,
                }),
                extensions: None,
            }
        };

        let wa = WebauthnAuthenticator::new();
        let r = wa.do_registration("https://localhost", chal)
            .map_err(|e| {
                eprintln!("Error -> {:x?}", e);
                e
            })
            .expect("Failed to register");

    }
    */

    #[test]
    fn webauthn_authenticator_wan_u2fhid_interact() {
        let _ = env_logger::builder().is_test(true).try_init();

        let wan_c = WebauthnEphemeralConfig::new(
            "https://localhost:8080/auth",
            "https://localhost:8080",
            "localhost",
            None,
        );

        let mut wan = Webauthn::new(wan_c);

        let username = "william".to_string();

        let (chal, reg_state) = wan
            .generate_challenge_register(&username, Some(UserVerificationPolicy::Discouraged))
            .unwrap();

        println!("🍿 challenge -> {:x?}", chal);

        let mut wa = WebauthnAuthenticator::new(U2FHid::new());
        let r = wa
            .do_registration("https://localhost:8080", chal)
            .map_err(|e| {
                eprintln!("Error -> {:x?}", e);
                e
            })
            .expect("Failed to register");

        let cred = wan
            .register_credential(&r, reg_state, |_| Ok(false))
            .unwrap();

        let (chal, auth_state) = wan
            .generate_challenge_authenticate(vec![cred])
            .unwrap();

        let r = wa
            .do_authentication("https://localhost:8080", chal)
            .map_err(|e| {
                eprintln!("Error -> {:x?}", e);
                e
            })
            .expect("Failed to auth");

        let auth_res = wan
            .authenticate_credential(&r, auth_state)
            .expect("webauth authentication denied");
        log::debug!("auth_res -> {:x?}", auth_res);
    }

    #[test]
    fn webauthn_authenticator_wan_softtoken() {
        let _ = env_logger::builder().is_test(true).try_init();

        let wan_c = WebauthnEphemeralConfig::new(
            "https://localhost:8080/auth",
            "https://localhost:8080",
            "localhost",
            None,
        );

        let mut wan = Webauthn::new(wan_c);

        let username = "william".to_string();

        let (chal, reg_state) = wan
            .generate_challenge_register(&username, Some(UserVerificationPolicy::Discouraged))
            .unwrap();

        println!("🍿 challenge -> {:x?}", chal);

        let mut wa = WebauthnAuthenticator::new(U2FSoft::new());
        let r = wa
            .do_registration("https://localhost:8080", chal)
            .map_err(|e| {
                eprintln!("Error -> {:x?}", e);
                e
            })
            .expect("Failed to register");

        let cred = wan
            .register_credential(&r, reg_state, |_| Ok(false))
            .unwrap();

        let (chal, auth_state) = wan
            .generate_challenge_authenticate(vec![cred])
            .unwrap();

        let r = wa
            .do_authentication("https://localhost:8080", chal)
            .map_err(|e| {
                eprintln!("Error -> {:x?}", e);
                e
            })
            .expect("Failed to auth");

        let auth_res = wan
            .authenticate_credential(&r, auth_state)
            .expect("webauth authentication denied");
        log::debug!("auth_res -> {:x?}", auth_res);
    }
}