refactor(users): Changes for Home and Signout APIs for TOTP Redis flows (#4851)

Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
Mani Chandra
2024-06-03 19:40:13 +05:30
committed by GitHub
parent 0cbb2928bd
commit d242850b63
3 changed files with 32 additions and 0 deletions

View File

@ -165,7 +165,10 @@ pub struct GetUserDetailsResponse {
#[serde(skip_serializing)] #[serde(skip_serializing)]
pub user_id: String, pub user_id: String,
pub org_id: String, pub org_id: String,
pub is_two_factor_auth_setup: bool,
pub recovery_codes_left: Option<usize>,
} }
#[derive(Debug, serde::Deserialize, serde::Serialize)] #[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct GetUserRoleDetailsRequest { pub struct GetUserRoleDetailsRequest {
pub email: pii::Email, pub email: pii::Email,

View File

@ -94,6 +94,8 @@ pub async fn get_user_details(
verification_days_left, verification_days_left,
role_id: user_from_token.role_id, role_id: user_from_token.role_id,
org_id: user_from_token.org_id, org_id: user_from_token.org_id,
is_two_factor_auth_setup: user.get_totp_status() == TotpStatus::Set,
recovery_codes_left: user.get_recovery_codes().map(|codes| codes.len()),
}, },
)) ))
} }
@ -328,6 +330,10 @@ pub async fn signout(
state: SessionState, state: SessionState,
user_from_token: auth::UserFromToken, user_from_token: auth::UserFromToken,
) -> UserResponse<()> { ) -> UserResponse<()> {
tfa_utils::delete_totp_from_redis(&state, &user_from_token.user_id).await?;
tfa_utils::delete_recovery_code_from_redis(&state, &user_from_token.user_id).await?;
tfa_utils::delete_totp_secret_from_redis(&state, &user_from_token.user_id).await?;
auth::blacklist::insert_user_in_blacklist(&state, &user_from_token.user_id).await?; auth::blacklist::insert_user_in_blacklist(&state, &user_from_token.user_id).await?;
auth::cookies::remove_cookie_response() auth::cookies::remove_cookie_response()
} }

View File

@ -116,3 +116,26 @@ pub async fn insert_recovery_code_in_redis(state: &SessionState, user_id: &str)
.await .await
.change_context(UserErrors::InternalServerError) .change_context(UserErrors::InternalServerError)
} }
pub async fn delete_totp_from_redis(state: &SessionState, user_id: &str) -> UserResult<()> {
let redis_conn = super::get_redis_connection(state)?;
let key = format!("{}{}", consts::user::REDIS_TOTP_PREFIX, user_id);
redis_conn
.delete_key(&key)
.await
.change_context(UserErrors::InternalServerError)
.map(|_| ())
}
pub async fn delete_recovery_code_from_redis(
state: &SessionState,
user_id: &str,
) -> UserResult<()> {
let redis_conn = super::get_redis_connection(state)?;
let key = format!("{}{}", consts::user::REDIS_RECOVERY_CODE_PREFIX, user_id);
redis_conn
.delete_key(&key)
.await
.change_context(UserErrors::InternalServerError)
.map(|_| ())
}