acton-service 0.20.0

Production-ready Rust backend framework with type-enforced API versioning
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
//! API key authentication module
//!
//! Provides API key generation, validation, and storage for service-to-service
//! authentication. API keys follow the format: `{prefix}_{random_base32}`.
//!
//! # Example
//!
//! ```rust,ignore
//! use acton_service::auth::{ApiKeyGenerator, ApiKey};
//!
//! let generator = ApiKeyGenerator::new("sk_live");
//!
//! // Generate a new API key
//! let (key, key_hash) = generator.generate();
//! // key = "sk_live_abc123..." (show to user once)
//! // key_hash = "$argon2id$..." (store in database)
//!
//! // Later, verify an incoming key
//! if generator.verify(&incoming_key, &stored_hash)? {
//!     // Key is valid
//! }
//! ```

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use crate::auth::password::PasswordHasher;
use crate::error::Error;

/// API key structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiKey {
    /// Database ID
    pub id: String,

    /// User/owner ID
    pub user_id: String,

    /// User-provided name for the key
    pub name: String,

    /// Key prefix (e.g., "sk_live")
    pub prefix: String,

    /// Hashed key value (stored, not the actual key)
    pub key_hash: String,

    /// Allowed scopes/permissions
    #[serde(default)]
    pub scopes: Vec<String>,

    /// Rate limit (requests per minute, None = default)
    pub rate_limit: Option<u32>,

    /// Whether this key has been revoked
    #[serde(default)]
    pub is_revoked: bool,

    /// When this key was last used
    pub last_used_at: Option<DateTime<Utc>>,

    /// When this key expires (None = never)
    pub expires_at: Option<DateTime<Utc>>,

    /// When this key was created
    pub created_at: DateTime<Utc>,
}

impl ApiKey {
    /// Check if the key is currently valid (not revoked, not expired)
    pub fn is_valid(&self) -> bool {
        if self.is_revoked {
            return false;
        }

        if let Some(expires_at) = self.expires_at {
            if expires_at < Utc::now() {
                return false;
            }
        }

        true
    }

    /// Check if the key has a specific scope
    pub fn has_scope(&self, scope: &str) -> bool {
        self.scopes.iter().any(|s| s == scope)
    }
}

/// API key generator
///
/// Generates API keys in the format `{prefix}_{random_base32}`.
/// Keys are hashed using Argon2id before storage.
#[derive(Clone)]
pub struct ApiKeyGenerator {
    prefix: String,
    hasher: PasswordHasher,
}

impl ApiKeyGenerator {
    /// Create a new API key generator with the given prefix
    ///
    /// # Arguments
    ///
    /// * `prefix` - Key prefix (e.g., "sk_live", "sk_test", "acton")
    pub fn new(prefix: impl Into<String>) -> Self {
        Self {
            prefix: prefix.into(),
            hasher: PasswordHasher::default(),
        }
    }

    /// Generate a new API key
    ///
    /// Returns a tuple of (key, hash) where:
    /// - `key` is the plaintext key to show to the user (once!)
    /// - `hash` is the Argon2id hash to store in the database
    pub fn generate(&self) -> (String, String) {
        // Generate 24 random bytes (192 bits of entropy)
        let random_bytes: [u8; 24] = rand::random();

        // Encode as base32 (no padding, lowercase)
        let encoded = base32_encode(&random_bytes);

        // Create the full key
        let key = format!("{}_{}", self.prefix, encoded);

        // Hash the key for storage
        // Note: We use a custom hasher config with lower memory for API keys
        // since they have high entropy and don't need the same protection as passwords
        let hash = self.hasher.hash(&key).expect("Failed to hash API key");

        (key, hash)
    }

    /// Verify an API key against a stored hash
    pub fn verify(&self, key: &str, hash: &str) -> Result<bool, Error> {
        self.hasher.verify(key, hash)
    }

    /// Extract the prefix from a key
    pub fn extract_prefix(key: &str) -> Option<&str> {
        key.split('_').next()
    }

    /// Get the first few characters of a key for lookup
    /// (useful for indexing without storing the full key)
    pub fn key_prefix_for_lookup(key: &str) -> Option<String> {
        // Return the prefix + first 8 chars of the random part
        // Key format is "{prefix}_{random}" where prefix can contain underscores (e.g., "sk_live")
        // So we split from the right to find the random part
        let parts: Vec<&str> = key.rsplitn(2, '_').collect();
        if parts.len() == 2 && parts[0].len() >= 8 {
            // parts[0] is the random part, parts[1] is the prefix
            Some(format!("{}_{}", parts[1], &parts[0][..8]))
        } else {
            None
        }
    }
}

