Skip to content

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

feat(payment-methods): add pub <> client secret auth for client PML endpoint (#12841)

Kashifad9ab398dc8a
diff --git a/crates/router/src/core/payment_methods/client.rs b/crates/router/src/core/payment_methods/client.rs
index 753b9c0393..9cf6ecf949 100644
--- a/crates/router/src/core/payment_methods/client.rs
+++ b/crates/router/src/core/payment_methods/client.rs
@@ -4,7 +4,11 @@
/// - merchant-enabled payment methods (filtered via Euclid constraint graph + session flow routing)
/// - customer saved payment methods (fetched from DB via `CustomerPaymentMethodsFetcher` trait)
///
-/// Auth: `SdkAuthorizationAuth` only (Authorization: base64(publishable_key:client_secret))
+/// Auth:
+/// - `SdkAuthorizationAuth` via Authorization header.
+/// - `PublishableKeyAuth` via `api-key: pk_...` and `client_secret` query param.
+///
+/// Merchant secret-key auth is not supported for this client endpoint.
use api_models::payment_methods::{
ClientPaymentMethodsListResponse, CustomerPaymentMethod, CustomerPaymentMethodDataForClient,
CustomerPaymentMethodForClient, PaymentMethodListIntentDataInput,
@@ -537,11 +541,19 @@ pub async fn list_payment_methods_client(
state: routes::SessionState,
platform: domain::Platform,
payment_id: id_type::PaymentId,
+ client_secret: Option<String>,
) -> errors::RouterResponse<ClientPaymentMethodsListResponse> {
// 1. Load payment intent + related context
let payment_intent_context =
load_payment_intent_context(&state, &platform, &payment_id).await?;
+ if let Some(client_secret) = client_secret.as_ref() {
+ helpers::authenticate_client_secret(
+ Some(client_secret),
+ &payment_intent_context.payment_intent,
+ )?;
+ }
+
// 2. Fetch enabled payment methods (Gate 1 + Gate 2 + consolidation)
let EnabledPmsResult {
payment_methods_enabled,
diff --git a/crates/router/src/routes/payment_methods.rs b/crates/router/src/routes/payment_methods.rs
index 075caee1bb..a1b9df63df 100644
--- a/crates/router/src/routes/payment_methods.rs
+++ b/crates/router/src/routes/payment_methods.rs
@@ -2090,32 +2090,55 @@ pub async fn payment_method_get_token_details_api(
/// Returns a unified response combining merchant-enabled payment methods and
/// customer saved payment methods, filtered via Euclid constraint graph and
/// session flow routing.
+///
+/// Supported client auth:
+/// - SDK auth via `Authorization`.
+/// - Publishable-key auth via `api-key: pk_...` and `client_secret` query param.
+///
+/// Merchant secret-key auth is intentionally not supported for this endpoint.
#[instrument(skip_all, fields(flow = ?Flow::PaymentMethodsList))]
pub async fn list_payment_methods_for_payments_client(
state: web::Data<AppState>,
req: HttpRequest,
path: web::Path<id_type::PaymentId>,
+ query_payload: web::Query<payment_methods::PaymentMethodListRequest>,
) -> HttpResponse {
let flow = Flow::PaymentMethodsList;
let payment_id = path.into_inner();
+ let payload = query_payload.into_inner();
+ let api_auth = auth::ApiKeyAuth {
+ allow_connected_scope_operation: true,
+ allow_platform_self_operation: true,
+ };
- Box::pin(api::server_wrap(
- flow,
- state,
- &req,
- payment_id.clone(),
- |state, auth: auth::AuthenticationData, payment_id, _| {
- payment_methods_routes::client::list_payment_methods_client(
+ match auth::check_sdk_auth_or_client_secret_auth(req.headers(), &payload, api_auth) {
+ Ok((auth_type, _auth_flow)) => {
+ Box::pin(api::server_wrap(
+ flow,
state,
- auth.platform,
- payment_id,
- )
- },
- &auth::SdkAuthorizationAuth {
- allow_connected_scope_operation: true,
- allow_platform_self_operation: true,
- },
- api_locking::LockAction::NotApplicable,
- ))
- .await
+ &req,
+ payload,
+ move |state, auth: auth::AuthenticationData, mut payload, _| {
+ let payment_id = payment_id.clone();
+ async move {
+ if let Some(client_secret) = auth.client_secret {
+ payload.client_secret = Some(client_secret);
+ }
+
+ Box::pin(payment_methods_routes::client::list_payment_methods_client(
+ state,
+ auth.platform,
+ payment_id,
+ payload.client_secret,
+ ))
+ .await
+ }
+ },
+ &*auth_type,
+ api_locking::LockAction::NotApplicable,
+ ))
+ .await
+ }
+ Err(err) => api::log_and_return_error_response(err),
+ }
}
diff --git a/crates/router/src/services/authentication.rs b/crates/router/src/services/authentication.rs
index 1d882bc2f8..fc3680e4db 100644
--- a/crates/router/src/services/authentication.rs
+++ b/crates/router/src/services/authentication.rs
@@ -5870,6 +5870,54 @@ where
}
}
+/// Checks SDK authorization first, then falls back to publishable-key auth with
+/// a required client secret. Merchant secret-key auth is intentionally rejected.
+#[cfg(feature = "v1")]
+pub fn check_sdk_auth_or_client_secret_auth<T>(
+ headers: &HeaderMap,
+ payload: &impl ClientSecretFetch,
+ api_auth: ApiKeyAuth,
+) -> RouterResult<(
+ Box<dyn AuthenticateAndFetch<AuthenticationData, T>>,
+ api::AuthFlow,
+)>
+where
+ T: SessionStateInfo + Sync + Send,
+ PublishableKeyAuth: AuthenticateAndFetch<AuthenticationData, T>,
+ SdkAuthorizationAuth: AuthenticateAndFetch<AuthenticationData, T>,
+{
+ match get_header_value_by_key(headers::AUTHORIZATION.into(), headers)? {
+ Some(_) => Ok((
+ Box::new(SdkAuthorizationAuth {
+ allow_connected_scope_operation: api_auth.allow_connected_scope_operation,
+ allow_platform_self_operation: api_auth.allow_platform_self_operation,
+ }),
+ api::AuthFlow::Client,
+ )),
+ None => {
+ let api_key = get_api_key(headers)?;
+
+ match (
+ api_key.starts_with("pk_"),
+ payload.get_client_secret().is_some(),
+ ) {
+ (true, true) => Ok((
+ Box::new(HeaderAuth(PublishableKeyAuth {
+ allow_connected_scope_operation: api_auth.allow_connected_scope_operation,
+ allow_platform_self_operation: api_auth.allow_platform_self_operation,
+ })),
+ api::AuthFlow::Client,
+ )),
+ (true, false) => Err(errors::ApiErrorResponse::MissingRequiredField {
+ field_name: "client_secret",
+ }
+ .into()),
+ (false, _) => Err(errors::ApiErrorResponse::Unauthorized.into()),
+ }
+ }
+ }
+}
+
pub async fn get_ephemeral_or_other_auth<T>(
headers: &HeaderMap,
is_merchant_flow: bool,