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
991
992
993
994
995
// Ave HTTP Auth System - API Key Endpoint Handlers
//
// REST API endpoints for API key management

use super::database::{AuthDatabase, DatabaseError};
use super::http_api::{DatabaseErrorMapping, run_db as shared_run_db};
use super::middleware::{AuthContextExtractor, check_permission};
use super::models::*;
use axum::{
    Extension, Json,
    extract::{Path, Query},
    http::StatusCode,
};
use serde::Deserialize;
use std::sync::Arc;
use utoipa::ToSchema;

async fn run_db<T, F>(
    db: &Arc<AuthDatabase>,
    operation: &'static str,
    work: F,
) -> Result<T, (StatusCode, Json<ErrorResponse>)>
where
    T: Send + 'static,
    F: FnOnce(AuthDatabase) -> Result<T, DatabaseError> + Send + 'static,
{
    shared_run_db(db, operation, DatabaseErrorMapping::admin(), work).await
}

// =============================================================================
// API KEY MANAGEMENT ENDPOINTS (ADMIN)
// =============================================================================

/// Create API key for a user (admin)
#[utoipa::path(
    post,
    path = "/admin/api-keys/user/{user_id}",
    operation_id = "createApiKeyForUser",
    tag = "API Key Management",
    params(
        ("user_id" = i64, Path, description = "User ID")
    ),
    request_body = CreateApiKeyRequest,
    responses(
        (status = 201, description = "API key created successfully", body = CreateApiKeyResponse),
        (status = 400, description = "Invalid request", body = ErrorResponse),
        (status = 403, description = "Permission denied", body = ErrorResponse),
        (status = 404, description = "User not found", body = ErrorResponse),
    ),
    security(("api_key" = []))
)]
pub async fn create_api_key_for_user(
    AuthContextExtractor(auth_ctx): AuthContextExtractor,
    Extension(db): Extension<Arc<AuthDatabase>>,
    Path(user_id): Path<i64>,
    Json(req): Json<CreateApiKeyRequest>,
) -> Result<
    (StatusCode, Json<CreateApiKeyResponse>),
    (StatusCode, Json<ErrorResponse>),
> {
    // Check permission
    check_permission(&auth_ctx, "admin_api_key", "post")?;

    // SECURITY FIX: Prevent API key impersonation
    // Only superadmin can create keys for other users
    if user_id != auth_ctx.user_id && !auth_ctx.is_superadmin() {
        return Err((
            StatusCode::FORBIDDEN,
            Json(ErrorResponse {
                error: "Only superadmin can create API keys for other users"
                    .to_string(),
            }),
        ));
    }

    let name = req.name.clone();
    let description = req.description.clone();
    let expires_in_seconds = req.expires_in_seconds;
    let actor_user_id = auth_ctx.user_id;
    let actor_api_key_id = auth_ctx.api_key_id.clone();
    let actor_ip_address = auth_ctx.ip_address.clone();
    let endpoint = format!("/admin/api-keys/user/{}", user_id);
    let audit_details = serde_json::to_string(&req).unwrap_or_default();
    let (api_key, key_info) =
        run_db(&db, "create_api_key_for_user", move |db| {
            db.create_api_key_transactional(
                user_id,
                Some(&name),
                description.as_deref(),
                expires_in_seconds,
                false,
                Some(crate::auth::database_audit::AuditLogParams {
                    user_id: Some(actor_user_id),
                    api_key_id: Some(&actor_api_key_id),
                    action_type: "api_key_created",
                    endpoint: Some(&endpoint),
                    http_method: Some("POST"),
                    ip_address: actor_ip_address.as_deref(),
                    user_agent: None,
                    request_id: None,
                    details: Some(&audit_details),
                    success: true,
                    error_message: None,
                }),
            )
        })
        .await?;

    let response = CreateApiKeyResponse { api_key, key_info };

    Ok((StatusCode::CREATED, Json(response)))
}

/// List all API keys (admin)
#[utoipa::path(
    get,
    path = "/admin/api-keys",
    operation_id = "listAllApiKeys",
    tag = "API Key Management",
    params(
        ("include_revoked" = Option<bool>, Query, description = "Include revoked keys")
    ),
    responses(
        (status = 200, description = "List of API keys", body = Vec<ApiKeyInfo>),
        (status = 403, description = "Permission denied", body = ErrorResponse),
    ),
    security(("api_key" = []))
)]
pub async fn list_all_api_keys(
    AuthContextExtractor(auth_ctx): AuthContextExtractor,
    Extension(db): Extension<Arc<AuthDatabase>>,
    Query(params): Query<ListApiKeysQuery>,
) -> Result<Json<Vec<ApiKeyInfo>>, (StatusCode, Json<ErrorResponse>)> {
    // Check permission
    check_permission(&auth_ctx, "admin_api_key", "get")?;

    let include_revoked = params.include_revoked.unwrap_or(false);
    let keys = run_db(&db, "list_all_api_keys", move |db| {
        db.list_all_api_keys(include_revoked)
    })
    .await?;

    Ok(Json(keys))
}

