Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(router): add webhook source verification support for multiple mca of the same connector #1897

Merged
merged 3 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/api_models/src/webhooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ impl From<IncomingWebhookEvent> for WebhookFlow {

pub type MerchantWebhookConfig = std::collections::HashSet<IncomingWebhookEvent>;

#[derive(Clone)]
pub enum RefundIdType {
RefundId(String),
ConnectorRefundId(String),
}

#[derive(Clone)]
pub enum ObjectReferenceId {
PaymentId(payments::PaymentIdType),
RefundId(RefundIdType),
Expand Down
12 changes: 9 additions & 3 deletions crates/router/src/connector/adyen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1372,24 +1372,30 @@ impl api::IncomingWebhook for Adyen {
&self,
db: &dyn StorageInterface,
request: &api::IncomingWebhookRequestDetails<'_>,
merchant_id: &str,
merchant_account: &domain::MerchantAccount,
connector_label: &str,
key_store: &domain::MerchantKeyStore,
object_reference_id: api_models::webhooks::ObjectReferenceId,
) -> CustomResult<bool, errors::ConnectorError> {
let signature = self
.get_webhook_source_verification_signature(request)
.change_context(errors::ConnectorError::WebhookSourceVerificationFailed)?;
let secret = self
.get_webhook_source_verification_merchant_secret(
db,
merchant_id,
merchant_account,
connector_label,
key_store,
object_reference_id,
)
.await
.change_context(errors::ConnectorError::WebhookSourceVerificationFailed)?;
let message = self
.get_webhook_source_verification_message(request, merchant_id, &secret)
.get_webhook_source_verification_message(
request,
&merchant_account.merchant_id,
&secret,
)
.change_context(errors::ConnectorError::WebhookSourceVerificationFailed)?;

let raw_key = hex::decode(secret)
Expand Down
12 changes: 9 additions & 3 deletions crates/router/src/connector/bluesnap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -983,9 +983,10 @@ impl api::IncomingWebhook for Bluesnap {
&self,
db: &dyn StorageInterface,
request: &api::IncomingWebhookRequestDetails<'_>,
merchant_id: &str,
merchant_account: &domain::MerchantAccount,
connector_label: &str,
key_store: &domain::MerchantKeyStore,
object_reference_id: api_models::webhooks::ObjectReferenceId,
) -> CustomResult<bool, errors::ConnectorError> {
let algorithm = self
.get_webhook_source_verification_algorithm(request)
Expand All @@ -997,14 +998,19 @@ impl api::IncomingWebhook for Bluesnap {
let mut secret = self
.get_webhook_source_verification_merchant_secret(
db,
merchant_id,
merchant_account,
connector_label,
key_store,
object_reference_id,
)
.await
.change_context(errors::ConnectorError::WebhookSourceVerificationFailed)?;
let mut message = self
.get_webhook_source_verification_message(request, merchant_id, &secret)
.get_webhook_source_verification_message(
request,
&merchant_account.merchant_id,
&secret,
)
.change_context(errors::ConnectorError::WebhookSourceVerificationFailed)?;
message.append(&mut secret);
algorithm
Expand Down
6 changes: 4 additions & 2 deletions crates/router/src/connector/cashtocode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,19 +322,21 @@ impl api::IncomingWebhook for Cashtocode {
&self,
db: &dyn StorageInterface,
request: &api::IncomingWebhookRequestDetails<'_>,
merchant_id: &str,
merchant_account: &domain::MerchantAccount,
connector_label: &str,
key_store: &domain::MerchantKeyStore,
object_reference_id: api_models::webhooks::ObjectReferenceId,
) -> CustomResult<bool, errors::ConnectorError> {
let signature = self
.get_webhook_source_verification_signature(request)
.change_context(errors::ConnectorError::WebhookSourceVerificationFailed)?;
let secret = self
.get_webhook_source_verification_merchant_secret(
db,
merchant_id,
merchant_account,
connector_label,
key_store,
object_reference_id,
)
.await
.change_context(errors::ConnectorError::WebhookSourceVerificationFailed)?;
Expand Down
12 changes: 9 additions & 3 deletions crates/router/src/connector/rapyd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,24 +739,30 @@ impl api::IncomingWebhook for Rapyd {
&self,
db: &dyn StorageInterface,
request: &api::IncomingWebhookRequestDetails<'_>,
merchant_id: &str,
merchant_account: &domain::MerchantAccount,
connector_label: &str,
key_store: &domain::MerchantKeyStore,
object_reference_id: api_models::webhooks::ObjectReferenceId,
) -> CustomResult<bool, errors::ConnectorError> {
let signature = self
.get_webhook_source_verification_signature(request)
.change_context(errors::ConnectorError::WebhookSourceVerificationFailed)?;
let secret = self
.get_webhook_source_verification_merchant_secret(
db,
merchant_id,
merchant_account,
connector_label,
key_store,
object_reference_id,
)
.await
.change_context(errors::ConnectorError::WebhookSourceVerificationFailed)?;
let message = self
.get_webhook_source_verification_message(request, merchant_id, &secret)
.get_webhook_source_verification_message(
request,
&merchant_account.merchant_id,
&secret,
)
.change_context(errors::ConnectorError::WebhookSourceVerificationFailed)?;

let stringify_auth = String::from_utf8(secret.to_vec())
Expand Down
13 changes: 9 additions & 4 deletions crates/router/src/connector/zen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,23 +557,28 @@ impl api::IncomingWebhook for Zen {
&self,
db: &dyn StorageInterface,
request: &api::IncomingWebhookRequestDetails<'_>,
merchant_id: &str,
merchant_account: &domain::MerchantAccount,
connector_label: &str,
key_store: &domain::MerchantKeyStore,
object_reference_id: api_models::webhooks::ObjectReferenceId,
) -> CustomResult<bool, errors::ConnectorError> {
let algorithm = self.get_webhook_source_verification_algorithm(request)?;

let signature = self.get_webhook_source_verification_signature(request)?;
let mut secret = self
.get_webhook_source_verification_merchant_secret(
db,
merchant_id,
merchant_account,
connector_label,
key_store,
object_reference_id,
)
.await?;
let mut message =
self.get_webhook_source_verification_message(request, merchant_id, &secret)?;
let mut message = self.get_webhook_source_verification_message(
request,
&merchant_account.merchant_id,
&secret,
)?;
message.append(&mut secret);
algorithm
.verify_signature(&secret, &signature, &message)
Expand Down
12 changes: 7 additions & 5 deletions crates/router/src/core/webhooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,13 +726,19 @@ pub async fn webhooks_core<W: types::OutgoingWebhookType>(

let flow_type: api::WebhookFlow = event_type.to_owned().into();
if process_webhook_further && !matches!(flow_type, api::WebhookFlow::ReturnResponse) {
let object_ref_id = connector
.get_webhook_object_reference_id(&request_details)
.switch()
.attach_printable("Could not find object reference id in incoming webhook body")?;

let source_verified = connector
.verify_webhook_source(
&*state.store,
&request_details,
&merchant_account.merchant_id,
&merchant_account,
connector_name,
&key_store,
object_ref_id.clone(),
)
.await
.switch()
Expand All @@ -750,10 +756,6 @@ pub async fn webhooks_core<W: types::OutgoingWebhookType>(
}

logger::info!(source_verified=?source_verified);
let object_ref_id = connector
.get_webhook_object_reference_id(&request_details)
.switch()
.attach_printable("Could not find object reference id in incoming webhook body")?;

let event_object = connector
.get_webhook_resource_object(&request_details)
Expand Down
31 changes: 24 additions & 7 deletions crates/router/src/types/api/webhooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
db::StorageInterface,
logger, services,
types::domain,
utils::crypto,
utils::{self, crypto},
};

pub struct IncomingWebhookRequestDetails<'a> {
Expand Down Expand Up @@ -78,19 +78,30 @@ pub trait IncomingWebhook: ConnectorCommon + Sync {
async fn get_webhook_source_verification_merchant_secret(
&self,
db: &dyn StorageInterface,
merchant_id: &str,
merchant_account: &domain::MerchantAccount,
connector_name: &str,
key_store: &domain::MerchantKeyStore,
object_reference_id: ObjectReferenceId,
) -> CustomResult<Vec<u8>, errors::ConnectorError> {
let merchant_id = merchant_account.merchant_id.as_str();
let debug_suffix = format!(
"For merchant_id: {}, and connector_name: {}",
merchant_id, connector_name
);
let default_secret = "default_secret".to_string();
let connector_label = utils::get_connector_label_using_object_reference_id(
db,
object_reference_id,
merchant_account,
connector_name,
)
.await
.change_context(errors::ConnectorError::WebhookSourceVerificationFailed)
.attach_printable("Error while fetching connector_label")?;
let merchant_connector_account_result = db
.find_merchant_connector_account_by_merchant_id_connector_name(
.find_merchant_connector_account_by_merchant_id_connector_label(
merchant_id,
connector_name,
&connector_label,
key_store,
)
.await;
Expand Down Expand Up @@ -149,9 +160,10 @@ pub trait IncomingWebhook: ConnectorCommon + Sync {
&self,
db: &dyn StorageInterface,
request: &IncomingWebhookRequestDetails<'_>,
merchant_id: &str,
merchant_account: &domain::MerchantAccount,
connector_label: &str,
key_store: &domain::MerchantKeyStore,
object_reference_id: ObjectReferenceId,
) -> CustomResult<bool, errors::ConnectorError> {
let algorithm = self
.get_webhook_source_verification_algorithm(request)
Expand All @@ -163,14 +175,19 @@ pub trait IncomingWebhook: ConnectorCommon + Sync {
let secret = self
.get_webhook_source_verification_merchant_secret(
db,
merchant_id,
merchant_account,
connector_label,
key_store,
object_reference_id,
)
.await
.change_context(errors::ConnectorError::WebhookSourceVerificationFailed)?;
let message = self
.get_webhook_source_verification_message(request, merchant_id, &secret)
.get_webhook_source_verification_message(
request,
&merchant_account.merchant_id,
&secret,
)
.change_context(errors::ConnectorError::WebhookSourceVerificationFailed)?;
algorithm
.verify_signature(&secret, &signature, &message)
Expand Down
Loading
Loading