ave-http 0.11.0

HTTP API server for the Ave runtime, auth system, and admin surface
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
// Ave HTTP Auth System - API Key Database Operations
//
// This module provides database operations for API keys

use super::crypto::{extract_key_prefix, generate_api_key, hash_api_key};
use super::database::{AuthDatabase, DatabaseError};
use super::database_audit::AuditLogParams;
use super::models::*;
use ave_actors::rusqlite::{
    self, OptionalExtension, Result as SqliteResult, TransactionBehavior,
    params,
};

pub struct RotateApiKeyParams<'a> {
    pub key_id: &'a str,
    pub name: Option<&'a str>,
    pub description: Option<&'a str>,
    pub expires_in_seconds: Option<i64>,
    pub revoked_by: Option<i64>,
    pub reason: Option<&'a str>,
    pub audit: Option<AuditLogParams<'a>>,
}

// =============================================================================
// API KEY OPERATIONS
// =============================================================================

impl AuthDatabase {
    fn validate_api_key_creation(
        conn: &rusqlite::Connection,
        user_id: i64,
        description: Option<&str>,
        expires_in_seconds: Option<i64>,
    ) -> Result<(), DatabaseError> {
        Self::validate_description(description)?;

        if let Some(ttl) = expires_in_seconds
            && ttl < 0
        {
            return Err(DatabaseError::Validation(format!(
                "Invalid TTL: {} (must be positive or 0)",
                ttl
            )));
        }

        let user = Self::get_user_by_id_internal(conn, user_id)?;
        if !user.is_active {
            return Err(DatabaseError::PermissionDenied(
                "User account is not active".to_string(),
            ));
        }

        Ok(())
    }

    fn validate_service_key_creation(
        &self,
        conn: &rusqlite::Connection,
        user_id: i64,
        name: Option<&str>,
    ) -> Result<(), DatabaseError> {
        let key_name = name.unwrap_or_default();

        let max_keys_per_user = self.api_key_max_keys_per_user();
        if max_keys_per_user > 0 {
            let key_count: i64 = conn
                .query_row(
                    "SELECT COUNT(*)
                     FROM api_keys
                     WHERE user_id = ?1 AND revoked = 0 AND is_management = 0",
                    params![user_id],
                    |row| row.get(0),
                )
                .map_err(|e| DatabaseError::Query(e.to_string()))?;

            if key_count >= max_keys_per_user as i64 {
                return Err(DatabaseError::Validation(format!(
                    "Maximum number of API keys ({}) reached for this user",
                    max_keys_per_user
                )));
            }
        }

        if key_name.is_empty() {
            return Err(DatabaseError::Validation(
                "API key name is required".to_string(),
            ));
        }

        Self::validate_api_key_name(key_name)?;

        let exists: Option<String> = conn
            .query_row(
                "SELECT id FROM api_keys WHERE user_id = ?1 AND name = ?2 AND revoked = 0",
                params![user_id, key_name],
                |row| row.get(0),
            )
            .optional()
            .map_err(|e| DatabaseError::Query(e.to_string()))?;

        if exists.is_some() {
            return Err(DatabaseError::Duplicate(
                "API key name already in use for this user".to_string(),
            ));
        }

        Ok(())
    }

    fn insert_api_key_with_conn(
        &self,
        conn: &rusqlite::Connection,
        user_id: i64,
        name: Option<&str>,
        description: Option<&str>,
        expires_in_seconds: Option<i64>,
        is_management: bool,
    ) -> Result<(String, ApiKeyInfo), DatabaseError> {
        // Generate API key
        let api_key = generate_api_key(&self.config.api_key.prefix);

        // Extract visible prefix
        let key_prefix =
            extract_key_prefix(&api_key, &self.config.api_key.prefix);

        // Hash the key for storage
        let key_hash = hash_api_key(&api_key);

        // Calculate expiration
        let now = Self::now();
        let config_ttl = self.api_key_default_ttl_seconds();
        let effective_ttl = match expires_in_seconds {
            Some(ttl) if ttl > 0 => Some(ttl),
            Some(0) => {
                // Explicit TTL=0 means never expire (useful for service keys)
                None
            }
            None => {
                // No TTL specified, use system default
                if config_ttl > 0 {
                    Some(config_ttl)
                } else {
                    None
                }
            }
            _ => unreachable!("Negative TTL already validated above"),
        };
        let expires_at = effective_ttl.map(|ttl| now + ttl);

        // Generate UUID for id
        let key_id = Self::generate_uuid();

        conn.execute(
            "INSERT INTO api_keys (id, user_id, key_hash, key_prefix, name, description, expires_at, is_management)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
            params![
                key_id,
                user_id,
                key_hash,
                key_prefix,
                name.unwrap_or_default(),
                description,
                expires_at,
                is_management
            ],
        )
        .map_err(|e| DatabaseError::Insert(e.to_string()))?;

        let key_info = Self::get_api_key_info_internal(conn, &key_id)?;

        Ok((api_key, key_info))
    }