#[derive(Deserialize, ToSchema)]
pub struct ListApiKeysQuery {
    pub include_revoked: Option<bool>,
}

/// List API keys for a user (admin)
#[utoipa::path(
    get,
    path = "/admin/api-keys/user/{user_id}",
    operation_id = "listUserApiKeys",
    tag = "API Key Management",
    params(
        ("user_id" = i64, Path, description = "User ID"),
        ("include_revoked" = Option<bool>, Query, description = "Include revoked keys")
    ),
    responses(
        (status = 200, description = "List of user API keys", body = Vec<ApiKeyInfo>),
        (status = 403, description = "Permission denied", body = ErrorResponse),
        (status = 404, description = "User not found", body = ErrorResponse),
    ),
    security(("api_key" = []))
)]
pub async fn list_user_api_keys_admin(
    AuthContextExtractor(auth_ctx): AuthContextExtractor,
    Extension(db): Extension<Arc<AuthDatabase>>,
    Path(user_id): Path<i64>,
    Query(params): Query<ListApiKeysQuery>,
) -> Result<Json<Vec<ApiKeyInfo>>, (StatusCode, Json<ErrorResponse>)> {
    // Check permission
    check_permission(&auth_ctx, "admin_api_key", "get")?;

    let include_revoked = params.include_revoked.unwrap_or(false);
    let keys = run_db(&db, "list_user_api_keys_admin", move |db| {
        db.list_user_api_keys(user_id, include_revoked)
    })
    .await?;

    Ok(Json(keys))
}

/// Get API key info (admin)
#[utoipa::path(
    get,
    path = "/admin/api-keys/{id}",
    operation_id = "getApiKey",
    tag = "API Key Management",
    params(
        ("id" = String, Path, description = "API Key ID (UUID)")
    ),
    responses(
        (status = 200, description = "API key information", body = ApiKeyInfo),
        (status = 403, description = "Permission denied", body = ErrorResponse),
        (status = 404, description = "API key not found", body = ErrorResponse),
    ),
    security(("api_key" = []))
)]
pub async fn get_api_key(
    AuthContextExtractor(auth_ctx): AuthContextExtractor,
    Extension(db): Extension<Arc<AuthDatabase>>,
    Path(id): Path<String>,
) -> Result<Json<ApiKeyInfo>, (StatusCode, Json<ErrorResponse>)> {
    // Check permission
    check_permission(&auth_ctx, "admin_api_key", "get")?;

    let key_info =
        run_db(&db, "get_api_key", move |db| db.get_api_key_info(&id)).await?;

    Ok(Json(key_info))
}

