alibi-plugins 0.2.0

Built-in authentication plugins and API metadata for Alibi
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
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
mod authentication;
mod options;
pub use options::{
    PasskeyAuthenticatorSelection, PasskeyExtensions, PasskeyExtensionsResolver,
    PasskeyOptionsContext,
};

pub(super) mod handlers;

mod raw_none;
mod source;

mod registration;

pub(super) mod types;

pub(super) mod webauthn;

use alibi_core::utils::cookie_utils::create_session_cookie;
use alibi_core::{AuthContext, AuthError, AuthResult};
use alibi_core::{AuthRequest, AuthResponse};
pub use authentication::{
    AuthenticationResult, PasskeyAuthenticationAfterVerification, PasskeyAuthenticationConfig,
    PasskeyAuthenticationContext, VerifiedPasskeyAuthentication,
};
use handlers::{
    PasskeyHandlerOutcome, delete_passkey_core, generate_authenticate_options_core,
    generate_register_options_core, list_user_passkeys_core, update_passkey_core,
    verify_authentication_core, verify_registration_core,
};
pub use registration::{
    PasskeyRegistrationAfterVerification, PasskeyRegistrationConfig, PasskeyRegistrationContext,
    PasskeyRegistrationOverride, PasskeyRegistrationUser, PasskeyUserResolver,
    VerifiedPasskeyRegistration,
};
use types::{
    DeletePasskeyRequest, UpdatePasskeyRequest, VerifyAuthenticationRequest,
    VerifyRegistrationRequest,
};

/// Passkey / `WebAuthn` authentication plugin.
///
/// Generates WebAuthn-compatible registration and authentication options,
/// stores challenge state via the auth store, and manages passkey CRUD.
pub struct PasskeyPlugin {
    config: PasskeyConfig,
}

#[derive(Debug, Clone, alibi_core::PluginConfig)]
#[plugin(name = "PasskeyPlugin")]
pub struct PasskeyConfig {
    #[config(default = String::new())]
    pub rp_id: String,
    #[config(default = "Better Auth".to_owned())]
    pub rp_name: String,
    #[config(default = String::new())]
    pub origin: String,
    /// Additional explicit origins accepted by signed ceremonies.
    #[config(default = Vec::new())]
    pub origins: Vec<String>,
    #[config(default = PasskeyAuthenticatorSelection::default())]
    pub authenticator_selection: PasskeyAuthenticatorSelection,
    #[config(default = 300)]
    pub challenge_ttl_secs: i64,
    #[config(default = "better-auth-passkey".to_owned())]
    pub web_authn_challenge_cookie: String,
    /// Per-format PEM roots, matching the verifier `SettingsService`.
    /// Unspecified formats retain published defaults.
    #[config(default = None)]
    pub attestation_root_certificates: Option<std::collections::BTreeMap<String, Vec<String>>>,
    #[config(default = PasskeyRegistrationConfig::default())]
    pub registration: PasskeyRegistrationConfig,
    #[config(default = PasskeyAuthenticationConfig::default())]
    pub authentication: PasskeyAuthenticationConfig,
}

impl PasskeyPlugin {
    /// GET /passkey/generate-register-options
    async fn handle_generate_register_options(
        &self,
        req: &AuthRequest,
        ctx: &AuthContext<impl alibi_core::AuthSchema>,
    ) -> AuthResult<AuthResponse> {
        use alibi_core::AuthUser;
        let session = self.registration_session(req, ctx).await?;
        let projected_user = session.as_ref().map(|(user, _)| ctx.user_view(user));
        let user = if let Some((user, _)) = session {
            let id = user.id().into_owned();
            let name = user
                .email()
                .filter(|email| !email.is_empty())
                .unwrap_or(&id)
                .to_owned();
            PasskeyRegistrationUser {
                id,
                name: name.clone(),
                display_name: Some(name),
            }
        } else {
            let Some(resolver) = &self.config.registration.resolve_user else {
                return Ok(AuthResponse::json(
                    400,
                    &serde_json::json!({"code":"RESOLVE_USER_REQUIRED","message":"Passkey registration requires either an authenticated session or a resolveUser callback when requireSession is false"}),
                )?);
            };
            let context = PasskeyRegistrationContext {
                request: req,
                auth_config: &ctx.config,
                extensions: &ctx.extensions,
            };
            match resolver
                .resolve_user(&context, req.query.get("context").map(String::as_str))
                .await
            {
                Ok(Some(user)) if !user.id.is_empty() && !user.name.is_empty() => user,
                Ok(_) => {
                    return Ok(AuthResponse::json(
                        400,
                        &serde_json::json!({"code":"RESOLVED_USER_INVALID","message":"Resolved user is invalid"}),
                    )?);
                }
                Err(error) if registration::is_application_error(&error) => return Err(error),
                Err(_) => return Ok(AuthResponse::new(500)),
            }
        };
        let extensions = if let Some(input) = &self.config.registration.extensions {
            Some(
                input
                    .resolve(&PasskeyOptionsContext {
                        request: req,
                        auth_config: &ctx.config,
                        extensions: &ctx.extensions,
                        user: projected_user.as_ref(),
                    })
                    .await?,
            )
        } else {
            None
        };
        let passkey_name = req.query.get("name").map(String::as_str);
        let authenticator_attachment = req.query.get("authenticatorAttachment").map(String::as_str);
        let (result, cookie_header) = generate_register_options_core(
            &user,
            req.query.get("context").map(String::as_str),
            passkey_name,
            authenticator_attachment,
            extensions,
            &self.config,
            ctx,
        )
        .await?;
        Ok(AuthResponse::json(200, &result)?.with_header("Set-Cookie", cookie_header))
    }