    fn create_service_api_key_with_conn(
        &self,
        conn: &rusqlite::Connection,
        user_id: i64,
        name: Option<&str>,
        description: Option<&str>,
        expires_in_seconds: Option<i64>,
    ) -> Result<(String, ApiKeyInfo), DatabaseError> {
        Self::validate_api_key_creation(
            conn,
            user_id,
            description,
            expires_in_seconds,
        )?;
        self.validate_service_key_creation(conn, user_id, name)?;
        self.insert_api_key_with_conn(
            conn,
            user_id,
            name,
            description,
            expires_in_seconds,
            false,
        )
    }

    pub(crate) fn issue_management_api_key_with_conn(
        &self,
        conn: &rusqlite::Connection,
        user_id: i64,
        name: Option<&str>,
        description: Option<&str>,
        expires_in_seconds: Option<i64>,
    ) -> Result<(String, ApiKeyInfo), DatabaseError> {
        Self::validate_api_key_creation(
            conn,
            user_id,
            description,
            expires_in_seconds,
        )?;
        Self::revoke_user_management_keys_internal(conn, user_id)?;
        self.insert_api_key_with_conn(
            conn,
            user_id,
            name,
            description,
            expires_in_seconds,
            true,
        )
    }

    fn revoke_user_management_keys_internal(
        conn: &rusqlite::Connection,
        user_id: i64,
    ) -> Result<(), DatabaseError> {
        conn.execute(
            "UPDATE api_keys
             SET revoked = 1, revoked_at = ?1, revoked_by = ?2, revoked_reason = 'rotated management key'
             WHERE user_id = ?3 AND is_management = 1 AND revoked = 0",
            params![Self::now(), Some(user_id), user_id],
        )
        .map_err(|e| DatabaseError::Update(e.to_string()))?;

        Ok(())
    }

    fn revoke_api_key_internal(
        conn: &rusqlite::Connection,
        key_id: &str,
        revoked_by: Option<i64>,
        reason: Option<&str>,
    ) -> Result<(), DatabaseError> {
        let now = Self::now();

        conn.execute(
            "UPDATE api_keys
             SET revoked = 1, revoked_at = ?1, revoked_by = ?2, revoked_reason = ?3
             WHERE id = ?4",
            params![now, revoked_by, reason, key_id],
        )
        .map_err(|e| DatabaseError::Update(e.to_string()))?;

        Ok(())
    }

    /// Internal: Get API key info by ID without acquiring lock
    fn get_api_key_info_internal(
        conn: &rusqlite::Connection,
        key_id: &str,
    ) -> Result<ApiKeyInfo, DatabaseError> {
        conn.query_row(
            "SELECT k.id, k.user_id, u.username, k.key_prefix, k.name, k.description,
                    k.is_management, k.created_at, k.expires_at, k.revoked,
                    k.revoked_at, k.revoked_reason, k.last_used_at,
                    k.last_used_ip, kp.plan_id, p.name
             FROM api_keys k
             INNER JOIN users u ON k.user_id = u.id
             LEFT JOIN api_key_plans kp ON kp.api_key_id = k.id
             LEFT JOIN usage_plans p ON p.id = kp.plan_id
             WHERE k.id = ?1",
            params![key_id],
            |row| {
                Ok(ApiKeyInfo {
                    id: row.get(0)?,
                    user_id: row.get(1)?,
                    username: row.get(2)?,
                    key_prefix: row.get(3)?,
                    name: row.get(4)?,
                    description: row.get(5)?,
                    is_management: row.get(6)?,
                    created_at: row.get(7)?,
                    expires_at: row.get(8)?,
                    revoked: row.get(9)?,
                    revoked_at: row.get(10)?,
                    revoked_reason: row.get(11)?,
                    last_used_at: row.get(12)?,
                    last_used_ip: row.get(13)?,
                    plan_id: row.get(14)?,
                    plan_name: row.get(15)?,
                })
            },
        )
        .optional()
        .map_err(|e| DatabaseError::Query(e.to_string()))?
        .ok_or_else(|| DatabaseError::NotFound(format!("API key {} not found", key_id)))
    }

