diff --git a/services/backup/src/http/handlers/backup.rs b/services/backup/src/http/handlers/backup.rs --- a/services/backup/src/http/handlers/backup.rs +++ b/services/backup/src/http/handlers/backup.rs @@ -305,14 +305,8 @@ path: web::Path, db_client: web::Data, blob_client: web::Data, - req: HttpRequest, + auth_service: AuthService, ) -> actix_web::Result { - let auth_service = req.app_data::().ok_or_else(|| { - tracing::error!( - "Failed to get AuthService from request. Check HTTP server config." - ); - ErrorInternalServerError("internal error") - })?; let services_token = auth_service .get_services_token() .await diff --git a/services/reports/src/http/service.rs b/services/reports/src/http/service.rs --- a/services/reports/src/http/service.rs +++ b/services/reports/src/http/service.rs @@ -27,23 +27,15 @@ ErrorInternalServerError("Internal server error") }); - let auth_service = - req.app_data::().cloned().ok_or_else(|| { - tracing::error!( - "FATAL! Failed to extract AuthService from actix app_data. \ - Check HTTP server configuration" - ); - ErrorInternalServerError("Internal server error") - }); - + let auth_service = AuthService::from_request(req, payload).into_inner(); let request_auth_value = - AuthorizationCredential::from_request(req, payload); + AuthorizationCredential::from_request(req, payload).into_inner(); Box::pin(async move { let auth_service = auth_service?; let base_service = base_service?; - let credential = request_auth_value.await.ok(); + let credential = request_auth_value.ok(); // This is Some if the request contains valid Authorization header let auth_token = match credential { diff --git a/shared/comm-lib/src/http/auth.rs b/shared/comm-lib/src/http/auth.rs --- a/shared/comm-lib/src/http/auth.rs +++ b/shared/comm-lib/src/http/auth.rs @@ -19,6 +19,27 @@ UserIdentity, }; +impl FromRequest for AuthService { + type Error = actix_web::Error; + type Future = Ready>; + + fn from_request( + req: &actix_web::HttpRequest, + _: &mut actix_web::dev::Payload, + ) -> Self::Future { + let auth_service = + req.app_data::().cloned().ok_or_else(|| { + tracing::error!( + "FATAL! Failed to get AuthService from request for `{}` handler. + Check HTTP server config - make sure it's passed to App::app_data().", + req.match_name().unwrap_or_else(|| req.path()) + ); + ErrorInternalServerError("internal error") + }); + ready(auth_service) + } +} + impl FromRequest for AuthorizationCredential { type Error = actix_web::Error; type Future = Ready>;