passkey-client 0.5.0

Webauthn client in Rust.
Documentation
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
use std::collections::HashMap;

use passkey_authenticator::extensions::HmacSecretConfig;
use passkey_types::{
    crypto::hmac_sha256,
    ctap2::{AuthenticatorData, Flags},
};

use super::*;

fn good_credential_creation_options_with_prf(
    eval: Option<webauthn::AuthenticationExtensionsPrfValues>,
) -> webauthn::CredentialCreationOptions {
    webauthn::CredentialCreationOptions {
        public_key: webauthn::PublicKeyCredentialCreationOptions {
            extensions: Some(webauthn::AuthenticationExtensionsClientInputs {
                prf: Some(webauthn::AuthenticationExtensionsPrfInputs {
                    eval,
                    eval_by_credential: None,
                }),
                ..Default::default()
            }),
            ..good_credential_creation_options()
        },
    }
}

#[tokio::test]
async fn registration_without_eval() {
    let auth = Authenticator::new(
        ctap2::Aaguid::new_empty(),
        MemoryStore::new(),
        uv_mock_with_creation(1),
    )
    .hmac_secret(HmacSecretConfig::new_without_uv());

    let mut client = Client::new(auth);

    let origin = Url::parse("https://future.1password.com").unwrap();
    let options = webauthn::CredentialCreationOptions {
        public_key: webauthn::PublicKeyCredentialCreationOptions {
            extensions: Some(webauthn::AuthenticationExtensionsClientInputs {
                prf: Some(webauthn::AuthenticationExtensionsPrfInputs {
                    eval: None,
                    eval_by_credential: None,
                }),
                ..Default::default()
            }),
            ..good_credential_creation_options()
        },
    };
    let cred = client
        .register(&origin, options, None)
        .await
        .expect("failed to register with options and prf ext");

    let prf_out = cred
        .client_extension_results
        .prf
        .expect("client extension results should contain PRF output");

    assert!(prf_out.enabled.expect("PRF should be enabled"));
    assert!(prf_out.results.is_none());
}

#[tokio::test]
async fn registration_with_single_input_eval() {
    let auth = Authenticator::new(
        ctap2::Aaguid::new_empty(),
        MemoryStore::new(),
        uv_mock_with_creation(1),
    )
    .hmac_secret(HmacSecretConfig::new_without_uv().enable_on_make_credential());
    let mut client = Client::new(auth);

    let first = vec![
        101, 195, 212, 161, 191, 112, 75, 189, 152, 52, 121, 17, 62, 113, 114, 164,
    ];

    let origin = Url::parse("https://future.1password.com").unwrap();
    let options = webauthn::CredentialCreationOptions {
        public_key: webauthn::PublicKeyCredentialCreationOptions {
            extensions: Some(webauthn::AuthenticationExtensionsClientInputs {
                prf: Some(webauthn::AuthenticationExtensionsPrfInputs {
                    eval: Some(webauthn::AuthenticationExtensionsPrfValues {
                        first: Bytes::from(first),
                        second: None,
                    }),
                    eval_by_credential: None,
                }),
                ..Default::default()
            }),
            ..good_credential_creation_options()
        },
    };
    let cred = client
        .register(&origin, options, None)
        .await
        .expect("failed to register with options and prf ext");

    let prf_out = cred
        .client_extension_results
        .prf
        .expect("client extension results should contain PRF output");

    assert!(prf_out.enabled.expect("PRF should be enabled"));
    // CTAP2's new extension hmac-secret-mc allows us to evaluate PRF inputs
    // at creation time. This is implemented by our in-memory authenticator.
    assert!(prf_out.results.is_some());
}

fn uv_mock_user_check_skip(times: usize) -> MockUserValidationMethod {
    let mut user_mock = MockUserValidationMethod::new();
    user_mock
        .expect_is_verification_enabled()
        .returning(|| Some(true));
    user_mock
        .expect_check_user()
        .with(
            mockall::predicate::always(),
            mockall::predicate::eq(true),
            mockall::predicate::eq(true),
        )
        .returning(|_, _, _| {
            Ok(UserCheck {
                presence: true,
                verification: true,
            })
        })
        .times(times - 1);
    user_mock.expect_is_presence_enabled().returning(|| true);
    user_mock
}