/// Encode bytes as lowercase base32 without padding
fn base32_encode(bytes: &[u8]) -> String {
    const ALPHABET: &[u8] = b"abcdefghijklmnopqrstuvwxyz234567";

    let mut result = String::with_capacity((bytes.len() * 8).div_ceil(5));
    let mut buffer = 0u64;
    let mut bits = 0;

    for &byte in bytes {
        buffer = (buffer << 8) | byte as u64;
        bits += 8;

        while bits >= 5 {
            bits -= 5;
            let index = ((buffer >> bits) & 0x1f) as usize;
            result.push(ALPHABET[index] as char);
        }
    }

    if bits > 0 {
        let index = ((buffer << (5 - bits)) & 0x1f) as usize;
        result.push(ALPHABET[index] as char);
    }

    result
}

use async_trait::async_trait;

/// API key storage trait
///
/// Implementations of this trait provide storage for API keys.
#[async_trait]
pub trait ApiKeyStorage: Send + Sync {
    /// Get an API key by the full key value (for verification)
    async fn get_by_key(&self, key: &str) -> Result<Option<ApiKey>, Error>;

    /// Get an API key by key prefix (for quick lookup)
    async fn get_by_prefix(&self, prefix: &str) -> Result<Option<ApiKey>, Error>;

    /// Get an API key by its database ID
    async fn get_by_id(&self, id: &str) -> Result<Option<ApiKey>, Error>;

    /// Store a new API key
    async fn create(&self, key: &ApiKey) -> Result<(), Error>;

    /// Update the last_used_at timestamp
    async fn update_last_used(&self, id: &str) -> Result<(), Error>;

    /// Revoke an API key
    async fn revoke(&self, id: &str) -> Result<(), Error>;

    /// List all API keys for a user
    async fn list_by_user(&self, user_id: &str) -> Result<Vec<ApiKey>, Error>;

    /// Delete an API key
    async fn delete(&self, id: &str) -> Result<(), Error>;
}

/// Redis-based API key storage
#[cfg(feature = "cache")]
pub mod redis_storage {
    use super::*;
    use deadpool_redis::Pool;
    use redis::AsyncCommands;

    /// Redis-backed API key storage
    #[derive(Clone)]
    pub struct RedisApiKeyStorage {
        pool: Pool,
        key_prefix: String,
        generator: ApiKeyGenerator,
    }

    impl RedisApiKeyStorage {
        /// Create a new Redis API key storage
        pub fn new(pool: Pool, api_key_prefix: impl Into<String>) -> Self {
            let prefix: String = api_key_prefix.into();
            Self {
                pool,
                key_prefix: "api_key".to_string(),
                generator: ApiKeyGenerator::new(&prefix),
            }
        }

        fn id_key(&self, id: &str) -> String {
            format!("{}:id:{}", self.key_prefix, id)
        }

        fn prefix_key(&self, prefix: &str) -> String {
            format!("{}:prefix:{}", self.key_prefix, prefix)
        }

        fn user_key(&self, user_id: &str) -> String {
            format!("{}:user:{}", self.key_prefix, user_id)
        }
    }

    #[async_trait]
    impl ApiKeyStorage for RedisApiKeyStorage {
        async fn get_by_key(&self, key: &str) -> Result<Option<ApiKey>, Error> {
            // Get by lookup prefix
            let lookup_prefix = ApiKeyGenerator::key_prefix_for_lookup(key)
                .ok_or_else(|| Error::ValidationError("Invalid API key format".to_string()))?;

            if let Some(api_key) = self.get_by_prefix(&lookup_prefix).await? {
                // Verify the full key matches
                if self.generator.verify(key, &api_key.key_hash)? {
                    return Ok(Some(api_key));
                }
            }
            Ok(None)
        }

        async fn get_by_prefix(&self, prefix: &str) -> Result<Option<ApiKey>, Error> {
            let mut conn =
                self.pool.get().await.map_err(|e| {
                    Error::Internal(format!("Failed to get Redis connection: {}", e))
                })?;

            let key = self.prefix_key(prefix);
            let id: Option<String> = conn
                .get(&key)
                .await
                .map_err(|e| Error::Internal(format!("Failed to get API key prefix: {}", e)))?;

            match id {
                Some(id) => self.get_by_id(&id).await,
                None => Ok(None),
            }
        }

        async fn get_by_id(&self, id: &str) -> Result<Option<ApiKey>, Error> {
            let mut conn =
                self.pool.get().await.map_err(|e| {
                    Error::Internal(format!("Failed to get Redis connection: {}", e))
                })?;

            let key = self.id_key(id);
            let json: Option<String> = conn
                .get(&key)
                .await
                .map_err(|e| Error::Internal(format!("Failed to get API key: {}", e)))?;

            match json {
                Some(j) => {
                    let api_key: ApiKey = serde_json::from_str(&j)
                        .map_err(|e| Error::Internal(format!("Failed to parse API key: {}", e)))?;
                    Ok(Some(api_key))
                }
                None => Ok(None),
            }
        }