    /// Get API key info by ID
    pub fn get_api_key_info(
        &self,
        key_id: &str,
    ) -> Result<ApiKeyInfo, DatabaseError> {
        let conn = self.lock_conn()?;
        Self::get_api_key_info_internal(&conn, key_id)
    }

    /// Create a new API key for a user
    pub fn create_api_key(
        &self,
        user_id: i64,
        name: Option<&str>,
        description: Option<&str>,
        expires_in_seconds: Option<i64>,
        is_management: bool,
    ) -> Result<(String, ApiKeyInfo), DatabaseError> {
        let mut conn = self.lock_conn()?;
        let tx = conn
            .transaction()
            .map_err(|e| DatabaseError::Insert(e.to_string()))?;

        let result = if is_management {
            self.issue_management_api_key_with_conn(
                &tx,
                user_id,
                name,
                description,
                expires_in_seconds,
            )?
        } else {
            self.create_service_api_key_with_conn(
                &tx,
                user_id,
                name,
                description,
                expires_in_seconds,
            )?
        };

        tx.commit()
            .map_err(|e| DatabaseError::Insert(e.to_string()))?;

        Ok(result)
    }

    pub fn create_api_key_transactional(
        &self,
        user_id: i64,
        name: Option<&str>,
        description: Option<&str>,
        expires_in_seconds: Option<i64>,
        is_management: bool,
        audit: Option<AuditLogParams>,
    ) -> Result<(String, ApiKeyInfo), DatabaseError> {
        if audit.is_none() {
            return self.create_api_key(
                user_id,
                name,
                description,
                expires_in_seconds,
                is_management,
            );
        }

        let mut conn = self.lock_conn()?;
        let tx = conn
            .transaction()
            .map_err(|e| DatabaseError::Insert(e.to_string()))?;

        let result = if is_management {
            self.issue_management_api_key_with_conn(
                &tx,
                user_id,
                name,
                description,
                expires_in_seconds,
            )?
        } else {
            self.create_service_api_key_with_conn(
                &tx,
                user_id,
                name,
                description,
                expires_in_seconds,
            )?
        };

        if let Some(audit) = audit {
            Self::create_audit_log_with_conn(
                &tx,
                self.audit_enabled(),
                AuditLogParams {
                    api_key_id: Some(&result.1.id),
                    ..audit
                },
            )?;
        }

        tx.commit()
            .map_err(|e| DatabaseError::Insert(e.to_string()))?;

        Ok(result)
    }

    pub fn issue_management_api_key_transactional(
        &self,
        user_id: i64,
        name: Option<&str>,
        description: Option<&str>,
        expires_in_seconds: Option<i64>,
        audit: Option<AuditLogParams>,
    ) -> Result<(String, ApiKeyInfo), DatabaseError> {
        let mut conn = self.lock_conn()?;
        let tx_started = std::time::Instant::now();
        let result = (|| {
            let tx = conn
                .transaction()
                .map_err(|e| DatabaseError::Insert(e.to_string()))?;

            let (api_key, key_info) = self.issue_management_api_key_with_conn(
                &tx,
                user_id,
                name,
                description,
                expires_in_seconds,
            )?;

            if let Some(audit) = audit {
                Self::create_audit_log_with_conn(
                    &tx,
                    self.audit_enabled(),
                    AuditLogParams {
                        api_key_id: Some(&key_info.id),
                        ..audit
                    },
                )?;
            }

            tx.commit()
                .map_err(|e| DatabaseError::Insert(e.to_string()))?;

            Ok((api_key, key_info))
        })();
        self.record_transaction_duration(
            "issue_management_api_key_transactional",
            tx_started.elapsed(),
        );
        result
    }