/// Revoke API key (admin)
#[utoipa::path(
    delete,
    path = "/admin/api-keys/{id}",
    operation_id = "revokeApiKey",
    tag = "API Key Management",
    params(
        ("id" = String, Path, description = "API Key ID (UUID)")
    ),
    request_body(content = RevokeApiKeyRequest, description = "Optional revocation reason", content_type = "application/json"),
    responses(
        (status = 204, description = "API key revoked successfully"),
        (status = 403, description = "Permission denied", body = ErrorResponse),
        (status = 404, description = "API key not found", body = ErrorResponse),
    ),
    security(("api_key" = []))
)]
pub async fn revoke_api_key(
    AuthContextExtractor(auth_ctx): AuthContextExtractor,
    Extension(db): Extension<Arc<AuthDatabase>>,
    Path(id): Path<String>,
    req: Option<Json<RevokeApiKeyRequest>>,
) -> Result<StatusCode, (StatusCode, Json<ErrorResponse>)> {
    // Check permission
    check_permission(&auth_ctx, "admin_api_key", "delete")?;

    // SECURITY FIX: Prevent revoking the currently used API key
    if id == auth_ctx.api_key_id {
        return Err((
            StatusCode::BAD_REQUEST,
            Json(ErrorResponse {
                error: "Cannot revoke the currently used API key".to_string(),
            }),
        ));
    }

    // SECURITY FIX: Prevent API key DoS by revoking other users' keys
    // Get the key to check ownership
    let lookup_id = id.clone();
    let key_info = run_db(&db, "get_api_key_for_revoke", move |db| {
        db.get_api_key_info(&lookup_id)
    })
    .await?;

    // Only superadmin can revoke keys of other users
    if key_info.user_id != auth_ctx.user_id && !auth_ctx.is_superadmin() {
        return Err((
            StatusCode::FORBIDDEN,
            Json(ErrorResponse {
                error: "Only superadmin can revoke API keys of other users"
                    .to_string(),
            }),
        ));
    }

    let reason = req.as_ref().and_then(|r| r.reason.clone());
    let audit_details = req
        .as_ref()
        .map(|r| serde_json::to_string(&r.0).unwrap_or_default())
        .unwrap_or_default();
    let revoke_id = id.clone();
    let auth_ctx_for_db = auth_ctx.clone();
    run_db(&db, "revoke_api_key", move |db| {
        db.revoke_api_key_transactional(
            &revoke_id,
            Some(auth_ctx_for_db.user_id),
            reason.as_deref(),
            Some(crate::auth::database_audit::AuditLogParams {
                user_id: Some(auth_ctx_for_db.user_id),
                api_key_id: Some(&auth_ctx_for_db.api_key_id),
                action_type: "api_key_revoked",
                endpoint: Some(&format!("/admin/api-keys/{}", revoke_id)),
                http_method: Some("DELETE"),
                ip_address: auth_ctx_for_db.ip_address.as_deref(),
                user_agent: None,
                request_id: None,
                details: Some(&audit_details),
                success: true,
                error_message: None,
            }),
        )
    })
    .await?;

    Ok(StatusCode::NO_CONTENT)
}

/// Rotate an existing API key (admin)
#[utoipa::path(
    post,
    path = "/admin/api-keys/{id}/rotate",
    operation_id = "rotateApiKey",
    tag = "API Key Management",
    params(
        ("id" = String, Path, description = "API Key ID (UUID)")
    ),
    request_body = RotateApiKeyRequest,
    responses(
        (status = 201, description = "API key rotated successfully", body = CreateApiKeyResponse),
        (status = 403, description = "Permission denied", body = ErrorResponse),
        (status = 404, description = "API key not found", body = ErrorResponse),
    ),
    security(("api_key" = []))
)]
pub async fn rotate_api_key(
    AuthContextExtractor(auth_ctx): AuthContextExtractor,
    Extension(db): Extension<Arc<AuthDatabase>>,
    Path(id): Path<String>,
    req: Option<Json<RotateApiKeyRequest>>,
) -> Result<
    (StatusCode, Json<CreateApiKeyResponse>),
    (StatusCode, Json<ErrorResponse>),
> {
    // Check permission
    check_permission(&auth_ctx, "admin_api_key", "post")?;

    // Fetch existing key for user and defaults
    let lookup_id = id.clone();
    let existing = run_db(&db, "get_api_key_for_rotate", move |db| {
        db.get_api_key_info(&lookup_id)
    })
    .await?;

    // SECURITY FIX: Prevent API key theft via rotation of other users' keys
    // Only superadmin can rotate keys of other users
    if existing.user_id != auth_ctx.user_id && !auth_ctx.is_superadmin() {
        return Err((
            StatusCode::FORBIDDEN,
            Json(ErrorResponse {
                error: "Only superadmin can rotate API keys of other users"
                    .to_string(),
            }),
        ));
    }

    // Extract request body or use defaults
    let req = req.as_ref().map(|r| &r.0);
    let audit_details = serde_json::to_string(&req).unwrap_or_default();

    let existing_id = existing.id.clone();
    let auth_ctx_for_db = auth_ctx.clone();
    let req_name = req.and_then(|r| r.name.clone());
    let req_description = req.and_then(|r| r.description.clone());
    let req_expires = req.and_then(|r| r.expires_in_seconds);
    let req_reason = req.and_then(|r| r.reason.clone());
    let rotate_endpoint = format!("/admin/api-keys/{}/rotate", id);
    let (api_key, key_info) = run_db(&db, "rotate_api_key", move |db| {
        db.rotate_api_key_transactional(crate::auth::RotateApiKeyParams {
            key_id: &existing_id,
            name: req_name.as_deref(),
            description: req_description.as_deref(),
            expires_in_seconds: req_expires,
            revoked_by: Some(auth_ctx_for_db.user_id),
            reason: req_reason.as_deref(),
            audit: Some(crate::auth::database_audit::AuditLogParams {
                user_id: Some(auth_ctx_for_db.user_id),
                api_key_id: Some(&auth_ctx_for_db.api_key_id),
                action_type: "api_key_rotated",
                endpoint: Some(&rotate_endpoint),
                http_method: Some("POST"),
                ip_address: auth_ctx_for_db.ip_address.as_deref(),
                user_agent: None,
                request_id: None,
                details: Some(&audit_details),
                success: true,
                error_message: None,
            }),
        })
    })
    .await?;

    let response = CreateApiKeyResponse { api_key, key_info };

    Ok((StatusCode::CREATED, Json(response)))
}