        async fn create(&self, api_key: &ApiKey) -> Result<(), Error> {
            let mut conn =
                self.pool.get().await.map_err(|e| {
                    Error::Internal(format!("Failed to get Redis connection: {}", e))
                })?;

            let json = serde_json::to_string(api_key)
                .map_err(|e| Error::Internal(format!("Failed to serialize API key: {}", e)))?;

            // Store by ID
            let id_key = self.id_key(&api_key.id);
            conn.set::<_, _, ()>(&id_key, &json)
                .await
                .map_err(|e| Error::Internal(format!("Failed to store API key: {}", e)))?;

            // Store prefix -> ID mapping
            let prefix_key = self.prefix_key(&api_key.prefix);
            conn.set::<_, _, ()>(&prefix_key, &api_key.id)
                .await
                .map_err(|e| Error::Internal(format!("Failed to store prefix mapping: {}", e)))?;

            // Add to user's key set
            let user_key = self.user_key(&api_key.user_id);
            conn.sadd::<_, _, ()>(&user_key, &api_key.id)
                .await
                .map_err(|e| Error::Internal(format!("Failed to add to user set: {}", e)))?;

            Ok(())
        }

        async fn update_last_used(&self, id: &str) -> Result<(), Error> {
            if let Some(mut api_key) = self.get_by_id(id).await? {
                api_key.last_used_at = Some(Utc::now());
                let json = serde_json::to_string(&api_key)
                    .map_err(|e| Error::Internal(format!("Failed to serialize API key: {}", e)))?;

                let mut conn = self.pool.get().await.map_err(|e| {
                    Error::Internal(format!("Failed to get Redis connection: {}", e))
                })?;

                let key = self.id_key(id);
                conn.set::<_, _, ()>(&key, &json)
                    .await
                    .map_err(|e| Error::Internal(format!("Failed to update API key: {}", e)))?;
            }
            Ok(())
        }

        async fn revoke(&self, id: &str) -> Result<(), Error> {
            if let Some(mut api_key) = self.get_by_id(id).await? {
                api_key.is_revoked = true;
                let json = serde_json::to_string(&api_key)
                    .map_err(|e| Error::Internal(format!("Failed to serialize API key: {}", e)))?;

                let mut conn = self.pool.get().await.map_err(|e| {
                    Error::Internal(format!("Failed to get Redis connection: {}", e))
                })?;

                let key = self.id_key(id);
                conn.set::<_, _, ()>(&key, &json)
                    .await
                    .map_err(|e| Error::Internal(format!("Failed to revoke API key: {}", e)))?;
            }
            Ok(())
        }

        async fn list_by_user(&self, user_id: &str) -> Result<Vec<ApiKey>, Error> {
            let mut conn =
                self.pool.get().await.map_err(|e| {
                    Error::Internal(format!("Failed to get Redis connection: {}", e))
                })?;

            let user_key = self.user_key(user_id);
            let ids: Vec<String> = conn
                .smembers(&user_key)
                .await
                .map_err(|e| Error::Internal(format!("Failed to get user keys: {}", e)))?;

            let mut keys = Vec::new();
            for id in ids {
                if let Some(api_key) = self.get_by_id(&id).await? {
                    keys.push(api_key);
                }
            }
            Ok(keys)
        }

        async fn delete(&self, id: &str) -> Result<(), Error> {
            if let Some(api_key) = self.get_by_id(id).await? {
                let mut conn = self.pool.get().await.map_err(|e| {
                    Error::Internal(format!("Failed to get Redis connection: {}", e))
                })?;

                // Remove from user set
                let user_key = self.user_key(&api_key.user_id);
                conn.srem::<_, _, ()>(&user_key, id).await.map_err(|e| {
                    Error::Internal(format!("Failed to remove from user set: {}", e))
                })?;

                // Remove prefix mapping
                let prefix_key = self.prefix_key(&api_key.prefix);
                conn.del::<_, ()>(&prefix_key)
                    .await
                    .map_err(|e| Error::Internal(format!("Failed to delete prefix: {}", e)))?;

                // Remove the key itself
                let id_key = self.id_key(id);
                conn.del::<_, ()>(&id_key)
                    .await
                    .map_err(|e| Error::Internal(format!("Failed to delete API key: {}", e)))?;
            }
            Ok(())
        }
    }
}

#[cfg(feature = "cache")]
pub use redis_storage::RedisApiKeyStorage;

