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

fix(router): enforce authentication profile scope (#15097)

Ayush Anand067dccc79e16
diff --git a/api-reference/v1/openapi_spec_v1.json b/api-reference/v1/openapi_spec_v1.json
index f2fd3f6680..7f84660c51 100644
--- a/api-reference/v1/openapi_spec_v1.json
+++ b/api-reference/v1/openapi_spec_v1.json
@@ -9376,6 +9376,9 @@
},
"400": {
"description": "Invalid data"
+ },
+ "422": {
+ "description": "Profile does not match the authentication"
}
},
"security": [
diff --git a/crates/openapi/src/routes/authentication.rs b/crates/openapi/src/routes/authentication.rs
index cccb179191..09c87ed9bf 100644
--- a/crates/openapi/src/routes/authentication.rs
+++ b/crates/openapi/src/routes/authentication.rs
@@ -26,7 +26,8 @@ pub async fn authentication_create() {}
request_body = AuthenticationEligibilityRequest,
responses(
(status = 200, description = "Authentication eligibility checked", body = AuthenticationEligibilityResponse),
- (status = 400, description = "Invalid data")
+ (status = 400, description = "Invalid data"),
+ (status = 422, description = "Profile does not match the authentication")
),
tag = "Authentication",
operation_id = "Check Authentication Eligibility",
diff --git a/crates/router/src/core/payments/operations/payment_confirm.rs b/crates/router/src/core/payments/operations/payment_confirm.rs
index e19c5a339b..ce597187da 100644
--- a/crates/router/src/core/payments/operations/payment_confirm.rs
+++ b/crates/router/src/core/payments/operations/payment_confirm.rs
@@ -2321,6 +2321,7 @@ impl<F: Clone + Send + Sync> Domain<F, api::PaymentsRequest, PaymentData<F>> for
let auth_create_resp = crate::core::unified_authentication_service::authentication_create_core(
state.clone(),
platform.clone(),
+ Some(business_profile.get_id().clone()),
auth_req
).await?
.get_json_body()
@@ -2332,6 +2333,7 @@ impl<F: Clone + Send + Sync> Domain<F, api::PaymentsRequest, PaymentData<F>> for
let elig_resp = Box::pin(crate::core::unified_authentication_service::authentication_eligibility_core(
state.clone(),
platform.clone(),
+ Some(business_profile.get_id().clone()),
eligibility_req,
auth_create_resp.authentication_id.clone(),
)).await?
@@ -2432,6 +2434,7 @@ impl<F: Clone + Send + Sync> Domain<F, api::PaymentsRequest, PaymentData<F>> for
let resp = Box::pin(crate::core::unified_authentication_service::authentication_sync_core(
state.clone(),
platform.clone(),
+ Some(business_profile.get_id().clone()),
services::api::AuthFlow::Merchant,
sync_req,
))
diff --git a/crates/router/src/core/unified_authentication_service.rs b/crates/router/src/core/unified_authentication_service.rs
index 4e60c164bf..b5ffca9e53 100644
--- a/crates/router/src/core/unified_authentication_service.rs
+++ b/crates/router/src/core/unified_authentication_service.rs
@@ -712,6 +712,7 @@ pub async fn create_new_authentication(
pub async fn authentication_create_core(
state: SessionState,
platform: domain::Platform,
+ profile_id_from_auth_layer: Option<common_utils::id_type::ProfileId>,
req: AuthenticationCreateRequest,
) -> RouterResponse<AuthenticationResponse> {
let db = &*state.store;
@@ -722,10 +723,13 @@ pub async fn authentication_create_core(
None,
None,
platform.get_processor(),
- req.profile_id.as_ref(),
+ req.profile_id
+ .as_ref()
+ .or(profile_id_from_auth_layer.as_ref()),
db,
)
.await?;
+ core_utils::validate_profile_id_from_auth_layer(profile_id_from_auth_layer, &business_profile)?;
let profile_id = business_profile.get_id().to_owned();
let organization_id = processor_merchant_account.organization_id.clone();
let authentication_id = common_utils::id_type::AuthenticationId::generate_authentication_id(
@@ -1031,6 +1035,7 @@ impl
pub async fn authentication_eligibility_core(
state: SessionState,
platform: domain::Platform,
+ profile_id_from_auth_layer: Option<common_utils::id_type::ProfileId>,
req: AuthenticationEligibilityRequest,
authentication_id: common_utils::id_type::AuthenticationId,
) -> RouterResponse<AuthenticationEligibilityResponse> {
@@ -1056,6 +1061,8 @@ pub async fn authentication_eligibility_core(
id: authentication_id.get_string_repr().to_owned(),
})?;
+ core_utils::validate_profile_id_from_auth_layer(profile_id_from_auth_layer, &authentication)?;
+
req.client_secret
.clone()
.map(|client_secret| {
@@ -1068,11 +1075,22 @@ pub async fn authentication_eligibility_core(
ensure_not_terminal_status(authentication.trans_status.clone())?;
+ common_utils::fp_utils::when(
+ req.profile_id
+ .as_ref()
+ .is_some_and(|profile_id| profile_id != &authentication.profile_id),
+ || {
+ Err(error_stack::report!(ApiErrorResponse::InvalidRequestData {
+ message: "profile_id must match the authentication's profile_id".to_owned(),
+ }))
+ },
+ )?;
+
let business_profile = core_utils::get_profile_from_business_details(
None,
None,
platform.get_processor(),
- req.profile_id.as_ref(),
+ Some(&authentication.profile_id),
db,
)
.await?;
@@ -2054,6 +2072,7 @@ async fn use_stored_authentication_value(
pub async fn authentication_sync_core(
state: SessionState,
platform: domain::Platform,
+ profile_id_from_auth_layer: Option<common_utils::id_type::ProfileId>,
auth_flow: AuthFlow,
req: AuthenticationSyncRequest,
) -> RouterResponse<AuthenticationSyncResponse> {
@@ -2075,6 +2094,8 @@ pub async fn authentication_sync_core(
id: authentication_id.get_string_repr().to_owned(),
})?;
+ core_utils::validate_profile_id_from_auth_layer(profile_id_from_auth_layer, &authentication)?;
+
req.client_secret
.map(|client_secret| {
utils::authenticate_authentication_client_secret_and_check_expiry(
diff --git a/crates/router/src/core/utils.rs b/crates/router/src/core/utils.rs
index 7f1c02a915..dbd22f5abe 100644
--- a/crates/router/src/core/utils.rs
+++ b/crates/router/src/core/utils.rs
@@ -2917,6 +2917,12 @@ impl GetProfileId for domain::Profile {
}
}
+impl GetProfileId for hyperswitch_domain_models::authentication::Authentication {
+ fn get_profile_id(&self) -> Option<&common_utils::id_type::ProfileId> {
+ Some(&self.profile_id)
+ }
+}
+
#[cfg(feature = "payouts")]
impl GetProfileId for storage::Payouts {
fn get_profile_id(&self) -> Option<&common_utils::id_type::ProfileId> {
diff --git a/crates/router/src/routes/authentication.rs b/crates/router/src/routes/authentication.rs
index 931f00591b..c7e48d01f1 100644
--- a/crates/router/src/routes/authentication.rs
+++ b/crates/router/src/routes/authentication.rs
@@ -30,7 +30,12 @@ pub async fn authentication_create(
&req,
json_payload.into_inner(),
|state, auth: auth::AuthenticationData, req, _| {
- unified_authentication_service::authentication_create_core(state, auth.platform, req)
+ unified_authentication_service::authentication_create_core(
+ state,
+ auth.platform,
+ auth.profile.map(|profile| profile.get_id().clone()),
+ req,
+ )
},
&auth::HeaderAuth(auth::ApiKeyAuth {
allow_connected_scope_operation: false,
@@ -73,6 +78,7 @@ pub async fn authentication_eligibility(
unified_authentication_service::authentication_eligibility_core(
state,
auth.platform,
+ auth.profile.map(|profile| profile.get_id().clone()),
req,
authentication_id.clone(),
)
@@ -242,6 +248,7 @@ pub async fn authentication_sync(
unified_authentication_service::authentication_sync_core(
state,
auth.platform,
+ auth.profile.map(|profile| profile.get_id().clone()),
auth_flow,
req,
)