    pub fn rotate_api_key_transactional(
        &self,
        params: RotateApiKeyParams<'_>,
    ) -> Result<(String, ApiKeyInfo), DatabaseError> {
        let RotateApiKeyParams {
            key_id,
            name,
            description,
            expires_in_seconds,
            revoked_by,
            reason,
            audit,
        } = params;
        let mut conn = self.lock_conn()?;
        let tx_started = std::time::Instant::now();
        let result = (|| {
            let tx = conn
                .transaction()
                .map_err(|e| DatabaseError::Update(e.to_string()))?;

            let existing = Self::get_api_key_info_internal(&tx, key_id)?;
            let new_name = name.unwrap_or(existing.name.as_str());
            let new_description =
                description.or(existing.description.as_deref());

            Self::revoke_api_key_internal(
                &tx,
                &existing.id,
                revoked_by,
                reason,
            )?;

            let (api_key, key_info) = if existing.is_management {
                Self::validate_api_key_creation(
                    &tx,
                    existing.user_id,
                    new_description,
                    expires_in_seconds,
                )?;
                self.insert_api_key_with_conn(
                    &tx,
                    existing.user_id,
                    Some(new_name),
                    new_description,
                    expires_in_seconds,
                    true,
                )?
            } else {
                self.create_service_api_key_with_conn(
                    &tx,
                    existing.user_id,
                    Some(new_name),
                    new_description,
                    expires_in_seconds,
                )?
            };

            Self::transfer_api_key_quota_state_internal(
                &tx,
                &existing.id,
                &key_info.id,
                revoked_by,
            )?;

            if let Some(audit) = audit {
                Self::create_audit_log_with_conn(
                    &tx,
                    self.audit_enabled(),
                    audit,
                )?;
            }

            tx.commit()
                .map_err(|e| DatabaseError::Update(e.to_string()))?;

            Ok((api_key, key_info))
        })();
        self.record_transaction_duration(
            "rotate_api_key_transactional",
            tx_started.elapsed(),
        );
        result
    }

    /// List API keys for a user
    pub fn list_user_api_keys(
        &self,
        user_id: i64,
        include_revoked: bool,
    ) -> Result<Vec<ApiKeyInfo>, DatabaseError> {
        let conn = self.lock_conn()?;

        let query = if include_revoked {
            "SELECT k.id, k.user_id, u.username, k.key_prefix, k.name, k.description,
                    k.is_management, k.created_at, k.expires_at, k.revoked,
                    k.revoked_at, k.revoked_reason, k.last_used_at,
                    k.last_used_ip, kp.plan_id, p.name
             FROM api_keys k
             INNER JOIN users u ON k.user_id = u.id
             LEFT JOIN api_key_plans kp ON kp.api_key_id = k.id
             LEFT JOIN usage_plans p ON p.id = kp.plan_id
             WHERE k.user_id = ?1
             ORDER BY k.created_at DESC"
        } else {
            "SELECT k.id, k.user_id, u.username, k.key_prefix, k.name, k.description,
                    k.is_management, k.created_at, k.expires_at, k.revoked,
                    k.revoked_at, k.revoked_reason, k.last_used_at,
                    k.last_used_ip, kp.plan_id, p.name
             FROM api_keys k
             INNER JOIN users u ON k.user_id = u.id
             LEFT JOIN api_key_plans kp ON kp.api_key_id = k.id
             LEFT JOIN usage_plans p ON p.id = kp.plan_id
             WHERE k.user_id = ?1 AND k.revoked = 0
             ORDER BY k.created_at DESC"
        };

        let mut stmt = conn
            .prepare(query)
            .map_err(|e| DatabaseError::Query(e.to_string()))?;

        let keys = stmt
            .query_map(params![user_id], |row| {
                Ok(ApiKeyInfo {
                    id: row.get(0)?,
                    user_id: row.get(1)?,
                    username: row.get(2)?,
                    key_prefix: row.get(3)?,
                    name: row.get(4)?,
                    description: row.get(5)?,
                    is_management: row.get(6)?,
                    created_at: row.get(7)?,
                    expires_at: row.get(8)?,
                    revoked: row.get(9)?,
                    revoked_at: row.get(10)?,
                    revoked_reason: row.get(11)?,
                    last_used_at: row.get(12)?,
                    last_used_ip: row.get(13)?,
                    plan_id: row.get(14)?,
                    plan_name: row.get(15)?,
                })
            })
            .map_err(|e| DatabaseError::Query(e.to_string()))?
            .collect::<SqliteResult<Vec<_>>>()
            .map_err(|e| DatabaseError::Query(e.to_string()))?;
        drop(stmt);
        drop(conn);

        Ok(keys)
    }