/// PostgreSQL-based API key storage
#[cfg(feature = "database")]
pub mod pg_storage {
    use super::*;
    use sqlx::PgPool;

    /// PostgreSQL-backed API key storage
    #[derive(Clone)]
    pub struct PgApiKeyStorage {
        pool: PgPool,
        generator: ApiKeyGenerator,
    }

    impl PgApiKeyStorage {
        /// Create a new PostgreSQL API key storage
        pub fn new(pool: PgPool, api_key_prefix: impl Into<String>) -> Self {
            Self {
                pool,
                generator: ApiKeyGenerator::new(api_key_prefix),
            }
        }
    }

    #[async_trait]
    impl ApiKeyStorage for PgApiKeyStorage {
        async fn get_by_key(&self, key: &str) -> Result<Option<ApiKey>, Error> {
            // Get by lookup prefix
            let lookup_prefix = ApiKeyGenerator::key_prefix_for_lookup(key)
                .ok_or_else(|| Error::ValidationError("Invalid API key format".to_string()))?;

            if let Some(api_key) = self.get_by_prefix(&lookup_prefix).await? {
                // Verify the full key matches
                if self.generator.verify(key, &api_key.key_hash)? {
                    return Ok(Some(api_key));
                }
            }
            Ok(None)
        }

        async fn get_by_prefix(&self, prefix: &str) -> Result<Option<ApiKey>, Error> {
            let row = sqlx::query_as::<_, (String, String, String, String, String, serde_json::Value, Option<i32>, bool, Option<DateTime<Utc>>, Option<DateTime<Utc>>, DateTime<Utc>)>(
                r#"
                SELECT id, user_id, name, key_prefix, key_hash, scopes, rate_limit, is_revoked, last_used_at, expires_at, created_at
                FROM api_keys
                WHERE key_prefix = $1
                "#,
            )
            .bind(prefix)
            .fetch_optional(&self.pool)
            .await
            .map_err(|e| Error::Internal(format!("Failed to get API key: {}", e)))?;

            match row {
                Some((
                    id,
                    user_id,
                    name,
                    prefix,
                    key_hash,
                    scopes_json,
                    rate_limit,
                    is_revoked,
                    last_used_at,
                    expires_at,
                    created_at,
                )) => {
                    let scopes: Vec<String> =
                        serde_json::from_value(scopes_json).unwrap_or_default();
                    Ok(Some(ApiKey {
                        id,
                        user_id,
                        name,
                        prefix,
                        key_hash,
                        scopes,
                        rate_limit: rate_limit.map(|r| r as u32),
                        is_revoked,
                        last_used_at,
                        expires_at,
                        created_at,
                    }))
                }
                None => Ok(None),
            }
        }

        async fn get_by_id(&self, id: &str) -> Result<Option<ApiKey>, Error> {
            let row = sqlx::query_as::<_, (String, String, String, String, String, serde_json::Value, Option<i32>, bool, Option<DateTime<Utc>>, Option<DateTime<Utc>>, DateTime<Utc>)>(
                r#"
                SELECT id, user_id, name, key_prefix, key_hash, scopes, rate_limit, is_revoked, last_used_at, expires_at, created_at
                FROM api_keys
                WHERE id = $1
                "#,
            )
            .bind(id)
            .fetch_optional(&self.pool)
            .await
            .map_err(|e| Error::Internal(format!("Failed to get API key: {}", e)))?;

            match row {
                Some((
                    id,
                    user_id,
                    name,
                    prefix,
                    key_hash,
                    scopes_json,
                    rate_limit,
                    is_revoked,
                    last_used_at,
                    expires_at,
                    created_at,
                )) => {
                    let scopes: Vec<String> =
                        serde_json::from_value(scopes_json).unwrap_or_default();
                    Ok(Some(ApiKey {
                        id,
                        user_id,
                        name,
                        prefix,
                        key_hash,
                        scopes,
                        rate_limit: rate_limit.map(|r| r as u32),
                        is_revoked,
                        last_used_at,
                        expires_at,
                        created_at,
                    }))
                }
                None => Ok(None),
            }
        }

        async fn create(&self, api_key: &ApiKey) -> Result<(), Error> {
            let scopes_json = serde_json::to_value(&api_key.scopes)
                .map_err(|e| Error::Internal(format!("Failed to serialize scopes: {}", e)))?;

            sqlx::query(
                r#"
                INSERT INTO api_keys (id, user_id, name, key_prefix, key_hash, scopes, rate_limit, is_revoked, expires_at, created_at)
                VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
                "#,
            )
            .bind(&api_key.id)
            .bind(&api_key.user_id)
            .bind(&api_key.name)
            .bind(&api_key.prefix)
            .bind(&api_key.key_hash)
            .bind(scopes_json)
            .bind(api_key.rate_limit.map(|r| r as i32))
            .bind(api_key.is_revoked)
            .bind(api_key.expires_at)
            .bind(api_key.created_at)
            .execute(&self.pool)
            .await
            .map_err(|e| Error::Internal(format!("Failed to create API key: {}", e)))?;

            Ok(())
        }