#[tokio::test]
async fn registration_with_eval_by_credential() {
    let auth = Authenticator::new(
        ctap2::Aaguid::new_empty(),
        MemoryStore::new(),
        uv_mock_user_check_skip(1),
    )
    .hmac_secret(HmacSecretConfig::new_without_uv());
    let mut client = Client::new(auth);

    let origin = Url::parse("https://future.1password.com").unwrap();
    let options = webauthn::CredentialCreationOptions {
        public_key: webauthn::PublicKeyCredentialCreationOptions {
            extensions: Some(webauthn::AuthenticationExtensionsClientInputs {
                prf: Some(webauthn::AuthenticationExtensionsPrfInputs {
                    eval: None,
                    eval_by_credential: Some(HashMap::new()),
                }),
                ..Default::default()
            }),
            ..good_credential_creation_options()
        },
    };
    let registration_res = client.register(&origin, options, None).await;

    assert!(matches!(
        registration_res,
        Err(WebauthnError::NotSupportedError)
    ));
}

#[cfg(test)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum PrfValuesConfig {
    None,
    One,
    Two,
}

impl PrfValuesConfig {
    fn build(&self) -> Option<webauthn::AuthenticationExtensionsPrfValues> {
        match self {
            PrfValuesConfig::None => None,
            PrfValuesConfig::One => Some(webauthn::AuthenticationExtensionsPrfValues {
                first: Bytes::from(random_vec(128)),
                second: None,
            }),
            PrfValuesConfig::Two => Some(webauthn::AuthenticationExtensionsPrfValues {
                first: Bytes::from(random_vec(128)),
                second: Some(Bytes::from(random_vec(128))),
            }),
        }
    }
}

#[cfg(test)]
macro_rules! valid_authentication_with_prf {
    ( $($test_name:ident: $eval:expr_2021, $eval_by_cred:expr_2021),+ ) => {
        $(
            #[tokio::test]
            async fn $test_name() {
                let auth = Authenticator::new(
                    ctap2::Aaguid::new_empty(),
                    MemoryStore::new(),
                    uv_mock_with_creation(2),
                )
                .hmac_secret(HmacSecretConfig::new_without_uv());
                let mut client = Client::new(auth);

                let origin = Url::parse("https://future.1password.com").unwrap();
                let eval = $eval.build();
                let eval_by_cred = $eval_by_cred.build();
                let options = good_credential_creation_options_with_prf(eval.clone().or_else(|| eval_by_cred.clone()));

                let cred = client
                    .register(&origin, options, None)
                    .await
                    .expect("failed to register with options");

                let auth_data = AuthenticatorData::from_slice(&cred.response.authenticator_data)
                    .expect("could not deserialize authenticator data");
                assert!(!auth_data.flags.contains(Flags::ED));

                let cred_id = cred.raw_id;

                let make_prf = cred
                    .client_extension_results
                    .prf;

                // prf should still be initialized if the dictionary key is present but has no value
                assert!(make_prf.is_some());
                assert_eq!(make_prf.unwrap().enabled, Some(true));

                let eval_by_credential = match eval_by_cred {
                    None => None,
                    Some(val) => Some(
                        [(String::from(cred_id.clone()), val)]
                            .into_iter()
                            .collect()
                    ),
                };

                let auth_options = webauthn::CredentialRequestOptions {
                    public_key: webauthn::PublicKeyCredentialRequestOptions {
                        extensions: Some(webauthn::AuthenticationExtensionsClientInputs {
                            prf: Some(webauthn::AuthenticationExtensionsPrfInputs {
                                eval,
                                eval_by_credential,
                            }),
                            ..Default::default()
                        }),
                        ..good_credential_request_options(cred_id)
                    }
                };

                let auth_res = client
                    .authenticate(&origin, auth_options, None)
                    .await
                    .expect("failed to authenticate with PRF input");

                let auth_data = AuthenticatorData::from_slice(&auth_res.response.authenticator_data)
                    .expect("could not deserialize authenticator data");
                assert!(!auth_data.flags.contains(Flags::ED));

                let prf_out = auth_res
                    .client_extension_results
                    .prf;

                // Base case: if no credentials were provided nor a fallback eval was given,
                // the PRF extension after authentication should be None.
                // NOTE: the [W3C spec](https://w3c.github.io/webauthn/#prf-extension) does not
                // explicitly say what must happen in this case, except that it should have
                // initialized the prf extension output to an empty directory at some point.
                // Instead of returning a Some(empty PRF outputs), our implementation sets
                // the prf field in client_extension_results to None directly.
                if $eval == PrfValuesConfig::None && $eval_by_cred == PrfValuesConfig::None {
                    assert!(prf_out.is_none());
                    return;
                }

                let prf_out = prf_out.expect("client extension results should contain PRF output");

                // Should only be present during registration
                assert!(prf_out.enabled.is_none());

                // Otherwise, there must at least be a single result.
                let prf_res = prf_out.results.expect("PRF output should contain results");

                // A PRF output must be non-empty
                assert!(!prf_res.first.is_empty());

                // If the input eval contains two values, we also expect the output from the
                // PRF extension to contain a second, non-empty result.
                match ($eval, $eval_by_cred) {
                    (PrfValuesConfig::Two, PrfValuesConfig::None) | (_, PrfValuesConfig::Two) => {
                        let second = prf_res.second.expect("PRF results should contain second result value");
                        assert!(!second.is_empty());
                    },
                    _ => {}
                }
            }
        )*
    };
}