    /// Get an active API key by name for a user
    pub fn get_active_api_key_by_name(
        &self,
        user_id: i64,
        name: &str,
    ) -> Result<ApiKeyInfo, DatabaseError> {
        let conn = self.lock_conn()?;

        let key_id: String = conn
            .query_row(
                "SELECT id FROM api_keys WHERE user_id = ?1 AND name = ?2 AND revoked = 0",
                params![user_id, name],
                |row| row.get(0),
            )
            .optional()
            .map_err(|e| DatabaseError::Query(e.to_string()))?
            .ok_or_else(|| {
                DatabaseError::NotFound(
                    "API key not found for this user/name".into(),
                )
            })?;

        let result = Self::get_api_key_info_internal(&conn, &key_id);
        drop(conn);
        result
    }

    /// List all API keys (admin)
    pub fn list_all_api_keys(
        &self,
        include_revoked: bool,
    ) -> Result<Vec<ApiKeyInfo>, DatabaseError> {
        let conn = self.lock_conn()?;

        let query = if include_revoked {
            "SELECT k.id, k.user_id, u.username, k.key_prefix, k.name, k.description,
                    k.is_management, k.created_at, k.expires_at, k.revoked,
                    k.revoked_at, k.revoked_reason, k.last_used_at,
                    k.last_used_ip, kp.plan_id, p.name
             FROM api_keys k
             INNER JOIN users u ON k.user_id = u.id
             LEFT JOIN api_key_plans kp ON kp.api_key_id = k.id
             LEFT JOIN usage_plans p ON p.id = kp.plan_id
             ORDER BY k.created_at DESC"
        } else {
            "SELECT k.id, k.user_id, u.username, k.key_prefix, k.name, k.description,
                    k.is_management, k.created_at, k.expires_at, k.revoked,
                    k.revoked_at, k.revoked_reason, k.last_used_at,
                    k.last_used_ip, kp.plan_id, p.name
             FROM api_keys k
             INNER JOIN users u ON k.user_id = u.id
             LEFT JOIN api_key_plans kp ON kp.api_key_id = k.id
             LEFT JOIN usage_plans p ON p.id = kp.plan_id
             WHERE k.revoked = 0
             ORDER BY k.created_at DESC"
        };

        let mut stmt = conn
            .prepare(query)
            .map_err(|e| DatabaseError::Query(e.to_string()))?;

        let keys = stmt
            .query_map([], |row| {
                Ok(ApiKeyInfo {
                    id: row.get(0)?,
                    user_id: row.get(1)?,
                    username: row.get(2)?,
                    key_prefix: row.get(3)?,
                    name: row.get(4)?,
                    description: row.get(5)?,
                    is_management: row.get(6)?,
                    created_at: row.get(7)?,
                    expires_at: row.get(8)?,
                    revoked: row.get(9)?,
                    revoked_at: row.get(10)?,
                    revoked_reason: row.get(11)?,
                    last_used_at: row.get(12)?,
                    last_used_ip: row.get(13)?,
                    plan_id: row.get(14)?,
                    plan_name: row.get(15)?,
                })
            })
            .map_err(|e| DatabaseError::Query(e.to_string()))?
            .collect::<SqliteResult<Vec<_>>>()
            .map_err(|e| DatabaseError::Query(e.to_string()))?;
        drop(stmt);
        drop(conn);

        Ok(keys)
    }

    pub fn revoke_api_key_transactional(
        &self,
        key_id: &str,
        revoked_by: Option<i64>,
        reason: Option<&str>,
        audit: Option<AuditLogParams>,
    ) -> Result<(), DatabaseError> {
        let mut conn = self.lock_conn()?;
        let tx = conn
            .transaction()
            .map_err(|e| DatabaseError::Update(e.to_string()))?;
        Self::revoke_api_key_internal(&tx, key_id, revoked_by, reason)?;
        if let Some(audit) = audit {
            Self::create_audit_log_with_conn(&tx, self.audit_enabled(), audit)?;
        }
        tx.commit()
            .map_err(|e| DatabaseError::Update(e.to_string()))?;
        Ok(())
    }