        async fn update_last_used(&self, id: &str) -> Result<(), Error> {
            sqlx::query("UPDATE api_keys SET last_used_at = NOW() WHERE id = $1")
                .bind(id)
                .execute(&self.pool)
                .await
                .map_err(|e| Error::Internal(format!("Failed to update last_used_at: {}", e)))?;

            Ok(())
        }

        async fn revoke(&self, id: &str) -> Result<(), Error> {
            sqlx::query("UPDATE api_keys SET is_revoked = true WHERE id = $1")
                .bind(id)
                .execute(&self.pool)
                .await
                .map_err(|e| Error::Internal(format!("Failed to revoke API key: {}", e)))?;

            Ok(())
        }

        async fn list_by_user(&self, user_id: &str) -> Result<Vec<ApiKey>, Error> {
            let rows = sqlx::query_as::<_, (String, String, String, String, String, serde_json::Value, Option<i32>, bool, Option<DateTime<Utc>>, Option<DateTime<Utc>>, DateTime<Utc>)>(
                r#"
                SELECT id, user_id, name, key_prefix, key_hash, scopes, rate_limit, is_revoked, last_used_at, expires_at, created_at
                FROM api_keys
                WHERE user_id = $1
                ORDER BY created_at DESC
                "#,
            )
            .bind(user_id)
            .fetch_all(&self.pool)
            .await
            .map_err(|e| Error::Internal(format!("Failed to list API keys: {}", e)))?;

            let keys = rows
                .into_iter()
                .map(
                    |(
                        id,
                        user_id,
                        name,
                        prefix,
                        key_hash,
                        scopes_json,
                        rate_limit,
                        is_revoked,
                        last_used_at,
                        expires_at,
                        created_at,
                    )| {
                        let scopes: Vec<String> =
                            serde_json::from_value(scopes_json).unwrap_or_default();
                        ApiKey {
                            id,
                            user_id,
                            name,
                            prefix,
                            key_hash,
                            scopes,
                            rate_limit: rate_limit.map(|r| r as u32),
                            is_revoked,
                            last_used_at,
                            expires_at,
                            created_at,
                        }
                    },
                )
                .collect();

            Ok(keys)
        }

        async fn delete(&self, id: &str) -> Result<(), Error> {
            sqlx::query("DELETE FROM api_keys WHERE id = $1")
                .bind(id)
                .execute(&self.pool)
                .await
                .map_err(|e| Error::Internal(format!("Failed to delete API key: {}", e)))?;

            Ok(())
        }
    }
}

#[cfg(feature = "database")]
pub use pg_storage::PgApiKeyStorage;

/// Turso/libsql-based API key storage
#[cfg(feature = "turso")]
pub mod turso_storage {
    use super::*;
    use libsql::Connection;
    use std::sync::Arc;

    /// Turso-backed API key storage
    #[derive(Clone)]
    pub struct TursoApiKeyStorage {
        conn: Arc<Connection>,
        generator: ApiKeyGenerator,
    }

    impl TursoApiKeyStorage {
        /// Create a new Turso API key storage
        pub fn new(conn: Arc<Connection>, api_key_prefix: impl Into<String>) -> Self {
            Self {
                conn,
                generator: ApiKeyGenerator::new(api_key_prefix),
            }
        }
    }

    #[async_trait]
    impl ApiKeyStorage for TursoApiKeyStorage {
        async fn get_by_key(&self, key: &str) -> Result<Option<ApiKey>, Error> {
            let lookup_prefix = ApiKeyGenerator::key_prefix_for_lookup(key)
                .ok_or_else(|| Error::ValidationError("Invalid API key format".to_string()))?;

            if let Some(api_key) = self.get_by_prefix(&lookup_prefix).await? {
                if self.generator.verify(key, &api_key.key_hash)? {
                    return Ok(Some(api_key));
                }
            }
            Ok(None)
        }

        async fn get_by_prefix(&self, prefix: &str) -> Result<Option<ApiKey>, Error> {
            let mut rows = self
                .conn
                .query(
                    "SELECT id, user_id, name, key_prefix, key_hash, scopes, rate_limit, is_revoked, last_used_at, expires_at, created_at FROM api_keys WHERE key_prefix = ?1",
                    libsql::params![prefix],
                )
                .await
                .map_err(|e| Error::Internal(format!("Failed to get API key: {}", e)))?;

            if let Some(row) = rows
                .next()
                .await
                .map_err(|e| Error::Internal(format!("Failed to fetch row: {}", e)))?
            {
                let api_key = parse_api_key_row(&row)?;
                Ok(Some(api_key))
            } else {
                Ok(None)
            }
        }