valid_authentication_with_prf! {
    auth_empty_evals: PrfValuesConfig::None, PrfValuesConfig::None,
    auth_two_inputs_eval_by_credential: PrfValuesConfig::None, PrfValuesConfig::Two,
    auth_single_input_eval: PrfValuesConfig::One, PrfValuesConfig::None,
    auth_both_eval_and_eval_by_credential: PrfValuesConfig::One, PrfValuesConfig::Two
}

#[tokio::test]
async fn auth_empty_allow_credentials() {
    let auth = Authenticator::new(
        ctap2::Aaguid::new_empty(),
        MemoryStore::new(),
        uv_mock_user_check_skip(2),
    )
    .hmac_secret(HmacSecretConfig::new_without_uv());
    let mut client = Client::new(auth);

    let origin = Url::parse("https://future.1password.com").unwrap();
    let eval_by_cred = webauthn::AuthenticationExtensionsPrfValues {
        first: Bytes::from(random_vec(128)),
        second: None,
    };
    let options = good_credential_creation_options_with_prf(Some(eval_by_cred.clone()));

    let cred = client
        .register(&origin, options, None)
        .await
        .expect("failed to register with options");

    let cred_id = cred.raw_id;

    let auth_options = webauthn::CredentialRequestOptions {
        public_key: webauthn::PublicKeyCredentialRequestOptions {
            allow_credentials: None,
            extensions: Some(webauthn::AuthenticationExtensionsClientInputs {
                prf: Some(webauthn::AuthenticationExtensionsPrfInputs {
                    eval: None,
                    eval_by_credential: Some(
                        [(String::from(cred_id.clone()), eval_by_cred)]
                            .into_iter()
                            .collect(),
                    ),
                }),
                ..Default::default()
            }),
            ..good_credential_request_options(cred_id)
        },
    };

    let auth_res = client.authenticate(&origin, auth_options, None).await;

    // See https://w3c.github.io/webauthn/#prf-extension
    //   - Client extension processing (authentication)
    //     - (1)
    assert!(matches!(auth_res, Err(WebauthnError::NotSupportedError)));
}

