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(core): added sdk authorisation support to sdk configs (#12887)

Sahkal Poddar8f582fc57fdd
diff --git a/crates/api_models/src/superposition_sdk_config.rs b/crates/api_models/src/superposition_sdk_config.rs
index a218795f63..118c72d20f 100644
--- a/crates/api_models/src/superposition_sdk_config.rs
+++ b/crates/api_models/src/superposition_sdk_config.rs
@@ -1,9 +1,17 @@
use common_utils::events::ApiEventMetric;
+use hyperswitch_masking::Secret;
use serde_json::{Map, Value};
use superposition_types::Config;
use crate::enums as api_enums;
+#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
+pub struct SdkConfigRequest {
+ pub client_secret: Option<Secret<String>>,
+}
+
+impl ApiEventMetric for SdkConfigRequest {}
+
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum PaymentMethodCriteria {
diff --git a/crates/router/src/lib.rs b/crates/router/src/lib.rs
index 68ca06debb..d691797b3b 100644
--- a/crates/router/src/lib.rs
+++ b/crates/router/src/lib.rs
@@ -93,8 +93,6 @@ pub mod headers {
pub const X_CLIENT_SECRET: &str = "X-Client-Secret";
pub const X_CUSTOMER_ID: &str = "X-Customer-Id";
pub const X_CONNECTED_MERCHANT_ID: &str = "x-connected-merchant-id";
- pub const CLIENT_SECRET: &str = "client-secret";
- pub const SDK_AUTHORIZATION: &str = "sdk-authorization";
// Header value for X_CONNECTOR_HTTP_STATUS_CODE differs by version.
// Constant name is kept the same for consistency across versions.
#[cfg(feature = "v1")]
diff --git a/crates/router/src/routes/superposition_sdk_config.rs b/crates/router/src/routes/superposition_sdk_config.rs
index cb609bd270..ed50af3136 100644
--- a/crates/router/src/routes/superposition_sdk_config.rs
+++ b/crates/router/src/routes/superposition_sdk_config.rs
@@ -1,7 +1,9 @@
use actix_web::{web, HttpRequest, HttpResponse};
+#[cfg(feature = "v1")]
use error_stack::ResultExt;
#[cfg(feature = "v1")]
use hyperswitch_domain_models::sdk_auth::SdkAuthorization;
+use hyperswitch_masking::PeekInterface;
use router_env::{instrument, tracing, Flow};
use super::app::AppState;
@@ -9,17 +11,16 @@ use super::app::AppState;
use crate::core::{payments, superposition_sdk_config};
use crate::{
core::{api_locking, errors},
- headers::{CLIENT_SECRET, SDK_AUTHORIZATION},
+ headers::AUTHORIZATION,
services::{api, authentication as auth},
};
#[cfg(feature = "v1")]
-fn get_payment_id_from_headers(
+fn get_payment_id_from_headers_or_payload(
headers: &actix_web::http::header::HeaderMap,
+ json_payload: &api_models::superposition_sdk_config::SdkConfigRequest,
) -> error_stack::Result<common_utils::id_type::PaymentId, errors::ApiErrorResponse> {
- if let Some(sdk_auth_val) =
- auth::get_header_value_by_key(SDK_AUTHORIZATION.to_string(), headers)?
- {
+ if let Some(sdk_auth_val) = auth::get_header_value_by_key(AUTHORIZATION.to_string(), headers)? {
let sdk_auth = SdkAuthorization::decode(sdk_auth_val)
.change_context(errors::ApiErrorResponse::Unauthorized)?;
sdk_auth.payment_id.ok_or_else(|| {
@@ -28,15 +29,17 @@ fn get_payment_id_from_headers(
})
})
} else {
- let client_secret = auth::get_header_value_by_key(CLIENT_SECRET.to_string(), headers)?
+ let client_secret = json_payload
+ .client_secret
+ .as_ref()
+ .map(|cs| cs.peek())
.ok_or_else(|| {
error_stack::report!(errors::ApiErrorResponse::MissingRequiredField {
- field_name: CLIENT_SECRET,
+ field_name: "client_secret",
})
- })?
- .to_string();
+ })?;
- let payment_id_str = payments::helpers::get_payment_id_from_client_secret(&client_secret)?;
+ let payment_id_str = payments::helpers::get_payment_id_from_client_secret(client_secret)?;
common_utils::id_type::PaymentId::wrap(payment_id_str).change_context(
errors::ApiErrorResponse::InvalidDataValue {
field_name: "payment_id",
@@ -51,15 +54,28 @@ pub async fn get_sdk_config(
state: web::Data<AppState>,
req: HttpRequest,
path: web::Path<String>,
+ json_payload: web::Query<api_models::superposition_sdk_config::SdkConfigRequest>,
) -> HttpResponse {
let flow = Flow::GetSuperpositionSdkConfig;
let _platform = path.into_inner();
+ let payload = json_payload.into_inner();
- let payment_id = match get_payment_id_from_headers(req.headers()) {
+ let payment_id = match get_payment_id_from_headers_or_payload(req.headers(), &payload) {
Ok(payment_id) => payment_id,
Err(err) => return api::log_and_return_error_response(err),
};
+ let api_auth = auth::ApiKeyAuth {
+ allow_connected_scope_operation: true,
+ allow_platform_self_operation: true,
+ };
+
+ let (auth_type, _auth_flow) =
+ match auth::check_sdk_auth_and_get_auth(req.headers(), &payload, api_auth) {
+ Ok(auth) => auth,
+ Err(e) => return api::log_and_return_error_response(e),
+ };
+
Box::pin(api::server_wrap(
flow,
state,
@@ -72,10 +88,7 @@ pub async fn get_sdk_config(
payment_id.clone(),
)
},
- &auth::HeaderAuth(auth::PublishableKeyAuth {
- allow_connected_scope_operation: true,
- allow_platform_self_operation: false,
- }),
+ &*auth_type,
api_locking::LockAction::NotApplicable,
))
.await
diff --git a/crates/router/src/services/authentication.rs b/crates/router/src/services/authentication.rs
index fc3680e4db..9b1998fcc5 100644
--- a/crates/router/src/services/authentication.rs
+++ b/crates/router/src/services/authentication.rs
@@ -5754,6 +5754,14 @@ impl ClientSecretFetch for api_models::authentication::AuthenticationSessionToke
}
}
+impl ClientSecretFetch for api_models::superposition_sdk_config::SdkConfigRequest {
+ fn get_client_secret(&self) -> Option<&String> {
+ self.client_secret
+ .as_ref()
+ .map(|client_secret| client_secret.peek())
+ }
+}
+
pub fn get_auth_type_and_flow<A: SessionStateInfo + Sync + Send>(
headers: &HeaderMap,
api_auth: ApiKeyAuth,