/// Create usage plan (admin)
#[utoipa::path(
    post,
    path = "/admin/usage-plans",
    operation_id = "createUsagePlan",
    tag = "API Key Management",
    request_body = CreateUsagePlanRequest,
    responses(
        (status = 201, description = "Usage plan created successfully", body = UsagePlan),
        (status = 400, description = "Invalid request", body = ErrorResponse),
        (status = 403, description = "Permission denied", body = ErrorResponse),
        (status = 409, description = "Plan already exists", body = ErrorResponse),
    ),
    security(("api_key" = []))
)]
pub async fn create_usage_plan(
    AuthContextExtractor(auth_ctx): AuthContextExtractor,
    Extension(db): Extension<Arc<AuthDatabase>>,
    Json(req): Json<CreateUsagePlanRequest>,
) -> Result<(StatusCode, Json<UsagePlan>), (StatusCode, Json<ErrorResponse>)> {
    check_permission(&auth_ctx, "admin_api_key", "post")?;

    let plan_id = req.id.clone();
    let plan_name = req.name.clone();
    let description = req.description.clone();
    let monthly_events = req.monthly_events;
    let auth_ctx_for_db = auth_ctx.clone();
    let audit_details = serde_json::to_string(&req).unwrap_or_default();
    let plan = run_db(&db, "create_usage_plan", move |db| {
        db.create_usage_plan_transactional(
            &plan_id,
            &plan_name,
            description.as_deref(),
            monthly_events,
            Some(crate::auth::database_audit::AuditLogParams {
                user_id: Some(auth_ctx_for_db.user_id),
                api_key_id: Some(&auth_ctx_for_db.api_key_id),
                action_type: "usage_plan_created",
                endpoint: Some("/admin/usage-plans"),
                http_method: Some("POST"),
                ip_address: auth_ctx_for_db.ip_address.as_deref(),
                user_agent: None,
                request_id: None,
                details: Some(&audit_details),
                success: true,
                error_message: None,
            }),
        )
    })
    .await?;

    Ok((StatusCode::CREATED, Json(plan)))
}

/// List usage plans (admin)
#[utoipa::path(
    get,
    path = "/admin/usage-plans",
    operation_id = "listUsagePlans",
    tag = "API Key Management",
    responses(
        (status = 200, description = "List usage plans", body = Vec<UsagePlan>),
        (status = 403, description = "Permission denied", body = ErrorResponse),
    ),
    security(("api_key" = []))
)]
pub async fn list_usage_plans(
    AuthContextExtractor(auth_ctx): AuthContextExtractor,
    Extension(db): Extension<Arc<AuthDatabase>>,
) -> Result<Json<Vec<UsagePlan>>, (StatusCode, Json<ErrorResponse>)> {
    check_permission(&auth_ctx, "admin_api_key", "get")?;

    let plans =
        run_db(&db, "list_usage_plans", move |db| db.list_usage_plans())
            .await?;
    Ok(Json(plans))
}

/// Get usage plan by id (admin)
#[utoipa::path(
    get,
    path = "/admin/usage-plans/{plan_id}",
    operation_id = "getUsagePlan",
    tag = "API Key Management",
    params(
        ("plan_id" = String, Path, description = "Usage plan id")
    ),
    responses(
        (status = 200, description = "Usage plan", body = UsagePlan),
        (status = 403, description = "Permission denied", body = ErrorResponse),
        (status = 404, description = "Plan not found", body = ErrorResponse),
    ),
    security(("api_key" = []))
)]
pub async fn get_usage_plan(
    AuthContextExtractor(auth_ctx): AuthContextExtractor,
    Extension(db): Extension<Arc<AuthDatabase>>,
    Path(plan_id): Path<String>,
) -> Result<Json<UsagePlan>, (StatusCode, Json<ErrorResponse>)> {
    check_permission(&auth_ctx, "admin_api_key", "get")?;

    let plan =
        run_db(&db, "get_usage_plan", move |db| db.get_usage_plan(&plan_id))
            .await?;
    Ok(Json(plan))
}