#[cfg(test)]
macro_rules! invalid_eval_by_credential_in_authentication {
    ( $($test_name:ident: $key:expr_2021 ),+ ) => {
        $(
            #[tokio::test]
            async fn $test_name() {
                let auth = Authenticator::new(
                    ctap2::Aaguid::new_empty(),
                    MemoryStore::new(),
                    uv_mock_user_check_skip(2),
                )
                .hmac_secret(HmacSecretConfig::new_without_uv());
                let mut client = Client::new(auth);

                let eval_by_cred = webauthn::AuthenticationExtensionsPrfValues {
                    first: Bytes::from(random_vec(128)),
                    second: None,
                };

                let origin = Url::parse("https://future.1password.com").unwrap();
                let options = good_credential_creation_options_with_prf(Some(eval_by_cred.clone()));

                let cred = client
                    .register(&origin, options, None)
                    .await
                    .expect("failed to register with options");

                let cred_id = cred.raw_id;

                let auth_options = webauthn::CredentialRequestOptions {
                    public_key: webauthn::PublicKeyCredentialRequestOptions {
                        extensions: Some(webauthn::AuthenticationExtensionsClientInputs {
                            prf: Some(webauthn::AuthenticationExtensionsPrfInputs {
                                eval: None,
                                eval_by_credential: Some(
                                    [(
                                        $key,
                                        eval_by_cred
                                    )]
                                    .into_iter()
                                    .collect(),
                                ),
                            }),
                            ..Default::default()
                        }),
                        ..good_credential_request_options(cred_id)
                    },
                };

                let auth_res = client.authenticate(&origin, auth_options, None).await;

                // See https://w3c.github.io/webauthn/#prf-extension
                //   - Client extension processing (authentication)
                //     - (2)
                assert!(matches!(auth_res, Err(WebauthnError::SyntaxError)));
            }
        )*
    };
}

invalid_eval_by_credential_in_authentication! {
    auth_empty_key_in_eval_by_credential: String::from(""),
    auth_invalid_base64url_key_in_eval_by_credential: String::from("xyz"),
    auth_no_matching_credential_id_in_allow_credentials: String::from(Bytes::from(random_vec(64)))
}

#[cfg(test)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum SameInputs {
    Yes,
    No,
}

#[cfg(test)]
macro_rules! compare_auth_calls {
    ( $($test_name:ident: $same_inputs:expr_2021),+ ) => {
        $(
            #[tokio::test]
            async fn $test_name() {
                let auth = Authenticator::new(
                    ctap2::Aaguid::new_empty(),
                    MemoryStore::new(),
                    uv_mock_with_creation(3),
                )
                .hmac_secret(HmacSecretConfig::new_without_uv());
                let mut client = Client::new(auth);

                let mut first = Bytes::from(random_vec(128));
                let mut second = Some(Bytes::from(random_vec(128)));

                let eval_by_cred = webauthn::AuthenticationExtensionsPrfValues {
                    first: first.clone(),
                    second: second.clone(),
                };

                let origin = Url::parse("https://future.1password.com").unwrap();
                let options = good_credential_creation_options_with_prf(Some(eval_by_cred.clone()));

                let cred = client
                    .register(&origin, options, None)
                    .await
                    .expect("failed to register with options");

                let cred_id = cred.raw_id;


                let auth_options = webauthn::CredentialRequestOptions {
                    public_key: webauthn::PublicKeyCredentialRequestOptions {
                        extensions: Some(webauthn::AuthenticationExtensionsClientInputs {
                            prf: Some(webauthn::AuthenticationExtensionsPrfInputs {
                                eval: None,
                                eval_by_credential: Some(
                                    [(
                                        String::from(cred_id.clone()),
                                        eval_by_cred
                                    )]
                                    .into_iter()
                                    .collect(),
                                ),
                            }),
                            ..Default::default()
                        }),
                        ..good_credential_request_options(cred_id.clone())
                    },
                };

                let auth_res_a = client
                    .authenticate(&origin, auth_options, None)
                    .await
                    .expect("failed to authenticate with PRF input");

                if $same_inputs == SameInputs::No {
                    first = Bytes::from(random_vec(128));
                    second = Some(Bytes::from(random_vec(128)));
                }

                let auth_options = webauthn::CredentialRequestOptions {
                    public_key: webauthn::PublicKeyCredentialRequestOptions {
                        extensions: Some(webauthn::AuthenticationExtensionsClientInputs {
                            prf: Some(webauthn::AuthenticationExtensionsPrfInputs {
                                eval: None,
                                eval_by_credential: Some(
                                    [(
                                        String::from(cred_id.clone()),
                                        webauthn::AuthenticationExtensionsPrfValues { first, second },
                                    )]
                                    .into_iter()
                                    .collect(),
                                ),
                            }),
                            ..Default::default()
                        }),
                        ..good_credential_request_options(cred_id)
                    },
                };

                let auth_res_b = client
                    .authenticate(&origin, auth_options, None)
                    .await
                    .expect("failed to authenticate with PRF input");

                let prf_results_a = auth_res_a
                    .client_extension_results
                    .prf
                    .expect("client extension results should contain PRF output")
                    .results
                    .expect("PRF output should contain results");
                let prf_results_b = auth_res_b
                    .client_extension_results
                    .prf
                    .expect("client extension results should contain PRF output")
                    .results
                    .expect("PRF output should contain results");

                match $same_inputs {
                    SameInputs::Yes => {
                        assert_eq!(prf_results_a.first, prf_results_b.first);
                        assert_eq!(prf_results_a.second, prf_results_b.second);
                    },
                    SameInputs::No => {
                        assert_ne!(prf_results_a.first, prf_results_b.first);
                        assert_ne!(prf_results_a.second, prf_results_b.second);
                    }
                }
            }
        )+
    }
}

