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

refactor(user_auth_method): populate default user auth method (#5257)

Apoorv Dixitf8f69728b330
diff --git a/crates/router/src/core/user.rs b/crates/router/src/core/user.rs
index 49993f3873..2abcb5ea78 100644
--- a/crates/router/src/core/user.rs
+++ b/crates/router/src/core/user.rs
@@ -27,6 +27,7 @@ use super::errors::{StorageErrorExt, UserErrors, UserResponse, UserResult};
use crate::services::email::types as email_types;
use crate::{
consts,
+ db::domain::user_authentication_method::DEFAULT_USER_AUTH_METHOD,
routes::{app::ReqState, SessionState},
services::{authentication as auth, authorization::roles, openidconnect, ApplicationResponse},
types::{domain, transformers::ForeignInto},
@@ -2306,38 +2307,25 @@ pub async fn terminate_auth_select(
.change_context(UserErrors::InternalServerError)?
.into();
- if let Some(id) = &req.id {
- let user_authentication_method = state
+ let user_authentication_method = if let Some(id) = &req.id {
+ state
.store
.get_user_authentication_method_by_id(id)
.await
- .to_not_found_response(UserErrors::InvalidUserAuthMethodOperation)?;
-
- let current_flow =
- domain::CurrentFlow::new(user_token, domain::SPTFlow::AuthSelect.into())?;
- let mut next_flow = current_flow.next(user_from_db.clone(), &state).await?;
-
- // Skip SSO if continue with password(TOTP)
- if next_flow.get_flow() == domain::UserFlow::SPTFlow(domain::SPTFlow::SSO)
- && !utils::user::is_sso_auth_type(&user_authentication_method.auth_type)
- {
- next_flow = next_flow.skip(user_from_db, &state).await?;
- }
- let token = next_flow.get_token(&state).await?;
-
- return auth::cookies::set_cookie_response(
- user_api::TokenResponse {
- token: token.clone(),
- token_type: next_flow.get_flow().into(),
- },
- token,
- );
- }
+ .to_not_found_response(UserErrors::InvalidUserAuthMethodOperation)?
+ } else {
+ DEFAULT_USER_AUTH_METHOD.clone()
+ };
- // Giving totp token for hyperswtich users when no id is present in the request body
let current_flow = domain::CurrentFlow::new(user_token, domain::SPTFlow::AuthSelect.into())?;
let mut next_flow = current_flow.next(user_from_db.clone(), &state).await?;
- next_flow = next_flow.skip(user_from_db, &state).await?;
+
+ // Skip SSO if continue with password(TOTP)
+ if next_flow.get_flow() == domain::UserFlow::SPTFlow(domain::SPTFlow::SSO)
+ && !utils::user::is_sso_auth_type(&user_authentication_method.auth_type)
+ {
+ next_flow = next_flow.skip(user_from_db, &state).await?;
+ }
let token = next_flow.get_token(&state).await?;
auth::cookies::set_cookie_response(
diff --git a/crates/router/src/types/domain/user.rs b/crates/router/src/types/domain/user.rs
index 4c48c21204..d5100c89c1 100644
--- a/crates/router/src/types/domain/user.rs
+++ b/crates/router/src/types/domain/user.rs
@@ -37,6 +37,7 @@ use crate::{
pub mod dashboard_metadata;
pub mod decision_manager;
pub use decision_manager::*;
+pub mod user_authentication_method;
use super::{types as domain_types, UserKeyStore};
diff --git a/crates/router/src/types/domain/user/user_authentication_method.rs b/crates/router/src/types/domain/user/user_authentication_method.rs
new file mode 100644
index 0000000000..570e144961
--- /dev/null
+++ b/crates/router/src/types/domain/user/user_authentication_method.rs
@@ -0,0 +1,17 @@
+use common_enums::{Owner, UserAuthType};
+use diesel_models::UserAuthenticationMethod;
+use once_cell::sync::Lazy;
+
+pub static DEFAULT_USER_AUTH_METHOD: Lazy<UserAuthenticationMethod> =
+ Lazy::new(|| UserAuthenticationMethod {
+ id: String::from("hyperswitch_default"),
+ auth_id: String::from("hyperswitch"),
+ owner_id: String::from("hyperswitch"),
+ owner_type: Owner::Tenant,
+ auth_type: UserAuthType::Password,
+ private_config: None,
+ public_config: None,
+ allow_signup: true,
+ created_at: common_utils::date_time::now(),
+ last_modified_at: common_utils::date_time::now(),
+ });