/// Update usage plan (admin)
#[utoipa::path(
    put,
    path = "/admin/usage-plans/{plan_id}",
    operation_id = "updateUsagePlan",
    tag = "API Key Management",
    params(
        ("plan_id" = String, Path, description = "Usage plan id")
    ),
    request_body = UpdateUsagePlanRequest,
    responses(
        (status = 200, description = "Updated usage plan", body = UsagePlan),
        (status = 403, description = "Permission denied", body = ErrorResponse),
        (status = 409, description = "Plan name already exists", body = ErrorResponse),
        (status = 404, description = "Plan not found", body = ErrorResponse),
    ),
    security(("api_key" = []))
)]
pub async fn update_usage_plan(
    AuthContextExtractor(auth_ctx): AuthContextExtractor,
    Extension(db): Extension<Arc<AuthDatabase>>,
    Path(plan_id): Path<String>,
    Json(req): Json<UpdateUsagePlanRequest>,
) -> Result<Json<UsagePlan>, (StatusCode, Json<ErrorResponse>)> {
    check_permission(&auth_ctx, "admin_api_key", "put")?;

    let req_name = req.name.clone();
    let req_description = req.description.clone();
    let req_monthly_events = req.monthly_events;
    let update_plan_id = plan_id.clone();
    let auth_ctx_for_db = auth_ctx.clone();
    let audit_details = serde_json::to_string(&req).unwrap_or_default();
    let plan = run_db(&db, "update_usage_plan", move |db| {
        db.update_usage_plan_transactional(
            &update_plan_id,
            req_name.as_deref(),
            req_description.as_deref(),
            req_monthly_events,
            Some(crate::auth::database_audit::AuditLogParams {
                user_id: Some(auth_ctx_for_db.user_id),
                api_key_id: Some(&auth_ctx_for_db.api_key_id),
                action_type: "usage_plan_updated",
                endpoint: Some(&format!("/admin/usage-plans/{}", plan_id)),
                http_method: Some("PUT"),
                ip_address: auth_ctx_for_db.ip_address.as_deref(),
                user_agent: None,
                request_id: None,
                details: Some(&audit_details),
                success: true,
                error_message: None,
            }),
        )
    })
    .await?;

    Ok(Json(plan))
}

/// Delete usage plan (admin)
#[utoipa::path(
    delete,
    path = "/admin/usage-plans/{plan_id}",
    operation_id = "deleteUsagePlan",
    tag = "API Key Management",
    params(
        ("plan_id" = String, Path, description = "Usage plan id")
    ),
    responses(
        (status = 204, description = "Usage plan deleted"),
        (status = 403, description = "Permission denied", body = ErrorResponse),
        (status = 404, description = "Plan not found", body = ErrorResponse),
    ),
    security(("api_key" = []))
)]
pub async fn delete_usage_plan(
    AuthContextExtractor(auth_ctx): AuthContextExtractor,
    Extension(db): Extension<Arc<AuthDatabase>>,
    Path(plan_id): Path<String>,
) -> Result<StatusCode, (StatusCode, Json<ErrorResponse>)> {
    check_permission(&auth_ctx, "admin_api_key", "delete")?;

    let delete_plan_id = plan_id.clone();
    let auth_ctx_for_db = auth_ctx.clone();
    run_db(&db, "delete_usage_plan", move |db| {
        db.delete_usage_plan_transactional(
            &delete_plan_id,
            Some(crate::auth::database_audit::AuditLogParams {
                user_id: Some(auth_ctx_for_db.user_id),
                api_key_id: Some(&auth_ctx_for_db.api_key_id),
                action_type: "usage_plan_deleted",
                endpoint: Some(&format!("/admin/usage-plans/{}", plan_id)),
                http_method: Some("DELETE"),
                ip_address: auth_ctx_for_db.ip_address.as_deref(),
                user_agent: None,
                request_id: None,
                details: Some(
                    &serde_json::json!({ "plan_id": plan_id }).to_string(),
                ),
                success: true,
                error_message: None,
            }),
        )
    })
    .await?;

    Ok(StatusCode::NO_CONTENT)
}