compare_auth_calls! {
    auth_same_inputs_should_give_same_outputs: SameInputs::Yes,
    auth_different_inputs_should_give_different_outputs: SameInputs::No
}

#[tokio::test]
async fn registration_and_authentication_with_unsupported_authenticator_ignores_prf() {
    let auth = Authenticator::new(
        ctap2::Aaguid::new_empty(),
        MemoryStore::new(),
        uv_mock_with_creation(2),
    );
    let mut client = Client::new(auth);

    let origin = Url::parse("https://future.1password.com").unwrap();
    let eval = PrfValuesConfig::Two.build();
    let options = good_credential_creation_options_with_prf(eval.clone());

    let cred = client
        .register(&origin, options, None)
        .await
        .expect("failed to register with options");
    assert!(cred.client_extension_results.prf.is_none());
    let auth_data = AuthenticatorData::from_slice(&cred.response.authenticator_data)
        .expect("could not decode Authenticator Data");
    assert!(auth_data.extensions.is_none());
    assert!(!auth_data.flags.contains(Flags::ED));

    let auth_options = webauthn::CredentialRequestOptions {
        public_key: webauthn::PublicKeyCredentialRequestOptions {
            extensions: Some(webauthn::AuthenticationExtensionsClientInputs {
                prf: Some(webauthn::AuthenticationExtensionsPrfInputs {
                    eval,
                    eval_by_credential: None,
                }),
                ..Default::default()
            }),
            ..good_credential_request_options(cred.raw_id)
        },
    };

    let auth_res = client
        .authenticate(&origin, auth_options, None)
        .await
        .expect("failed to authenticate with PRF input");

    assert!(auth_res.client_extension_results.prf.is_none());
    let auth_data = AuthenticatorData::from_slice(&auth_res.response.authenticator_data)
        .expect("could not decode Authenticator Data");
    assert!(auth_data.extensions.is_none());
    assert!(!auth_data.flags.contains(Flags::ED));
}

#[tokio::test]
async fn empty_extension_and_no_hmac_secret_support() {
    let auth = Authenticator::new(
        ctap2::Aaguid::new_empty(),
        MemoryStore::new(),
        uv_mock_with_creation(2),
    );
    let mut client = Client::new(auth);

    let origin = Url::parse("https://future.1password.com").unwrap();
    let options = webauthn::CredentialCreationOptions {
        public_key: webauthn::PublicKeyCredentialCreationOptions {
            extensions: Some(Default::default()),
            ..good_credential_creation_options()
        },
    };

    let cred = client
        .register(&origin, options, None)
        .await
        .expect("failed to register with options");
    assert!(cred.client_extension_results.prf.is_none());
    let auth_data = AuthenticatorData::from_slice(&cred.response.authenticator_data)
        .expect("could not decode Authenticator Data");
    assert!(auth_data.extensions.is_none());
    assert!(!auth_data.flags.contains(Flags::ED));

    let auth_options = webauthn::CredentialRequestOptions {
        public_key: webauthn::PublicKeyCredentialRequestOptions {
            extensions: Some(Default::default()),
            ..good_credential_request_options(cred.raw_id)
        },
    };

    let auth_res = client
        .authenticate(&origin, auth_options, None)
        .await
        .expect("failed to authenticate");

    assert!(auth_res.client_extension_results.prf.is_none());
    let auth_data = AuthenticatorData::from_slice(&auth_res.response.authenticator_data)
        .expect("could not decode Authenticator Data");
    assert!(auth_data.extensions.is_none());
    assert!(!auth_data.flags.contains(Flags::ED));
}