        async fn get_by_id(&self, id: &str) -> Result<Option<ApiKey>, Error> {
            let mut rows = self
                .conn
                .query(
                    "SELECT id, user_id, name, key_prefix, key_hash, scopes, rate_limit, is_revoked, last_used_at, expires_at, created_at FROM api_keys WHERE id = ?1",
                    libsql::params![id],
                )
                .await
                .map_err(|e| Error::Internal(format!("Failed to get API key: {}", e)))?;

            if let Some(row) = rows
                .next()
                .await
                .map_err(|e| Error::Internal(format!("Failed to fetch row: {}", e)))?
            {
                let api_key = parse_api_key_row(&row)?;
                Ok(Some(api_key))
            } else {
                Ok(None)
            }
        }

        async fn create(&self, api_key: &ApiKey) -> Result<(), Error> {
            let scopes_json = serde_json::to_string(&api_key.scopes)
                .map_err(|e| Error::Internal(format!("Failed to serialize scopes: {}", e)))?;

            let expires_at = api_key.expires_at.map(|dt| dt.to_rfc3339());

            self.conn
                .execute(
                    "INSERT INTO api_keys (id, user_id, name, key_prefix, key_hash, scopes, rate_limit, is_revoked, expires_at, created_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)",
                    libsql::params![
                        api_key.id.clone(),
                        api_key.user_id.clone(),
                        api_key.name.clone(),
                        api_key.prefix.clone(),
                        api_key.key_hash.clone(),
                        scopes_json,
                        api_key.rate_limit.map(|r| r as i64),
                        if api_key.is_revoked { 1i64 } else { 0i64 },
                        expires_at,
                        api_key.created_at.to_rfc3339()
                    ],
                )
                .await
                .map_err(|e| Error::Internal(format!("Failed to create API key: {}", e)))?;

            Ok(())
        }

        async fn update_last_used(&self, id: &str) -> Result<(), Error> {
            self.conn
                .execute(
                    "UPDATE api_keys SET last_used_at = datetime('now') WHERE id = ?1",
                    libsql::params![id],
                )
                .await
                .map_err(|e| Error::Internal(format!("Failed to update last_used_at: {}", e)))?;

            Ok(())
        }

        async fn revoke(&self, id: &str) -> Result<(), Error> {
            self.conn
                .execute(
                    "UPDATE api_keys SET is_revoked = 1 WHERE id = ?1",
                    libsql::params![id],
                )
                .await
                .map_err(|e| Error::Internal(format!("Failed to revoke API key: {}", e)))?;

            Ok(())
        }

        async fn list_by_user(&self, user_id: &str) -> Result<Vec<ApiKey>, Error> {
            let mut rows = self
                .conn
                .query(
                    "SELECT id, user_id, name, key_prefix, key_hash, scopes, rate_limit, is_revoked, last_used_at, expires_at, created_at FROM api_keys WHERE user_id = ?1 ORDER BY created_at DESC",
                    libsql::params![user_id],
                )
                .await
                .map_err(|e| Error::Internal(format!("Failed to list API keys: {}", e)))?;

            let mut keys = Vec::new();
            while let Some(row) = rows
                .next()
                .await
                .map_err(|e| Error::Internal(format!("Failed to fetch row: {}", e)))?
            {
                let api_key = parse_api_key_row(&row)?;
                keys.push(api_key);
            }
            Ok(keys)
        }

        async fn delete(&self, id: &str) -> Result<(), Error> {
            self.conn
                .execute("DELETE FROM api_keys WHERE id = ?1", libsql::params![id])
                .await
                .map_err(|e| Error::Internal(format!("Failed to delete API key: {}", e)))?;

            Ok(())
        }
    }

