Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
feat(payment_methods): provision internal API key auth for v2 create and update routes (#11082)
Kashifbf5c3cca91c4
diff --git a/crates/router/src/routes/payment_methods.rs b/crates/router/src/routes/payment_methods.rsindex 8f9f5f4f5b..a66e672c0f 100644--- a/crates/router/src/routes/payment_methods.rs+++ b/crates/router/src/routes/payment_methods.rs@@ -77,6 +77,24 @@ pub async fn create_payment_method_api() -> HttpResponse {let flow = Flow::PaymentMethodsCreate;+ let api_auth = auth::V2ApiKeyAuth {+ allow_connected_scope_operation: false,+ allow_platform_self_operation: false,+ };+ let jwt_auth = auth::JWTAuth {+ permission: Permission::MerchantCustomerRead,+ };+ let (auth_type, _api_key_type) =+ match auth::check_internal_api_key_or_dashboard_auth_no_client_secret(+ req.headers(),+ api_auth,+ jwt_auth,+ state.conf.internal_merchant_id_profile_id_auth.clone(),+ ) {+ Ok(auth) => auth,+ Err(err) => return api::log_and_return_error_response(error_stack::report!(err)),+ };+Box::pin(api::server_wrap(flow,state,@@ -92,10 +110,7 @@ pub async fn create_payment_method_api()).await},- &auth::V2ApiKeyAuth {- allow_connected_scope_operation: false,- allow_platform_self_operation: false,- },+ &*auth_type,api_locking::LockAction::NotApplicable,)).await@@ -202,6 +217,20 @@ pub async fn payment_method_update_api(let payment_method_id = path.into_inner();let payload = json_payload.into_inner();+ let api_auth = auth::V2ApiKeyAuth {+ allow_connected_scope_operation: false,+ allow_platform_self_operation: false,+ };++ let (auth_type, api_key_type) = match auth::check_internal_api_key_auth_no_client_secret(+ req.headers(),+ api_auth,+ state.conf.internal_merchant_id_profile_id_auth.clone(),+ ) {+ Ok(auth) => auth,+ Err(err) => return api::log_and_return_error_response(error_stack::report!(err)),+ };+Box::pin(api::server_wrap(flow,state,@@ -216,10 +245,7 @@ pub async fn payment_method_update_api(&payment_method_id,)},- &auth::V2ApiKeyAuth {- allow_connected_scope_operation: false,- allow_platform_self_operation: false,- },+ &*auth_type,api_locking::LockAction::NotApplicable,)).await@@ -287,6 +313,20 @@ pub async fn payment_method_delete_api(}).into_inner();+ let api_auth = auth::V2ApiKeyAuth {+ allow_connected_scope_operation: false,+ allow_platform_self_operation: false,+ };++ let (auth_type, api_key_type) = match auth::check_internal_api_key_auth_no_client_secret(+ req.headers(),+ api_auth,+ state.conf.internal_merchant_id_profile_id_auth.clone(),+ ) {+ Ok(auth) => auth,+ Err(err) => return api::log_and_return_error_response(error_stack::report!(err)),+ };+Box::pin(api::server_wrap(flow,state,@@ -295,10 +335,7 @@ pub async fn payment_method_delete_api(|state, auth: auth::AuthenticationData, pm, _| {payment_methods_routes::delete_payment_method(state, pm, auth.platform, auth.profile)},- &auth::V2ApiKeyAuth {- allow_connected_scope_operation: false,- allow_platform_self_operation: false,- },+ &*auth_type,api_locking::LockAction::NotApplicable,)).await@@ -771,6 +808,20 @@ pub async fn list_customer_payment_method_api(let payload = query_payload.into_inner();let customer_id = customer_id.into_inner();+ let api_auth = auth::V2ApiKeyAuth {+ allow_connected_scope_operation: false,+ allow_platform_self_operation: false,+ };++ let (auth_type, api_key_type) = match auth::check_internal_api_key_auth_no_client_secret(+ req.headers(),+ api_auth,+ state.conf.internal_merchant_id_profile_id_auth.clone(),+ ) {+ Ok(auth) => auth,+ Err(err) => return api::log_and_return_error_response(error_stack::report!(err)),+ };+Box::pin(api::server_wrap(flow,state,@@ -783,16 +834,7 @@ pub async fn list_customer_payment_method_api(customer_id.clone(),)},- auth::auth_type(- &auth::V2ApiKeyAuth {- allow_connected_scope_operation: false,- allow_platform_self_operation: false,- },- &auth::JWTAuth {- permission: Permission::MerchantCustomerRead,- },- req.headers(),- ),+ &*auth_type,api_locking::LockAction::NotApplicable,)).await@@ -1744,6 +1786,20 @@ pub async fn payment_method_get_token_details_api(let flow = Flow::PaymentMethodGetTokenDetails;let temporary_token = path.into_inner();+ let api_auth = auth::V2ApiKeyAuth {+ allow_connected_scope_operation: false,+ allow_platform_self_operation: false,+ };++ let (auth_type, api_key_type) = match auth::check_internal_api_key_auth_no_client_secret(+ req.headers(),+ api_auth,+ state.conf.internal_merchant_id_profile_id_auth.clone(),+ ) {+ Ok(auth) => auth,+ Err(err) => return api::log_and_return_error_response(error_stack::report!(err)),+ };+Box::pin(api::server_wrap(flow,state,@@ -1761,10 +1817,7 @@ pub async fn payment_method_get_token_details_api(.await}},- &auth::V2ApiKeyAuth {- allow_connected_scope_operation: false,- allow_platform_self_operation: false,- },+ &*auth_type,api_locking::LockAction::NotApplicable,)).awaitdiff --git a/crates/router/src/services/authentication.rs b/crates/router/src/services/authentication.rsindex b335ae15eb..7a08199c07 100644--- a/crates/router/src/services/authentication.rs+++ b/crates/router/src/services/authentication.rs@@ -5072,6 +5072,36 @@ where}}+#[cfg(feature = "v2")]+pub(crate) fn check_internal_api_key_or_dashboard_auth_no_client_secret<T>(+ headers: &HeaderMap,+ api_auth: V2ApiKeyAuth,+ jwt_auth: JWTAuth,+ internal_api_key_auth: settings::InternalMerchantIdProfileIdAuthSettings,+) -> RouterResult<(+ Box<dyn AuthenticateAndFetch<AuthenticationData, T>>,+ common_enums::ApiKeyType,+)>+where+ T: SessionStateInfo + Sync + Send,+ ApiKeyAuth: AuthenticateAndFetch<AuthenticationData, T>,+{+ if is_internal_api_key_merchant_id_profile_id_auth(headers, internal_api_key_auth) {+ Ok((+ // HeaderAuth(api_auth) will never be called in this case as the internal auth will be checked first+ Box::new(InternalMerchantIdProfileIdAuth(HeaderAuth(api_auth))),+ common_enums::ApiKeyType::Internal,+ ))+ } else if is_jwt_auth(headers) {+ Ok((Box::new(jwt_auth), common_enums::ApiKeyType::External))+ } else {+ Ok((+ Box::new(HeaderAuth(api_auth)),+ common_enums::ApiKeyType::External,+ ))+ }+}+pub async fn decode_jwt<T>(token: &str, state: &impl SessionStateInfo) -> RouterResult<T>whereT: serde::de::DeserializeOwned,