pray-core 1.5.1

Prayfile core: parse, resolve, lock, render, verify, and build distribution artifacts
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
use crate::hashing::sha256_prefixed;
#[cfg(feature = "auth")]
use crate::trust::EmailConfirmationPolicy;
use crate::{PrayError, PrayResult};
#[cfg(feature = "auth")]
use base64::{engine::general_purpose::STANDARD, Engine as _};
#[cfg(feature = "auth")]
use ed25519_dalek::{Signature, Verifier, VerifyingKey};
#[cfg(feature = "auth")]
use rusqlite::{Connection, OptionalExtension};
use serde::{Deserialize, Serialize};
#[cfg(feature = "auth")]
use std::fs;
#[cfg(feature = "auth")]
use std::path::{Path, PathBuf};
#[cfg(feature = "auth")]
use std::time::{SystemTime, UNIX_EPOCH};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthRegistrationRequest {
    pub email: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthVerificationRequest {
    pub email: String,
    pub code: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthSessionRequest {
    pub email: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthPasskeyEnrollmentRequest {
    pub email: String,
    pub credential_id: String,
    pub public_key: String,
    #[serde(default)]
    pub label: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthPasskeyChallengeRequest {
    pub credential_id: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthPasskeyChallengeResponse {
    pub credential_id: String,
    pub challenge_id: String,
    pub challenge: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthPasskeyLoginRequest {
    pub credential_id: String,
    pub challenge_id: String,
    pub signature: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthSshKeyEnrollmentRequest {
    pub email: String,
    pub public_key: String,
    #[serde(default)]
    pub label: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthSshKeyChallengeRequest {
    pub public_key: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthSshKeyChallengeResponse {
    pub fingerprint: String,
    pub challenge_id: String,
    pub challenge: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthSshKeyLoginRequest {
    pub public_key: String,
    pub challenge_id: String,
    pub signature: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthRegistrationResponse {
    pub email: String,
    pub verified: bool,
    #[serde(default)]
    pub verification_code: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthVerificationResponse {
    pub email: String,
    pub verified: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AuthSessionKind {
    Email,
    Passkey,
    SshKey,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthSessionResponse {
    pub email: String,
    pub token: String,
    pub kind: AuthSessionKind,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthPasskeyEnrollmentResponse {
    pub email: String,
    pub credential_id: String,
    pub enrolled: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthPasskeyLoginResponse {
    pub email: String,
    pub token: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthChallengeResponse {
    pub challenge_id: String,
    pub challenge: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthSshKeyEnrollmentResponse {
    pub email: String,
    pub fingerprint: String,
    pub enrolled: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthSshKeyLoginResponse {
    pub email: String,
    pub token: String,
}

#[cfg(feature = "auth")]
mod store {
    use super::*;

    #[derive(Debug, Clone)]
    pub struct RegistryAuthStore {
        database_path: PathBuf,
    }

    #[derive(Debug, Clone)]
    struct StoredChallenge {
        challenge: String,
    }

    impl RegistryAuthStore {
        pub fn open(root: &Path) -> PrayResult<Self> {
            let database_path = root.join(".pray/auth.db");
            if let Some(parent) = database_path.parent() {
                fs::create_dir_all(parent)?;
            }
            let store = Self { database_path };
            store.initialize()?;
            Ok(store)
        }

        pub fn register_email(
            &self,
            email: &str,
            policy: EmailConfirmationPolicy,
        ) -> PrayResult<AuthRegistrationResponse> {
            validate_email(email)?;
            let connection = self.connection()?;
            let timestamp = current_unix_timestamp()?;
            let verified = matches!(policy, EmailConfirmationPolicy::Disabled);
            let verification_code = if verified {
                None
            } else {
                Some(generate_verification_code(email, timestamp))
            };
            let policy_text = email_confirmation_policy_text(policy);

            connection.execute(
            "INSERT INTO users (email, email_verified, email_confirmation_policy, created_at) VALUES (?1, ?2, ?3, ?4)
             ON CONFLICT(email) DO UPDATE SET email_verified = excluded.email_verified, email_confirmation_policy = excluded.email_confirmation_policy",
            rusqlite::params![email, verified, policy_text, timestamp],
        )?;
            if let Some(code) = verification_code.as_ref() {
                connection.execute(
                "INSERT INTO email_verification_codes (email, code, created_at, verified_at)
                 VALUES (?1, ?2, ?3, NULL)
                 ON CONFLICT(email) DO UPDATE SET code = excluded.code, created_at = excluded.created_at, verified_at = NULL",
                rusqlite::params![email, code, timestamp],
            )?;
            }

            Ok(AuthRegistrationResponse {
                email: email.to_string(),
                verified,
                verification_code,
            })
        }

        pub fn verify_email(
            &self,
            email: &str,
            code: &str,
        ) -> PrayResult<AuthVerificationResponse> {
            validate_email(email)?;
            if code.trim().is_empty() {
                return Err(PrayError::Unsupported(
                    "verification code cannot be empty".to_string(),
                ));
            }
            let connection = self.connection()?;
            let stored_code: Option<String> = connection
                .query_row(
                    "SELECT code FROM email_verification_codes WHERE email = ?1",
                    rusqlite::params![email],
                    |row| row.get(0),
                )
                .optional()?;
            let Some(stored_code) = stored_code else {
                return Err(PrayError::Resolution(format!(
                    "no verification code found for {email}"
                )));
            };
            if stored_code != code {
                return Err(PrayError::Resolution(format!(
                    "verification code mismatch for {email}"
                )));
            }
            let timestamp = current_unix_timestamp()?;
            connection.execute(
                "UPDATE users SET email_verified = 1 WHERE email = ?1",
                rusqlite::params![email],
            )?;
            connection.execute(
                "UPDATE email_verification_codes SET verified_at = ?2 WHERE email = ?1",
                rusqlite::params![email, timestamp],
            )?;
            Ok(AuthVerificationResponse {
                email: email.to_string(),
                verified: true,
            })
        }

        pub fn user_verified(&self, email: &str) -> PrayResult<bool> {
            validate_email(email)?;
            let connection = self.connection()?;
            let verified: Option<bool> = connection
                .query_row(
                    "SELECT email_verified FROM users WHERE email = ?1",
                    rusqlite::params![email],
                    |row| row.get(0),
                )
                .optional()?;
            Ok(verified.unwrap_or(false))
        }

        pub fn request_passkey_challenge(
            &self,
            credential_id: &str,
        ) -> PrayResult<AuthPasskeyChallengeResponse> {
            validate_identifier(credential_id, "credential id")?;
            let connection = self.connection()?;
            let email: String = connection.query_row(
                "SELECT email FROM passkeys WHERE credential_id = ?1",
                rusqlite::params![credential_id],
                |row| row.get(0),
            )?;
            let challenge = generate_auth_challenge("passkey", credential_id)?;
            let challenge_id = generate_challenge_id(&email, credential_id, "passkey", &challenge)?;
            store_challenge(&connection, &challenge_id, &email, &challenge, "passkey")?;
            Ok(AuthPasskeyChallengeResponse {
                credential_id: credential_id.to_string(),
                challenge_id,
                challenge,
            })
        }

        pub fn respond_passkey_challenge(
            &self,
            credential_id: &str,
            challenge_id: &str,
            signature: &str,
        ) -> PrayResult<AuthPasskeyLoginResponse> {
            validate_identifier(credential_id, "credential id")?;
            validate_identifier(challenge_id, "challenge id")?;
            validate_signature(signature)?;
            let connection = self.connection()?;
            let email: String = connection.query_row(
                "SELECT email FROM passkeys WHERE credential_id = ?1",
                rusqlite::params![credential_id],
                |row| row.get(0),
            )?;
            let challenge = load_challenge(&connection, challenge_id, &email, "passkey")?;
            let public_key = load_passkey_public_key(&connection, credential_id)?;
            verify_signature(&public_key, challenge.challenge.as_bytes(), signature)?;
            mark_challenge_used(&connection, challenge_id)?;
            let session = self.issue_session(&email, AuthSessionKind::Passkey)?;
            Ok(AuthPasskeyLoginResponse {
                email,
                token: session.token,
            })
        }

        pub fn request_ssh_key_challenge(
            &self,
            public_key: &str,
        ) -> PrayResult<AuthSshKeyChallengeResponse> {
            validate_public_key(public_key)?;
            let connection = self.connection()?;
            let (public_key, _) = parse_ssh_ed25519_public_key(public_key)?;
            let fingerprint = ssh_key_fingerprint(&public_key);
            let email: String = connection.query_row(
                "SELECT email FROM ssh_keys WHERE fingerprint = ?1",
                rusqlite::params![fingerprint],
                |row| row.get(0),
            )?;
            let challenge = generate_auth_challenge("ssh_key", &public_key)?;
            let challenge_id = generate_challenge_id(&email, &fingerprint, "ssh_key", &challenge)?;
            store_challenge(&connection, &challenge_id, &email, &challenge, "ssh_key")?;
            Ok(AuthSshKeyChallengeResponse {
                fingerprint,
                challenge_id,
                challenge,
            })
        }

        pub fn respond_ssh_key_challenge(
            &self,
            public_key: &str,
            challenge_id: &str,
            signature: &str,
        ) -> PrayResult<AuthSshKeyLoginResponse> {
            validate_public_key(public_key)?;
            validate_identifier(challenge_id, "challenge id")?;
            validate_signature(signature)?;
            let connection = self.connection()?;
            let (public_key, _) = parse_ssh_ed25519_public_key(public_key)?;
            let fingerprint = ssh_key_fingerprint(&public_key);
            let email: String = connection.query_row(
                "SELECT email FROM ssh_keys WHERE fingerprint = ?1",
                rusqlite::params![fingerprint],
                |row| row.get(0),
            )?;
            let challenge = load_challenge(&connection, challenge_id, &email, "ssh_key")?;
            verify_signature(&public_key, challenge.challenge.as_bytes(), signature)?;
            mark_challenge_used(&connection, challenge_id)?;
            let session = self.issue_session(&email, AuthSessionKind::SshKey)?;
            Ok(AuthSshKeyLoginResponse {
                email,
                token: session.token,
            })
        }

        pub fn issue_session(
            &self,
            email: &str,
            kind: AuthSessionKind,
        ) -> PrayResult<AuthSessionResponse> {
            validate_email(email)?;
            let connection = self.connection()?;
            let user: Option<(bool, String)> = connection
                .query_row(
                    "SELECT email_verified, email_confirmation_policy FROM users WHERE email = ?1",
                    rusqlite::params![email],
                    |row| Ok((row.get(0)?, row.get(1)?)),
                )
                .optional()?;
            let Some((verified, policy)) = user else {
                return Err(PrayError::Resolution(format!("unknown user: {email}")));
            };
            if !verified
                && policy != email_confirmation_policy_text(EmailConfirmationPolicy::Optional)
            {
                return Err(PrayError::Resolution(format!(
                    "email confirmation required for {email}"
                )));
            }
            let timestamp = current_unix_timestamp()?;
            let token = generate_session_token(email, &kind, timestamp);
            connection.execute(
                "INSERT INTO sessions (token, email, kind, created_at, last_used_at)
             VALUES (?1, ?2, ?3, ?4, ?4)
             ON CONFLICT(token) DO UPDATE SET last_used_at = excluded.last_used_at",
                rusqlite::params![token, email, auth_session_kind_text(&kind), timestamp],
            )?;
            Ok(AuthSessionResponse {
                email: email.to_string(),
                token,
                kind,
            })
        }

        pub fn resolve_session(&self, token: &str) -> PrayResult<Option<AuthSessionResponse>> {
            if token.trim().is_empty() {
                return Ok(None);
            }
            let connection = self.connection()?;
            let session: Option<(String, String)> = connection
                .query_row(
                    "SELECT email, kind FROM sessions WHERE token = ?1",
                    rusqlite::params![token],
                    |row| Ok((row.get(0)?, row.get(1)?)),
                )
                .optional()?;
            let Some((email, kind_text)) = session else {
                return Ok(None);
            };
            let kind = parse_auth_session_kind(&kind_text)?;
            let timestamp = current_unix_timestamp()?;
            connection.execute(
                "UPDATE sessions SET last_used_at = ?2 WHERE token = ?1",
                rusqlite::params![token, timestamp],
            )?;
            Ok(Some(AuthSessionResponse {
                email,
                token: token.to_string(),
                kind,
            }))
        }

        pub fn enroll_passkey(
            &self,
            email: &str,
            credential_id: &str,
            public_key: &str,
            label: Option<&str>,
        ) -> PrayResult<AuthPasskeyEnrollmentResponse> {
            validate_email(email)?;
            validate_identifier(credential_id, "credential id")?;
            validate_public_key(public_key)?;
            let connection = self.connection()?;
            ensure_user_can_authenticate(&connection, email)?;
            let timestamp = current_unix_timestamp()?;
            connection.execute(
            "INSERT INTO passkeys (credential_id, email, public_key, label, created_at, last_used_at)
             VALUES (?1, ?2, ?3, ?4, ?5, NULL)
             ON CONFLICT(credential_id) DO UPDATE SET email = excluded.email, public_key = excluded.public_key, label = excluded.label",
            rusqlite::params![credential_id, email, public_key, label.unwrap_or(""), timestamp],
        )?;
            Ok(AuthPasskeyEnrollmentResponse {
                email: email.to_string(),
                credential_id: credential_id.to_string(),
                enrolled: true,
            })
        }

        pub fn login_with_passkey(
            &self,
            credential_id: &str,
        ) -> PrayResult<AuthPasskeyLoginResponse> {
            validate_identifier(credential_id, "credential id")?;
            let connection = self.connection()?;
            let email: Option<String> = connection
                .query_row(
                    "SELECT email FROM passkeys WHERE credential_id = ?1",
                    rusqlite::params![credential_id],
                    |row| row.get(0),
                )
                .optional()?;
            let Some(email) = email else {
                return Err(PrayError::Resolution(format!(
                    "unknown passkey credential: {credential_id}"
                )));
            };
            let session = self.issue_session(&email, AuthSessionKind::Passkey)?;
            connection.execute(
                "UPDATE passkeys SET last_used_at = ?2 WHERE credential_id = ?1",
                rusqlite::params![credential_id, current_unix_timestamp()?],
            )?;
            Ok(AuthPasskeyLoginResponse {
                email,
                token: session.token,
            })
        }

        pub fn enroll_ssh_key(
            &self,
            email: &str,
            public_key: &str,
            label: Option<&str>,
        ) -> PrayResult<AuthSshKeyEnrollmentResponse> {
            validate_email(email)?;
            validate_public_key(public_key)?;
            let connection = self.connection()?;
            ensure_user_can_authenticate(&connection, email)?;
            let (public_key, _) = parse_ssh_ed25519_public_key(public_key)?;
            let fingerprint = ssh_key_fingerprint(&public_key);
            let timestamp = current_unix_timestamp()?;
            connection.execute(
            "INSERT INTO ssh_keys (fingerprint, email, public_key, label, created_at, last_used_at)
             VALUES (?1, ?2, ?3, ?4, ?5, NULL)
             ON CONFLICT(fingerprint) DO UPDATE SET email = excluded.email, public_key = excluded.public_key, label = excluded.label",
            rusqlite::params![fingerprint, email, public_key, label.unwrap_or(""), timestamp],
        )?;
            Ok(AuthSshKeyEnrollmentResponse {
                email: email.to_string(),
                fingerprint,
                enrolled: true,
            })
        }

        pub fn login_with_ssh_key(&self, public_key: &str) -> PrayResult<AuthSshKeyLoginResponse> {
            validate_public_key(public_key)?;
            let connection = self.connection()?;
            let (public_key, _) = parse_ssh_ed25519_public_key(public_key)?;
            let fingerprint = ssh_key_fingerprint(&public_key);
            let email: Option<String> = connection
                .query_row(
                    "SELECT email FROM ssh_keys WHERE fingerprint = ?1",
                    rusqlite::params![fingerprint],
                    |row| row.get(0),
                )
                .optional()?;
            let Some(email) = email else {
                return Err(PrayError::Resolution(format!(
                    "unknown ssh key fingerprint: {fingerprint}"
                )));
            };
            let session = self.issue_session(&email, AuthSessionKind::SshKey)?;
            connection.execute(
                "UPDATE ssh_keys SET last_used_at = ?2 WHERE fingerprint = ?1",
                rusqlite::params![fingerprint, current_unix_timestamp()?],
            )?;
            Ok(AuthSshKeyLoginResponse {
                email,
                token: session.token,
            })
        }

        fn initialize(&self) -> PrayResult<()> {
            let connection = self.connection()?;
            connection.execute_batch(
                "CREATE TABLE IF NOT EXISTS users (
                email TEXT PRIMARY KEY,
                email_verified INTEGER NOT NULL,
                email_confirmation_policy TEXT NOT NULL,
                created_at INTEGER NOT NULL
            );
            CREATE TABLE IF NOT EXISTS email_verification_codes (
                email TEXT PRIMARY KEY,
                code TEXT NOT NULL,
                created_at INTEGER NOT NULL,
                verified_at INTEGER
            );
            CREATE TABLE IF NOT EXISTS passkeys (
                credential_id TEXT PRIMARY KEY,
                email TEXT NOT NULL,
                public_key TEXT NOT NULL,
                label TEXT,
                created_at INTEGER NOT NULL,
                last_used_at INTEGER,
                FOREIGN KEY(email) REFERENCES users(email) ON DELETE CASCADE
            );
            CREATE TABLE IF NOT EXISTS ssh_keys (
                fingerprint TEXT PRIMARY KEY,
                email TEXT NOT NULL,
                public_key TEXT NOT NULL,
                label TEXT,
                created_at INTEGER NOT NULL,
                last_used_at INTEGER,
                FOREIGN KEY(email) REFERENCES users(email) ON DELETE CASCADE
            );
            CREATE TABLE IF NOT EXISTS sessions (
                token TEXT PRIMARY KEY,
                email TEXT NOT NULL,
                kind TEXT NOT NULL,
                created_at INTEGER NOT NULL,
                last_used_at INTEGER,
                FOREIGN KEY(email) REFERENCES users(email) ON DELETE CASCADE
            );
            CREATE TABLE IF NOT EXISTS auth_challenges (
                challenge_id TEXT PRIMARY KEY,
                email TEXT NOT NULL,
                kind TEXT NOT NULL,
                challenge TEXT NOT NULL,
                created_at INTEGER NOT NULL,
                used_at INTEGER,
                FOREIGN KEY(email) REFERENCES users(email) ON DELETE CASCADE
            );",
            )?;
            Ok(())
        }

        fn connection(&self) -> PrayResult<Connection> {
            let connection = Connection::open(&self.database_path)?;
            connection.pragma_update(None, "foreign_keys", "ON")?;
            Ok(connection)
        }
    }

    fn validate_email(email: &str) -> PrayResult<()> {
        let email = email.trim();
        if email.is_empty()
            || !email.contains('@')
            || email.starts_with('@')
            || email.ends_with('@')
        {
            return Err(PrayError::Unsupported(
                "email must be a non-empty address".to_string(),
            ));
        }
        Ok(())
    }

    fn validate_identifier(value: &str, label: &str) -> PrayResult<()> {
        if value.trim().is_empty() {
            return Err(PrayError::Unsupported(format!("{label} cannot be empty")));
        }
        Ok(())
    }

    fn validate_public_key(public_key: &str) -> PrayResult<()> {
        let public_key = public_key.trim();
        if public_key.is_empty() {
            return Err(PrayError::Unsupported(
                "public key cannot be empty".to_string(),
            ));
        }
        Ok(())
    }

    fn ensure_user_can_authenticate(connection: &Connection, email: &str) -> PrayResult<()> {
        let user: Option<(bool, String)> = connection
            .query_row(
                "SELECT email_verified, email_confirmation_policy FROM users WHERE email = ?1",
                rusqlite::params![email],
                |row| Ok((row.get(0)?, row.get(1)?)),
            )
            .optional()?;
        let Some((verified, policy)) = user else {
            return Err(PrayError::Resolution(format!("unknown user: {email}")));
        };
        if verified || policy == email_confirmation_policy_text(EmailConfirmationPolicy::Optional) {
            Ok(())
        } else {
            Err(PrayError::Resolution(format!(
                "email confirmation required for {email}"
            )))
        }
    }

    fn email_confirmation_policy_text(policy: EmailConfirmationPolicy) -> &'static str {
        match policy {
            EmailConfirmationPolicy::Required => "required",
            EmailConfirmationPolicy::Optional => "optional",
            EmailConfirmationPolicy::Disabled => "disabled",
        }
    }

    fn auth_session_kind_text(kind: &AuthSessionKind) -> &'static str {
        match kind {
            AuthSessionKind::Email => "email",
            AuthSessionKind::Passkey => "passkey",
            AuthSessionKind::SshKey => "ssh_key",
        }
    }

    fn parse_auth_session_kind(kind: &str) -> PrayResult<AuthSessionKind> {
        match kind {
            "email" => Ok(AuthSessionKind::Email),
            "passkey" => Ok(AuthSessionKind::Passkey),
            "ssh_key" => Ok(AuthSessionKind::SshKey),
            other => Err(PrayError::Resolution(format!(
                "unknown auth session kind: {other}"
            ))),
        }
    }

    fn current_unix_timestamp() -> PrayResult<u64> {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map_err(|error| PrayError::Resolution(error.to_string()))
            .map(|duration| duration.as_secs())
    }

    fn generate_auth_challenge(kind: &str, subject: &str) -> PrayResult<String> {
        let timestamp = current_unix_timestamp()?;
        Ok(sha256_prefixed(
            format!("{kind}\0{subject}\0{timestamp}").as_bytes(),
        ))
    }

    fn generate_challenge_id(
        email: &str,
        subject: &str,
        kind: &str,
        challenge: &str,
    ) -> PrayResult<String> {
        Ok(sha256_prefixed(
            format!("challenge\0{email}\0{subject}\0{kind}\0{challenge}").as_bytes(),
        ))
    }

    fn store_challenge(
        connection: &Connection,
        challenge_id: &str,
        email: &str,
        challenge: &str,
        kind: &str,
    ) -> PrayResult<()> {
        let timestamp = current_unix_timestamp()?;
        connection.execute(
        "INSERT INTO auth_challenges (challenge_id, email, kind, challenge, created_at, used_at)
         VALUES (?1, ?2, ?3, ?4, ?5, NULL)
         ON CONFLICT(challenge_id) DO UPDATE SET email = excluded.email, kind = excluded.kind, challenge = excluded.challenge, created_at = excluded.created_at, used_at = NULL",
        rusqlite::params![challenge_id, email, kind, challenge, timestamp],
    )?;
        Ok(())
    }

    fn load_challenge(
        connection: &Connection,
        challenge_id: &str,
        email: &str,
        kind: &str,
    ) -> PrayResult<StoredChallenge> {
        let challenge: Option<StoredChallenge> = connection
        .query_row(
            "SELECT challenge FROM auth_challenges WHERE challenge_id = ?1 AND email = ?2 AND kind = ?3 AND used_at IS NULL",
            rusqlite::params![challenge_id, email, kind],
            |row| Ok(StoredChallenge { challenge: row.get(0)? }),
        )
        .optional()?;
        challenge.ok_or_else(|| PrayError::Resolution(format!("challenge not found for {email}")))
    }

    fn mark_challenge_used(connection: &Connection, challenge_id: &str) -> PrayResult<()> {
        let timestamp = current_unix_timestamp()?;
        connection.execute(
            "UPDATE auth_challenges SET used_at = ?2 WHERE challenge_id = ?1",
            rusqlite::params![challenge_id, timestamp],
        )?;
        Ok(())
    }

    fn load_passkey_public_key(connection: &Connection, credential_id: &str) -> PrayResult<String> {
        let public_key: String = connection.query_row(
            "SELECT public_key FROM passkeys WHERE credential_id = ?1",
            rusqlite::params![credential_id],
            |row| row.get(0),
        )?;
        Ok(public_key)
    }

    fn validate_signature(signature: &str) -> PrayResult<()> {
        if signature.trim().is_empty() {
            return Err(PrayError::Unsupported(
                "signature cannot be empty".to_string(),
            ));
        }
        Ok(())
    }

    fn verify_signature(public_key: &str, message: &[u8], signature: &str) -> PrayResult<()> {
        let (_, key_bytes) = parse_ssh_ed25519_public_key(public_key)?;
        let verifying_key =
            VerifyingKey::from_bytes(&key_bytes).map_err(|error| PrayError::Parse {
                kind: "public key",
                message: error.to_string(),
            })?;
        let signature_bytes =
            STANDARD
                .decode(signature.as_bytes())
                .map_err(|error| PrayError::Parse {
                    kind: "signature",
                    message: error.to_string(),
                })?;
        let signature = Signature::from_slice(&signature_bytes)
            .map_err(|error| PrayError::Verify(error.to_string()))?;
        verifying_key
            .verify(message, &signature)
            .map_err(|error| PrayError::Verify(error.to_string()))
    }

    fn parse_ssh_ed25519_public_key(public_key: &str) -> PrayResult<(String, [u8; 32])> {
        let mut fields = public_key.split_whitespace();
        let algorithm = fields.next().ok_or_else(|| {
            PrayError::Unsupported("public key must include an algorithm".to_string())
        })?;
        if algorithm != "ssh-ed25519" {
            return Err(PrayError::Unsupported(format!(
                "unsupported public key algorithm: {algorithm}"
            )));
        }
        let key_value = fields.next().ok_or_else(|| {
            PrayError::Unsupported("public key must include key bytes".to_string())
        })?;
        let blob = STANDARD
            .decode(key_value.as_bytes())
            .map_err(|error| PrayError::Parse {
                kind: "public key",
                message: error.to_string(),
            })?;
        let mut cursor = blob.as_slice();
        let blob_algorithm = read_ssh_string(&mut cursor)?;
        if blob_algorithm != b"ssh-ed25519" {
            return Err(PrayError::Parse {
                kind: "public key",
                message: "ed25519 public key blob must start with ssh-ed25519".to_string(),
            });
        }
        let key_bytes = read_ssh_string(&mut cursor)?;
        let key_bytes: [u8; 32] =
            key_bytes
                .as_slice()
                .try_into()
                .map_err(|_| PrayError::Parse {
                    kind: "public key",
                    message: "ed25519 public key must be 32 bytes".to_string(),
                })?;
        Ok((format!("ssh-ed25519 {key_value}"), key_bytes))
    }

    fn read_ssh_string(cursor: &mut &[u8]) -> PrayResult<Vec<u8>> {
        let length = read_u32_from_slice(cursor)? as usize;
        if cursor.len() < length {
            return Err(PrayError::Resolution(
                "truncated ssh public key blob".to_string(),
            ));
        }
        let (value, rest) = cursor.split_at(length);
        *cursor = rest;
        Ok(value.to_vec())
    }

    fn read_u32_from_slice(cursor: &mut &[u8]) -> PrayResult<u32> {
        if cursor.len() < 4 {
            return Err(PrayError::Resolution("truncated ssh field".to_string()));
        }
        let (length_bytes, rest) = cursor.split_at(4);
        *cursor = rest;
        Ok(u32::from_be_bytes(
            length_bytes.try_into().expect("length bytes"),
        ))
    }

    fn generate_verification_code(email: &str, timestamp: u64) -> String {
        let payload = format!("{email}\0{timestamp}");
        let hash = sha256_prefixed(payload.as_bytes());
        let hex = hash.trim_start_matches("sha256:");
        let numeric = u32::from_str_radix(&hex[..8], 16).unwrap_or(0) % 1_000_000;
        format!("{:06}", numeric)
    }

    fn generate_session_token(email: &str, kind: &AuthSessionKind, timestamp: u64) -> String {
        let payload = format!("{email}\0{}\0{timestamp}", auth_session_kind_text(kind));
        sha256_prefixed(payload.as_bytes())
    }

    pub fn ssh_public_key_fingerprint_text(public_key: &str) -> PrayResult<String> {
        let (canonical, _) = parse_ssh_ed25519_public_key(public_key)?;
        Ok(normalize_ssh_fingerprint(&ssh_key_fingerprint(&canonical)))
    }

    fn normalize_ssh_fingerprint(fingerprint: &str) -> String {
        fingerprint.trim().to_ascii_uppercase()
    }

    fn ssh_key_fingerprint(public_key: &str) -> String {
        sha256_prefixed(public_key.as_bytes())
    }
}

#[cfg(not(feature = "auth"))]
pub fn ssh_public_key_fingerprint_text(public_key: &str) -> PrayResult<String> {
    let mut fields = public_key.split_whitespace();
    let algorithm = fields.next().ok_or_else(|| PrayError::Parse {
        kind: "public key",
        message: "public key must include an algorithm".to_string(),
    })?;
    let encoded_key = fields.next().ok_or_else(|| PrayError::Parse {
        kind: "public key",
        message: "public key must include key bytes".to_string(),
    })?;
    if algorithm != "ssh-ed25519" {
        return Err(PrayError::Unsupported(format!(
            "unsupported public key algorithm: {algorithm}"
        )));
    }

    Ok(sha256_prefixed(format!("{algorithm} {encoded_key}").as_bytes()).to_ascii_uppercase())
}

#[cfg(feature = "auth")]
pub use store::{ssh_public_key_fingerprint_text, RegistryAuthStore};

#[cfg(all(test, feature = "auth"))]
mod tests {
    use super::*;
    use ed25519_dalek::SigningKey;

    fn temporary_directory(prefix: &str) -> PathBuf {
        let unique = format!(
            "{}-{}-{}",
            prefix,
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .expect("system time")
                .as_nanos()
        );
        let path = std::env::temp_dir().join(unique);
        fs::create_dir_all(&path).expect("temporary directory");
        path
    }

    #[test]
    fn registers_and_verifies_email_with_required_confirmation() {
        let root = temporary_directory("pray-auth-required");
        let store = RegistryAuthStore::open(&root).expect("open store");

        let registration = store
            .register_email("alice@example.com", EmailConfirmationPolicy::Required)
            .expect("register");
        assert!(!registration.verified);
        let code = registration
            .verification_code
            .as_ref()
            .expect("verification code");
        assert_eq!(code.len(), 6);
        assert!(!store
            .user_verified("alice@example.com")
            .expect("user state"));

        let verification = store
            .verify_email("alice@example.com", code)
            .expect("verify");
        assert!(verification.verified);
        assert!(store
            .user_verified("alice@example.com")
            .expect("user state"));
    }

    #[test]
    fn registers_email_without_confirmation_when_disabled() {
        let root = temporary_directory("pray-auth-disabled");
        let store = RegistryAuthStore::open(&root).expect("open store");

        let registration = store
            .register_email("bob@example.com", EmailConfirmationPolicy::Disabled)
            .expect("register");
        assert!(registration.verified);
        assert!(registration.verification_code.is_none());
        assert!(store.user_verified("bob@example.com").expect("user state"));
    }

    #[test]
    fn issues_session_for_optional_email_without_confirmation() {
        let root = temporary_directory("pray-auth-session");
        let store = RegistryAuthStore::open(&root).expect("open store");

        store
            .register_email("carol@example.com", EmailConfirmationPolicy::Optional)
            .expect("register");
        let session = store
            .issue_session("carol@example.com", AuthSessionKind::Email)
            .expect("session");
        assert_eq!(session.email, "carol@example.com");
        assert!(session.token.starts_with("sha256:"));
        assert_eq!(session.kind, AuthSessionKind::Email);
        assert_eq!(
            store
                .resolve_session(&session.token)
                .expect("resolve session")
                .map(|session| session.email),
            Some("carol@example.com".to_string())
        );
    }

    #[test]
    fn enrolls_and_logs_in_with_passkey_and_ssh_key() {
        let root = temporary_directory("pray-auth-keys");
        let store = RegistryAuthStore::open(&root).expect("open store");

        let signing_key = signing_key_from_seed(17);
        let public_key = ssh_public_key_text(&signing_key);

        store
            .register_email("dave@example.com", EmailConfirmationPolicy::Optional)
            .expect("register");
        let passkey = store
            .enroll_passkey(
                "dave@example.com",
                "credential-1",
                &public_key,
                Some("laptop passkey"),
            )
            .expect("passkey enrollment");
        assert!(passkey.enrolled);
        let passkey_login = store
            .login_with_passkey("credential-1")
            .expect("passkey login");
        assert_eq!(passkey_login.email, "dave@example.com");

        let ssh_key = store
            .enroll_ssh_key("dave@example.com", &public_key, Some("workstation"))
            .expect("ssh enrollment");
        assert!(ssh_key.enrolled);
        let ssh_login = store.login_with_ssh_key(&public_key).expect("ssh login");
        assert_eq!(ssh_login.email, "dave@example.com");
    }

    fn ssh_public_key_text(signing_key: &SigningKey) -> String {
        let mut blob = Vec::new();
        write_ssh_string(&mut blob, b"ssh-ed25519");
        write_ssh_string(&mut blob, &signing_key.verifying_key().to_bytes());
        format!("ssh-ed25519 {}", STANDARD.encode(blob))
    }

    fn write_ssh_string(buffer: &mut Vec<u8>, bytes: &[u8]) {
        buffer.extend_from_slice(&(bytes.len() as u32).to_be_bytes());
        buffer.extend_from_slice(bytes);
    }

    fn signing_key_from_seed(seed: u8) -> SigningKey {
        SigningKey::from_bytes(&[seed; 32])
    }
}