    /// Revoke all API keys for a user
    /// Internal: Revoke all API keys for a user without acquiring lock
    pub(crate) fn revoke_user_api_keys_internal(
        conn: &rusqlite::Connection,
        user_id: i64,
        revoked_by: Option<i64>,
        reason: &str,
    ) -> Result<(), DatabaseError> {
        let now = Self::now();

        conn.execute(
            "UPDATE api_keys
             SET revoked = 1, revoked_at = ?1, revoked_by = ?2, revoked_reason = ?3
             WHERE user_id = ?4 AND revoked = 0",
            params![now, revoked_by, reason, user_id],
        ).map_err(|e| DatabaseError::Update(e.to_string()))?;

        Ok(())
    }

    fn verify_api_key_with_conn(
        &self,
        conn: &rusqlite::Connection,
        api_key: &str,
        ip_address: Option<&str>,
    ) -> Result<AuthContext, DatabaseError> {
        let key_hash = hash_api_key(api_key);

        // Get API key from database
        let key_result = conn
            .query_row(
                "SELECT id, user_id, revoked, expires_at, is_management
                 FROM api_keys
                 WHERE key_hash = ?1",
                params![key_hash],
                |row| {
                    Ok((
                        row.get::<_, String>(0)?,
                        row.get::<_, i64>(1)?,
                        row.get::<_, bool>(2)?,
                        row.get::<_, Option<i64>>(3)?,
                        row.get::<_, bool>(4)?,
                    ))
                },
            )
            .optional()
            .map_err(|e| DatabaseError::Query(e.to_string()))?;

        // SECURITY: Constant-time API key enumeration mitigation
        // If key doesn't exist, perform dummy user query to match timing
        let (key_id, user_id, revoked, expires_at, is_management) =
            match key_result {
                Some(k) => k,
                None => {
                    // API key doesn't exist - perform dummy query to prevent timing attack
                    let _ = conn
                        .query_row(
                            "SELECT id FROM users WHERE id = ?1",
                            params![999999], // Non-existent user
                            |row| row.get::<_, i64>(0),
                        )
                        .optional();

                    return Err(DatabaseError::PermissionDenied(
                        "Invalid API key".to_string(),
                    ));
                }
            };

        // Check if revoked
        if revoked {
            return Err(DatabaseError::PermissionDenied(
                "API key has been revoked".to_string(),
            ));
        }

        // Check if expired
        if let Some(exp) = expires_at {
            let now = Self::now();
            if exp < now {
                return Err(DatabaseError::PermissionDenied(
                    "API key has expired".to_string(),
                ));
            }
        }

        // Get user
        let user = Self::get_user_by_id_internal(conn, user_id)?;

        // Check if user is active
        if !user.is_active {
            return Err(DatabaseError::PermissionDenied(
                "User account is not active".to_string(),
            ));
        }

        // Check if account is locked
        if let Some(locked_until) = user.locked_until
            && locked_until > Self::now()
        {
            return Err(DatabaseError::AccountLocked(
                "Account is temporarily locked".to_string(),
            ));
        }

        // SECURITY FIX: Enforce must_change_password policy for API keys
        // Users with must_change_password cannot use API keys until they change their password
        // This prevents bypassing the forced password change requirement
        if user.must_change_password {
            return Err(DatabaseError::PasswordChangeRequired(
                "Password change required. Please change your password before using API keys".to_string(),
            ));
        }

        // Get user roles
        let roles = Self::get_user_roles_internal(conn, user_id)?;

        let now = Self::now();
        conn.execute(
            "UPDATE api_keys SET last_used_at = ?1, last_used_ip = ?2 WHERE id = ?3",
            params![now, ip_address, &key_id],
        )
        .map_err(|e| DatabaseError::Update(e.to_string()))?;

        let mut permissions =
            Self::get_effective_permissions_internal(conn, user_id)?;

        // Service keys cannot carry admin/panel permissions
        if !is_management {
            let admin_resources = [
                "admin_users",
                "admin_roles",
                "admin_api_key",
                "admin_system",
                "user_api_key",
                "node_maintenance",
            ];
            permissions
                .retain(|p| !admin_resources.contains(&p.resource.as_str()));
        }

        Ok(AuthContext {
            user_id,
            username: user.username,
            roles,
            permissions,
            api_key_id: key_id,
            is_management_key: is_management,
            ip_address: ip_address.map(str::to_string),
        })
    }