/// Assign (or clear) usage plan from API key (admin)
#[utoipa::path(
    put,
    path = "/admin/api-keys/{id}/plan",
    operation_id = "assignApiKeyPlan",
    tag = "API Key Management",
    params(
        ("id" = String, Path, description = "API key id")
    ),
    request_body = AssignApiKeyPlanRequest,
    responses(
        (status = 200, description = "API key plan updated", body = ApiKeyInfo),
        (status = 403, description = "Permission denied", body = ErrorResponse),
        (status = 404, description = "API key or plan not found", body = ErrorResponse),
    ),
    security(("api_key" = []))
)]
pub async fn assign_api_key_plan(
    AuthContextExtractor(auth_ctx): AuthContextExtractor,
    Extension(db): Extension<Arc<AuthDatabase>>,
    Path(id): Path<String>,
    Json(req): Json<AssignApiKeyPlanRequest>,
) -> Result<Json<ApiKeyInfo>, (StatusCode, Json<ErrorResponse>)> {
    check_permission(&auth_ctx, "admin_api_key", "put")?;

    let plan_id = req.plan_id.clone();
    let auth_ctx_user_id = auth_ctx.user_id;
    let assign_id = id.clone();
    let auth_ctx_for_db = auth_ctx.clone();
    let audit_details = serde_json::to_string(&req).unwrap_or_default();
    let updated = run_db(&db, "assign_api_key_plan", move |db| {
        db.assign_api_key_plan_transactional(
            &assign_id,
            plan_id.as_deref(),
            Some(auth_ctx_user_id),
            Some(crate::auth::database_audit::AuditLogParams {
                user_id: Some(auth_ctx_for_db.user_id),
                api_key_id: Some(&auth_ctx_for_db.api_key_id),
                action_type: "api_key_plan_updated",
                endpoint: Some(&format!("/admin/api-keys/{}/plan", id)),
                http_method: Some("PUT"),
                ip_address: auth_ctx_for_db.ip_address.as_deref(),
                user_agent: None,
                request_id: None,
                details: Some(&audit_details),
                success: true,
                error_message: None,
            }),
        )?;
        db.get_api_key_info(&assign_id)
    })
    .await?;
    Ok(Json(updated))
}

#[derive(Debug, Clone, Deserialize, ToSchema)]
pub struct QuotaStatusQuery {
    pub usage_month: Option<String>,
}

/// Get monthly quota status for API key (admin)
#[utoipa::path(
    get,
    path = "/admin/api-keys/{id}/quota",
    operation_id = "getApiKeyQuotaStatus",
    tag = "API Key Management",
    params(
        ("id" = String, Path, description = "API key id"),
        ("usage_month" = Option<String>, Query, description = "UTC month in YYYY-MM")
    ),
    responses(
        (status = 200, description = "API key quota status", body = ApiKeyQuotaStatus),
        (status = 403, description = "Permission denied", body = ErrorResponse),
        (status = 404, description = "API key not found", body = ErrorResponse),
    ),
    security(("api_key" = []))
)]
pub async fn get_api_key_quota_status(
    AuthContextExtractor(auth_ctx): AuthContextExtractor,
    Extension(db): Extension<Arc<AuthDatabase>>,
    Path(id): Path<String>,
    Query(params): Query<QuotaStatusQuery>,
) -> Result<Json<ApiKeyQuotaStatus>, (StatusCode, Json<ErrorResponse>)> {
    check_permission(&auth_ctx, "admin_api_key", "get")?;

    let usage_month = params.usage_month.clone();
    let status = run_db(&db, "get_api_key_quota_status", move |db| {
        db.get_api_key_quota_status(&id, usage_month.as_deref())
    })
    .await?;

    Ok(Json(status))
}

/// Add monthly quota extension for API key (admin)
#[utoipa::path(
    post,
    path = "/admin/api-keys/{id}/quota-extensions",
    operation_id = "addApiKeyQuotaExtension",
    tag = "API Key Management",
    params(
        ("id" = String, Path, description = "API key id")
    ),
    request_body = CreateQuotaExtensionRequest,
    responses(
        (status = 201, description = "Quota extension created", body = QuotaExtensionInfo),
        (status = 400, description = "Invalid request", body = ErrorResponse),
        (status = 403, description = "Permission denied", body = ErrorResponse),
        (status = 404, description = "API key not found", body = ErrorResponse),
    ),
    security(("api_key" = []))
)]
pub async fn add_api_key_quota_extension(
    AuthContextExtractor(auth_ctx): AuthContextExtractor,
    Extension(db): Extension<Arc<AuthDatabase>>,
    Path(id): Path<String>,
    Json(req): Json<CreateQuotaExtensionRequest>,
) -> Result<
    (StatusCode, Json<QuotaExtensionInfo>),
    (StatusCode, Json<ErrorResponse>),