    fn parse_api_key_row(row: &libsql::Row) -> Result<ApiKey, Error> {
        let id: String = row
            .get(0)
            .map_err(|e| Error::Internal(format!("Failed to get id: {}", e)))?;
        let user_id: String = row
            .get(1)
            .map_err(|e| Error::Internal(format!("Failed to get user_id: {}", e)))?;
        let name: String = row
            .get(2)
            .map_err(|e| Error::Internal(format!("Failed to get name: {}", e)))?;
        let prefix: String = row
            .get(3)
            .map_err(|e| Error::Internal(format!("Failed to get key_prefix: {}", e)))?;
        let key_hash: String = row
            .get(4)
            .map_err(|e| Error::Internal(format!("Failed to get key_hash: {}", e)))?;
        let scopes_str: String = row
            .get(5)
            .map_err(|e| Error::Internal(format!("Failed to get scopes: {}", e)))?;
        let rate_limit: Option<i64> = row.get(6).ok();
        let is_revoked: i64 = row
            .get(7)
            .map_err(|e| Error::Internal(format!("Failed to get is_revoked: {}", e)))?;
        let last_used_at_str: Option<String> = row.get(8).ok();
        let expires_at_str: Option<String> = row.get(9).ok();
        let created_at_str: String = row
            .get(10)
            .map_err(|e| Error::Internal(format!("Failed to get created_at: {}", e)))?;

        let scopes: Vec<String> = serde_json::from_str(&scopes_str).unwrap_or_default();
        let last_used_at = last_used_at_str
            .and_then(|s| DateTime::parse_from_rfc3339(&s).ok())
            .map(|dt| dt.with_timezone(&Utc));
        let expires_at = expires_at_str
            .and_then(|s| DateTime::parse_from_rfc3339(&s).ok())
            .map(|dt| dt.with_timezone(&Utc));
        let created_at = DateTime::parse_from_rfc3339(&created_at_str)
            .map(|dt| dt.with_timezone(&Utc))
            .unwrap_or_else(|_| Utc::now());

        Ok(ApiKey {
            id,
            user_id,
            name,
            prefix,
            key_hash,
            scopes,
            rate_limit: rate_limit.map(|r| r as u32),
            is_revoked: is_revoked != 0,
            last_used_at,
            expires_at,
            created_at,
        })
    }
}

#[cfg(feature = "turso")]
pub use turso_storage::TursoApiKeyStorage;

/// SurrealDB-based API key storage
#[cfg(feature = "surrealdb")]
pub mod surrealdb_storage {
    use super::*;
    use crate::surrealdb_backend::SurrealClient;
    use std::sync::Arc;

    /// SurrealDB-backed API key storage
    #[derive(Clone)]
    pub struct SurrealDbApiKeyStorage {
        client: Arc<SurrealClient>,
        generator: ApiKeyGenerator,
    }

    impl SurrealDbApiKeyStorage {
        /// Create a new SurrealDB API key storage
        pub fn new(client: Arc<SurrealClient>, api_key_prefix: impl Into<String>) -> Self {
            Self {
                client,
                generator: ApiKeyGenerator::new(api_key_prefix),
            }
        }
    }

    #[async_trait]
    impl ApiKeyStorage for SurrealDbApiKeyStorage {
        async fn get_by_key(&self, key: &str) -> Result<Option<ApiKey>, Error> {
            let lookup_prefix = ApiKeyGenerator::key_prefix_for_lookup(key)
                .ok_or_else(|| Error::ValidationError("Invalid API key format".to_string()))?;

            if let Some(api_key) = self.get_by_prefix(&lookup_prefix).await? {
                if self.generator.verify(key, &api_key.key_hash)? {
                    return Ok(Some(api_key));
                }
            }
            Ok(None)
        }

        async fn get_by_prefix(&self, prefix: &str) -> Result<Option<ApiKey>, Error> {
            let mut result = self
                .client
                .query("SELECT * FROM api_keys WHERE prefix = $prefix LIMIT 1")
                .bind(("prefix", prefix.to_string()))
                .await
                .map_err(|e| Error::Internal(format!("Failed to get API key: {}", e)))?;

            let api_key: Option<ApiKey> = result
                .take(0)
                .map_err(|e| Error::Internal(format!("Failed to parse API key: {}", e)))?;

            Ok(api_key)
        }

        async fn get_by_id(&self, id: &str) -> Result<Option<ApiKey>, Error> {
            let mut result = self
                .client
                .query("SELECT * FROM api_keys WHERE id = $id LIMIT 1")
                .bind(("id", id.to_string()))
                .await
                .map_err(|e| Error::Internal(format!("Failed to get API key: {}", e)))?;

            let api_key: Option<ApiKey> = result
                .take(0)
                .map_err(|e| Error::Internal(format!("Failed to parse API key: {}", e)))?;

            Ok(api_key)
        }

        async fn create(&self, api_key: &ApiKey) -> Result<(), Error> {
            self.client
                .query("CREATE api_keys CONTENT $data")
                .bind(("data", api_key.clone()))
                .await
                .map_err(|e| Error::Internal(format!("Failed to create API key: {}", e)))?;

            Ok(())
        }

        async fn update_last_used(&self, id: &str) -> Result<(), Error> {
            self.client
                .query("UPDATE api_keys SET last_used_at = time::now() WHERE id = $id")
                .bind(("id", id.to_string()))
                .await
                .map_err(|e| Error::Internal(format!("Failed to update last_used_at: {}", e)))?;

            Ok(())
        }

