From ec5f9de0cbfecd584cc200e26b13a37aebe97f16 Mon Sep 17 00:00:00 2001 From: chikke srujan <121822803+srujanchikke@users.noreply.github.com> Date: Mon, 5 Aug 2024 12:52:29 +0530 Subject: [PATCH] feat(router): add accept language header middleware (#5500) Co-authored-by: Chikke Srujan Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com> --- config/deployments/integration_test.toml | 2 +- config/deployments/production.toml | 2 +- config/deployments/sandbox.toml | 2 +- config/development.toml | 2 +- config/docker_compose.toml | 2 +- crates/router/Cargo.toml | 4 +- crates/router/src/lib.rs | 1 + crates/router/src/middleware.rs | 81 ++++++++++++++++++++++++ loadtest/config/development.toml | 2 +- 9 files changed, 90 insertions(+), 8 deletions(-) diff --git a/config/deployments/integration_test.toml b/config/deployments/integration_test.toml index 69d268e21629..be2d22d92a04 100644 --- a/config/deployments/integration_test.toml +++ b/config/deployments/integration_test.toml @@ -357,7 +357,7 @@ outgoing_enabled = true connectors_with_webhook_source_verification_call = "paypal" # List of connectors which has additional source verification api-call [unmasked_headers] -keys = "user-agent" +keys = "accept-language,user-agent" [saved_payment_methods] sdk_eligible_payment_methods = "card" diff --git a/config/deployments/production.toml b/config/deployments/production.toml index 17f4b2fa9948..f26568848715 100644 --- a/config/deployments/production.toml +++ b/config/deployments/production.toml @@ -370,7 +370,7 @@ outgoing_enabled = true connectors_with_webhook_source_verification_call = "paypal" # List of connectors which has additional source verification api-call [unmasked_headers] -keys = "user-agent" +keys = "accept-language,user-agent" [saved_payment_methods] sdk_eligible_payment_methods = "card" diff --git a/config/deployments/sandbox.toml b/config/deployments/sandbox.toml index 730f7829198c..146a9bb87f81 100644 --- a/config/deployments/sandbox.toml +++ b/config/deployments/sandbox.toml @@ -374,7 +374,7 @@ outgoing_enabled = true connectors_with_webhook_source_verification_call = "paypal" # List of connectors which has additional source verification api-call [unmasked_headers] -keys = "user-agent" +keys = "accept-language,user-agent" [saved_payment_methods] sdk_eligible_payment_methods = "card" diff --git a/config/development.toml b/config/development.toml index b380135e195f..8888048ccfae 100644 --- a/config/development.toml +++ b/config/development.toml @@ -682,7 +682,7 @@ enabled = true file_storage_backend = "file_system" [unmasked_headers] -keys = "user-agent" +keys = "accept-language,user-agent" [opensearch] host = "https://localhost:9200" diff --git a/config/docker_compose.toml b/config/docker_compose.toml index 4d8f287a0772..fa5ccaac0a60 100644 --- a/config/docker_compose.toml +++ b/config/docker_compose.toml @@ -506,7 +506,7 @@ source = "logs" file_storage_backend = "file_system" [unmasked_headers] -keys = "user-agent" +keys = "accept-language,user-agent" [opensearch] host = "https://opensearch:9200" diff --git a/crates/router/Cargo.toml b/crates/router/Cargo.toml index 3e6f470d80eb..958b2d286d18 100644 --- a/crates/router/Cargo.toml +++ b/crates/router/Cargo.toml @@ -18,7 +18,7 @@ keymanager_create = [] keymanager_mtls = ["reqwest/rustls-tls", "common_utils/keymanager_mtls"] encryption_service = ["hyperswitch_domain_models/encryption_service", "common_utils/encryption_service"] frm = ["api_models/frm", "hyperswitch_domain_models/frm", "hyperswitch_connectors/frm", "hyperswitch_interfaces/frm"] -stripe = ["dep:serde_qs"] +stripe = [] release = ["stripe", "email", "accounts_cache", "kv_store", "vergen", "recon", "external_services/aws_kms", "external_services/aws_s3", "keymanager_mtls", "keymanager_create", "encryption_service"] oltp = ["storage_impl/oltp"] kv_store = ["scheduler/kv_store"] @@ -102,7 +102,7 @@ rustls-pemfile = "2" serde = { version = "1.0.197", features = ["derive"] } serde_json = "1.0.115" serde_path_to_error = "0.1.16" -serde_qs = { version = "0.12.0", optional = true } +serde_qs = "0.12.0" serde_repr = "0.1.19" serde_urlencoded = "0.7.1" serde_with = "3.7.0" diff --git a/crates/router/src/lib.rs b/crates/router/src/lib.rs index 6581436c2511..9691a5366263 100644 --- a/crates/router/src/lib.rs +++ b/crates/router/src/lib.rs @@ -341,6 +341,7 @@ pub fn get_application_builder( .wrap(cors::cors(cors)) // this middleware works only for Http1.1 requests .wrap(middleware::Http400RequestDetailsLogger) + .wrap(middleware::AddAcceptLanguageHeader) .wrap(middleware::LogSpanInitializer) .wrap(router_env::tracing_actix_web::TracingLogger::default()) } diff --git a/crates/router/src/middleware.rs b/crates/router/src/middleware.rs index 7b9919d3afaf..1276db58dc42 100644 --- a/crates/router/src/middleware.rs +++ b/crates/router/src/middleware.rs @@ -315,3 +315,84 @@ where }) } } + +/// Middleware for Adding Accept-Language header based on query params +pub struct AddAcceptLanguageHeader; + +impl actix_web::dev::Transform + for AddAcceptLanguageHeader +where + S: actix_web::dev::Service< + actix_web::dev::ServiceRequest, + Response = actix_web::dev::ServiceResponse, + Error = actix_web::Error, + >, + S::Future: 'static, + B: 'static, +{ + type Response = actix_web::dev::ServiceResponse; + type Error = actix_web::Error; + type Transform = AddAcceptLanguageHeaderMiddleware; + type InitError = (); + type Future = std::future::Ready>; + + fn new_transform(&self, service: S) -> Self::Future { + std::future::ready(Ok(AddAcceptLanguageHeaderMiddleware { + service: std::rc::Rc::new(service), + })) + } +} + +pub struct AddAcceptLanguageHeaderMiddleware { + service: std::rc::Rc, +} + +impl actix_web::dev::Service + for AddAcceptLanguageHeaderMiddleware +where + S: actix_web::dev::Service< + actix_web::dev::ServiceRequest, + Response = actix_web::dev::ServiceResponse, + Error = actix_web::Error, + > + 'static, + S::Future: 'static, + B: 'static, +{ + type Response = actix_web::dev::ServiceResponse; + type Error = actix_web::Error; + type Future = futures::future::LocalBoxFuture<'static, Result>; + + actix_web::dev::forward_ready!(service); + fn call(&self, mut req: actix_web::dev::ServiceRequest) -> Self::Future { + let svc = self.service.clone(); + Box::pin(async move { + #[derive(serde::Deserialize)] + struct LocaleQueryParam { + locale: Option, + } + let query_params = req.query_string(); + let locale_param = + serde_qs::from_str::(query_params).map_err(|error| { + actix_web::error::ErrorBadRequest(format!( + "Could not convert query params to locale query parmas: {:?}", + error + )) + })?; + let accept_language_header = req.headers().get(http::header::ACCEPT_LANGUAGE); + if let Some(locale) = locale_param.locale { + req.headers_mut().insert( + http::header::ACCEPT_LANGUAGE, + http::HeaderValue::from_str(&locale)?, + ); + } else if accept_language_header.is_none() { + req.headers_mut().insert( + http::header::ACCEPT_LANGUAGE, + http::HeaderValue::from_static("en"), + ); + } + let response_fut = svc.call(req); + let response = response_fut.await?; + Ok(response) + }) + } +} diff --git a/loadtest/config/development.toml b/loadtest/config/development.toml index d64d246dff24..6bae052394a1 100644 --- a/loadtest/config/development.toml +++ b/loadtest/config/development.toml @@ -344,7 +344,7 @@ client_secret = "" partner_id = "" [unmasked_headers] -keys = "user-agent" +keys = "accept-language,user-agent" [multitenancy] enabled = false