#[tokio::test]
async fn empty_extension_with_hmac_secret_support() {
    let auth = Authenticator::new(
        ctap2::Aaguid::new_empty(),
        MemoryStore::new(),
        uv_mock_with_creation(2),
    )
    .hmac_secret(HmacSecretConfig::new_without_uv());
    let mut client = Client::new(auth);

    let origin = Url::parse("https://future.1password.com").unwrap();
    let options = webauthn::CredentialCreationOptions {
        public_key: webauthn::PublicKeyCredentialCreationOptions {
            extensions: Some(Default::default()),
            ..good_credential_creation_options()
        },
    };

    let cred = client
        .register(&origin, options, None)
        .await
        .expect("failed to register with options");
    assert!(cred.client_extension_results.prf.is_none());
    let auth_data = AuthenticatorData::from_slice(&cred.response.authenticator_data)
        .expect("could not decode Authenticator Data");
    assert!(auth_data.extensions.is_none());
    assert!(!auth_data.flags.contains(Flags::ED));

    let auth_options = webauthn::CredentialRequestOptions {
        public_key: webauthn::PublicKeyCredentialRequestOptions {
            extensions: Some(Default::default()),
            ..good_credential_request_options(cred.raw_id)
        },
    };
    let auth_res = client
        .authenticate(&origin, auth_options, None)
        .await
        .expect("failed to authenticate");

    assert!(auth_res.client_extension_results.prf.is_none());
    let auth_data = AuthenticatorData::from_slice(&auth_res.response.authenticator_data)
        .expect("could not decode Authenticator Data");
    assert!(auth_data.extensions.is_none());
    assert!(!auth_data.flags.contains(Flags::ED));
}

