alibi-plugins 0.7.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
mod authentication;
mod endpoint;
pub use endpoint::{
    PasskeyAuthenticated, PasskeyDeleted, PasskeyRegistrationInput, PasskeyUpdated,
};
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> {
        self.generate_register_options(req, &req.query, ctx).await
    }

    async fn generate_register_options<S: alibi_core::AuthSchema>(
        &self,
        req: &impl alibi_core::session::SessionRequest,
        query: &std::collections::HashMap<String, String>,
        ctx: &AuthContext<S>,
    ) -> 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.physical_request(),
                endpoint: req.endpoint_call(),
                auth_config: &ctx.config,
                extensions: &ctx.extensions,
            };
            match resolver
                .resolve_user(&context, 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.physical_request(),
                        endpoint: req.endpoint_call(),
                        auth_config: &ctx.config,
                        extensions: &ctx.extensions,
                        user: projected_user.as_ref(),
                    })
                    .await?,
            )
        } else {
            None
        };
        let passkey_name = query.get("name").map(String::as_str);
        let authenticator_attachment = query.get("authenticatorAttachment").map(String::as_str);
        let (result, cookie_header) = generate_register_options_core(
            &user,
            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),
        };
        self.verify_registration(&body, req, ctx).await
    }

    async fn verify_registration<S: alibi_core::AuthSchema>(
        &self,
        body: &VerifyRegistrationRequest,
        req: &impl alibi_core::session::SessionRequest,
        ctx: &AuthContext<S>,
    ) -> AuthResult<AuthResponse> {
        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: &impl alibi_core::session::SessionRequest,
        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> {
        self.generate_authentication_options(req, ctx).await
    }

    async fn generate_authentication_options<S: alibi_core::AuthSchema>(
        &self,
        req: &impl alibi_core::session::SessionRequest,
        ctx: &AuthContext<S>,
    ) -> 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.physical_request(),
                        endpoint: req.endpoint_call(),
                        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),
        };
        self.verify_authentication(&body, req, ctx).await
    }

    async fn verify_authentication<S: alibi_core::AuthSchema>(
        &self,
        body: &VerifyAuthenticationRequest,
        req: &impl alibi_core::session::SessionRequest,
        ctx: &AuthContext<S>,
    ) -> AuthResult<AuthResponse> {
        let metadata = if req.endpoint_call().is_some() {
            alibi_core::RequestMeta {
                ip_address: Some(
                    ctx.config
                        .advanced
                        .ip_address
                        .resolve_ip(req.session_headers())
                        .unwrap_or_default(),
                ),
                user_agent: Some(
                    req.session_headers()
                        .iter()
                        .find(|(name, _)| name.eq_ignore_ascii_case("user-agent"))
                        .map(|(_, value)| value.clone())
                        .unwrap_or_default(),
                ),
            }
        } else {
            req.physical_request()
                .map(alibi_core::RequestMeta::from_request)
                .unwrap_or_default()
        };
        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 {
    fn server_endpoints(&self) -> Vec<alibi_core::endpoint::EndpointDefinition> {
        endpoint::definitions()
    }
    fn validate_endpoint(&self, call: &alibi_core::endpoint::EndpointCall, _ctx: &AuthContext<S>) -> AuthResult<alibi_core::endpoint::EndpointInput> {
        endpoint::validate(call)
    }
    async fn on_endpoint(&self, call: &alibi_core::endpoint::EndpointCall, ctx: &AuthContext<S>) -> AuthResult<alibi_core::endpoint::EndpointResponse> {
        if call.operation_id() == "passkeyVerifyAuthentication" { self.verify_authentication_endpoint(call, ctx).await } else if call.operation_id() == "passkeyVerifyRegistration" { self.verify_registration_endpoint(call, ctx).await } else if call.operation_id() == "passkeyGenerateAuthenticateOptions" { self.authentication_options_endpoint(call, ctx).await } else if call.operation_id() == "generatePasskeyRegistrationOptions" { self.registration_options_endpoint(call, ctx).await } else if call.operation_id() == "deletePasskey" { self.delete_endpoint(call, ctx).await } else if call.operation_id() == "updatePasskey" { self.update_endpoint(call, ctx).await } else { self.list_endpoint(call, ctx).await }
    }
    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()),

                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()),

                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,

                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,

                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,

                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,
        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)
            );
            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));
                }
            }
            assert_eq!(
                ctx.database.get_user_sessions(&user.id).await?.len(),
                if curve == 6 { 3 } else { 1 }
            );
        }
        Ok(())
    }
}
// LCOV_EXCL_STOP