    /// POST /passkey/verify-registration
    async fn handle_verify_registration(
        &self,
        req: &AuthRequest,
        ctx: &AuthContext<impl alibi_core::AuthSchema>,
    ) -> AuthResult<AuthResponse> {
        let body: VerifyRegistrationRequest = match alibi_core::validate_request_body(req) {
            Ok(v) => v,
            Err(resp) => return Ok(resp),
        };
        let session = if self.config.registration.require_session {
            self.registration_session(req, ctx).await?
        } else {
            None
        };
        let owner_id = session
            .as_ref()
            .map(|(user, _)| alibi_core::AuthUser::id(user).into_owned());
        match verify_registration_core(&body, req, owner_id.as_deref(), &self.config, ctx).await? {
            PasskeyHandlerOutcome::Success(result) => {
                let token = result
                    .get("session")
                    .and_then(|session_2| session_2.get("token"))
                    .and_then(serde_json::Value::as_str);
                let response = AuthResponse::json(200, &result)?;
                if let Some(token) = token {
                    use alibi_core::utils::cookie_utils::{
                        create_session_cookie_with_max_age, create_session_like_cookie,
                        related_cookie_name, sign_cookie_value, verify_cookie_value,
                    };
                    let preference = related_cookie_name(&ctx.config, "dont_remember");
                    let dont_remember = crate::helpers::get_cookie(req, &preference)
                        .and_then(|value| verify_cookie_value(&value, ctx.config.current_secret()))
                        .is_some_and(|value| !value.is_empty());
                    let mut response = response.with_appended_header(
                        "Set-Cookie",
                        create_session_cookie_with_max_age(
                            Some(token),
                            if dont_remember {
                                None
                            } else {
                                Some(ctx.config.session.expires_in.num_seconds())
                            },
                            &ctx.config,
                        )?,
                    );
                    if dont_remember {
                        response.headers.append(
                            "Set-Cookie",
                            create_session_like_cookie(
                                &preference,
                                &sign_cookie_value("true", ctx.config.current_secret()),
                                None,
                                &ctx.config,
                            )?,
                        );
                    }
                    Ok(response)
                } else {
                    Ok(response)
                }
            }
            PasskeyHandlerOutcome::Response(response) => Ok(response),
        }
    }

    async fn registration_session<S: alibi_core::AuthSchema>(
        &self,
        req: &AuthRequest,
        ctx: &AuthContext<S>,
    ) -> AuthResult<
        Option<(
            alibi_core::AuthenticatedUser<S>,
            alibi_core::wire::SessionView,
        )>,
    > {
        let session = match ctx.require_cached_session(req).await {
            Ok(session) => Some(session),
            Err(AuthError::Unauthenticated | AuthError::SessionNotFound)
                if !self.config.registration.require_session =>
            {
                None
            }
            Err(error) => return Err(error),
        };
        if self.config.registration.require_session
            && let Some((_, session)) = &session
            && !ctx.session_manager().is_session_fresh(session)
        {
            return Err(AuthError::Upstream {
                status: 403,
                code: "SESSION_NOT_FRESH",
                message: "Session is not fresh",
            });
        }
        Ok(session)
    }

    /// GET /passkey/generate-authenticate-options
    async fn handle_generate_authenticate_options(
        &self,
        req: &AuthRequest,
        ctx: &AuthContext<impl alibi_core::AuthSchema>,
    ) -> AuthResult<AuthResponse> {
        let maybe_user = ctx.require_cached_session(req).await.ok().map(|(u, _)| u);
        let projected_user = maybe_user.as_ref().map(|user| ctx.user_view(user));
        let extensions = if let Some(input) = &self.config.authentication.extensions {
            Some(
                input
                    .resolve(&PasskeyOptionsContext {
                        request: req,
                        auth_config: &ctx.config,
                        extensions: &ctx.extensions,
                        user: projected_user.as_ref(),
                    })
                    .await?,
            )
        } else {
            None
        };
        let (result, cookie_header) =
            generate_authenticate_options_core(maybe_user.as_ref(), extensions, &self.config, ctx)
                .await?;
        Ok(AuthResponse::json(200, &result)?.with_header("Set-Cookie", cookie_header))
    }

    /// POST /passkey/verify-authentication
    async fn handle_verify_authentication(
        &self,
        req: &AuthRequest,
        ctx: &AuthContext<impl alibi_core::AuthSchema>,
    ) -> AuthResult<AuthResponse> {
        let body: VerifyAuthenticationRequest = match alibi_core::validate_request_body(req) {
            Ok(v) => v,
            Err(resp) => return Ok(resp),
        };
        let metadata = alibi_core::RequestMeta::from_request(req);
        match verify_authentication_core(
            &body,
            req,
            &self.config,
            metadata.ip_address,
            metadata.user_agent,
            ctx,
        )
        .await?
        {
            PasskeyHandlerOutcome::Success((response, token)) => {
                let cookie_header = create_session_cookie(&token, &ctx.config)?;
                Ok(AuthResponse::json(200, &response)?.with_header("Set-Cookie", cookie_header))
            }
            PasskeyHandlerOutcome::Response(response) => Ok(response),
        }
    }

    /// GET /passkey/list-user-passkeys
    async fn handle_list_user_passkeys(
        &self,
        req: &AuthRequest,
        ctx: &AuthContext<impl alibi_core::AuthSchema>,
    ) -> AuthResult<AuthResponse> {
        let (user, _session) = super::helpers::ordinary_session(req, ctx).await?;
        let result = list_user_passkeys_core(&user, ctx).await?;
        AuthResponse::json(200, &result).map_err(AuthError::from)
    }

    /// POST /passkey/delete-passkey
    async fn handle_delete_passkey(
        &self,
        req: &AuthRequest,
        ctx: &AuthContext<impl alibi_core::AuthSchema>,
    ) -> AuthResult<AuthResponse> {
        let (user, _session) = super::helpers::ordinary_session(req, ctx).await?;
        let body: DeletePasskeyRequest = match alibi_core::validate_request_body(req) {
            Ok(v) => v,
            Err(resp) => return Ok(resp),
        };
        match delete_passkey_core(&body, &user, ctx).await? {
            PasskeyHandlerOutcome::Success(result) => {
                AuthResponse::json(200, &result).map_err(AuthError::from)
            }
            PasskeyHandlerOutcome::Response(response) => Ok(response),
        }
    }

    /// POST /passkey/update-passkey
    async fn handle_update_passkey(
        &self,
        req: &AuthRequest,
        ctx: &AuthContext<impl alibi_core::AuthSchema>,
    ) -> AuthResult<AuthResponse> {
        let (user, _session) = super::helpers::ordinary_session(req, ctx).await?;
        let body: UpdatePasskeyRequest = match alibi_core::validate_request_body(req) {
            Ok(v) => v,
            Err(resp) => return Ok(resp),
        };
        match update_passkey_core(&body, &user, ctx).await? {
            PasskeyHandlerOutcome::Success(result) => {
                AuthResponse::json(200, &result).map_err(AuthError::from)
            }
            PasskeyHandlerOutcome::Response(response) => Ok(response),
        }
    }
}

