Skip to content

Commit

Permalink
refactor: add ConnectorAuthType in api_models
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisIsMani committed Nov 28, 2023
1 parent c23218f commit 7b0ccfb
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 16 deletions.
32 changes: 32 additions & 0 deletions crates/api_models/src/admin.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use common_utils::{
crypto::{Encryptable, OptionalEncryptableName},
pii,
Expand Down Expand Up @@ -614,6 +616,36 @@ pub struct MerchantConnectorCreate {
pub status: Option<api_enums::ConnectorStatus>,
}

// Different patterns of authentication.
#[derive(Default, Debug, Clone, serde::Deserialize, serde::Serialize)]
#[serde(tag = "auth_type")]
pub enum ConnectorAuthType {
TemporaryAuth,
HeaderKey {
api_key: Secret<String>,
},
BodyKey {
api_key: Secret<String>,
key1: Secret<String>,
},
SignatureKey {
api_key: Secret<String>,
key1: Secret<String>,
api_secret: Secret<String>,
},
MultiAuthKey {
api_key: Secret<String>,
key1: Secret<String>,
api_secret: Secret<String>,
key2: Secret<String>,
},
CurrencyAuthKey {
auth_key_map: HashMap<common_enums::Currency, pii::SecretSerdeValue>,
},
#[default]
NoKey,
}

#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(deny_unknown_fields)]
pub struct MerchantConnectorWebhookDetails {
Expand Down
1 change: 1 addition & 0 deletions crates/api_models/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ pub mod routing;
pub mod surcharge_decision_configs;
pub mod user;
pub mod verifications;
pub mod verify_connector;
pub mod webhooks;
11 changes: 11 additions & 0 deletions crates/api_models/src/verify_connector.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use common_utils::events::{ApiEventMetric, ApiEventsType};

use crate::{admin, enums};

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub struct VerifyConnectorRequest {
pub connector_name: enums::Connector,
pub connector_account_details: admin::ConnectorAuthType,
}

common_utils::impl_misc_api_event_type!(VerifyConnectorRequest);
2 changes: 1 addition & 1 deletion crates/kgraph_utils/src/mca.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ mod tests {
connector_label: Some("something".to_string()),
business_label: Some("food".to_string()),
business_sub_label: None,
connector_account_details: masking::Secret::new(serde_json::json!({})),
connector_account_details: ConnectorAuthType::default(),
test_mode: None,
disabled: None,
metadata: None,
Expand Down
8 changes: 4 additions & 4 deletions crates/router/src/core/verify_connector.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use api_models::enums::Connector;
use api_models::{enums::Connector, verify_connector::VerifyConnectorRequest};
use error_stack::{IntoReport, ResultExt};

use crate::{
Expand All @@ -15,7 +15,7 @@ use crate::{

pub async fn verify_connector_credentials(
state: AppState,
req: types::VerifyConnectorRequest,
req: VerifyConnectorRequest,
) -> errors::RouterResponse<()> {
let boxed_connector = api::ConnectorData::get_connector_by_name(
&state.conf.connectors,
Expand All @@ -38,7 +38,7 @@ pub async fn verify_connector_credentials(
&state,
types::VerifyConnectorData {
connector: *boxed_connector.connector,
connector_auth: req.connector_account_details,
connector_auth: req.connector_account_details.into(),
card_details,
},
)
Expand All @@ -48,7 +48,7 @@ pub async fn verify_connector_credentials(
&state,
types::VerifyConnectorData {
connector: *boxed_connector.connector,
connector_auth: req.connector_account_details,
connector_auth: req.connector_account_details.into(),
card_details,
},
)
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/routes/verify_connector.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use actix_web::{web, HttpRequest, HttpResponse};
use api_models::verify_connector::VerifyConnectorRequest;
use router_env::{instrument, tracing, Flow};

use super::AppState;
use crate::{
core::{api_locking, verify_connector},
services::{self, authentication as auth, authorization::permissions::Permission},
types::api::verify_connector::VerifyConnectorRequest,
};

#[instrument(skip_all, fields(flow = ?Flow::VerifyPaymentConnector))]
Expand Down
76 changes: 76 additions & 0 deletions crates/router/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,82 @@ pub enum ConnectorAuthType {
NoKey,
}

impl From<api_models::admin::ConnectorAuthType> for ConnectorAuthType {
fn from(value: api_models::admin::ConnectorAuthType) -> Self {
match value {
api_models::admin::ConnectorAuthType::TemporaryAuth => Self::TemporaryAuth,
api_models::admin::ConnectorAuthType::HeaderKey { api_key } => {
Self::HeaderKey { api_key }
}
api_models::admin::ConnectorAuthType::BodyKey { api_key, key1 } => {
Self::BodyKey { api_key, key1 }
}
api_models::admin::ConnectorAuthType::SignatureKey {
api_key,
key1,
api_secret,
} => Self::SignatureKey {
api_key,
key1,
api_secret,
},
api_models::admin::ConnectorAuthType::MultiAuthKey {
api_key,
key1,
api_secret,
key2,
} => Self::MultiAuthKey {
api_key,
key1,
api_secret,
key2,
},
api_models::admin::ConnectorAuthType::CurrencyAuthKey { auth_key_map } => {
Self::CurrencyAuthKey { auth_key_map }
}
api_models::admin::ConnectorAuthType::NoKey => Self::NoKey,
}
}
}

impl Into<api_models::admin::ConnectorAuthType> for ConnectorAuthType {
fn into(self) -> api_models::admin::ConnectorAuthType {
match self {
Self::TemporaryAuth => api_models::admin::ConnectorAuthType::TemporaryAuth,
Self::HeaderKey { api_key } => {
api_models::admin::ConnectorAuthType::HeaderKey { api_key }
}
Self::BodyKey { api_key, key1 } => {
api_models::admin::ConnectorAuthType::BodyKey { api_key, key1 }
}
Self::SignatureKey {
api_key,
key1,
api_secret,
} => api_models::admin::ConnectorAuthType::SignatureKey {
api_key,
key1,
api_secret,
},
Self::MultiAuthKey {
api_key,
key1,
api_secret,
key2,
} => api_models::admin::ConnectorAuthType::MultiAuthKey {
api_key,
key1,
api_secret,
key2,
},
Self::CurrencyAuthKey { auth_key_map } => {
api_models::admin::ConnectorAuthType::CurrencyAuthKey { auth_key_map }
}
Self::NoKey => api_models::admin::ConnectorAuthType::NoKey,
}
}
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ConnectorsList {
pub connectors: Vec<String>,
Expand Down
10 changes: 0 additions & 10 deletions crates/router/src/types/api/verify_connector.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
pub mod paypal;
pub mod stripe;

use api_models::enums as api_enums;
use common_utils::events::{ApiEventMetric, ApiEventsType};
use error_stack::{IntoReport, ResultExt};
use router_env as env;

Expand All @@ -15,14 +13,6 @@ use crate::{
AppState,
};

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub struct VerifyConnectorRequest {
pub connector_name: api_enums::Connector,
pub connector_account_details: types::ConnectorAuthType,
}

common_utils::impl_misc_api_event_type!(VerifyConnectorRequest);

#[derive(Clone, Debug)]
pub struct VerifyConnectorData {
pub connector: &'static (dyn types::api::Connector + Sync),
Expand Down

0 comments on commit 7b0ccfb

Please sign in to comment.