From 1cda7ad5fccb64c1adefc24a47b79b8315f91a59 Mon Sep 17 00:00:00 2001 From: Amisha Prabhat <55580080+Aprabhat19@users.noreply.github.com> Date: Thu, 27 Jul 2023 11:54:16 +0530 Subject: [PATCH] fix(router): add validation for all the connector auth type (#1748) --- connector-template/mod.rs | 8 +++++ crates/router/src/connector/aci.rs | 8 +++++ crates/router/src/connector/adyen.rs | 8 +++++ crates/router/src/connector/airwallex.rs | 8 +++++ .../src/connector/airwallex/transformers.rs | 19 ++++++++++++ .../router/src/connector/authorizedotnet.rs | 8 +++++ .../connector/authorizedotnet/transformers.rs | 2 +- crates/router/src/connector/bambora.rs | 8 +++++ crates/router/src/connector/bitpay.rs | 8 +++++ crates/router/src/connector/bluesnap.rs | 8 ++++- crates/router/src/connector/boku.rs | 7 +++++ crates/router/src/connector/braintree.rs | 8 +++++ crates/router/src/connector/cashtocode.rs | 8 +++++ crates/router/src/connector/checkout.rs | 8 ++++- crates/router/src/connector/coinbase.rs | 8 +++++ crates/router/src/connector/cryptopay.rs | 8 +++++ crates/router/src/connector/cybersource.rs | 7 +++++ crates/router/src/connector/dlocal.rs | 8 +++++ crates/router/src/connector/dummyconnector.rs | 8 +++++ crates/router/src/connector/fiserv.rs | 8 +++++ crates/router/src/connector/forte.rs | 7 +++++ crates/router/src/connector/globalpay.rs | 9 ++++++ crates/router/src/connector/globepay.rs | 7 +++++ crates/router/src/connector/iatapay.rs | 8 +++++ crates/router/src/connector/klarna.rs | 8 +++++ crates/router/src/connector/mollie.rs | 7 +++++ crates/router/src/connector/multisafepay.rs | 8 +++++ crates/router/src/connector/nexinets.rs | 7 +++++ crates/router/src/connector/nmi.rs | 7 +++++ crates/router/src/connector/noon.rs | 7 +++++ crates/router/src/connector/nuvei.rs | 8 ++++- crates/router/src/connector/opayo.rs | 8 +++++ crates/router/src/connector/opennode.rs | 7 +++++ crates/router/src/connector/payeezy.rs | 7 +++++ crates/router/src/connector/payme.rs | 7 +++++ crates/router/src/connector/paypal.rs | 7 +++++ crates/router/src/connector/payu.rs | 8 +++++ crates/router/src/connector/powertranz.rs | 8 +++++ crates/router/src/connector/rapyd.rs | 8 +++++ crates/router/src/connector/shift4.rs | 8 +++++ crates/router/src/connector/stax.rs | 7 +++++ crates/router/src/connector/stripe.rs | 10 +++++- crates/router/src/connector/trustpay.rs | 8 +++++ crates/router/src/connector/tsys.rs | 8 +++++ crates/router/src/connector/wise.rs | 7 +++++ crates/router/src/connector/worldline.rs | 8 +++++ crates/router/src/connector/worldpay.rs | 8 +++++ crates/router/src/connector/zen.rs | 7 +++++ crates/router/src/core/admin.rs | 31 ++++++++++++++----- .../payments/operations/payment_session.rs | 17 +++++----- crates/router/src/routes/admin.rs | 2 +- crates/router/src/types/api.rs | 7 ++++- 52 files changed, 411 insertions(+), 23 deletions(-) diff --git a/connector-template/mod.rs b/connector-template/mod.rs index 653c28962b..67745e8b4c 100644 --- a/connector-template/mod.rs +++ b/connector-template/mod.rs @@ -77,6 +77,14 @@ impl ConnectorCommon for {{project-name | downcase | pascal_case}} { fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.{{project-name}}.base_url.as_ref() } + + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + {{project-name | downcase}}::{{project-name | downcase | pascal_case}}AuthType::try_from(val)?; + Ok(()) + } fn get_auth_header(&self, auth_type:&types::ConnectorAuthType)-> CustomResult)>,errors::ConnectorError> { let auth = {{project-name | downcase}}::{{project-name | downcase | pascal_case}}AuthType::try_from(auth_type) diff --git a/crates/router/src/connector/aci.rs b/crates/router/src/connector/aci.rs index 5bed999903..6cc3f9e832 100644 --- a/crates/router/src/connector/aci.rs +++ b/crates/router/src/connector/aci.rs @@ -34,6 +34,14 @@ impl ConnectorCommon for Aci { "application/x-www-form-urlencoded" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + aci::AciAuthType::try_from(val)?; + Ok(()) + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.aci.base_url.as_ref() } diff --git a/crates/router/src/connector/adyen.rs b/crates/router/src/connector/adyen.rs index 3d5b4d9609..5dcc38e46c 100644 --- a/crates/router/src/connector/adyen.rs +++ b/crates/router/src/connector/adyen.rs @@ -49,6 +49,14 @@ impl ConnectorCommon for Adyen { )]) } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + adyen::AdyenAuthType::try_from(val)?; + Ok(()) + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.adyen.base_url.as_ref() } diff --git a/crates/router/src/connector/airwallex.rs b/crates/router/src/connector/airwallex.rs index 7cd5eb7124..c8f253c410 100644 --- a/crates/router/src/connector/airwallex.rs +++ b/crates/router/src/connector/airwallex.rs @@ -72,6 +72,14 @@ impl ConnectorCommon for Airwallex { connectors.airwallex.base_url.as_ref() } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + airwallex::AirwallexAuthType::try_from(val)?; + Ok(()) + } + fn build_error_response( &self, res: Response, diff --git a/crates/router/src/connector/airwallex/transformers.rs b/crates/router/src/connector/airwallex/transformers.rs index 68b990951b..4e4dc6f823 100644 --- a/crates/router/src/connector/airwallex/transformers.rs +++ b/crates/router/src/connector/airwallex/transformers.rs @@ -13,6 +13,25 @@ use crate::{ types::{self, api, storage::enums}, }; +pub struct AirwallexAuthType { + pub x_api_key: Secret, + pub x_client_id: Secret, +} + +impl TryFrom<&types::ConnectorAuthType> for AirwallexAuthType { + type Error = error_stack::Report; + + fn try_from(auth_type: &types::ConnectorAuthType) -> Result { + if let types::ConnectorAuthType::BodyKey { api_key, key1 } = auth_type { + Ok(Self { + x_api_key: api_key.clone(), + x_client_id: key1.clone(), + }) + } else { + Err(errors::ConnectorError::FailedToObtainAuthType)? + } + } +} #[derive(Default, Debug, Serialize, Eq, PartialEq)] pub struct AirwallexIntentRequest { // Unique ID to be sent for each transaction/operation request to the connector diff --git a/crates/router/src/connector/authorizedotnet.rs b/crates/router/src/connector/authorizedotnet.rs index 6b7ed503f9..ca7f793abd 100644 --- a/crates/router/src/connector/authorizedotnet.rs +++ b/crates/router/src/connector/authorizedotnet.rs @@ -52,6 +52,14 @@ impl ConnectorCommon for Authorizedotnet { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + authorizedotnet::MerchantAuthentication::try_from(val)?; + Ok(()) + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.authorizedotnet.base_url.as_ref() } diff --git a/crates/router/src/connector/authorizedotnet/transformers.rs b/crates/router/src/connector/authorizedotnet/transformers.rs index aaaadd77ed..f1b1c3a452 100644 --- a/crates/router/src/connector/authorizedotnet/transformers.rs +++ b/crates/router/src/connector/authorizedotnet/transformers.rs @@ -33,7 +33,7 @@ pub enum TransactionType { } #[derive(Debug, Serialize)] #[serde(rename_all = "camelCase")] -struct MerchantAuthentication { +pub struct MerchantAuthentication { name: Secret, transaction_key: Secret, } diff --git a/crates/router/src/connector/bambora.rs b/crates/router/src/connector/bambora.rs index e6e6b2ae93..cd5e848403 100644 --- a/crates/router/src/connector/bambora.rs +++ b/crates/router/src/connector/bambora.rs @@ -60,6 +60,14 @@ impl ConnectorCommon for Bambora { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + bambora::BamboraAuthType::try_from(val)?; + Ok(()) + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.bambora.base_url.as_ref() } diff --git a/crates/router/src/connector/bitpay.rs b/crates/router/src/connector/bitpay.rs index eeb9802026..1e86764a42 100644 --- a/crates/router/src/connector/bitpay.rs +++ b/crates/router/src/connector/bitpay.rs @@ -86,6 +86,14 @@ impl ConnectorCommon for Bitpay { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + bitpay::BitpayAuthType::try_from(val)?; + Ok(()) + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.bitpay.base_url.as_ref() } diff --git a/crates/router/src/connector/bluesnap.rs b/crates/router/src/connector/bluesnap.rs index 296b641e35..1b88ea8523 100644 --- a/crates/router/src/connector/bluesnap.rs +++ b/crates/router/src/connector/bluesnap.rs @@ -67,7 +67,13 @@ impl ConnectorCommon for Bluesnap { fn common_get_content_type(&self) -> &'static str { "application/json" } - + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + bluesnap::BluesnapAuthType::try_from(val)?; + Ok(()) + } fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.bluesnap.base_url.as_ref() } diff --git a/crates/router/src/connector/boku.rs b/crates/router/src/connector/boku.rs index e0af2af95e..70aa46e99b 100644 --- a/crates/router/src/connector/boku.rs +++ b/crates/router/src/connector/boku.rs @@ -78,6 +78,13 @@ impl ConnectorCommon for Boku { fn common_get_content_type(&self) -> &'static str { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + boku::BokuAuthType::try_from(val)?; + Ok(()) + } fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.boku.base_url.as_ref() diff --git a/crates/router/src/connector/braintree.rs b/crates/router/src/connector/braintree.rs index 717bf1747c..3c2838f74b 100644 --- a/crates/router/src/connector/braintree.rs +++ b/crates/router/src/connector/braintree.rs @@ -34,6 +34,14 @@ impl ConnectorCommon for Braintree { connectors.braintree.base_url.as_ref() } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + braintree::BraintreeAuthType::try_from(val)?; + Ok(()) + } + fn get_auth_header( &self, auth_type: &types::ConnectorAuthType, diff --git a/crates/router/src/connector/cashtocode.rs b/crates/router/src/connector/cashtocode.rs index 249ca3fe61..c14c666a7a 100644 --- a/crates/router/src/connector/cashtocode.rs +++ b/crates/router/src/connector/cashtocode.rs @@ -97,6 +97,14 @@ impl ConnectorCommon for Cashtocode { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + cashtocode::CashtocodeAuthType::try_from(val)?; + Ok(()) + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.cashtocode.base_url.as_ref() } diff --git a/crates/router/src/connector/checkout.rs b/crates/router/src/connector/checkout.rs index c9d4edce7e..42b29d2483 100644 --- a/crates/router/src/connector/checkout.rs +++ b/crates/router/src/connector/checkout.rs @@ -62,7 +62,13 @@ impl ConnectorCommon for Checkout { fn common_get_content_type(&self) -> &'static str { "application/json" } - + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + checkout::CheckoutAuthType::try_from(val)?; + Ok(()) + } fn get_auth_header( &self, auth_type: &types::ConnectorAuthType, diff --git a/crates/router/src/connector/coinbase.rs b/crates/router/src/connector/coinbase.rs index 1d4d5764e2..a4bbf77ab4 100644 --- a/crates/router/src/connector/coinbase.rs +++ b/crates/router/src/connector/coinbase.rs @@ -76,6 +76,14 @@ impl ConnectorCommon for Coinbase { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + coinbase::CoinbaseAuthType::try_from(val)?; + Ok(()) + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.coinbase.base_url.as_ref() } diff --git a/crates/router/src/connector/cryptopay.rs b/crates/router/src/connector/cryptopay.rs index 4fa8c4e8da..39bc441192 100644 --- a/crates/router/src/connector/cryptopay.rs +++ b/crates/router/src/connector/cryptopay.rs @@ -133,6 +133,14 @@ impl ConnectorCommon for Cryptopay { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + cryptopay::CryptopayAuthType::try_from(val)?; + Ok(()) + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.cryptopay.base_url.as_ref() } diff --git a/crates/router/src/connector/cybersource.rs b/crates/router/src/connector/cybersource.rs index ba8b35964f..e9e51df5b5 100644 --- a/crates/router/src/connector/cybersource.rs +++ b/crates/router/src/connector/cybersource.rs @@ -88,6 +88,13 @@ impl ConnectorCommon for Cybersource { "application/json;charset=utf-8" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + cybersource::CybersourceAuthType::try_from(val)?; + Ok(()) + } fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.cybersource.base_url.as_ref() } diff --git a/crates/router/src/connector/dlocal.rs b/crates/router/src/connector/dlocal.rs index be0855e435..02be442c75 100644 --- a/crates/router/src/connector/dlocal.rs +++ b/crates/router/src/connector/dlocal.rs @@ -111,6 +111,14 @@ impl ConnectorCommon for Dlocal { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + dlocal::DlocalAuthType::try_from(val)?; + Ok(()) + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.dlocal.base_url.as_ref() } diff --git a/crates/router/src/connector/dummyconnector.rs b/crates/router/src/connector/dummyconnector.rs index 80787b35d1..3506e2ecb3 100644 --- a/crates/router/src/connector/dummyconnector.rs +++ b/crates/router/src/connector/dummyconnector.rs @@ -90,6 +90,14 @@ impl ConnectorCommon for DummyConnector { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + dummyconnector::DummyConnectorAuthType::try_from(val)?; + Ok(()) + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.dummyconnector.base_url.as_ref() } diff --git a/crates/router/src/connector/fiserv.rs b/crates/router/src/connector/fiserv.rs index 25a593f3ff..921cd1fc91 100644 --- a/crates/router/src/connector/fiserv.rs +++ b/crates/router/src/connector/fiserv.rs @@ -105,6 +105,14 @@ impl ConnectorCommon for Fiserv { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + fiserv::FiservAuthType::try_from(val)?; + Ok(()) + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.fiserv.base_url.as_ref() } diff --git a/crates/router/src/connector/forte.rs b/crates/router/src/connector/forte.rs index 5dde831071..b184a4364d 100644 --- a/crates/router/src/connector/forte.rs +++ b/crates/router/src/connector/forte.rs @@ -79,6 +79,13 @@ impl ConnectorCommon for Forte { fn common_get_content_type(&self) -> &'static str { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + forte::ForteAuthType::try_from(val)?; + Ok(()) + } fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.forte.base_url.as_ref() diff --git a/crates/router/src/connector/globalpay.rs b/crates/router/src/connector/globalpay.rs index e15da504a8..a233da05a3 100644 --- a/crates/router/src/connector/globalpay.rs +++ b/crates/router/src/connector/globalpay.rs @@ -15,6 +15,7 @@ use self::{ GlobalpayPaymentsResponse, GlobalpayRefreshTokenErrorResponse, GlobalpayRefreshTokenResponse, }, + transformers::GlobalpayAuthType, }; use super::utils::RefundsRequestData; use crate::{ @@ -78,6 +79,14 @@ impl ConnectorCommon for Globalpay { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + GlobalpayAuthType::try_from(val)?; + Ok(()) + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.globalpay.base_url.as_ref() } diff --git a/crates/router/src/connector/globepay.rs b/crates/router/src/connector/globepay.rs index bd3bfdb920..29e4eaa704 100644 --- a/crates/router/src/connector/globepay.rs +++ b/crates/router/src/connector/globepay.rs @@ -103,6 +103,13 @@ impl ConnectorCommon for Globepay { fn common_get_content_type(&self) -> &'static str { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + globepay::GlobepayAuthType::try_from(val)?; + Ok(()) + } fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.globepay.base_url.as_ref() diff --git a/crates/router/src/connector/iatapay.rs b/crates/router/src/connector/iatapay.rs index f3f5a4eb12..4b104cde73 100644 --- a/crates/router/src/connector/iatapay.rs +++ b/crates/router/src/connector/iatapay.rs @@ -90,6 +90,14 @@ impl ConnectorCommon for Iatapay { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + iatapay::IatapayAuthType::try_from(val)?; + Ok(()) + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.iatapay.base_url.as_ref() } diff --git a/crates/router/src/connector/klarna.rs b/crates/router/src/connector/klarna.rs index 4f15fb77db..dbb1a93b4b 100644 --- a/crates/router/src/connector/klarna.rs +++ b/crates/router/src/connector/klarna.rs @@ -35,6 +35,14 @@ impl ConnectorCommon for Klarna { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + klarna::KlarnaAuthType::try_from(val)?; + Ok(()) + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.klarna.base_url.as_ref() } diff --git a/crates/router/src/connector/mollie.rs b/crates/router/src/connector/mollie.rs index 46385e2ec6..042c593dcf 100644 --- a/crates/router/src/connector/mollie.rs +++ b/crates/router/src/connector/mollie.rs @@ -65,6 +65,13 @@ impl ConnectorCommon for Mollie { fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.mollie.base_url.as_ref() } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + mollie::MollieAuthType::try_from(val)?; + Ok(()) + } fn get_auth_header( &self, diff --git a/crates/router/src/connector/multisafepay.rs b/crates/router/src/connector/multisafepay.rs index 07daef20e2..143fdf0474 100644 --- a/crates/router/src/connector/multisafepay.rs +++ b/crates/router/src/connector/multisafepay.rs @@ -48,6 +48,14 @@ impl ConnectorCommon for Multisafepay { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + multisafepay::MultisafepayAuthType::try_from(val)?; + Ok(()) + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.multisafepay.base_url.as_ref() } diff --git a/crates/router/src/connector/nexinets.rs b/crates/router/src/connector/nexinets.rs index 2d90c2c4c1..e9d8a06282 100644 --- a/crates/router/src/connector/nexinets.rs +++ b/crates/router/src/connector/nexinets.rs @@ -76,6 +76,13 @@ impl ConnectorCommon for Nexinets { fn common_get_content_type(&self) -> &'static str { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + nexinets::NexinetsAuthType::try_from(val)?; + Ok(()) + } fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.nexinets.base_url.as_ref() diff --git a/crates/router/src/connector/nmi.rs b/crates/router/src/connector/nmi.rs index 37fc6e5cf8..c6b6241973 100644 --- a/crates/router/src/connector/nmi.rs +++ b/crates/router/src/connector/nmi.rs @@ -59,6 +59,13 @@ impl ConnectorCommon for Nmi { fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.nmi.base_url.as_ref() } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + nmi::NmiAuthType::try_from(val)?; + Ok(()) + } fn build_error_response( &self, diff --git a/crates/router/src/connector/noon.rs b/crates/router/src/connector/noon.rs index 22c778334e..fce88d86f9 100644 --- a/crates/router/src/connector/noon.rs +++ b/crates/router/src/connector/noon.rs @@ -86,6 +86,13 @@ impl ConnectorCommon for Noon { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + noon::NoonAuthType::try_from(val)?; + Ok(()) + } fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.noon.base_url.as_ref() } diff --git a/crates/router/src/connector/nuvei.rs b/crates/router/src/connector/nuvei.rs index 560eea6f09..6cbb44e587 100644 --- a/crates/router/src/connector/nuvei.rs +++ b/crates/router/src/connector/nuvei.rs @@ -56,7 +56,13 @@ impl ConnectorCommon for Nuvei { fn common_get_content_type(&self) -> &'static str { "application/json" } - + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + nuvei::NuveiAuthType::try_from(val)?; + Ok(()) + } fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.nuvei.base_url.as_ref() } diff --git a/crates/router/src/connector/opayo.rs b/crates/router/src/connector/opayo.rs index 4203bd7152..2716796837 100644 --- a/crates/router/src/connector/opayo.rs +++ b/crates/router/src/connector/opayo.rs @@ -76,6 +76,14 @@ impl ConnectorCommon for Opayo { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + opayo::OpayoAuthType::try_from(val)?; + Ok(()) + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.opayo.base_url.as_ref() } diff --git a/crates/router/src/connector/opennode.rs b/crates/router/src/connector/opennode.rs index 36eba82093..c4f75d7fba 100644 --- a/crates/router/src/connector/opennode.rs +++ b/crates/router/src/connector/opennode.rs @@ -75,6 +75,13 @@ impl ConnectorCommon for Opennode { fn common_get_content_type(&self) -> &'static str { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + opennode::OpennodeAuthType::try_from(val)?; + Ok(()) + } fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.opennode.base_url.as_ref() diff --git a/crates/router/src/connector/payeezy.rs b/crates/router/src/connector/payeezy.rs index d210b04df2..04f5853842 100644 --- a/crates/router/src/connector/payeezy.rs +++ b/crates/router/src/connector/payeezy.rs @@ -91,6 +91,13 @@ impl ConnectorCommon for Payeezy { fn common_get_content_type(&self) -> &'static str { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + payeezy::PayeezyAuthType::try_from(val)?; + Ok(()) + } fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.payeezy.base_url.as_ref() diff --git a/crates/router/src/connector/payme.rs b/crates/router/src/connector/payme.rs index b33b78ee5c..6d5bda6e2b 100644 --- a/crates/router/src/connector/payme.rs +++ b/crates/router/src/connector/payme.rs @@ -73,6 +73,13 @@ impl ConnectorCommon for Payme { fn common_get_content_type(&self) -> &'static str { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + payme::PaymeAuthType::try_from(val)?; + Ok(()) + } fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.payme.base_url.as_ref() diff --git a/crates/router/src/connector/paypal.rs b/crates/router/src/connector/paypal.rs index dc6a70431a..ec74a8f021 100644 --- a/crates/router/src/connector/paypal.rs +++ b/crates/router/src/connector/paypal.rs @@ -150,6 +150,13 @@ impl ConnectorCommon for Paypal { fn common_get_content_type(&self) -> &'static str { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + paypal::PaypalAuthType::try_from(val)?; + Ok(()) + } fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.paypal.base_url.as_ref() diff --git a/crates/router/src/connector/payu.rs b/crates/router/src/connector/payu.rs index 1b5bc904bf..e6a2afbf99 100644 --- a/crates/router/src/connector/payu.rs +++ b/crates/router/src/connector/payu.rs @@ -63,6 +63,14 @@ impl ConnectorCommon for Payu { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + payu::PayuAuthType::try_from(val)?; + Ok(()) + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.payu.base_url.as_ref() } diff --git a/crates/router/src/connector/powertranz.rs b/crates/router/src/connector/powertranz.rs index 252f847988..f980b9d117 100644 --- a/crates/router/src/connector/powertranz.rs +++ b/crates/router/src/connector/powertranz.rs @@ -84,6 +84,14 @@ impl ConnectorCommon for Powertranz { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + powertranz::PowertranzAuthType::try_from(val)?; + Ok(()) + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.powertranz.base_url.as_ref() } diff --git a/crates/router/src/connector/rapyd.rs b/crates/router/src/connector/rapyd.rs index b1e814afb4..f6e8056505 100644 --- a/crates/router/src/connector/rapyd.rs +++ b/crates/router/src/connector/rapyd.rs @@ -67,6 +67,14 @@ impl ConnectorCommon for Rapyd { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + rapyd::RapydAuthType::try_from(val)?; + Ok(()) + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.rapyd.base_url.as_ref() } diff --git a/crates/router/src/connector/shift4.rs b/crates/router/src/connector/shift4.rs index ba0e655977..b66dff8510 100644 --- a/crates/router/src/connector/shift4.rs +++ b/crates/router/src/connector/shift4.rs @@ -64,6 +64,14 @@ impl ConnectorCommon for Shift4 { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + shift4::Shift4AuthType::try_from(val)?; + Ok(()) + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.shift4.base_url.as_ref() } diff --git a/crates/router/src/connector/stax.rs b/crates/router/src/connector/stax.rs index 86a1e7196d..460539930b 100644 --- a/crates/router/src/connector/stax.rs +++ b/crates/router/src/connector/stax.rs @@ -73,6 +73,13 @@ impl ConnectorCommon for Stax { fn id(&self) -> &'static str { "stax" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + stax::StaxAuthType::try_from(val)?; + Ok(()) + } fn common_get_content_type(&self) -> &'static str { "application/json" diff --git a/crates/router/src/connector/stripe.rs b/crates/router/src/connector/stripe.rs index a717022e78..6703e54293 100644 --- a/crates/router/src/connector/stripe.rs +++ b/crates/router/src/connector/stripe.rs @@ -1,4 +1,4 @@ -mod transformers; +pub mod transformers; use std::{collections::HashMap, fmt::Debug, ops::Deref}; @@ -40,6 +40,14 @@ impl ConnectorCommon for Stripe { "application/x-www-form-urlencoded" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + stripe::StripeAuthType::try_from(val)?; + Ok(()) + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { // &self.base_url connectors.stripe.base_url.as_ref() diff --git a/crates/router/src/connector/trustpay.rs b/crates/router/src/connector/trustpay.rs index e0a039e579..267ed06ab9 100644 --- a/crates/router/src/connector/trustpay.rs +++ b/crates/router/src/connector/trustpay.rs @@ -81,6 +81,14 @@ impl ConnectorCommon for Trustpay { "application/x-www-form-urlencoded" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + trustpay::TrustpayAuthType::try_from(val)?; + Ok(()) + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.trustpay.base_url.as_ref() } diff --git a/crates/router/src/connector/tsys.rs b/crates/router/src/connector/tsys.rs index 222e20926d..5613cd439e 100644 --- a/crates/router/src/connector/tsys.rs +++ b/crates/router/src/connector/tsys.rs @@ -73,6 +73,14 @@ impl ConnectorCommon for Tsys { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + tsys::TsysAuthType::try_from(val)?; + Ok(()) + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.tsys.base_url.as_ref() } diff --git a/crates/router/src/connector/wise.rs b/crates/router/src/connector/wise.rs index 7d1f4b2786..7519697e77 100644 --- a/crates/router/src/connector/wise.rs +++ b/crates/router/src/connector/wise.rs @@ -59,6 +59,13 @@ impl ConnectorCommon for Wise { fn id(&self) -> &'static str { "wise" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + wise::WiseAuthType::try_from(val)?; + Ok(()) + } fn get_auth_header( &self, diff --git a/crates/router/src/connector/worldline.rs b/crates/router/src/connector/worldline.rs index b356945fc6..cb07e7a1a5 100644 --- a/crates/router/src/connector/worldline.rs +++ b/crates/router/src/connector/worldline.rs @@ -116,6 +116,14 @@ impl ConnectorCommon for Worldline { connectors.worldline.base_url.as_ref() } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + worldline::AuthType::try_from(val)?; + Ok(()) + } + fn build_error_response( &self, res: types::Response, diff --git a/crates/router/src/connector/worldpay.rs b/crates/router/src/connector/worldpay.rs index 12f5abf6b3..ad6962e0fb 100644 --- a/crates/router/src/connector/worldpay.rs +++ b/crates/router/src/connector/worldpay.rs @@ -59,6 +59,14 @@ impl ConnectorCommon for Worldpay { "application/vnd.worldpay.payments-v6+json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + worldpay::WorldpayAuthType::try_from(val)?; + Ok(()) + } + fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.worldpay.base_url.as_ref() } diff --git a/crates/router/src/connector/zen.rs b/crates/router/src/connector/zen.rs index a583e0d834..43e392c2ad 100644 --- a/crates/router/src/connector/zen.rs +++ b/crates/router/src/connector/zen.rs @@ -83,6 +83,13 @@ impl ConnectorCommon for Zen { fn common_get_content_type(&self) -> &'static str { mime::APPLICATION_JSON.essence_str() } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report> { + zen::ZenAuthType::try_from(val)?; + Ok(()) + } fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str { connectors.zen.base_url.as_ref() diff --git a/crates/router/src/core/admin.rs b/crates/router/src/core/admin.rs index c8efdf5397..320c59c84d 100644 --- a/crates/router/src/core/admin.rs +++ b/crates/router/src/core/admin.rs @@ -16,10 +16,11 @@ use crate::{ payments::helpers, }, db::StorageInterface, - routes::metrics, + routes::{metrics, AppState}, services::{self, api as service_api}, types::{ - self, api, + self, + api::{self, ConnectorData}, domain::{ self, types::{self as domain_types, AsyncLift}, @@ -440,12 +441,16 @@ fn validate_certificate_in_mca_metadata( } pub async fn create_payment_connector( - store: &dyn StorageInterface, + state: &AppState, req: api::MerchantConnectorCreate, merchant_id: &String, ) -> RouterResponse { - let key_store = store - .get_merchant_key_store_by_merchant_id(merchant_id, &store.get_master_key().to_vec().into()) + let key_store = state + .store + .get_merchant_key_store_by_merchant_id( + merchant_id, + &state.store.get_master_key().to_vec().into(), + ) .await .to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)?; @@ -454,7 +459,8 @@ pub async fn create_payment_connector( .map(validate_certificate_in_mca_metadata) .transpose()?; - let merchant_account = store + let merchant_account = state + .store .find_merchant_account_by_merchant_id(merchant_id, &key_store) .await .to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)?; @@ -485,7 +491,7 @@ pub async fn create_payment_connector( }; // Validate Merchant api details and return error if not in correct format - let _: types::ConnectorAuthType = req + let auth: types::ConnectorAuthType = req .connector_account_details .clone() .parse_value("ConnectorAuthType") @@ -494,6 +500,14 @@ pub async fn create_payment_connector( expected_format: "auth_type and api_key".to_string(), })?; + let conn_name = + ConnectorData::convert_connector(&state.conf.connectors, &req.connector_name.to_string())?; + conn_name.validate_auth_type(&auth).change_context( + errors::ApiErrorResponse::InvalidRequestData { + message: "The auth type is not supported for connector".to_string(), + }, + )?; + let frm_configs = get_frm_config_as_secret(req.frm_configs); let merchant_connector_account = domain::MerchantConnectorAccount { @@ -538,7 +552,8 @@ pub async fn create_payment_connector( }, }; - let mca = store + let mca = state + .store .insert_merchant_connector_account(merchant_connector_account, &key_store) .await .to_duplicate_response( diff --git a/crates/router/src/core/payments/operations/payment_session.rs b/crates/router/src/core/payments/operations/payment_session.rs index ccfb3bfda1..7fa976b2d3 100644 --- a/crates/router/src/core/payments/operations/payment_session.rs +++ b/crates/router/src/core/payments/operations/payment_session.rs @@ -310,7 +310,6 @@ where payment_intent: &storage::payment_intent::PaymentIntent, key_store: &domain::MerchantKeyStore, ) -> RouterResult { - let connectors = &state.conf.connectors; let db = &state.store; let all_connector_accounts = db @@ -390,13 +389,15 @@ where connector_and_supporting_payment_method_type { let connector_type = api::GetToken::from(payment_method_type); - if let Ok(connector_data) = - api::ConnectorData::get_connector_by_name(connectors, &connector, connector_type) - .map_err(|err| { - logger::error!(session_token_error=?err); - err - }) - { + if let Ok(connector_data) = api::ConnectorData::get_connector_by_name( + &state.conf.connectors, + &connector, + connector_type, + ) + .map_err(|err| { + logger::error!(session_token_error=?err); + err + }) { session_connector_data.push(api::SessionConnectorData { payment_method_type, connector: connector_data, diff --git a/crates/router/src/routes/admin.rs b/crates/router/src/routes/admin.rs index dd8ca8d323..8695e17a1b 100644 --- a/crates/router/src/routes/admin.rs +++ b/crates/router/src/routes/admin.rs @@ -180,7 +180,7 @@ pub async fn payment_connector_create( state.get_ref(), &req, json_payload.into_inner(), - |state, _, req| create_payment_connector(&*state.store, req, &merchant_id), + |state, _, req| create_payment_connector(state, req, &merchant_id), &auth::AdminApiAuth, ) .await diff --git a/crates/router/src/types/api.rs b/crates/router/src/types/api.rs index e8f4d497e6..44e14c949c 100644 --- a/crates/router/src/types/api.rs +++ b/crates/router/src/types/api.rs @@ -65,6 +65,11 @@ pub trait ConnectorCommon { "application/json" } + fn validate_auth_type( + &self, + val: &types::ConnectorAuthType, + ) -> Result<(), error_stack::Report>; + // FIXME write doc - think about this // fn headers(&self) -> Vec<(&str, &str)>; @@ -266,7 +271,7 @@ impl ConnectorData { }) } - fn convert_connector( + pub fn convert_connector( _connectors: &Connectors, connector_name: &str, ) -> CustomResult {