// When evalByCredential contains credential ID not registered with the authenticator,
// it should never use those values as input to the salts sent to the authenicator's
// hmac-secret extension.
#[tokio::test]
async fn two_eval_by_credential_entries() {
    let auth = Authenticator::new(
        ctap2::Aaguid::new_empty(),
        MemoryStore::new(),
        uv_mock_with_creation(3),
    )
    .hmac_secret(HmacSecretConfig::new_without_uv());
    let mut client = Client::new(auth);

    let eval_values = webauthn::AuthenticationExtensionsPrfValues {
        first: Bytes::from(random_vec(128)),
        second: Some(Bytes::from(random_vec(128))),
    };

    let origin = Url::parse("https://future.1password.com").unwrap();
    let options = good_credential_creation_options_with_prf(Some(eval_values.clone()));

    let cred = client
        .register(&origin, options, None)
        .await
        .expect("failed to register with options");

    let cred_id = cred.raw_id;

    let auth_options = webauthn::CredentialRequestOptions {
        public_key: webauthn::PublicKeyCredentialRequestOptions {
            extensions: Some(webauthn::AuthenticationExtensionsClientInputs {
                prf: Some(webauthn::AuthenticationExtensionsPrfInputs {
                    eval: None,
                    eval_by_credential: Some(
                        [(String::from(cred_id.clone()), eval_values.clone())]
                            .into_iter()
                            .collect(),
                    ),
                }),
                ..Default::default()
            }),
            ..good_credential_request_options(cred_id.clone())
        },
    };

    let auth_res_control = client
        .authenticate(&origin, auth_options, None)
        .await
        .expect("failed to authenticate with PRF input");

    let eval_values_2 = webauthn::AuthenticationExtensionsPrfValues {
        first: Bytes::from(random_vec(128)),
        second: Some(Bytes::from(random_vec(128))),
    };

    let mut cred_id_2 = cred_id.clone();
    cred_id_2.reverse();

    // Include an entry referencing a credential ID that does not exist
    // on the authenticator. The implementation should always pick the
    // eval input from the credential ID it has registered.
    let auth_options = webauthn::CredentialRequestOptions {
        public_key: webauthn::PublicKeyCredentialRequestOptions {
            allow_credentials: Some(vec![
                webauthn::PublicKeyCredentialDescriptor {
                    ty: webauthn::PublicKeyCredentialType::PublicKey,
                    id: cred_id_2.clone(),
                    transports: None,
                },
                webauthn::PublicKeyCredentialDescriptor {
                    ty: webauthn::PublicKeyCredentialType::PublicKey,
                    id: cred_id.clone(),
                    transports: None,
                },
            ]),
            extensions: Some(webauthn::AuthenticationExtensionsClientInputs {
                prf: Some(webauthn::AuthenticationExtensionsPrfInputs {
                    eval: None,
                    eval_by_credential: Some(
                        [
                            (String::from(cred_id_2.clone()), eval_values_2),
                            (String::from(cred_id.clone()), eval_values),
                        ]
                        .into_iter()
                        .collect(),
                    ),
                }),
                ..Default::default()
            }),
            ..good_credential_request_options(cred_id.clone())
        },
    };

    let auth_res_treatment = client
        .authenticate(&origin, auth_options, None)
        .await
        .expect("failed to authenticate with PRF input");

    let treatment_prf_res = auth_res_treatment
        .client_extension_results
        .prf
        .expect("should have PRF extension results")
        .results
        .expect("should have PRF extension outputs");

    let control_prf_res = auth_res_control
        .client_extension_results
        .prf
        .expect("should have PRF extension results")
        .results
        .expect("should have PRF extension outputs");

    assert_eq!(treatment_prf_res.first, control_prf_res.first);
    assert_eq!(treatment_prf_res.second, control_prf_res.second);
}

#[tokio::test]
async fn prf_already_hashed_does_not_hash_again() {
    let salt = [2; 32];

    let hashed_salt = sha256(&[b"WebAuthn PRF".as_slice(), &[0x00], salt.as_slice()].concat());

    let origin = Url::parse("https://future.1password.com").unwrap();

    let auth = Authenticator::new(ctap2::Aaguid::new_empty(), None, uv_mock_with_creation(2))
        .hmac_secret(HmacSecretConfig::new_without_uv().enable_on_make_credential());
    let mut client = Client::new(auth);
    let create_request = webauthn::CredentialCreationOptions {
        public_key: webauthn::PublicKeyCredentialCreationOptions {
            extensions: Some(webauthn::AuthenticationExtensionsClientInputs {
                prf_already_hashed: Some(webauthn::AuthenticationExtensionsPrfInputs {
                    eval: Some(webauthn::AuthenticationExtensionsPrfValues {
                        first: hashed_salt.as_slice().into(),
                        second: None,
                    }),
                    eval_by_credential: None,
                }),
                ..Default::default()
            }),
            ..good_credential_creation_options()
        },
    };
    let created = client
        .register(&origin, create_request, None)
        .await
        .expect("could not register a new passkey with PRF already hashed");

    let passkey = client
        .authenticator
        .store()
        .clone()
        .expect("no passkey was stored after its creation");

    let hmac_secret = passkey
        .extensions
        .hmac_secret
        .as_ref()
        .expect("no HMAC secret was created with PRF already hashed")
        .cred_with_uv
        .clone();

    let expected_output = hmac_sha256(&hmac_secret, &hashed_salt);

    let prf_results = created
        .client_extension_results
        .prf
        .expect("no PRF was returned")
        .results
        .expect("no results were returned with make credential support");
    assert_eq!(prf_results.first.as_slice(), expected_output.as_slice());

    let request = webauthn::CredentialRequestOptions {
        public_key: webauthn::PublicKeyCredentialRequestOptions {
            allow_credentials: None,
            extensions: Some(webauthn::AuthenticationExtensionsClientInputs {
                prf_already_hashed: Some(webauthn::AuthenticationExtensionsPrfInputs {
                    eval: Some(webauthn::AuthenticationExtensionsPrfValues {
                        first: hashed_salt.as_slice().into(),
                        second: None,
                    }),
                    eval_by_credential: None,
                }),
                ..Default::default()
            }),
            ..good_credential_request_options(vec![])
        },
    };

    let response = client
        .authenticate(&origin, request, None)
        .await
        .expect("could not authenticate with PRF already hashed");

    let prf = response
        .client_extension_results
        .prf
        .expect("no PRF output was provided");

    let prf_results = prf
        .results
        .expect("no PRF results were included in the output");

    assert_eq!(prf_results.first.as_slice(), expected_output.as_slice());
}

