Skip to content

Commit 05e99af

Browse files
fix(middleware): correct order - Auth runs BEFORE RBAC
In Axum, layers are applied bottom-to-top (last added runs first). So Auth middleware must be added AFTER RBAC in the chain to run BEFORE it. Previous order (wrong): RBAC -> Auth -> Handler New order (correct): Auth -> RBAC -> Handler
1 parent c181353 commit 05e99af

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/main.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -500,18 +500,19 @@ async fn run_axum_server(
500500
.layer(rate_limit_extension)
501501
// Request ID tracking for all requests
502502
.layer(middleware::from_fn(request_id_middleware))
503-
// RBAC middleware - checks permissions AFTER authentication
503+
// Authentication middleware using provider registry
504+
// NOTE: In Axum, layers are applied bottom-to-top, so this runs BEFORE RBAC
504505
.layer(middleware::from_fn(move |req: axum::http::Request<axum::body::Body>, next: axum::middleware::Next| {
505-
let rbac = Arc::clone(&rbac_manager_for_middleware);
506+
let state = auth_middleware_state.clone();
506507
async move {
507-
botserver::security::rbac_middleware_fn(req, next, rbac).await
508+
botserver::security::auth_middleware_with_providers(req, next, state).await
508509
}
509510
}))
510-
// Authentication middleware using provider registry
511+
// RBAC middleware - checks permissions AFTER authentication
511512
.layer(middleware::from_fn(move |req: axum::http::Request<axum::body::Body>, next: axum::middleware::Next| {
512-
let state = auth_middleware_state.clone();
513+
let rbac = Arc::clone(&rbac_manager_for_middleware);
513514
async move {
514-
botserver::security::auth_middleware_with_providers(req, next, state).await
515+
botserver::security::rbac_middleware_fn(req, next, rbac).await
515516
}
516517
}))
517518
// Panic handler catches panics and returns safe 500 responses

0 commit comments

Comments
 (0)