chat2response 0.1.1

Translate and proxy OpenAI Chat Completions requests to the Responses API.
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
/* chat2response/src/auth.rs

Refactored Auth module with pluggable storage backends and async-ready API.

- Default backend: sled (embedded) when compiled with feature "auth-sled".
- Optional backend: Redis (when compiled with feature "auth-redis").
- Fallback (builds without extra deps): in-memory store (for tests and non-feature builds).

Token design:
- Opaque bearer tokens: "sk_<id>.<secret>"
  • id: 32-char hex (UUID v4 as hex)
  • secret: 64-char hex (32 random bytes)
- Storage never keeps plaintext secrets; per-key random salt (16 bytes) +
  SHA-256(salt || secret) hex digest is stored.
- Expiration is enforced at creation-time by default (configurable).

Environment variables:
- CHAT2RESPONSE_KEYS_REQUIRE_EXPIRATION = 1|true|yes|on (default: false)
- CHAT2RESPONSE_KEYS_ALLOW_NO_EXPIRATION = 1|true|yes|on (default: false)
- CHAT2RESPONSE_KEYS_DEFAULT_TTL_SECONDS = <u64 seconds> (optional default)
- CHAT2RESPONSE_SLED_PATH = ./data/keys.db (path for sled)
- CHAT2RESPONSE_REDIS_URL = redis://127.0.0.1/ (url for Redis)

Public API (kept compatible with previous module where possible):
- Types: ApiKeyInfo, GeneratedKey, Verification
- Manager: AuthManager (type alias ApiKeyManager = AuthManager)
- Functions (sync for compatibility, async variants provided with *_async suffix):
    generate_key / generate_key_async
    verify / verify_async
    revoke / revoke_async
    set_expiration / set_expiration_async
    list_keys / list_keys_async
    purge / purge_async
    verify_bearer (helper for Authorization header)

Note:
- The async methods are "async-ready" wrappers over synchronous backend calls.
  sled is synchronous; Redis implementation (when enabled) executes blocking commands
  synchronously here as a simple placeholder and should be adapted to use async Redis
  drivers in an async executor if desired.

*/

#![forbid(unsafe_code)]

use anyhow::{anyhow, Result};

use rand::{rngs::OsRng, RngCore};
use serde::{Deserialize, Serialize};
use std::sync::Arc;

use std::time::{Duration, SystemTime, UNIX_EPOCH};
use uuid::Uuid;