> {
    check_permission(&auth_ctx, "admin_api_key", "post")?;

    let extra_events = req.extra_events;
    let usage_month = req.usage_month.clone();
    let reason = req.reason.clone();
    let auth_ctx_user_id = auth_ctx.user_id;
    let quota_key_id = id.clone();
    let auth_ctx_for_db = auth_ctx.clone();
    let audit_details = serde_json::to_string(&req).unwrap_or_default();
    let extension = run_db(&db, "add_api_key_quota_extension", move |db| {
        db.add_quota_extension_transactional(
            &quota_key_id,
            extra_events,
            usage_month.as_deref(),
            reason.as_deref(),
            Some(auth_ctx_user_id),
            Some(crate::auth::database_audit::AuditLogParams {
                user_id: Some(auth_ctx_for_db.user_id),
                api_key_id: Some(&auth_ctx_for_db.api_key_id),
                action_type: "api_key_quota_extension_created",
                endpoint: Some(&format!(
                    "/admin/api-keys/{}/quota-extensions",
                    id
                )),
                http_method: Some("POST"),
                ip_address: auth_ctx_for_db.ip_address.as_deref(),
                user_agent: None,
                request_id: None,
                details: Some(&audit_details),
                success: true,
                error_message: None,
            }),
        )
    })
    .await?;

    Ok((StatusCode::CREATED, Json(extension)))
}

// =============================================================================
// MY API KEYS ENDPOINTS (NON-ADMIN)
// =============================================================================

/// Create own API key
#[utoipa::path(
    post,
    path = "/me/api-keys",
    operation_id = "createMyApiKey",
    tag = "My Account",
    request_body = CreateApiKeyRequest,
    responses(
        (status = 201, description = "API key created successfully", body = CreateApiKeyResponse),
        (status = 400, description = "Invalid request", body = ErrorResponse),
    ),
    security(("api_key" = []))
)]
pub async fn create_my_api_key(
    AuthContextExtractor(auth_ctx): AuthContextExtractor,
    Extension(db): Extension<Arc<AuthDatabase>>,
    Json(req): Json<CreateApiKeyRequest>,
) -> Result<
    (StatusCode, Json<CreateApiKeyResponse>),
    (StatusCode, Json<ErrorResponse>),
> {
    // Only management key (login) can manage service keys
    if !auth_ctx.is_management_key {
        return Err((
            StatusCode::FORBIDDEN,
            Json(ErrorResponse {
                error: "Only management API key can create service keys".into(),
            }),
        ));
    }

    // Require permission to manage personal API keys
    if !auth_ctx.has_permission("user_api_key", "post") {
        return Err((
            StatusCode::FORBIDDEN,
            Json(ErrorResponse {
                error: "User is not allowed to manage personal API keys".into(),
            }),
        ));
    }

    let name = req.name.clone();
    let description = req.description.clone();
    let expires_in_seconds = req.expires_in_seconds;
    let user_id = auth_ctx.user_id;
    let actor_api_key_id = auth_ctx.api_key_id.clone();
    let actor_ip_address = auth_ctx.ip_address.clone();
    let audit_details = serde_json::to_string(&req).unwrap_or_default();
    let (api_key, key_info) = run_db(&db, "create_my_api_key", move |db| {
        db.create_api_key_transactional(
            user_id,
            Some(&name),
            description.as_deref(),
            expires_in_seconds,
            false,
            Some(crate::auth::database_audit::AuditLogParams {
                user_id: Some(user_id),
                api_key_id: Some(&actor_api_key_id),
                action_type: "api_key_created",
                endpoint: Some("/me/api-keys"),
                http_method: Some("POST"),
                ip_address: actor_ip_address.as_deref(),
                user_agent: None,
                request_id: None,
                details: Some(&audit_details),
                success: true,
                error_message: None,
            }),
        )
    })
    .await?;

    let response = CreateApiKeyResponse { api_key, key_info };

    Ok((StatusCode::CREATED, Json(response)))
}

