refactor(router): Refactored Authentication (#327)

This commit is contained in:
Rachit Naithani
2023-01-10 15:06:34 +05:30
committed by GitHub
parent 98816c05bd
commit 59573efe91
17 changed files with 363 additions and 384 deletions

View File

@ -8,7 +8,7 @@ use crate::{
compatibility::{stripe::errors, wrap},
core::customers,
routes,
services::api,
services::{api, authentication as auth},
types::api::customers as customer_types,
};
@ -44,7 +44,7 @@ pub async fn customer_create(
|state, merchant_account, req| {
customers::create_customer(&*state.store, merchant_account, req)
},
api::MerchantAuthentication::ApiKey,
&auth::ApiKeyAuth,
)
.await
}
@ -75,7 +75,7 @@ pub async fn customer_retrieve(
|state, merchant_account, req| {
customers::retrieve_customer(&*state.store, merchant_account, req)
},
api::MerchantAuthentication::ApiKey,
&auth::ApiKeyAuth,
)
.await
}
@ -115,7 +115,7 @@ pub async fn customer_update(
|state, merchant_account, req| {
customers::update_customer(&*state.store, merchant_account, req)
},
api::MerchantAuthentication::ApiKey,
&auth::ApiKeyAuth,
)
.await
}
@ -144,7 +144,7 @@ pub async fn customer_delete(
&req,
payload,
customers::delete_customer,
api::MerchantAuthentication::ApiKey,
&auth::ApiKeyAuth,
)
.await
}

View File

@ -9,7 +9,7 @@ use crate::{
compatibility::{stripe::errors, wrap},
core::payments,
routes,
services::api,
services::{api, authentication as auth},
types::api::{self as api_types},
};
@ -58,7 +58,7 @@ pub async fn payment_intents_create(
payments::CallConnectorAction::Trigger,
)
},
api::MerchantAuthentication::ApiKey,
&auth::ApiKeyAuth,
)
.await
}
@ -78,11 +78,10 @@ pub async fn payment_intents_retrieve(
param: None,
};
let auth_type = match api::get_auth_type(&req) {
Ok(auth_type) => auth_type,
let (auth_type, auth_flow) = match auth::get_auth_type_and_flow(req.headers()) {
Ok(auth) => auth,
Err(err) => return api::log_and_return_error_response(report!(err)),
};
let auth_flow = api::get_auth_flow(&auth_type);
wrap::compatibility_api_wrap::<
_,
@ -107,7 +106,7 @@ pub async fn payment_intents_retrieve(
payments::CallConnectorAction::Trigger,
)
},
auth_type,
&*auth_type,
)
.await
}
@ -138,12 +137,11 @@ pub async fn payment_intents_update(
payload.payment_id = Some(api_types::PaymentIdType::PaymentIntentId(payment_id));
let auth_type;
(payload, auth_type) = match api::get_auth_type_and_check_client_secret(&req, payload) {
Ok(values) => values,
Err(err) => return api::log_and_return_error_response(err),
let (auth_type, auth_flow) = match auth::get_auth_type_and_flow(req.headers()) {
Ok(auth) => auth,
Err(err) => return api::log_and_return_error_response(report!(err)),
};
let auth_flow = api::get_auth_flow(&auth_type);
wrap::compatibility_api_wrap::<
_,
_,
@ -168,7 +166,7 @@ pub async fn payment_intents_update(
payments::CallConnectorAction::Trigger,
)
},
auth_type,
&*auth_type,
)
.await
}
@ -200,12 +198,12 @@ pub async fn payment_intents_confirm(
payload.payment_id = Some(api_types::PaymentIdType::PaymentIntentId(payment_id));
payload.confirm = Some(true);
let auth_type;
(payload, auth_type) = match api::get_auth_type_and_check_client_secret(&req, payload) {
Ok(values) => values,
Err(err) => return api::log_and_return_error_response(err),
};
let auth_flow = api::get_auth_flow(&auth_type);
let (auth_type, auth_flow) =
match auth::check_client_secret_and_get_auth(req.headers(), &payload) {
Ok(auth) => auth,
Err(err) => return api::log_and_return_error_response(err),
};
wrap::compatibility_api_wrap::<
_,
_,
@ -230,7 +228,7 @@ pub async fn payment_intents_confirm(
payments::CallConnectorAction::Trigger,
)
},
auth_type,
&*auth_type,
)
.await
}
@ -280,7 +278,7 @@ pub async fn payment_intents_capture(
payments::CallConnectorAction::Trigger,
)
},
api::MerchantAuthentication::ApiKey,
&auth::ApiKeyAuth,
)
.await
}
@ -307,11 +305,11 @@ pub async fn payment_intents_cancel(
let mut payload: payment_types::PaymentsCancelRequest = stripe_payload.into();
payload.payment_id = payment_id;
let auth_type = match api::get_auth_type(&req) {
Ok(values) => values,
Err(err) => return api::log_and_return_error_response(err),
let (auth_type, auth_flow) = match auth::get_auth_type_and_flow(req.headers()) {
Ok(auth) => auth,
Err(err) => return api::log_and_return_error_response(report!(err)),
};
let auth_flow = api::get_auth_flow(&auth_type);
wrap::compatibility_api_wrap::<
_,
_,
@ -335,7 +333,7 @@ pub async fn payment_intents_cancel(
payments::CallConnectorAction::Trigger,
)
},
auth_type,
&*auth_type,
)
.await
}
@ -366,7 +364,7 @@ pub async fn payment_intent_list(
|state, merchant_account, req| {
payments::list_payments(&*state.store, merchant_account, req)
},
api::MerchantAuthentication::ApiKey,
&auth::ApiKeyAuth,
)
.await
}