// ==============================
// Public model
// ==============================

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiKeyInfo {
    pub id: String,
    pub label: Option<String>,
    pub created_at: u64,
    pub expires_at: Option<u64>,
    pub revoked_at: Option<u64>,
    pub scopes: Option<Vec<String>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GeneratedKey {
    pub id: String,
    pub token: String, // "sk_<id>.<secret>"
    pub created_at: u64,
    pub expires_at: Option<u64>,
    pub label: Option<String>,
    pub scopes: Option<Vec<String>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Verification {
    Valid {
        id: String,
        label: Option<String>,
        expires_at: Option<u64>,
        scopes: Option<Vec<String>>,
    },
    InvalidTokenFormat,
    NotFound,
    Revoked {
        revoked_at: u64,
    },
    Expired {
        expired_at: u64,
    },
    HashMismatch,
}

// Internal record persisted in the backend.
#[derive(Debug, Clone, Serialize, Deserialize)]
struct ApiKeyRecord {
    id: String,
    label: Option<String>,
    created_at: u64,
    expires_at: Option<u64>,
    revoked_at: Option<u64>,
    salt_hex: String,
    hash_hex: String,
    scopes: Option<Vec<String>>,
}

// ==============================
// Storage trait
// ==============================

trait KeyStore: Send + Sync {
    fn put(&self, rec: &ApiKeyRecord) -> Result<()>;
    fn get(&self, id: &str) -> Result<Option<ApiKeyRecord>>;
    fn list(&self) -> Result<Vec<ApiKeyRecord>>;
    fn purge(&self, cutoff_epoch: u64) -> Result<usize>;
}

// ==============================
// sled backend (feature: auth-sled)
// ==============================

mod sled_store_impl {
    use super::*;
    use std::path::PathBuf;

    pub struct SledStore {
        _db: sled::Db,
        tree: sled::Tree,
    }

    impl SledStore {
        pub fn open_default() -> Result<Self> {
            let path = std::env::var("CHAT2RESPONSE_SLED_PATH")
                .ok()
                .filter(|s| !s.trim().is_empty())
                .unwrap_or_else(|| "./data/keys.db".to_string());
            Self::open_path(PathBuf::from(path))
        }

        pub fn open_path(path: PathBuf) -> Result<Self> {
            if let Some(parent) = path.parent() {
                std::fs::create_dir_all(parent).ok();
            }
            let db = sled::open(path)?;
            let tree = db.open_tree("keys")?;
            Ok(Self { _db: db, tree })
        }
    }

    impl KeyStore for SledStore {
        fn put(&self, rec: &ApiKeyRecord) -> Result<()> {
            let key = rec.id.as_bytes().to_vec();
            let val = serde_json::to_vec(rec)?;
            self.tree.insert(key, val)?;
            self.tree.flush()?;
            Ok(())
        }

        fn get(&self, id: &str) -> Result<Option<ApiKeyRecord>> {
            if let Some(ivec) = self.tree.get(id.as_bytes())? {
                let rec: ApiKeyRecord = serde_json::from_slice(&ivec)?;
                Ok(Some(rec))
            } else {
                Ok(None)
            }
        }

        /* delete removed */

        fn list(&self) -> Result<Vec<ApiKeyRecord>> {
            let mut out = Vec::new();
            for item in self.tree.iter() {
                let (_k, v) = item?;
                let rec: ApiKeyRecord = serde_json::from_slice(&v)?;
                out.push(rec);
            }
            Ok(out)
        }

        fn purge(&self, cutoff_epoch: u64) -> Result<usize> {
            let mut removed = 0usize;
            for item in self.tree.iter() {
                let (k, v) = item?;
                let rec: ApiKeyRecord = serde_json::from_slice(&v)?;
                let expired = rec.expires_at.map(|e| e <= cutoff_epoch).unwrap_or(false);
                let revoked = rec.revoked_at.map(|r| r <= cutoff_epoch).unwrap_or(false);
                if expired || revoked {
                    self.tree.remove(k)?;
                    removed += 1;
                }
            }
            self.tree.flush()?;
            Ok(removed)
        }
    }

    pub fn make_store() -> Result<Arc<dyn KeyStore>> {
        Ok(Arc::new(SledStore::open_default()?))
    }
}

// ==============================
// Redis backend (feature: auth-redis)
// ==============================

mod redis_store_impl {
    use super::*;
    // This placeholder uses blocking Redis commands; replace with async driver if desired.
    // Requires the "redis" crate with the "tokio-comp" or async features for production use.
    // Custom r2d2 manager using redis 0.24
    pub struct RedisConnectionManager {
        client: redis::Client,
    }

    impl r2d2::ManageConnection for RedisConnectionManager {
        type Connection = redis::Connection;
        type Error = redis::RedisError;

        fn connect(&self) -> Result<Self::Connection, Self::Error> {
            self.client.get_connection()
        }

        fn is_valid(&self, conn: &mut Self::Connection) -> Result<(), Self::Error> {
            let _: String = redis::cmd("PING").query(conn)?;
            Ok(())
        }

        fn has_broken(&self, _conn: &mut Self::Connection) -> bool {
            // Conservatively assume connection is fine; r2d2 will recycle on errors.
            false
        }
    }

    pub struct RedisStore {
        pool: r2d2::Pool<RedisConnectionManager>,
        key_ns: String,
    }

    impl RedisStore {
        pub fn connect_default() -> Result<Self> {
            let url = std::env::var("CHAT2RESPONSE_REDIS_URL")
                .unwrap_or_else(|_| "redis://127.0.0.1/".to_string());
            Self::connect_url(&url)
        }

        pub fn connect_url(url: &str) -> Result<Self> {
            let client = redis::Client::open(url)?;
            let manager = RedisConnectionManager { client };
            let max_size = std::env::var("CHAT2RESPONSE_REDIS_POOL_MAX")
                .ok()
                .and_then(|s| s.trim().parse::<u32>().ok())
                .unwrap_or(16);
            let pool = r2d2::Pool::builder().max_size(max_size).build(manager)?;
            Ok(Self {
                pool,
                key_ns: "chat2response:keys:".to_string(),
            })
        }

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

    impl KeyStore for RedisStore {
        fn put(&self, rec: &ApiKeyRecord) -> Result<()> {
            let mut conn = self.pool.get()?;
            let key = self.key_for(&rec.id);
            let val = serde_json::to_string(rec)?;
            let _: () = redis::cmd("SET").arg(&key).arg(val).query(&mut *conn)?;
            Ok(())
        }

        fn get(&self, id: &str) -> Result<Option<ApiKeyRecord>> {
            let mut conn = self.pool.get()?;
            let key = self.key_for(id);
            let val: Option<String> = redis::cmd("GET").arg(&key).query(&mut *conn)?;
            match val {
                Some(s) => Ok(Some(serde_json::from_str(&s)?)),
                None => Ok(None),
            }
        }

        /* delete removed */

        fn list(&self) -> Result<Vec<ApiKeyRecord>> {
            let mut conn = self.pool.get()?;
            let pattern = format!("{}*", self.key_ns);
            let keys: Vec<String> = redis::cmd("KEYS").arg(pattern).query(&mut *conn)?;
            let mut out = Vec::new();
            for k in keys {
                if let Ok(s) = redis::cmd("GET").arg(&k).query::<String>(&mut *conn) {
                    if let Ok(rec) = serde_json::from_str::<ApiKeyRecord>(&s) {
                        out.push(rec);
                    }
                }
            }
            Ok(out)
        }

        fn purge(&self, cutoff_epoch: u64) -> Result<usize> {
            let recs = self.list()?;
            let mut conn = self.pool.get()?;
            let mut removed = 0usize;
            for rec in recs {
                let expired = rec.expires_at.map(|e| e <= cutoff_epoch).unwrap_or(false);
                let revoked = rec.revoked_at.map(|r| r <= cutoff_epoch).unwrap_or(false);
                if expired || revoked {
                    let key = self.key_for(&rec.id);
                    let n: i64 = redis::cmd("DEL").arg(&key).query(&mut *conn)?;
                    if n > 0 {
                        removed += 1;
                    }
                }
            }
            Ok(removed)
        }
    }

    pub fn make_store() -> Result<Arc<dyn KeyStore>> {
        Ok(Arc::new(RedisStore::connect_default()?))
    }
    pub fn make_store_with_url(url: &str) -> Result<Arc<dyn KeyStore>> {
        Ok(Arc::new(RedisStore::connect_url(url)?))
    }
}

// ==============================
// In-memory fallback (no features)
// ==============================

mod memory_store_impl {
    use super::*;
    use std::collections::HashMap;
    use std::sync::RwLock;

    #[derive(Default)]
    pub struct MemoryStore {
        inner: RwLock<HashMap<String, ApiKeyRecord>>,
    }

    impl KeyStore for MemoryStore {
        fn put(&self, rec: &ApiKeyRecord) -> Result<()> {
            self.inner
                .write()
                .expect("lock")
                .insert(rec.id.clone(), rec.clone());
            Ok(())
        }

        fn get(&self, id: &str) -> Result<Option<ApiKeyRecord>> {
            Ok(self.inner.read().expect("lock").get(id).cloned())
        }

        /* delete removed */

        fn list(&self) -> Result<Vec<ApiKeyRecord>> {
            Ok(self.inner.read().expect("lock").values().cloned().collect())
        }

        fn purge(&self, cutoff_epoch: u64) -> Result<usize> {
            let mut g = self.inner.write().expect("lock");
            let before = g.len();
            g.retain(|_, rec| {
                let expired = rec.expires_at.map(|e| e <= cutoff_epoch).unwrap_or(false);
                let revoked = rec.revoked_at.map(|r| r <= cutoff_epoch).unwrap_or(false);
                !(expired || revoked)
            });
            Ok(before.saturating_sub(g.len()))
        }
    }

    pub fn make_store() -> Result<Arc<dyn KeyStore>> {
        Ok(Arc::new(MemoryStore::default()))
    }
}

// Select the default make_store() according to enabled features (priority: redis > sled > memory)
fn make_default_store() -> Result<Arc<dyn KeyStore>> {
    if let Ok(url) = std::env::var("CHAT2RESPONSE_REDIS_URL") {
        let u = url.trim();
        if !u.is_empty() {
            return redis_store_impl::make_store_with_url(u);
        }
    }
    sled_store_impl::make_store()
}

// ==============================
// Manager
// ==============================

pub struct AuthManager {
    store: Arc<dyn KeyStore>,
}

impl AuthManager {
    pub fn new_default() -> Result<Self> {
        Ok(Self {
            store: make_default_store()?,
        })
    }

    pub fn new_with_redis_url(url: &str) -> Result<Self> {
        Ok(Self {
            store: redis_store_impl::make_store_with_url(url)?,
        })
    }

    pub fn new_with_sled_path(path: &std::path::Path) -> Result<Self> {
        Ok(Self {
            store: sled_store_impl::SledStore::open_path(path.to_path_buf())
                .map(|s| Arc::new(s) as Arc<dyn KeyStore>)?,
        })
    }

    pub fn new_sled_default() -> Result<Self> {
        Ok(Self {
            store: sled_store_impl::make_store()?,
        })
    }

    pub fn new_redis_default() -> Result<Self> {
        Ok(Self {
            store: redis_store_impl::make_store()?,
        })
    }

    // --------------------------
    // Key lifecycle (sync API)
    // --------------------------

    pub fn generate_key(
        &self,
        label: Option<String>,
        ttl: Option<Duration>,
        scopes: Option<Vec<String>>,
    ) -> Result<GeneratedKey> {
        let created_at = now_epoch();
        // Policy: enforce expiration by default; allow override by env flags
        let require_exp = env_truthy("CHAT2RESPONSE_KEYS_REQUIRE_EXPIRATION", false);
        let allow_no_exp = env_truthy("CHAT2RESPONSE_KEYS_ALLOW_NO_EXPIRATION", false);

        // Optional default TTL
        let default_ttl = std::env::var("CHAT2RESPONSE_KEYS_DEFAULT_TTL_SECONDS")
            .ok()
            .and_then(|s| s.trim().parse::<u64>().ok())
            .map(Duration::from_secs);

        let ttl_eff = ttl.or(default_ttl);

        if require_exp && ttl_eff.is_none() && !allow_no_exp {
            return Err(anyhow!(
                "expiration required: provide ttl_seconds (or set default TTL via env)"
            ));
        }
        if let Some(d) = ttl_eff {
            if d.as_secs() == 0 {
                return Err(anyhow!("ttl must be > 0 seconds"));
            }
        }

        let id = uuid_hex();
        let expires_at = ttl_eff.map(|d| created_at.saturating_add(d.as_secs()));

        // Generate secret and salted hash
        let secret_hex = random_secret_hex(32);
        let salt = random_bytes(16);
        let salt_hex = hex_encode(&salt);

        let mut data = Vec::with_capacity(salt.len() + secret_hex.len() / 2);
        data.extend_from_slice(&salt);
        let secret_bytes = hex_decode(&secret_hex)?;
        data.extend_from_slice(&secret_bytes);
        let digest = sha256(&data);
        let hash_hex = hex_encode(&digest);

        let rec = ApiKeyRecord {
            id: id.clone(),
            label: label.clone(),
            created_at,
            expires_at,
            revoked_at: None,
            salt_hex,
            hash_hex,
            scopes: scopes.clone(),
        };

        // Store (blocking sync under the hood of our async trait wrapper)
        self.store.put(&rec)?;

        Ok(GeneratedKey {
            id,
            token: format!("sk_{}.{}", rec.id, secret_hex),
            created_at,
            expires_at,
            label,
            scopes,
        })
    }

    pub fn verify(&self, token: &str) -> Verification {
        let (id, secret_hex) = match parse_token(token) {
            Some(p) => p,
            None => return Verification::InvalidTokenFormat,
        };

        let rec_opt = self.store.get(&id).unwrap_or(None);

        let rec = match rec_opt {
            Some(r) => r,
            None => return Verification::NotFound,
        };

        let now = now_epoch();
        if let Some(ts) = rec.revoked_at {
            return Verification::Revoked { revoked_at: ts };
        }
        if let Some(exp) = rec.expires_at {
            if now >= exp {
                return Verification::Expired { expired_at: exp };
            }
        }

        let salt = match hex_decode(&rec.salt_hex) {
            Ok(s) => s,
            Err(_) => return Verification::HashMismatch,
        };
        let secret_bytes = match hex_decode(&secret_hex) {
            Ok(b) => b,
            Err(_) => return Verification::InvalidTokenFormat,
        };

        let mut data = Vec::with_capacity(salt.len() + secret_bytes.len());
        data.extend_from_slice(&salt);
        data.extend_from_slice(&secret_bytes);
        let digest = sha256(&data);

        match hex_decode(&rec.hash_hex) {
            Ok(expected) if ct_eq(&digest, &expected) => Verification::Valid {
                id: rec.id,
                label: rec.label,
                expires_at: rec.expires_at,
                scopes: rec.scopes,
            },
            _ => Verification::HashMismatch,
        }
    }

    pub fn revoke(&self, id: &str) -> Result<bool> {
        if let Some(mut rec) = self.store.get(id)? {
            if rec.revoked_at.is_none() {
                rec.revoked_at = Some(now_epoch());
                self.store.put(&rec)?;
                return Ok(true);
            }
            return Ok(false);
        }
        Ok(false)
    }

    pub fn set_expiration(&self, id: &str, expires_at: Option<u64>) -> Result<bool> {
        if let Some(mut rec) = self.store.get(id)? {
            rec.expires_at = expires_at;
            self.store.put(&rec)?;
            return Ok(true);
        }
        Ok(false)
    }

    pub fn list_keys(&self) -> Result<Vec<ApiKeyInfo>> {
        let recs = self.store.list()?;
        Ok(recs
            .into_iter()
            .map(|r| ApiKeyInfo {
                id: r.id,
                label: r.label,
                created_at: r.created_at,
                expires_at: r.expires_at,
                revoked_at: r.revoked_at,
                scopes: r.scopes,
            })
            .collect())
    }

    pub fn purge(&self, cutoff_epoch: u64) -> Result<usize> {
        self.store.purge(cutoff_epoch)
    }

    // --------------------------
    // Async wrappers
    // --------------------------

    pub async fn generate_key_async(
        &self,
        label: Option<String>,
        ttl: Option<Duration>,
        scopes: Option<Vec<String>>,
    ) -> Result<GeneratedKey> {
        self.generate_key(label, ttl, scopes)
    }

    pub async fn verify_async(&self, token: &str) -> Verification {
        self.verify(token)
    }

    pub async fn revoke_async(&self, id: &str) -> Result<bool> {
        self.revoke(id)
    }

    pub async fn set_expiration_async(&self, id: &str, expires_at: Option<u64>) -> Result<bool> {
        self.set_expiration(id, expires_at)
    }

    pub async fn list_keys_async(&self) -> Result<Vec<ApiKeyInfo>> {
        self.list_keys()
    }

    pub async fn purge_async(&self, cutoff_epoch: u64) -> Result<usize> {
        self.purge(cutoff_epoch)
    }
}

// Backends selectable at runtime via CLI/env
#[derive(Debug, Clone)]
pub enum KeyBackend {
    Redis { url: String },
    Sled { path: std::path::PathBuf },
    Memory,
}

impl AuthManager {
    /// Build a manager from an explicit backend selection.
    pub fn from_backend(backend: KeyBackend) -> Result<Self> {
        match backend {
            KeyBackend::Redis { url } => Self::new_with_redis_url(&url),
            KeyBackend::Sled { path } => Self::new_with_sled_path(&path),
            KeyBackend::Memory => Ok(Self {
                store: memory_store_impl::make_store()?,
            }),
        }
    }

    /// Parse a backend spec string:
    /// - "redis://..." → Redis at URL
    /// - "sled:<path>" → Sled database at path
    /// - "memory"      → In-memory backend
    pub fn backend_from_arg_spec(spec: &str) -> Option<KeyBackend> {
        if spec.starts_with("redis://") {
            return Some(KeyBackend::Redis {
                url: spec.to_string(),
            });
        }
        if let Some(rest) = spec.strip_prefix("sled:") {
            return Some(KeyBackend::Sled {
                path: std::path::PathBuf::from(rest),
            });
        }
        if spec.eq_ignore_ascii_case("memory") {
            return Some(KeyBackend::Memory);
        }
        None
    }

    /// Scan CLI args for a flag like "--keys-backend=<spec>" and return the parsed backend if found.
    pub fn backend_from_args(args: &[String]) -> Option<KeyBackend> {
        for a in args {
            if let Some(spec) = a.strip_prefix("--keys-backend=") {
                if let Some(b) = Self::backend_from_arg_spec(spec) {
                    return Some(b);
                }
            }
        }
        None
    }
}

// Backwards-compatible alias
pub type ApiKeyManager = AuthManager;

// ==============================
// - Helpers: token parsing, hashing, header verification
// ==============================

pub fn verify_bearer(manager: &AuthManager, auth_header: Option<&str>) -> Verification {
    let token = match auth_header {
        Some(raw) => {
            let s = raw.trim();
            if s.len() < 7 {
                return Verification::InvalidTokenFormat;
            }
            let (scheme, rest) = s.split_at(6);
            if !scheme.eq_ignore_ascii_case("bearer") {
                return Verification::InvalidTokenFormat;
            }
            let t = rest.trim();
            if t.is_empty() {
                return Verification::InvalidTokenFormat;
            }
            t
        }
        None => return Verification::InvalidTokenFormat,
    };
    manager.verify(token)
}

fn env_truthy(name: &str, default: bool) -> bool {
    match std::env::var(name) {
        Ok(v) => {
            let s = v.trim().to_ascii_lowercase();
            s == "1" || s == "true" || s == "yes" || s == "on"
        }
        Err(_) => default,
    }
}

fn now_epoch() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
}

fn uuid_hex() -> String {
    let u = Uuid::new_v4().as_u128();
    format!("{:032x}", u)
}

fn parse_token(token: &str) -> Option<(String, String)> {
    if !token.starts_with("sk_") {
        return None;
    }
    let rest = &token[3..];
    let (id_part, secret_part) = rest.split_once('.')?;
    if id_part.len() != 32 || !is_hex(id_part) {
        return None;
    }
    if secret_part.len() < 32 || !is_hex(secret_part) {
        return None;
    }
    Some((id_part.to_string(), secret_part.to_string()))
}

fn is_hex(s: &str) -> bool {
    s.bytes()
        .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b) || (b'A'..=b'F').contains(&b))
}