alibi_core::impl_auth_plugin! {
    PasskeyPlugin, "passkey";
    routes {
        get  "/passkey/generate-register-options"      => handle_generate_register_options,      "passkey_generate_register_options";
        post "/passkey/verify-registration"            => handle_verify_registration,            "passkey_verify_registration";
        get  "/passkey/generate-authenticate-options"  => handle_generate_authenticate_options,  "passkey_generate_authenticate_options";
        post "/passkey/verify-authentication"          => handle_verify_authentication,          "passkey_verify_authentication";
        get  "/passkey/list-user-passkeys"             => handle_list_user_passkeys,             "passkey_list_user_passkeys";
        post "/passkey/delete-passkey"                 => handle_delete_passkey,                 "passkey_delete_passkey";
        post "/passkey/update-passkey"                 => handle_update_passkey,                 "passkey_update_passkey";
    }

 extra {
    route_openapi_metadata!(S);
 }
}

impl std::fmt::Debug for PasskeyPlugin {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PasskeyPlugin").finish_non_exhaustive()
    }
}

// LCOV_EXCL_START
#[cfg(test)]
mod tests {
    mod revocation;
    use super::*;
    use crate::test_helpers;
    use alibi_core::{CreatePasskey, CreateUser, HttpMethod};
    use base64::Engine;
    use base64::engine::general_purpose::URL_SAFE_NO_PAD;
    use chrono::Duration;
    use std::collections::HashMap;
    use std::fmt::Write;

    fn passkey_plugin() -> PasskeyPlugin {
        PasskeyPlugin::new()
            .rp_id("localhost")
            .rp_name("Better Auth Test")
            .origin("http://localhost:3000")
    }

    fn cookie_header(response: &AuthResponse) -> &str {
        response
            .headers
            .get("Set-Cookie")
            .expect("response should include a Set-Cookie header")
    }

    fn credential_id(label: &str) -> String {
        URL_SAFE_NO_PAD.encode(label.as_bytes())
    }

    #[test]
    fn test_extract_passkey_snapshot_fields_requires_all_expected_fields() {
        let value = serde_json::json!({
            "cred": {
                "counter": 7,
                "backup_state": true
            }
        });

        let err = webauthn::extract_passkey_snapshot_fields(&value).unwrap_err();
        assert_eq!(
            err.to_string(),
            "Internal server error: Stored passkey JSON missing backup_eligible"
        );
    }

    #[test]
    fn test_extract_passkey_snapshot_fields_reads_expected_values() {
        let value = serde_json::json!({
            "cred": {
                "counter": 11,
                "backup_state": true,
                "backup_eligible": false
            }
        });

        let (counter, backed_up, backup_eligible) =
            webauthn::extract_passkey_snapshot_fields(&value).unwrap();
        assert_eq!(counter, 11);
        assert!(backed_up);
        assert!(!backup_eligible);
    }

    #[tokio::test]
    async fn test_generate_register_options_sets_cookie_and_uses_query_name() {
        let plugin = passkey_plugin();
        let (ctx, user, session) = test_helpers::create_test_context_with_user(
            CreateUser::new()
                .with_email("passkey-test@example.com")
                .with_name("Passkey Tester"),
            Duration::hours(1),
        )
        .await;

        ctx.database
            .create_passkey(CreatePasskey {
                user_id: user.id.clone(),
                name: Some("Existing Key".to_owned()),
                credential_id: credential_id("cred-existing"),
                public_key: "public-key".to_owned(),
                counter: 0,
                device_type: "singleDevice".to_owned(),
                backed_up: false,
                transports: Some("usb,nfc".to_owned()),
                credential: "invalid-stored-passkey".to_owned(),
                aaguid: Some("00000000-0000-0000-0000-000000000000".to_owned()),
            })
            .await
            .unwrap();

        let req = test_helpers::create_auth_request(
            HttpMethod::Get,
            "/passkey/generate-register-options",
            Some(&session.token),
            None,
            HashMap::from([
                ("name".to_owned(), "Custom Account Label".to_owned()),
                (
                    "authenticatorAttachment".to_owned(),
                    "cross-platform".to_owned(),
                ),
            ]),
        );

        let response = plugin
            .handle_generate_register_options(&req, &ctx)
            .await
            .unwrap();
        assert_eq!(response.status, 200);
        assert!(cookie_header(&response).contains("better-auth-passkey="));

        let body: serde_json::Value = serde_json::from_slice(&response.body).unwrap();
        assert!((*(body).get("challenge").unwrap_or(&serde_json::Value::Null)).is_string());
        assert_eq!(
            (*(*(body).get("user").unwrap_or(&serde_json::Value::Null))
                .get("name")
                .unwrap_or(&serde_json::Value::Null)),
            "Custom Account Label"
        );
        assert_eq!(
            (*(*(body)
                .get("authenticatorSelection")
                .unwrap_or(&serde_json::Value::Null))
            .get("authenticatorAttachment")
            .unwrap_or(&serde_json::Value::Null)),
            "cross-platform"
        );
        assert_eq!(
            (*(*(*(body)
                .get("excludeCredentials")
                .unwrap_or(&serde_json::Value::Null))
            .get(0)
            .unwrap_or(&serde_json::Value::Null))
            .get("id")
            .unwrap_or(&serde_json::Value::Null)),
            credential_id("cred-existing")
        );
        assert_eq!(
            (*(*(*(*(body)
                .get("excludeCredentials")
                .unwrap_or(&serde_json::Value::Null))
            .get(0)
            .unwrap_or(&serde_json::Value::Null))
            .get("transports")
            .unwrap_or(&serde_json::Value::Null))
            .get(0)
            .unwrap_or(&serde_json::Value::Null)),
            "usb"
        );
    }

    #[tokio::test]
    async fn test_generate_authenticate_options_is_get_and_sets_cookie_without_auth() {
        let plugin = passkey_plugin();
        let ctx = test_helpers::create_test_context().await;
        let req = test_helpers::create_auth_request_no_query(
            HttpMethod::Get,
            "/passkey/generate-authenticate-options",
            None,
            None,
        );

        let response = plugin
            .handle_generate_authenticate_options(&req, &ctx)
            .await
            .unwrap();
        assert_eq!(response.status, 200);
        assert!(cookie_header(&response).contains("better-auth-passkey="));

        let body: serde_json::Value = serde_json::from_slice(&response.body).unwrap();
        assert!((*(body).get("challenge").unwrap_or(&serde_json::Value::Null)).is_string());
        assert!(body.get("allowCredentials").is_none());
    }

