Skip to content

Commit d92daca

Browse files
committed
Refactor middleware
1 parent 924f718 commit d92daca

12 files changed

Lines changed: 33 additions & 28 deletions

File tree

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ use axum::{
2424
use tower_cookies::Cookies;
2525
use uuid::Uuid;
2626

27-
use super::{ClientInfo, RequestId};
2827
use crate::{
2928
AppState,
3029
auth::{AuthError, AuthenticatedRequest, Identity, IdentityKind},
30+
middleware::{ClientInfo, RequestId},
3131
observability::metrics,
3232
services::audit_logs::{AuthEventParams, auth_events},
3333
};
@@ -647,7 +647,7 @@ async fn try_api_key_admin_auth(
647647
headers: &axum::http::HeaderMap,
648648
state: &AppState,
649649
) -> Result<Option<Identity>, AuthError> {
650-
let api_key_auth = match super::combined::try_api_key_auth(headers, state).await? {
650+
let api_key_auth = match super::api::try_api_key_auth(headers, state).await? {
651651
Some(auth) => auth,
652652
None => return Ok(None),
653653
};
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,22 @@ use axum::{
88
use chrono::Utc;
99

1010
use super::{
11-
RequestId,
12-
budget::{BudgetCheckResult, BudgetError, adjust_budget_reservation},
1311
rate_limit::{
1412
RateLimitError, TokenRateLimitCheckResult, TokenRateLimitResult, TokenReservation,
1513
add_rate_limit_headers, add_token_rate_limit_headers, adjust_token_reservation,
1614
},
17-
scope::required_scope_for_path,
18-
usage::{UsageTracker, extract_full_usage_from_response, tracker_from_headers},
15+
request_id::RequestId,
1916
};
2017
use crate::{
2118
AppState,
2219
auth::{ApiKeyAuth, AuthError, AuthenticatedRequest, Identity, IdentityKind},
2320
cache::{BudgetCheckParams, Cache, CacheKeys, RateLimitCheckParams, RateLimitResult},
2421
events::{BudgetType, ServerEvent},
22+
middleware::util::{
23+
budget::{BudgetCheckResult, BudgetError, adjust_budget_reservation},
24+
scope::required_scope_for_path,
25+
usage::{UsageTracker, extract_full_usage_from_response, tracker_from_headers},
26+
},
2527
models::{AuditActorType, BudgetPeriod, CreateAuditLog, has_valid_prefix, hash_api_key},
2628
observability::metrics,
2729
};
@@ -584,7 +586,7 @@ pub async fn api_middleware(
584586
.map(|ci| ci.0.ip());
585587

586588
// Insert client info for audit logging
587-
let client_info = super::ClientInfo {
589+
let client_info = crate::middleware::ClientInfo {
588590
ip_address: connecting_ip.map(|ip| ip.to_string()),
589591
user_agent: headers
590592
.get(axum::http::header::USER_AGENT)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,8 @@ pub async fn permissive_authz_middleware(
596596
};
597597

598598
// Insert ClientInfo for unprotected routes (no admin middleware to extract it).
599-
req.extensions_mut().insert(super::ClientInfo::default());
599+
req.extensions_mut()
600+
.insert(crate::middleware::ClientInfo::default());
600601

601602
// Insert a default AdminAuth with system identity for unprotected routes.
602603
// This allows handlers to extract AdminAuth for audit logging purposes.

src/middleware/layers/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub mod admin;
2+
pub mod api;
3+
pub mod authz;
4+
pub mod rate_limit;
5+
pub mod request_id;
6+
pub mod security_headers;

src/middleware/mod.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,23 @@
2121
//! ## Unprotected admin routes (login, session info)
2222
//! - [`permissive_authz_middleware`] — Injects allow-all authz context
2323
24-
// ── Middleware layers ──────────────────────────────────────────────────────────
25-
mod admin;
26-
mod authz;
27-
mod combined;
28-
mod rate_limit;
29-
mod request_id;
30-
mod security_headers;
24+
// ── True middleware (Axum middleware layers) ────────────────────────────────────
25+
mod layers;
3126

32-
// ── Internal helpers (used only by combined.rs) ────────────────────────────────
33-
mod budget;
34-
mod scope;
35-
mod usage;
27+
// ── Internal utilities (budget, scope, usage helpers for combined middleware) ──
28+
pub(crate) mod util;
3629

3730
// ── Middleware layer exports ───────────────────────────────────────────────────
38-
pub use admin::{AdminAuth, admin_auth_middleware};
39-
pub use authz::{
40-
AuthzContext, api_authz_middleware, authz_middleware, permissive_authz_middleware,
41-
};
42-
pub use combined::api_middleware;
4331
#[cfg(feature = "sso")]
44-
pub use rate_limit::extract_client_ip_from_parts;
45-
pub use rate_limit::rate_limit_middleware;
46-
pub use request_id::{RequestId, request_id_middleware};
47-
pub use security_headers::security_headers_middleware;
32+
pub use layers::rate_limit::extract_client_ip_from_parts;
33+
pub use layers::{
34+
admin::{AdminAuth, admin_auth_middleware},
35+
api::api_middleware,
36+
authz::{AuthzContext, api_authz_middleware, authz_middleware, permissive_authz_middleware},
37+
rate_limit::rate_limit_middleware,
38+
request_id::{RequestId, request_id_middleware},
39+
security_headers::security_headers_middleware,
40+
};
4841

4942
// ── Types extracted by middleware (used by route handlers via Extension<T>) ────
5043

src/middleware/util/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod budget;
2+
pub mod scope;
3+
pub mod usage;

0 commit comments

Comments
 (0)