fn random_bytes(n: usize) -> Vec<u8> {
    let mut v = vec![0u8; n];
    OsRng.fill_bytes(&mut v);
    v
}

fn random_secret_hex(bytes: usize) -> String {
    hex_encode(&random_bytes(bytes))
}

fn hex_encode(bytes: &[u8]) -> String {
    const HEX: &[u8; 16] = b"0123456789abcdef";
    let mut out = String::with_capacity(bytes.len() * 2);
    for &b in bytes {
        out.push(HEX[(b >> 4) as usize] as char);
        out.push(HEX[(b & 0x0f) as usize] as char);
    }
    out
}

fn hex_decode(s: &str) -> Result<Vec<u8>> {
    let s = s.trim();
    if s.len() % 2 != 0 {
        return Err(anyhow!("odd-length hex"));
    }
    let mut out = Vec::with_capacity(s.len() / 2);
    for pair in s.as_bytes().chunks(2) {
        let hi = hex_val(pair[0]).ok_or_else(|| anyhow!("invalid hex"))?;
        let lo = hex_val(*pair.get(1).ok_or_else(|| anyhow!("invalid hex"))?)
            .ok_or_else(|| anyhow!("invalid hex"))?;
        out.push((hi << 4) | lo);
    }
    Ok(out)
}