    #[tokio::test]
    async fn test_generate_authenticate_options_with_auth_lists_allow_credentials() {
        let plugin = passkey_plugin();
        let (ctx, user, session) = test_helpers::create_test_context_with_user(
            CreateUser::new()
                .with_email("passkey-test@example.com")
                .with_name("Passkey Tester"),
            Duration::hours(1),
        )
        .await;

        ctx.database
            .create_passkey(CreatePasskey {
                user_id: user.id.clone(),
                name: Some("Authenticator".to_owned()),
                credential_id: credential_id("cred-auth"),
                public_key: "public-key".to_owned(),
                counter: 0,
                device_type: "singleDevice".to_owned(),
                backed_up: false,
                transports: Some("internal".to_owned()),
                credential: "invalid-stored-passkey".to_owned(),
                aaguid: None,
            })
            .await
            .unwrap();

        let req = test_helpers::create_auth_request_no_query(
            HttpMethod::Get,
            "/passkey/generate-authenticate-options",
            Some(&session.token),
            None,
        );

        let response = plugin
            .handle_generate_authenticate_options(&req, &ctx)
            .await
            .unwrap();
        assert_eq!(response.status, 200);

        let body: serde_json::Value = serde_json::from_slice(&response.body).unwrap();
        assert_eq!(
            (*(*(*(body)
                .get("allowCredentials")
                .unwrap_or(&serde_json::Value::Null))
            .get(0)
            .unwrap_or(&serde_json::Value::Null))
            .get("id")
            .unwrap_or(&serde_json::Value::Null)),
            credential_id("cred-auth")
        );
        assert_eq!(
            (*(*(*(*(body)
                .get("allowCredentials")
                .unwrap_or(&serde_json::Value::Null))
            .get(0)
            .unwrap_or(&serde_json::Value::Null))
            .get("transports")
            .unwrap_or(&serde_json::Value::Null))
            .get(0)
            .unwrap_or(&serde_json::Value::Null)),
            "internal"
        );
    }

    #[tokio::test]
    async fn test_verify_registration_without_challenge_cookie_returns_challenge_not_found() {
        let plugin = passkey_plugin();
        let (ctx, _user, session) = test_helpers::create_test_context_with_user(
            CreateUser::new()
                .with_email("passkey-test@example.com")
                .with_name("Passkey Tester"),
            Duration::hours(1),
        )
        .await;

        let body = serde_json::json!({
            "response": {
                "id": "fake-credential-id",
                "rawId": "ZmFrZS1yYXctaWQ",
                "response": {
                    "attestationObject": "ZmFrZS1hdHRlc3RhdGlvbg",
                    "clientDataJSON": "ZmFrZS1jbGllbnQtZGF0YQ"
                },
                "type": "public-key"
            }
        });
        let mut req = test_helpers::create_auth_json_request_no_query(
            HttpMethod::Post,
            "/passkey/verify-registration",
            Some(&session.token),
            Some(body),
        );
        req.headers
            .insert("origin".to_owned(), "http://localhost:3000".to_owned());

        let response = plugin.handle_verify_registration(&req, &ctx).await.unwrap();
        assert_eq!(response.status, 400);

        let body_2: serde_json::Value = serde_json::from_slice(&response.body).unwrap();
        assert_eq!(
            (*(body_2).get("message").unwrap_or(&serde_json::Value::Null)),
            "Challenge not found"
        );
    }

    #[tokio::test]
    async fn test_verify_authentication_without_challenge_cookie_returns_challenge_not_found() {
        let plugin = passkey_plugin();
        let ctx = test_helpers::create_test_context().await;

        let body = serde_json::json!({
            "response": {
                "id": credential_id("cred-auth"),
                "rawId": credential_id("cred-auth"),
                "response": {
                    "authenticatorData": "ZmFrZS1hdXRoLWRhdGE",
                    "clientDataJSON": "ZmFrZS1jbGllbnQtZGF0YQ",
                    "signature": "ZmFrZS1zaWduYXR1cmU"
                },
                "type": "public-key"
            }
        });
        let mut req = test_helpers::create_auth_json_request_no_query(
            HttpMethod::Post,
            "/passkey/verify-authentication",
            None,
            Some(body),
        );
        req.headers
            .insert("origin".to_owned(), "http://localhost:3000".to_owned());

        let response = plugin
            .handle_verify_authentication(&req, &ctx)
            .await
            .unwrap();
        assert_eq!(response.status, 400);

        let body_2: serde_json::Value = serde_json::from_slice(&response.body).unwrap();
        assert_eq!(
            (*(body_2).get("message").unwrap_or(&serde_json::Value::Null)),
            "Challenge not found"
        );
    }

    #[tokio::test]
    async fn test_list_user_passkeys_omits_updated_at_and_retains_sql_null_fields() {
        let plugin = passkey_plugin();
        let (ctx, user, session) = test_helpers::create_test_context_with_user(
            CreateUser::new()
                .with_email("passkey-test@example.com")
                .with_name("Passkey Tester"),
            Duration::hours(1),
        )
        .await;

        ctx.database
            .create_passkey(CreatePasskey {
                user_id: user.id.clone(),
                name: None,
                credential_id: credential_id("cred-list"),
                public_key: "public-key".to_owned(),
                counter: 0,
                device_type: "singleDevice".to_owned(),
                backed_up: false,
                transports: None,
                credential: "invalid-stored-passkey".to_owned(),
                aaguid: Some("00000000-0000-0000-0000-000000000000".to_owned()),
            })
            .await
            .unwrap();

        let req = test_helpers::create_auth_request_no_query(
            HttpMethod::Get,
            "/passkey/list-user-passkeys",
            Some(&session.token),
            None,
        );
        let response = plugin.handle_list_user_passkeys(&req, &ctx).await.unwrap();
        assert_eq!(response.status, 200);

        let body: serde_json::Value = serde_json::from_slice(&response.body).unwrap();
        assert!(
            (*(body).get(0).unwrap_or(&serde_json::Value::Null))
                .get("updatedAt")
                .is_none()
        );
        assert_eq!(
            (*(*(body).get(0).unwrap_or(&serde_json::Value::Null))
                .get("aaguid")
                .unwrap_or(&serde_json::Value::Null)),
            "00000000-0000-0000-0000-000000000000"
        );
        let passkey = body.get(0).unwrap();
        // The published SQL adapter returns nullable columns as own properties.
        assert_eq!(passkey.get("name"), Some(&serde_json::Value::Null));
        assert_eq!(passkey.get("transports"), Some(&serde_json::Value::Null));
    }

