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(auth): add embedded auth for payments and refunds modules (#14681)

Kanika Bansal6e0981f869fb
diff --git a/crates/router/src/analytics.rs b/crates/router/src/analytics.rs
index 493b5fc225..7d395867fa 100644
--- a/crates/router/src/analytics.rs
+++ b/crates/router/src/analytics.rs
@@ -1780,8 +1780,9 @@ pub mod routes {
.await
.map(ApplicationResponse::Json)
},
- &auth::JWTAuth {
- permission: Permission::ProfileAnalyticsRead,
+ &auth::JWTAndEmbeddedAuth {
+ merchant_id_from_route: None,
+ permission: Some(Permission::ProfileAnalyticsRead),
allow_connected: true,
allow_platform: false,
},
@@ -1825,8 +1826,9 @@ pub mod routes {
.await
.map(ApplicationResponse::Json)
},
- &auth::JWTAuth {
- permission: Permission::ProfileAnalyticsRead,
+ &auth::JWTAndEmbeddedAuth {
+ merchant_id_from_route: None,
+ permission: Some(Permission::ProfileAnalyticsRead),
allow_connected: true,
allow_platform: false,
},
@@ -1865,8 +1867,9 @@ pub mod routes {
.await
.map(ApplicationResponse::Json)
},
- &auth::JWTAuth {
- permission: Permission::ProfileAnalyticsRead,
+ &auth::JWTAndEmbeddedAuth {
+ merchant_id_from_route: None,
+ permission: Some(Permission::ProfileAnalyticsRead),
allow_connected: true,
allow_platform: false,
},
@@ -3708,8 +3711,9 @@ pub mod routes {
.await
.map(ApplicationResponse::Json)
},
- &auth::JWTAuth {
- permission: Permission::ProfileAnalyticsRead,
+ &auth::JWTAndEmbeddedAuth {
+ merchant_id_from_route: None,
+ permission: Some(Permission::ProfileAnalyticsRead),
allow_connected: true,
allow_platform: false,
},
@@ -3785,8 +3789,9 @@ pub mod routes {
allow_connected_scope_operation: false,
allow_platform_self_operation: false,
}),
- &auth::JWTAuth {
- permission: Permission::ProfileAnalyticsRead,
+ &auth::JWTAndEmbeddedAuth {
+ merchant_id_from_route: None,
+ permission: Some(Permission::ProfileAnalyticsRead),
allow_connected: true,
allow_platform: false,
},
diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rs
index 0d669af3ab..a924a235d6 100644
--- a/crates/router/src/core/payments.rs
+++ b/crates/router/src/core/payments.rs
@@ -14142,6 +14142,7 @@ fn get_eligible_manual_update_statuses(
pub async fn payments_manual_status_update_eligible_statuses(
state: SessionState,
platform: domain::Platform,
+ profile_id_from_auth_layer: Option<id_type::ProfileId>,
payment_id: id_type::PaymentId,
) -> RouterResponse<api_models::payments::PaymentsManualStatusUpdateEligibleStatusesResponse> {
let merchant_id = platform.get_processor().get_account().get_id();
@@ -14175,6 +14176,8 @@ pub async fn payments_manual_status_update_eligible_statuses(
.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)
.attach_printable("Error while fetching the payment_intent by payment_id, merchant_id")?;
+ core_utils::validate_profile_id_from_auth_layer(profile_id_from_auth_layer, &payment_intent)?;
+
if payment_intent.status != enums::IntentStatus::Conflicted {
return Err(errors::ApiErrorResponse::InvalidRequestData {
message: format!(
@@ -14214,6 +14217,7 @@ pub async fn payments_manual_status_update_eligible_statuses(
pub async fn payments_manual_status_update(
state: SessionState,
platform: domain::Platform,
+ profile_id_from_auth_layer: Option<id_type::ProfileId>,
payment_id: id_type::PaymentId,
req: api_models::payments::PaymentsManualStatusUpdateRequest,
) -> RouterResponse<api_models::payments::PaymentsManualStatusUpdateResponse> {
@@ -14250,6 +14254,8 @@ pub async fn payments_manual_status_update(
.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)
.attach_printable("Error while fetching the payment_intent by payment_id, merchant_id")?;
+ core_utils::validate_profile_id_from_auth_layer(profile_id_from_auth_layer, &payment_intent)?;
+
if !matches!(
payment_intent.status,
enums::IntentStatus::Review | enums::IntentStatus::Conflicted
diff --git a/crates/router/src/routes/payments.rs b/crates/router/src/routes/payments.rs
index a590c8502d..3c1cd3a4ae 100644
--- a/crates/router/src/routes/payments.rs
+++ b/crates/router/src/routes/payments.rs
@@ -861,8 +861,9 @@ pub async fn payments_retrieve(
},
auth::auth_type(
&*auth_type,
- &auth::JWTAuth {
- permission: Permission::ProfilePaymentRead,
+ &auth::JWTAndEmbeddedAuth {
+ merchant_id_from_route: None,
+ permission: Some(Permission::ProfilePaymentRead),
allow_connected: true,
allow_platform: false,
},
@@ -1400,8 +1401,9 @@ pub async fn payments_capture(
allow_connected_scope_operation: true,
allow_platform_self_operation: false,
}),
- &auth::JWTAuth {
- permission: Permission::ProfilePaymentWrite,
+ &auth::JWTAndEmbeddedAuth {
+ merchant_id_from_route: None,
+ permission: Some(Permission::ProfilePaymentWrite),
allow_connected: true,
allow_platform: false,
},
@@ -2077,8 +2079,9 @@ pub async fn payments_cancel(
allow_connected_scope_operation: true,
allow_platform_self_operation: false,
}),
- &auth::JWTAuth {
- permission: Permission::ProfilePaymentWrite,
+ &auth::JWTAndEmbeddedAuth {
+ merchant_id_from_route: None,
+ permission: Some(Permission::ProfilePaymentWrite),
allow_connected: true,
allow_platform: false,
},
@@ -2510,8 +2513,9 @@ pub async fn profile_payments_list_by_filter(
req,
)
},
- &auth::JWTAuth {
- permission: Permission::ProfilePaymentRead,
+ &auth::JWTAndEmbeddedAuth {
+ merchant_id_from_route: None,
+ permission: Some(Permission::ProfilePaymentRead),
allow_connected: true,
allow_platform: false,
},
@@ -2620,8 +2624,9 @@ pub async fn get_payment_filters_profile(
auth.profile.map(|profile| vec![profile.get_id().clone()]),
)
},
- &auth::JWTAuth {
- permission: Permission::ProfilePaymentRead,
+ &auth::JWTAndEmbeddedAuth {
+ merchant_id_from_route: None,
+ permission: Some(Permission::ProfilePaymentRead),
allow_connected: true,
allow_platform: false,
},
@@ -3356,7 +3361,7 @@ pub async fn payments_manual_update(
}
#[cfg(all(feature = "olap", feature = "v1"))]
-/// Manually update payment status from Review to Succeeded or Failed (Dashboard API with JWT auth)
+/// Manually resolve a Review or Conflicted payment using dashboard or embedded authentication.
#[instrument(skip_all, fields(flow = ?Flow::PaymentsManualStatusUpdate, payment_id))]
pub async fn payments_manual_status_update(
state: web::Data<app::AppState>,
@@ -3376,10 +3381,17 @@ pub async fn payments_manual_status_update(
&req,
payload,
|state, auth: auth::AuthenticationData, req, _req_state| {
- payments::payments_manual_status_update(state, auth.platform, payment_id.clone(), req)
+ payments::payments_manual_status_update(
+ state,
+ auth.platform,
+ auth.profile.map(|profile| profile.get_id().clone()),
+ payment_id.clone(),
+ req,
+ )
},
- &auth::JWTAuth {
- permission: Permission::ProfilePaymentWrite,
+ &auth::JWTAndEmbeddedAuth {
+ merchant_id_from_route: None,
+ permission: Some(Permission::ProfilePaymentWrite),
allow_connected: true,
allow_platform: true,
},
@@ -3390,7 +3402,7 @@ pub async fn payments_manual_status_update(
#[cfg(all(feature = "olap", feature = "v1"))]
/// List the statuses a conflicted payment is currently eligible for a manual status update to
-/// (Dashboard or API-key auth, mirroring the payment retrieve endpoint's auth).
+/// (Dashboard, embedded or API-key auth, mirroring the payment retrieve endpoint's auth).
#[instrument(skip_all, fields(flow = ?Flow::PaymentsManualStatusUpdateEligibleStatuses, payment_id))]
pub async fn payments_manual_status_update_eligible_statuses(
state: web::Data<app::AppState>,
@@ -3425,13 +3437,15 @@ pub async fn payments_manual_status_update_eligible_statuses(
payments::payments_manual_status_update_eligible_statuses(
state,
auth.platform,
+ auth.profile.map(|profile| profile.get_id().clone()),
payment_id.clone(),
)
},
auth::auth_type(
&*auth_type,
- &auth::JWTAuth {
- permission: Permission::ProfilePaymentRead,
+ &auth::JWTAndEmbeddedAuth {
+ merchant_id_from_route: None,
+ permission: Some(Permission::ProfilePaymentRead),
allow_connected: true,
allow_platform: false,
},
@@ -4007,8 +4021,9 @@ pub async fn get_payments_aggregates_profile(
req,
)
},
- &auth::JWTAuth {
- permission: Permission::ProfilePaymentRead,
+ &auth::JWTAndEmbeddedAuth {
+ merchant_id_from_route: None,
+ permission: Some(Permission::ProfilePaymentRead),
allow_connected: true,
allow_platform: false,
},
diff --git a/crates/router/src/routes/refunds.rs b/crates/router/src/routes/refunds.rs
index a0972b1ac2..18343157a5 100644
--- a/crates/router/src/routes/refunds.rs
+++ b/crates/router/src/routes/refunds.rs
@@ -69,8 +69,9 @@ pub async fn refunds_create(
allow_connected_scope_operation: true,
allow_platform_self_operation: false,
}),
- &auth::JWTAuth {
- permission: Permission::ProfileRefundWrite,
+ &auth::JWTAndEmbeddedAuth {
+ merchant_id_from_route: None,
+ permission: Some(Permission::ProfileRefundWrite),
allow_connected: true,
allow_platform: false,
},
@@ -178,8 +179,9 @@ pub async fn refunds_retrieve(
allow_connected_scope_operation: true,
allow_platform_self_operation: false,
}),
- &auth::JWTAuth {
- permission: Permission::ProfileRefundRead,
+ &auth::JWTAndEmbeddedAuth {
+ merchant_id_from_route: None,
+ permission: Some(Permission::ProfileRefundRead),
allow_connected: true,
allow_platform: false,
},
@@ -618,8 +620,9 @@ pub async fn refunds_list_profile(
allow_connected_scope_operation: true,
allow_platform_self_operation: false,
}),
- &auth::JWTAuth {
- permission: Permission::ProfileRefundRead,
+ &auth::JWTAndEmbeddedAuth {
+ merchant_id_from_route: None,
+ permission: Some(Permission::ProfileRefundRead),
allow_connected: true,
allow_platform: false,
},
@@ -725,8 +728,9 @@ pub async fn get_refunds_filters_profile(
allow_connected_scope_operation: true,
allow_platform_self_operation: false,
}),
- &auth::JWTAuth {
- permission: Permission::ProfileRefundRead,
+ &auth::JWTAndEmbeddedAuth {
+ merchant_id_from_route: None,
+ permission: Some(Permission::ProfileRefundRead),
allow_connected: true,
allow_platform: false,
},
@@ -821,8 +825,9 @@ pub async fn get_refunds_aggregate_profile(
allow_connected_scope_operation: true,
allow_platform_self_operation: false,
}),
- &auth::JWTAuth {
- permission: Permission::ProfileRefundRead,
+ &auth::JWTAndEmbeddedAuth {
+ merchant_id_from_route: None,
+ permission: Some(Permission::ProfileRefundRead),
allow_connected: true,
allow_platform: false,
},
diff --git a/crates/router/src/services/authentication.rs b/crates/router/src/services/authentication.rs
index 230ed7e4b3..b8bf89486c 100644
--- a/crates/router/src/services/authentication.rs
+++ b/crates/router/src/services/authentication.rs
@@ -5678,6 +5678,125 @@ where
}
}
+#[cfg(feature = "v2")]
+#[async_trait]
+impl<A> AuthenticateAndFetch<AuthenticationData, A> for JWTAndEmbeddedAuth
+where
+ A: SessionStateInfo + Sync,
+{
+ async fn authenticate_and_fetch(
+ &self,
+ request_headers: &HeaderMap,
+ state: &A,
+ ) -> RouterResult<(AuthenticationData, AuthenticationType)> {
+ let payload = parse_jwt_payload::<A, AuthOrEmbeddedClaims>(request_headers, state).await?;
+
+ if let AuthOrEmbeddedClaims::AuthToken(ref auth_payload) = payload {
+ if auth_payload.check_in_blacklist(state).await? {
+ return Err(errors::ApiErrorResponse::InvalidJwtToken.into());
+ }
+ if let Some(required_permission) = self.permission {
+ let role_info = authorization::get_role_info(state, auth_payload).await?;
+ authorization::check_permission(required_permission, &role_info)?;
+ }
+ }
+
+ authorization::check_tenant(
+ payload.get_tenant_id().cloned(),
+ &state.session_state().tenant.tenant_id,
+ )?;
+
+ let profile_id = match &payload {
+ AuthOrEmbeddedClaims::AuthToken(_) => {
+ HeaderMapStruct::new(request_headers)
+ .get_id_type_from_header::<id_type::ProfileId>(headers::X_PROFILE_ID)?
+ }
+ AuthOrEmbeddedClaims::EmbeddedToken(embedded_payload) => {
+ embedded_payload.profile_id.clone()
+ }
+ };
+
+ let key_store = state
+ .store()
+ .get_merchant_key_store_by_merchant_id(
+ payload.get_merchant_id(),
+ &state.store().get_master_key().to_vec().into(),
+ )
+ .await
+ .to_not_found_response(errors::ApiErrorResponse::InvalidJwtToken)
+ .attach_printable("Failed to fetch merchant key store for the merchant id")?;
+
+ let profile = state
+ .store()
+ .find_business_profile_by_merchant_id_profile_id(
+ &key_store,
+ payload.get_merchant_id(),
+ &profile_id,
+ )
+ .await
+ .to_not_found_response(errors::ApiErrorResponse::Unauthorized)?;
+
+ let merchant = state
+ .store()
+ .find_merchant_account_by_merchant_id(payload.get_merchant_id(), &key_store)
+ .await
+ .to_not_found_response(errors::ApiErrorResponse::InvalidJwtToken)
+ .attach_printable("Failed to fetch merchant account for the merchant id")?;
+
+ fp_utils::when(
+ self.merchant_id_from_route
+ .as_ref()
+ .is_some_and(|mid_from_route| payload.get_merchant_id() != mid_from_route),
+ || Err(report!(errors::ApiErrorResponse::InvalidJwtToken)),
+ )?;
+
+ check_merchant_access_for_jwt(
+ state,
+ merchant.merchant_account_type,
+ self.allow_connected,
+ self.allow_platform,
+ )?;
+
+ let initiator = if let AuthOrEmbeddedClaims::AuthToken(ref auth_payload) = payload {
+ Some(domain::Initiator::Jwt {
+ user_id: auth_payload.user_id.clone(),
+ })
+ } else {
+ Some(domain::Initiator::EmbeddedToken {
+ merchant_id: payload.get_merchant_id().clone(),
+ })
+ };
+
+ let platform = resolve_platform(
+ state,
+ request_headers,
+ merchant.clone(),
+ key_store,
+ initiator,
+ )
+ .await?;
+
+ let auth = AuthenticationData {
+ platform,
+ profile,
+ client_secret: None,
+ };
+ let auth_type = match payload {
+ AuthOrEmbeddedClaims::AuthToken(auth_payload) => AuthenticationType::MerchantJwt {
+ merchant_id: auth_payload.merchant_id,
+ user_id: Some(auth_payload.user_id),
+ },
+ AuthOrEmbeddedClaims::EmbeddedToken(embedded_payload) => {
+ AuthenticationType::EmbeddedJwt {
+ merchant_id: embedded_payload.merchant_id,
+ profile_id: embedded_payload.profile_id,
+ }
+ }
+ };
+ Ok((auth, auth_type))
+ }
+}
+
pub trait ClientSecretFetch {
fn get_client_secret(&self) -> Option<&String>;
}