View File

@ -7,7 +7,7 @@ use crate::{
compatibility::{stripe::errors, wrap},
core::refunds,
routes,
services::api,
services::authentication as auth,
types::api::refunds as refund_types,
};
@ -34,7 +34,7 @@ pub async fn refund_create(
&req,
create_refund_req,
refunds::refund_create_core,
api::MerchantAuthentication::ApiKey,
&auth::ApiKeyAuth,
)
.await
}
@ -67,7 +67,7 @@ pub async fn refund_retrieve(
refunds::refund_retrieve_core,
)
},
api::MerchantAuthentication::ApiKey,
&auth::ApiKeyAuth,
)
.await
}
@ -99,7 +99,7 @@ pub async fn refund_update(
|state, merchant_account, req| {
refunds::refund_update_core(&*state.store, merchant_account, &refund_id, req)
},
api::MerchantAuthentication::ApiKey,
&auth::ApiKeyAuth,
)
.await
}

View File

@ -9,7 +9,7 @@ use crate::{
compatibility::{stripe::errors, wrap},
core::payments,
routes,
services::api,
services::{api, authentication as auth},
types::api as api_types,
};
@ -55,7 +55,7 @@ pub async fn setup_intents_create(
payments::CallConnectorAction::Trigger,
)
},
api::MerchantAuthentication::ApiKey,
&auth::ApiKeyAuth,
)
.await
}
@ -75,11 +75,10 @@ pub async fn setup_intents_retrieve(
param: None,
};
let auth_type = match api::get_auth_type(&req) {
Ok(auth_type) => auth_type,
let (auth_type, auth_flow) = match auth::get_auth_type_and_flow(req.headers()) {
Ok(auth) => auth,
Err(err) => return api::log_and_return_error_response(report!(err)),
};
let auth_flow = api::get_auth_flow(&auth_type);
wrap::compatibility_api_wrap::<
_,
@ -104,7 +103,7 @@ pub async fn setup_intents_retrieve(
payments::CallConnectorAction::Trigger,
)
},
auth_type,
&*auth_type,
)
.await
}
@ -131,12 +130,12 @@ pub async fn setup_intents_update(
let mut payload: payment_types::PaymentsRequest = stripe_payload.into();
payload.payment_id = Some(api_types::PaymentIdType::PaymentIntentId(setup_id));
let auth_type;
(payload, auth_type) = match api::get_auth_type_and_check_client_secret(&req, payload) {
Ok(values) => values,
Err(err) => return api::log_and_return_error_response(err),
};
let auth_flow = api::get_auth_flow(&auth_type);
let (auth_type, auth_flow) =
match auth::check_client_secret_and_get_auth(req.headers(), &payload) {
Ok(auth) => auth,
Err(err) => return api::log_and_return_error_response(err),
};
wrap::compatibility_api_wrap::<
_,
_,
@ -161,7 +160,7 @@ pub async fn setup_intents_update(
payments::CallConnectorAction::Trigger,
)
},
auth_type,
&*auth_type,
)
.await
}
@ -189,12 +188,12 @@ pub async fn setup_intents_confirm(
payload.payment_id = Some(api_types::PaymentIdType::PaymentIntentId(setup_id));
payload.confirm = Some(true);
let auth_type;
(payload, auth_type) = match api::get_auth_type_and_check_client_secret(&req, payload) {
Ok(values) => values,
Err(err) => return api::log_and_return_error_response(err),
};
let auth_flow = api::get_auth_flow(&auth_type);
let (auth_type, auth_flow) =
match auth::check_client_secret_and_get_auth(req.headers(), &payload) {
Ok(auth) => auth,
Err(err) => return api::log_and_return_error_response(err),
};
wrap::compatibility_api_wrap::<
_,
_,
@ -219,7 +218,7 @@ pub async fn setup_intents_confirm(
payments::CallConnectorAction::Trigger,
)
},
auth_type,
&*auth_type,
)
.await
}

View File

@ -8,28 +8,25 @@ use serde::Serialize;
use crate::{
core::errors::{self, RouterResult},
routes,
services::{api, logger},
types::storage,
services::{api, authentication as auth, logger},
};
#[instrument(skip(request, payload, state, func))]
pub async fn compatibility_api_wrap<'a, 'b, A, T, Q, F, Fut, S, E>(
#[instrument(skip(request, payload, state, func, api_authentication))]
pub async fn compatibility_api_wrap<'a, 'b, U, T, Q, F, Fut, S, E>(
state: &'b routes::AppState,
request: &'a HttpRequest,
payload: T,
func: F,
api_authentication: A,
api_authentication: &dyn auth::AuthenticateAndFetch<U>,
) -> HttpResponse
where
A: Into<api::ApiAuthentication<'a>> + std::fmt::Debug,
F: Fn(&'b routes::AppState, storage::MerchantAccount, T) -> Fut,
F: Fn(&'b routes::AppState, U, T) -> Fut,
Fut: Future<Output = RouterResult<api::BachResponse<Q>>>,
Q: Serialize + std::fmt::Debug + 'a,
S: From<Q> + Serialize,
E: From<errors::ApiErrorResponse> + Serialize + error_stack::Context + actix_web::ResponseError,
T: std::fmt::Debug,
{
let api_authentication = api_authentication.into();
let resp = api::server_wrap_util(state, request, payload, func, api_authentication).await;
match resp {
Ok(api::BachResponse::Json(router_resp)) => {