    #[tokio::test]
    async fn test_delete_passkey_non_owner_is_unauthorized() {
        let plugin = passkey_plugin();
        let (ctx, _user, session) = test_helpers::create_test_context_with_user(
            CreateUser::new()
                .with_email("owner@example.com")
                .with_name("Owner"),
            Duration::hours(1),
        )
        .await;
        let other = test_helpers::create_user(
            &ctx,
            CreateUser::new()
                .with_email("other@example.com")
                .with_name("Other"),
        )
        .await;

        let passkey = ctx
            .database
            .create_passkey(CreatePasskey {
                user_id: other.id.clone(),
                name: Some("Other Key".to_owned()),
                credential_id: credential_id("cred-other-delete"),
                public_key: "public-key".to_owned(),
                counter: 0,
                device_type: "singleDevice".to_owned(),
                backed_up: false,
                transports: None,
                credential: "invalid-stored-passkey".to_owned(),
                aaguid: None,
            })
            .await
            .unwrap();

        let req = test_helpers::create_auth_json_request_no_query(
            HttpMethod::Post,
            "/passkey/delete-passkey",
            Some(&session.token),
            Some(serde_json::json!({ "id": passkey.id })),
        );

        let response = plugin.handle_delete_passkey(&req, &ctx).await.unwrap();
        assert_eq!(response.status, 401);
        assert_eq!(response.body.len(), 0);
        let preserved = ctx
            .database
            .get_passkey_by_id(&passkey.id)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(preserved.name.as_deref(), Some("Other Key"));
    }

    #[tokio::test]
    async fn test_update_passkey_non_owner_is_unauthorized() {
        let plugin = passkey_plugin();
        let (ctx, _user, session) = test_helpers::create_test_context_with_user(
            CreateUser::new()
                .with_email("owner@example.com")
                .with_name("Owner"),
            Duration::hours(1),
        )
        .await;
        let other = test_helpers::create_user(
            &ctx,
            CreateUser::new()
                .with_email("other@example.com")
                .with_name("Other"),
        )
        .await;

        let passkey = ctx
            .database
            .create_passkey(CreatePasskey {
                user_id: other.id.clone(),
                name: Some("Other Key".to_owned()),
                credential_id: credential_id("cred-other-update"),
                public_key: "public-key".to_owned(),
                counter: 0,
                device_type: "singleDevice".to_owned(),
                backed_up: false,
                transports: None,
                credential: "invalid-stored-passkey".to_owned(),
                aaguid: None,
            })
            .await
            .unwrap();

        let req = test_helpers::create_auth_json_request_no_query(
            HttpMethod::Post,
            "/passkey/update-passkey",
            Some(&session.token),
            Some(serde_json::json!({
                "id": passkey.id,
                "name": "Hijacked",
            })),
        );

        let response = plugin.handle_update_passkey(&req, &ctx).await.unwrap();
        assert_eq!(response.status, 401);
        let body: serde_json::Value = serde_json::from_slice(&response.body).unwrap();
        assert_eq!(
            (*(body).get("code").unwrap_or(&serde_json::Value::Null)),
            "YOU_ARE_NOT_ALLOWED_TO_REGISTER_THIS_PASSKEY"
        );
        let preserved = ctx
            .database
            .get_passkey_by_id(&passkey.id)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(preserved.name.as_deref(), Some("Other Key"));
    }