    pub fn authenticate_api_key_request(
        &self,
        api_key: &str,
        ip_address: Option<&str>,
        endpoint: &str,
    ) -> Result<AuthContext, DatabaseError> {
        let mut conn = self.lock_conn()?;
        let tx_started = std::time::Instant::now();
        let result = (|| {
            let tx = conn
                .transaction_with_behavior(TransactionBehavior::Immediate)
                .map_err(|e| DatabaseError::Query(e.to_string()))?;

            let auth_ctx =
                self.verify_api_key_with_conn(&tx, api_key, ip_address)?;

            self.check_rate_limit_with_conn(
                &tx,
                Some(&auth_ctx.api_key_id),
                ip_address,
                Some(endpoint),
            )?;

            if !auth_ctx.is_management_key {
                Self::consume_monthly_quota_with_conn(
                    &tx,
                    &auth_ctx.api_key_id,
                )?;
            }

            tx.commit()
                .map_err(|e| DatabaseError::Update(e.to_string()))?;

            Ok(auth_ctx)
        })();
        self.record_transaction_duration(
            "authenticate_api_key_request",
            tx_started.elapsed(),
        );
        result
    }

    /// Delete expired API keys
    pub fn cleanup_expired_api_keys(&self) -> Result<usize, DatabaseError> {
        let conn = self.lock_maintenance_conn()?;

        let now = Self::now();

        let mut total_deleted = 0usize;
        let batch_size = self.expired_api_key_cleanup_batch_size();

        loop {
            let deleted = conn
                .execute(
                    "DELETE FROM api_keys
                     WHERE id IN (
                        SELECT id
                        FROM api_keys
                        WHERE expires_at IS NOT NULL AND expires_at < ?1
                        ORDER BY expires_at ASC
                        LIMIT ?2
                     )",
                    params![now, batch_size],
                )
                .map_err(|e| DatabaseError::Delete(e.to_string()))?;

            total_deleted += deleted;

            if deleted < batch_size as usize {
                break;
            }
        }

        Ok(total_deleted)
    }

    /// Validate API key name for dangerous characters
    ///
    /// SECURITY FIX: Prevents XSS, SQL injection, command injection, and path traversal
    /// by rejecting names containing dangerous characters.
    ///
    /// Allowed characters: alphanumeric, underscore, hyphen, space, period
    /// Max length: 100 characters
    fn validate_api_key_name(name: &str) -> Result<(), DatabaseError> {
        // Check length
        if name.len() > 100 {
            return Err(DatabaseError::Validation(
                "API key name must be 100 characters or less".to_string(),
            ));
        }

        // Check for null bytes (command injection, path traversal)
        if name.contains('\0') {
            return Err(DatabaseError::Validation(
                "API key name contains invalid characters".to_string(),
            ));
        }

        // Check for control characters (including newlines, tabs, etc)
        if name.chars().any(|c| c.is_control()) {
            return Err(DatabaseError::Validation(
                "API key name contains invalid control characters".to_string(),
            ));
        }

        // Check for dangerous characters that could be used for attacks
        let dangerous_chars = [
            '<', '>', '"', '\'', '`', '&', '|', ';', '$', '(', ')', '{', '}',
            '[', ']', '\\', '/', ':', '*', '?', '%',
        ];
        if name.chars().any(|c| dangerous_chars.contains(&c)) {
            return Err(DatabaseError::Validation(
                "API key name contains invalid characters. Only alphanumeric, underscore, hyphen, space, and period are allowed".to_string()
            ));
        }

        // Check for path traversal patterns
        if name.contains("..") || name.contains("./") || name.contains(".\\") {
            return Err(DatabaseError::Validation(
                "API key name contains invalid patterns".to_string(),
            ));
        }

        Ok(())
    }
}