fn hex_val(b: u8) -> Option<u8> {
    match b {
        b'0'..=b'9' => Some(b - b'0'),
        b'a'..=b'f' => Some(b - b'a' + 10),
        b'A'..=b'F' => Some(b - b'A' + 10),
        _ => None,
    }
}

/// Constant-time equality for two byte slices.
fn ct_eq(a: &[u8], b: &[u8]) -> bool {
    if a.len() != b.len() {
        return false;
    }
    let mut acc: u8 = 0;
    for i in 0..a.len() {
        acc |= a[i] ^ b[i];
    }
    acc == 0
}

// Minimal pure-Rust SHA-256 (streaming)
const K256: [u32; 64] = [
    0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
    0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
    0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
    0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
    0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
    0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
    0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
    0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
];

#[inline(always)]
fn rotr(x: u32, n: u32) -> u32 {
    (x >> n) | (x << (32 - n))
}
#[inline(always)]
fn ch(x: u32, y: u32, z: u32) -> u32 {
    (x & y) ^ ((!x) & z)
}
#[inline(always)]
fn maj(x: u32, y: u32, z: u32) -> u32 {
    (x & y) ^ (x & z) ^ (y & z)
}
#[inline(always)]
fn big_sigma0(x: u32) -> u32 {
    rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22)
}
#[inline(always)]
fn big_sigma1(x: u32) -> u32 {
    rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25)
}
#[inline(always)]
fn small_sigma0(x: u32) -> u32 {
    rotr(x, 7) ^ rotr(x, 18) ^ (x >> 3)
}
#[inline(always)]
fn small_sigma1(x: u32) -> u32 {
    rotr(x, 17) ^ rotr(x, 19) ^ (x >> 10)
}