    /// Previously persisted ceremonies keep both their codec and original Required policy.
    #[tokio::test]
    #[expect(
        clippy::panic_in_result_fn,
        clippy::too_many_lines,
        reason = "Keep this ordered integration scenario and its assertions together; Result propagates setup failures"
    )]
    async fn pending_registration_challenges_keep_original_verification_policy()
    -> Result<(), Box<dyn std::error::Error>> {
        use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
        use p256::elliptic_curve::{Generate as _, sec1::ToSec1Point as _};
        use serde_cbor_2::Value as Cbor;
        use sha2::{Digest, Sha256};
        use std::collections::BTreeMap;
        use webauthn_rs::prelude::RegisterPublicKeyCredential;

        let config = PasskeyConfig {
            rp_id: "localhost".into(),
            origin: "http://localhost:3100".into(),
            ..Default::default()
        };
        let webauthn =
            webauthn::build_webauthn(&config, &alibi_core::AuthConfig::default(), &config.origin)?;
        let (options, legacy) = webauthn.start_passkey_registration(
            uuid::Uuid::new_v4(),
            "Legacy owner",
            "Legacy owner",
            None,
        )?;
        // This is the old externally persisted protocol, with a real library state.
        let stored = serde_json::to_string(&serde_json::json!({
            "user_id": "legacy-owner", "user": null, "context": "old-context", "state": legacy,
        }))?;
        let decoded: webauthn::StoredRegistrationState = serde_json::from_str(&stored)?;
        assert_eq!(decoded.user_id, "legacy-owner");
        assert_eq!(decoded.context.as_deref(), Some("old-context"));
        let webauthn::StoredRegistrationVerifier::Legacy(state) = decoded.state else {
            panic!("Old challenge changed verifier policy");
        };
        let secret = p256::SecretKey::generate();
        let point = secret.public_key().to_sec1_point(false);
        let key = Cbor::Map(BTreeMap::from([
            (Cbor::Integer(1), Cbor::Integer(2)),
            (Cbor::Integer(3), Cbor::Integer(-7)),
            (Cbor::Integer(-1), Cbor::Integer(1)),
            (
                Cbor::Integer(-2),
                Cbor::Bytes(point.x().ok_or("missing generated X coordinate")?.to_vec()),
            ),
            (
                Cbor::Integer(-3),
                Cbor::Bytes(point.y().ok_or("missing generated Y coordinate")?.to_vec()),
            ),
        ]));
        let core = webauthn::build_verification_core(
            &config,
            &alibi_core::AuthConfig::default(),
            &config.origin,
        )?;
        let builder = core
            .new_challenge_register_builder(b"actual-core-owner", "Core owner", "Core owner")?
            .user_verification_policy(webauthn_rs_core::proto::UserVerificationPolicy::Preferred);
        let (core_options, core_state) = core.generate_challenge_register(builder)?;
        let old_core_wire = serde_json::to_string(&serde_json::json!({
            "user_id": "core-owner", "user": null, "context": "old-core-context",
            "state": {"kind": "core", "state": core_state},
        }))?;
        let decoded_core: webauthn::StoredRegistrationState = serde_json::from_str(&old_core_wire)?;
        let webauthn::StoredRegistrationVerifier::Source(
            webauthn::StoredCoreRegistrationState::Core { state: old_core },
        ) = decoded_core.state
        else {
            panic!("Old Core protocol changed verifier");
        };
        let credential_id = b"actual-legacy-credential";
        let client_data = serde_json::to_vec(&serde_json::json!({
            "type": "webauthn.create", "challenge": options.public_key.challenge,
            "origin": config.origin, "crossOrigin": false,
        }))?;
        for verified in [true, false] {
            let mut auth_data = Sha256::digest(config.rp_id.as_bytes()).to_vec();
            auth_data.push(if verified { 0x45 } else { 0x41 });
            auth_data.extend_from_slice(&[0; 20]);
            auth_data.extend_from_slice(&u16::try_from(credential_id.len())?.to_be_bytes());
            auth_data.extend_from_slice(credential_id);
            auth_data.extend_from_slice(&serde_cbor_2::to_vec(&key)?);
            let attestation = Cbor::Map(BTreeMap::from([
                (Cbor::Text("fmt".into()), Cbor::Text("none".into())),
                (Cbor::Text("attStmt".into()), Cbor::Map(BTreeMap::new())),
                (Cbor::Text("authData".into()), Cbor::Bytes(auth_data)),
            ]));
            let response: RegisterPublicKeyCredential = serde_json::from_value(
                serde_json::json!({
                    "id": URL_SAFE_NO_PAD.encode(credential_id), "rawId": URL_SAFE_NO_PAD.encode(credential_id),
                    "type": "public-key", "clientExtensionResults": {}, "response": {
                        "clientDataJSON": URL_SAFE_NO_PAD.encode(&client_data),
                        "attestationObject": URL_SAFE_NO_PAD.encode(serde_cbor_2::to_vec(&attestation)?),
                        "transports": ["internal"],
                    },
                }),
            )?;
            // Feed the historical Core wire's genuinely issued challenge to its
            // actual consumer. This old policy accepts UV absent as well as present.
            let core_client_data = serde_json::to_vec(&serde_json::json!({
                "type": "webauthn.create", "challenge": core_options.public_key.challenge,
                "origin": config.origin, "crossOrigin": false,
            }))?;
            let mut core_response = response.clone();
            core_response.response.client_data_json = core_client_data.into();
            let restored_core = webauthn::finish_core_registration(
                &core,
                &core_response,
                &old_core,
                &config.origin,
            )?;
            assert_eq!(restored_core.cred_id().as_ref(), credential_id);
            let stored_credential = serde_json::to_string(&restored_core)?;
            let raw_none::StoredCredential::Core(decoded_credential) =
                serde_json::from_str(&stored_credential)?
            else {
                panic!("historical typed credential must retain its codec")
            };
            assert_eq!(decoded_credential.cred_id(), restored_core.cred_id());
            let result = webauthn.finish_passkey_registration(&response, &state);
            if verified {
                let legacy_key = result?;
                assert_eq!(legacy_key.cred_id().as_ref(), credential_id);
                complete_historical_authentication(&config, &secret, &key, legacy_key).await?;
            } else {
                assert!(matches!(
                    result,
                    Err(webauthn_rs_core::error::WebauthnError::UserNotVerified)
                ));
            }
        }
        Ok(())
    }

    async fn complete_historical_authentication(
        config: &PasskeyConfig,
        secret: &p256::SecretKey,
        cose: &serde_cbor_2::Value,
        mut key: webauthn_rs::prelude::Passkey,
    ) -> Result<(), Box<dyn std::error::Error>> {
        use p256::pkcs8::EncodePrivateKey;
        use sha2::{Digest, Sha256};
        let (ctx, user, _) = test_helpers::create_test_context_with_user(
            CreateUser::new().with_email("legacy-auth@example.test"),
            Duration::hours(1),
        )
        .await;
        let plugin = PasskeyPlugin {
            config: config.clone(),
        };
        let webauthn = webauthn::build_webauthn(config, &ctx.config, &config.origin)?;
        let credential_id = URL_SAFE_NO_PAD.encode(key.cred_id().as_ref());
        let persisted = ctx
            .database
            .create_passkey(CreatePasskey {
                user_id: user.id.clone(),
                name: Some("Historical".into()),
                credential_id: credential_id.clone(),
                public_key: base64::engine::general_purpose::STANDARD
                    .encode(serde_cbor_2::to_vec(cose)?),
                credential: serde_json::to_string(&key)?,
                counter: 0,
                device_type: "singleDevice".into(),
                backed_up: false,
                transports: None,
                aaguid: None,
            })
            .await?;
        let signing = openssl::pkey::PKey::private_key_from_der(secret.to_pkcs8_der()?.as_bytes())?;
        for (kind, counter, backup) in [
            ("passkey", 1_u32, 0x08_u8),
            ("discoverable", 2, 0x18),
            ("discoverable", 3, 0x08),
        ] {
            for verified in [false, true] {
                let (challenge, state) = if kind == "passkey" {
                    let (options, state) = webauthn.start_passkey_authentication(&[key.clone()])?;
                    (
                        options.public_key.challenge,
                        serde_json::json!({"kind":"passkey","state":state}),
                    )
                } else {
                    let (options, state) = webauthn.start_discoverable_authentication()?;
                    (
                        options.public_key.challenge,
                        serde_json::json!({"kind":"discoverable","state":state}),
                    )
                };
                let token = uuid::Uuid::new_v4().to_string();
                _ = ctx
                    .verifications()
                    .create(alibi_core::CreateVerification {
                        identifier: token.clone(),
                        value: serde_json::to_string(&state)?,
                        expires_at: chrono::Utc::now() + Duration::minutes(5),
                    })
                    .await?;
                let cookie = webauthn::create_challenge_cookie(&ctx.config, 300, &token, config)?;
                let client = serde_json::to_vec(
                    &serde_json::json!({"type":"webauthn.get","challenge":challenge,"origin":config.origin}),
                )?;
                let mut data = Sha256::digest(config.rp_id.as_bytes()).to_vec();
                data.push(backup | if verified { 0x05 } else { 0x01 });
                data.extend_from_slice(&counter.to_be_bytes());
                let mut signer =
                    openssl::sign::Signer::new(openssl::hash::MessageDigest::sha256(), &signing)?;
                signer.update(&data)?;
                signer.update(&Sha256::digest(&client))?;
                let body = serde_json::json!({"response":{"id":credential_id,"rawId":credential_id,"type":"public-key","clientExtensionResults":{},"response":{"clientDataJSON":URL_SAFE_NO_PAD.encode(client),"authenticatorData":URL_SAFE_NO_PAD.encode(data),"signature":URL_SAFE_NO_PAD.encode(signer.sign_to_vec()?)}}});
                let mut request = test_helpers::create_auth_request(
                    HttpMethod::Post,
                    "/passkey/verify-authentication",
                    None,
                    Some(serde_json::to_vec(&body)?),
                    HashMap::new(),
                );
                _ = request
                    .headers
                    .insert("cookie".into(), cookie.split(';').next().unwrap().into());
                _ = request
                    .headers
                    .insert("origin".into(), config.origin.clone());
                let before = ctx
                    .database
                    .get_passkey_by_id(&persisted.id)
                    .await?
                    .unwrap();
                let sessions = ctx.database.get_user_sessions(&user.id).await?.len();
                let result = plugin.handle_verify_authentication(&request, &ctx).await?;
                assert_eq!(
                    result.status,
                    if verified { 200 } else { 400 },
                    "{kind}: {}",
                    String::from_utf8_lossy(&result.body)
                );
                let after = ctx
                    .database
                    .get_passkey_by_id(&persisted.id)
                    .await?
                    .unwrap();
                if verified {
                    assert_eq!(
                        ctx.database.get_user_sessions(&user.id).await?.len(),
                        sessions + 1
                    );
                    assert_eq!(after.counter, u64::from(counter));
                    let opaque: serde_json::Value = serde_json::from_str(&after.credential)?;
                    assert_eq!(
                        opaque.get("cred").and_then(|c| c.get("backup_eligible")),
                        Some(&serde_json::json!(true))
                    );
                    assert_eq!(
                        opaque.get("cred").and_then(|c| c.get("backup_state")),
                        Some(&serde_json::json!(backup == 0x18))
                    );
                    assert!(!after.backed_up);
                    assert_eq!(after.device_type, "singleDevice");
                    key = serde_json::from_str(&after.credential)?;
                    assert_eq!(
                        plugin
                            .handle_verify_authentication(&request, &ctx)
                            .await?
                            .status,
                        400
                    );
                } else {
                    assert_eq!(
                        serde_json::to_value(&after)?,
                        serde_json::to_value(&before)?
                    );
                    assert_eq!(
                        ctx.database.get_user_sessions(&user.id).await?.len(),
                        sessions
                    );
                }
                assert!(ctx.verifications().find(&token).await?.is_none());
            }
        }
        Ok(())
    }

    #[tokio::test]
    #[expect(
        clippy::panic_in_result_fn,
        reason = "Keep this ordered integration scenario and its assertions together; Result propagates setup failures"
    )]
    async fn raw_none_credential_sql_readback_keeps_original_key_and_hidden_codec()
    -> Result<(), Box<dyn std::error::Error>> {
        use serde_cbor_2::Value as Cbor;
        use sha2::{Digest, Sha256};
        use std::collections::BTreeMap;
        // The raw storage owner also protects the usable mismatched-key codec.
        // SDK observations cannot read the Native-only hidden credential column.
        for (algorithm, curve) in [(-8, 8), (-7, 6)] {
            let signing = ed25519_dalek::SigningKey::from_bytes(&[9; 32]);
            let plugin = passkey_plugin();
            let (ctx, user, session) = test_helpers::create_test_context_with_user(
                CreateUser::new()
                    .with_email("raw-codec-owner@fixture.test")
                    .with_name("Raw codec owner"),
                Duration::hours(1),
            )
            .await;
            let request = test_helpers::create_auth_request(
                HttpMethod::Get,
                "/passkey/generate-register-options",
                Some(&session.token),
                None,
                HashMap::new(),
            );
            let options = plugin
                .handle_generate_register_options(&request, &ctx)
                .await?;
            let issued: serde_json::Value = serde_json::from_slice(&options.body)?;
            let key = serde_cbor_2::to_vec(&Cbor::Map(BTreeMap::from([
                (Cbor::Integer(1), Cbor::Integer(1)),
                (Cbor::Integer(3), Cbor::Integer(algorithm)),
                (Cbor::Integer(-1), Cbor::Integer(curve)),
                (
                    Cbor::Integer(-2),
                    Cbor::Bytes(signing.verifying_key().as_bytes().to_vec()),
                ),
            ])))?;
            let id = b"raw-none-actual-persisted-credential";
            let mut data = Sha256::digest(b"localhost").to_vec();
            data.push(0x41);
            data.extend_from_slice(&25_u32.to_be_bytes());
            data.extend_from_slice(&[0; 16]);
            data.extend_from_slice(&u16::try_from(id.len())?.to_be_bytes());
            data.extend_from_slice(id);
            data.extend_from_slice(&key);
            let attestation = serde_cbor_2::to_vec(&Cbor::Map(BTreeMap::from([
                (Cbor::Text("fmt".into()), Cbor::Text("none".into())),
                (Cbor::Text("attStmt".into()), Cbor::Map(BTreeMap::new())),
                (Cbor::Text("authData".into()), Cbor::Bytes(data)),
            ])))?;
            let proof = serde_json::json!({"id":URL_SAFE_NO_PAD.encode(id),"rawId":URL_SAFE_NO_PAD.encode(id),"type":"public-key","clientExtensionResults":{},"response":{
                "clientDataJSON":URL_SAFE_NO_PAD.encode(serde_json::to_vec(&serde_json::json!({"type":"webauthn.create","challenge":(*(issued).get("challenge").expect("fixture contains the requested index")),"origin":"http://localhost:3000"}))?),
                "attestationObject":URL_SAFE_NO_PAD.encode(attestation),"transports":["internal"],
            }});
            let mut request_2 = test_helpers::create_auth_request(
                HttpMethod::Post,
                "/passkey/verify-registration",
                Some(&session.token),
                Some(serde_json::to_vec(&serde_json::json!({"response":proof}))?),
                HashMap::new(),
            );
            let issued_cookie = cookie_header(&options)
                .split(';')
                .next()
                .ok_or("issued challenge cookie required")?;
            _ = write!(
                request_2
                    .headers
                    .get_mut("cookie")
                    .ok_or("signed owner cookie required")?,
                "; {issued_cookie}"
            );
            let result = plugin.handle_verify_registration(&request_2, &ctx).await?;
            assert_eq!(result.status, 200);
            let wire: serde_json::Value = serde_json::from_slice(&result.body)?;
            assert!(wire.get("credential").is_none());
            let row = ctx
                .database
                .get_passkey_by_credential_id(&URL_SAFE_NO_PAD.encode(id))
                .await?
                .ok_or("actual credential row required")?;
            assert_eq!(row.user_id, user.id);
            assert_eq!(row.counter, 25);
            assert_eq!(
                row.public_key,
                base64::engine::general_purpose::STANDARD.encode(&key)
            );
            let raw_none::StoredCredential::Raw(raw) = serde_json::from_str(&row.credential)?
            else {
                panic!("actual raw codec required")
            };
            assert_eq!(raw.credential_id(), id);
            assert_eq!(raw.public_key(), key);
            assert_eq!(raw.snapshot()?.counter, 25);
            assert!(
                serde_json::from_str::<webauthn_rs::prelude::Passkey>(&row.credential).is_err()
            );
            if curve == 6 {
                use ed25519_dalek::Signer;
                let mut persisted = serde_json::to_value(&row)?;
                for control in [
                    "first",
                    "signature",
                    "counter",
                    "origin",
                    "challenge",
                    "rp",
                    "presence",
                    "backup",
                    "last",
                ] {
                    let successful = matches!(control, "first" | "last");
                    let counter = if matches!(control, "first" | "counter") {
                        26_u32
                    } else {
                        27_u32
                    };
                    let request = test_helpers::create_auth_request(
                        HttpMethod::Get,
                        "/passkey/generate-authenticate-options",
                        None,
                        None,
                        HashMap::new(),
                    );
                    let options = plugin
                        .handle_generate_authenticate_options(&request, &ctx)
                        .await?;
                    let issued: serde_json::Value = serde_json::from_slice(&options.body)?;
                    let challenge = issued
                        .get("challenge")
                        .expect("issued authentication challenge");
                    let client = serde_json::to_vec(
                        &serde_json::json!({"type":"webauthn.get", "challenge":if control == "challenge" { serde_json::Value::String("Zm9yZWlnbg".into()) } else { challenge.clone() },"origin":if control == "origin" { "http://foreign.test" } else { "http://localhost:3000" }}),
                    )?;
                    let mut data = Sha256::digest(if control == "rp" {
                        b"foreign.test".as_slice()
                    } else {
                        b"localhost".as_slice()
                    })
                    .to_vec();
                    data.push(match control {
                        "presence" => 0,
                        "backup" => 0x11,
                        _ => 1,
                    });
                    data.extend_from_slice(&counter.to_be_bytes());
                    let mut signed = data.clone();
                    signed.extend_from_slice(&Sha256::digest(&client));
                    let mut signature = signing.sign(&signed).to_bytes();
                    if control == "signature" {
                        signature[0] ^= 1;
                    }
                    let response = serde_json::json!({"id":URL_SAFE_NO_PAD.encode(id),"rawId":URL_SAFE_NO_PAD.encode(id),"type":"public-key","clientExtensionResults":{},"response":{
                        "clientDataJSON":URL_SAFE_NO_PAD.encode(client),"authenticatorData":URL_SAFE_NO_PAD.encode(data),"signature":URL_SAFE_NO_PAD.encode(signature)
                    }});
                    let mut request = test_helpers::create_auth_request(
                        HttpMethod::Post,
                        "/passkey/verify-authentication",
                        None,
                        Some(serde_json::to_vec(
                            &serde_json::json!({"response":response}),
                        )?),
                        HashMap::new(),
                    );
                    request.headers.insert(
                        "cookie".into(),
                        cookie_header(&options)
                            .split(';')
                            .next()
                            .ok_or("issued authentication cookie required")?
                            .into(),
                    );
                    let result = plugin.handle_verify_authentication(&request, &ctx).await?;
                    assert_eq!(
                        result.status,
                        if successful {
                            200
                        } else if control == "signature" {
                            401
                        } else {
                            400
                        },
                        "{control}"
                    );
                    let updated = ctx
                        .database
                        .get_passkey_by_credential_id(&URL_SAFE_NO_PAD.encode(id))
                        .await?
                        .ok_or("updated raw row required")?;
                    if !successful {
                        let wire: serde_json::Value = serde_json::from_slice(&result.body)?;
                        assert_eq!(
                            wire.get("code"),
                            Some(&serde_json::json!("AUTHENTICATION_FAILED")),
                            "{control}"
                        );
                        assert!(
                            result
                                .headers
                                .get_all("set-cookie")
                                .all(|cookie| !cookie.contains("session_token="))
                        );
                        assert_eq!(serde_json::to_value(&updated)?, persisted, "{control}");
                        assert_eq!(ctx.database.get_user_sessions(&user.id).await?.len(), 2);
                        continue;
                    }
                    persisted = serde_json::to_value(&updated)?;
                    assert_eq!(updated.public_key, row.public_key);
                    assert_eq!(updated.credential_id, row.credential_id);
                    assert_eq!(updated.user_id, row.user_id);
                    assert_eq!(updated.counter, u64::from(counter));
                    let raw_none::StoredCredential::Raw(raw) =
                        serde_json::from_str(&updated.credential)?
                    else {
                        panic!("authentication must retain raw codec")
                    };
                    assert_eq!(raw.public_key(), key);
                    assert_eq!(raw.credential_id(), id);
                    assert_eq!(raw.snapshot()?.counter, u64::from(counter));
                    assert!(
                        serde_json::from_str::<webauthn_rs::prelude::Passkey>(&updated.credential)
                            .is_err()
                    );
                }
            }
            assert_eq!(
                ctx.database.get_user_sessions(&user.id).await?.len(),
                if curve == 6 { 3 } else { 1 }
            );
        }
        Ok(())
    }
}
// LCOV_EXCL_STOP