#[tokio::test]
async fn prf_takes_precedence_over_prf_already_hashed() {
    let salt = [2; 32];

    let hashed_salt = sha256(&[b"WebAuthn PRF".as_slice(), &[0x00], salt.as_slice()].concat());

    let origin = Url::parse("https://future.1password.com").unwrap();

    let auth = Authenticator::new(ctap2::Aaguid::new_empty(), None, uv_mock_with_creation(2))
        .hmac_secret(HmacSecretConfig::new_without_uv().enable_on_make_credential());
    let mut client = Client::new(auth);
    let create_request = webauthn::CredentialCreationOptions {
        public_key: webauthn::PublicKeyCredentialCreationOptions {
            extensions: Some(webauthn::AuthenticationExtensionsClientInputs {
                prf_already_hashed: Some(webauthn::AuthenticationExtensionsPrfInputs {
                    eval: Some(webauthn::AuthenticationExtensionsPrfValues {
                        first: hashed_salt.as_slice().into(),
                        second: None,
                    }),
                    eval_by_credential: None,
                }),
                ..Default::default()
            }),
            ..good_credential_creation_options()
        },
    };
    let created = client
        .register(&origin, create_request, None)
        .await
        .expect("could not register a new passkey with PRF already hashed");

    let passkey = client
        .authenticator
        .store()
        .clone()
        .expect("no passkey was stored after its creation");

    let hmac_secret = passkey
        .extensions
        .hmac_secret
        .as_ref()
        .expect("no HMAC secret was created with PRF already hashed")
        .cred_with_uv
        .clone();

    let expected_output = hmac_sha256(&hmac_secret, &hashed_salt);

    let prf_results = created
        .client_extension_results
        .prf
        .expect("no PRF was returned")
        .results
        .expect("no results were returned with make credential support");
    assert_eq!(prf_results.first.as_slice(), expected_output.as_slice());

    let request = webauthn::CredentialRequestOptions {
        public_key: webauthn::PublicKeyCredentialRequestOptions {
            allow_credentials: None,
            extensions: Some(webauthn::AuthenticationExtensionsClientInputs {
                prf: Some(webauthn::AuthenticationExtensionsPrfInputs {
                    eval: Some(webauthn::AuthenticationExtensionsPrfValues {
                        first: salt.as_slice().into(),
                        second: None,
                    }),
                    eval_by_credential: None,
                }),
                prf_already_hashed: Some(webauthn::AuthenticationExtensionsPrfInputs {
                    eval: Some(webauthn::AuthenticationExtensionsPrfValues {
                        // Input nonsense here so if it is selected it fails
                        first: [3; 32].as_slice().into(),
                        second: None,
                    }),
                    eval_by_credential: None,
                }),
                ..Default::default()
            }),
            ..good_credential_request_options(vec![])
        },
    };

    let response = client
        .authenticate(&origin, request, None)
        .await
        .expect("could not authenticate with PRF already hashed");

    let prf = response
        .client_extension_results
        .prf
        .expect("no PRF output was provided");

    let prf_results = prf
        .results
        .expect("no PRF results were included in the output");

    assert_eq!(prf_results.first.as_slice(), expected_output.as_slice());
}