        async fn revoke(&self, id: &str) -> Result<(), Error> {
            self.client
                .query("UPDATE api_keys SET is_revoked = true WHERE id = $id")
                .bind(("id", id.to_string()))
                .await
                .map_err(|e| Error::Internal(format!("Failed to revoke API key: {}", e)))?;

            Ok(())
        }

        async fn list_by_user(&self, user_id: &str) -> Result<Vec<ApiKey>, Error> {
            let mut result = self
                .client
                .query("SELECT * FROM api_keys WHERE user_id = $user_id ORDER BY created_at DESC")
                .bind(("user_id", user_id.to_string()))
                .await
                .map_err(|e| Error::Internal(format!("Failed to list API keys: {}", e)))?;

            let keys: Vec<ApiKey> = result
                .take(0)
                .map_err(|e| Error::Internal(format!("Failed to parse API keys: {}", e)))?;

            Ok(keys)
        }

        async fn delete(&self, id: &str) -> Result<(), Error> {
            self.client
                .query("DELETE FROM api_keys WHERE id = $id")
                .bind(("id", id.to_string()))
                .await
                .map_err(|e| Error::Internal(format!("Failed to delete API key: {}", e)))?;

            Ok(())
        }
    }
}

#[cfg(feature = "surrealdb")]
pub use surrealdb_storage::SurrealDbApiKeyStorage;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_generate_api_key() {
        let generator = ApiKeyGenerator::new("sk_live");
        let (key, hash) = generator.generate();

        assert!(key.starts_with("sk_live_"));
        assert!(hash.starts_with("$argon2id$"));

        // Key should be unique each time
        let (key2, _) = generator.generate();
        assert_ne!(key, key2);
    }

    #[test]
    fn test_verify_api_key() {
        let generator = ApiKeyGenerator::new("sk_test");
        let (key, hash) = generator.generate();

        assert!(generator.verify(&key, &hash).unwrap());
        assert!(!generator.verify("wrong_key", &hash).unwrap());
    }

    #[test]
    fn test_extract_prefix() {
        assert_eq!(
            ApiKeyGenerator::extract_prefix("sk_live_abc123"),
            Some("sk")
        );
        assert_eq!(
            ApiKeyGenerator::extract_prefix("acton_xyz789"),
            Some("acton")
        );
    }

    #[test]
    fn test_key_prefix_for_lookup() {
        let lookup = ApiKeyGenerator::key_prefix_for_lookup("sk_live_abcdefghijklmnop");
        assert_eq!(lookup, Some("sk_live_abcdefgh".to_string()));
    }

    #[test]
    fn test_api_key_validity() {
        let key = ApiKey {
            id: "1".to_string(),
            user_id: "user:123".to_string(),
            name: "Test Key".to_string(),
            prefix: "sk_live".to_string(),
            key_hash: "hash".to_string(),
            scopes: vec!["read".to_string(), "write".to_string()],
            rate_limit: None,
            is_revoked: false,
            last_used_at: None,
            expires_at: None,
            created_at: Utc::now(),
        };

        assert!(key.is_valid());
        assert!(key.has_scope("read"));
        assert!(key.has_scope("write"));
        assert!(!key.has_scope("admin"));
    }

    #[test]
    fn test_api_key_revoked() {
        let key = ApiKey {
            id: "1".to_string(),
            user_id: "user:123".to_string(),
            name: "Test Key".to_string(),
            prefix: "sk_live".to_string(),
            key_hash: "hash".to_string(),
            scopes: vec![],
            rate_limit: None,
            is_revoked: true,
            last_used_at: None,
            expires_at: None,
            created_at: Utc::now(),
        };

        assert!(!key.is_valid());
    }

    #[test]
    fn test_api_key_expired() {
        let key = ApiKey {
            id: "1".to_string(),
            user_id: "user:123".to_string(),
            name: "Test Key".to_string(),
            prefix: "sk_live".to_string(),
            key_hash: "hash".to_string(),
            scopes: vec![],
            rate_limit: None,
            is_revoked: false,
            last_used_at: None,
            expires_at: Some(Utc::now() - chrono::Duration::hours(1)),
            created_at: Utc::now(),
        };

        assert!(!key.is_valid());
    }

    #[test]
    fn test_base32_encode() {
        // Test with known values
        let bytes = [0x48, 0x65, 0x6c, 0x6c, 0x6f]; // "Hello"
        let encoded = base32_encode(&bytes);
        assert_eq!(encoded, "jbswy3dp"); // lowercase base32 of "Hello"
    }
}