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

This commit is contained in:
Apoorv Dixit
2024-07-11 00:33:08 +05:30
committed by GitHub
parent 5e4b0826e6
commit f8f69728b3
3 changed files with 32 additions and 26 deletions

View File

@ -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)?;
.to_not_found_response(UserErrors::InvalidUserAuthMethodOperation)?
} else {
DEFAULT_USER_AUTH_METHOD.clone()
};
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,
);
}
// 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(

View File

@ -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};

View File

@ -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(),
});