struct Sha256 {
    state: [u32; 8],
    buffer: [u8; 64],
    buflen: usize,
    bit_len: u64,
}

impl Sha256 {
    fn new() -> Self {
        Self {
            state: [
                0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab,
                0x5be0cd19,
            ],
            buffer: [0u8; 64],
            buflen: 0,
            bit_len: 0,
        }
    }
    fn update(&mut self, data: &[u8]) {
        let mut off = 0usize;
        self.bit_len = self.bit_len.wrapping_add((data.len() as u64) * 8);

        if self.buflen > 0 {
            let need = 64 - self.buflen;
            let take = need.min(data.len());
            self.buffer[self.buflen..self.buflen + take].copy_from_slice(&data[off..off + take]);
            self.buflen += take;
            off += take;
            if self.buflen == 64 {
                let mut block = [0u8; 64];
                block.copy_from_slice(&self.buffer);
                self.compress(&block);
                self.buflen = 0;
            }
        }

        while off + 64 <= data.len() {
            self.compress(&data[off..off + 64]);
            off += 64;
        }

        let rem = data.len() - off;
        if rem > 0 {
            self.buffer[..rem].copy_from_slice(&data[off..]);
            self.buflen = rem;
        }
    }
    fn finalize(mut self) -> [u8; 32] {
        let mut block = [0u8; 128];
        let n = self.buflen;
        block[..n].copy_from_slice(&self.buffer[..n]);
        block[n] = 0x80;

        if n + 1 + 8 <= 64 {
            let bit_len_be = self.bit_len.to_be_bytes();
            block[64 - 8..64].copy_from_slice(&bit_len_be);
            self.compress(&block[..64]);
        } else {
            let bit_len_be = self.bit_len.to_be_bytes();
            block[128 - 8..128].copy_from_slice(&bit_len_be);
            self.compress(&block[..64]);
            self.compress(&block[64..128]);
        }

        let mut out = [0u8; 32];
        for (i, v) in self.state.iter().enumerate() {
            out[i * 4..i * 4 + 4].copy_from_slice(&v.to_be_bytes());
        }
        out
    }
    fn compress(&mut self, block: &[u8]) {
        debug_assert_eq!(block.len(), 64);
        let mut w = [0u32; 64];

        for (i, chunk) in block.chunks(4).take(16).enumerate() {
            w[i] = u32::from_be_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]);
        }
        for i in 16..64 {
            w[i] = small_sigma1(w[i - 2])
                .wrapping_add(w[i - 7])
                .wrapping_add(small_sigma0(w[i - 15]))
                .wrapping_add(w[i - 16]);
        }

        let mut a = self.state[0];
        let mut b = self.state[1];
        let mut c = self.state[2];
        let mut d = self.state[3];
        let mut e = self.state[4];
        let mut f = self.state[5];
        let mut g = self.state[6];
        let mut h = self.state[7];

        for i in 0..64 {
            let t1 = h
                .wrapping_add(big_sigma1(e))
                .wrapping_add(ch(e, f, g))
                .wrapping_add(K256[i])
                .wrapping_add(w[i]);
            let t2 = big_sigma0(a).wrapping_add(maj(a, b, c));
            h = g;
            g = f;
            f = e;
            e = d.wrapping_add(t1);
            d = c;
            c = b;
            b = a;
            a = t1.wrapping_add(t2);
        }

        self.state[0] = self.state[0].wrapping_add(a);
        self.state[1] = self.state[1].wrapping_add(b);
        self.state[2] = self.state[2].wrapping_add(c);
        self.state[3] = self.state[3].wrapping_add(d);
        self.state[4] = self.state[4].wrapping_add(e);
        self.state[5] = self.state[5].wrapping_add(f);
        self.state[6] = self.state[6].wrapping_add(g);
        self.state[7] = self.state[7].wrapping_add(h);
    }
}

fn sha256(data: &[u8]) -> [u8; 32] {
    let mut s = Sha256::new();
    s.update(data);
    s.finalize()
}

// ==============================
// Tests
// ==============================

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

    #[tokio::test]
    async fn round_trip_memory_backend() {
        let mgr = AuthManager {
            store: super::memory_store_impl::make_store().unwrap(),
        };

        // generate with TTL
        let out = mgr
            .generate_key_async(Some("unit".into()), Some(Duration::from_secs(3)), None)
            .await
            .unwrap();

        // verify ok
        match mgr.verify_async(&out.token).await {
            Verification::Valid { id, .. } => assert_eq!(id, out.id),
            x => panic!("expected valid, got {:?}", x),
        }

        // revoke then verify
        assert!(mgr.revoke_async(&out.id).await.unwrap());
        match mgr.verify_async(&out.token).await {
            Verification::Revoked { .. } => {}
            x => panic!("expected revoked, got {:?}", x),
        }
    }
}