/// List own API keys
#[utoipa::path(
    get,
    path = "/me/api-keys",
    operation_id = "listMyApiKeys",
    tag = "My Account",
    params(
        ("include_revoked" = Option<bool>, Query, description = "Include revoked keys")
    ),
    responses(
        (status = 200, description = "List of own API keys", body = Vec<ApiKeyInfo>),
    ),
    security(("api_key" = []))
)]
pub async fn list_my_api_keys(
    AuthContextExtractor(auth_ctx): AuthContextExtractor,
    Extension(db): Extension<Arc<AuthDatabase>>,
    Query(params): Query<ListApiKeysQuery>,
) -> Result<Json<Vec<ApiKeyInfo>>, (StatusCode, Json<ErrorResponse>)> {
    if !auth_ctx.is_management_key {
        return Err((
            StatusCode::FORBIDDEN,
            Json(ErrorResponse {
                error: "Only management API key can list service keys".into(),
            }),
        ));
    }

    if !auth_ctx.has_permission("user_api_key", "get") {
        return Err((
            StatusCode::FORBIDDEN,
            Json(ErrorResponse {
                error: "User is not allowed to view personal API keys".into(),
            }),
        ));
    }

    let user_id = auth_ctx.user_id;
    let include_revoked = params.include_revoked.unwrap_or(false);
    let keys = run_db(&db, "list_my_api_keys", move |db| {
        db.list_user_api_keys(user_id, include_revoked)
    })
    .await?;

    Ok(Json(keys))
}

/// Revoke own API key
#[utoipa::path(
    delete,
    path = "/me/api-keys/{name}",
    operation_id = "revokeMyApiKey",
    tag = "My Account",
    params(
        ("name" = String, Path, description = "API Key name")
    ),
    request_body = RevokeApiKeyRequest,
    responses(
        (status = 204, description = "API key revoked successfully"),
        (status = 403, description = "Cannot revoke other user's key", body = ErrorResponse),
        (status = 404, description = "API key not found", body = ErrorResponse),
    ),
    security(("api_key" = []))
)]
pub async fn revoke_my_api_key(
    AuthContextExtractor(auth_ctx): AuthContextExtractor,
    Extension(db): Extension<Arc<AuthDatabase>>,
    Path(name): Path<String>,
    req: Option<Json<RevokeApiKeyRequest>>,
) -> Result<StatusCode, (StatusCode, Json<ErrorResponse>)> {
    if !auth_ctx.is_management_key {
        return Err((
            StatusCode::FORBIDDEN,
            Json(ErrorResponse {
                error: "Only management API key can revoke service keys".into(),
            }),
        ));
    }

    if !auth_ctx.has_permission("user_api_key", "delete") {
        return Err((
            StatusCode::FORBIDDEN,
            Json(ErrorResponse {
                error: "User is not allowed to revoke personal API keys".into(),
            }),
        ));
    }

    // Verify the key belongs to the user and is active by name
    let user_id = auth_ctx.user_id;
    let lookup_name = name.clone();
    let key_info = run_db(&db, "get_active_api_key_by_name", move |db| {
        db.get_active_api_key_by_name(user_id, &lookup_name)
    })
    .await?;

    // Cannot revoke the current key
    if key_info.id == auth_ctx.api_key_id {
        return Err((
            StatusCode::BAD_REQUEST,
            Json(ErrorResponse {
                error: "Cannot revoke the currently used API key".to_string(),
            }),
        ));
    }

    // Prevent revoking management key
    if key_info.is_management {
        return Err((
            StatusCode::BAD_REQUEST,
            Json(ErrorResponse {
                error: "Cannot revoke the management API key".to_string(),
            }),
        ));
    }

    let reason = req.as_ref().and_then(|r| r.reason.clone());
    let audit_details = req
        .as_ref()
        .map(|r| serde_json::to_string(&r.0).unwrap_or_default())
        .unwrap_or_default();
    let revoke_id = key_info.id.clone();
    let auth_ctx_for_db = auth_ctx.clone();
    let endpoint = format!("/me/api-keys/{}", name);
    run_db(&db, "revoke_my_api_key", move |db| {
        db.revoke_api_key_transactional(
            &revoke_id,
            Some(auth_ctx_for_db.user_id),
            reason.as_deref(),
            Some(crate::auth::database_audit::AuditLogParams {
                user_id: Some(auth_ctx_for_db.user_id),
                api_key_id: Some(&auth_ctx_for_db.api_key_id),
                action_type: "api_key_revoked",
                endpoint: Some(&endpoint),
                http_method: Some("DELETE"),
                ip_address: auth_ctx_for_db.ip_address.as_deref(),
                user_agent: None,
                request_id: None,
                details: Some(&audit_details),
                success: true,
                error_message: None,
            }),
        )
    })
    .await?;

    Ok(StatusCode::NO_CONTENT)
}