mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-02 04:04:43 +08:00
feat(users): Create generate recovery codes API (#4708)
This commit is contained in:
@ -1,14 +1,11 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use api_models::user as user_api;
|
||||
use common_utils::{errors::CustomResult, pii};
|
||||
use common_utils::errors::CustomResult;
|
||||
use diesel_models::{enums::UserStatus, user_role::UserRole};
|
||||
use error_stack::ResultExt;
|
||||
use masking::ExposeInterface;
|
||||
use totp_rs::{Algorithm, TOTP};
|
||||
|
||||
use crate::{
|
||||
consts,
|
||||
core::errors::{StorageError, UserErrors, UserResult},
|
||||
routes::AppState,
|
||||
services::{
|
||||
@ -22,6 +19,7 @@ pub mod dashboard_metadata;
|
||||
pub mod password;
|
||||
#[cfg(feature = "dummy_connector")]
|
||||
pub mod sample_data;
|
||||
pub mod two_factor_auth;
|
||||
|
||||
impl UserFromToken {
|
||||
pub async fn get_merchant_account_from_db(
|
||||
@ -193,25 +191,3 @@ pub fn get_token_from_signin_response(resp: &user_api::SignInResponse) -> maskin
|
||||
user_api::SignInResponse::MerchantSelect(data) => data.token.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn generate_default_totp(
|
||||
email: pii::Email,
|
||||
secret: Option<masking::Secret<String>>,
|
||||
) -> UserResult<TOTP> {
|
||||
let secret = secret
|
||||
.map(|sec| totp_rs::Secret::Encoded(sec.expose()))
|
||||
.unwrap_or_else(totp_rs::Secret::generate_secret)
|
||||
.to_bytes()
|
||||
.change_context(UserErrors::InternalServerError)?;
|
||||
|
||||
TOTP::new(
|
||||
Algorithm::SHA1,
|
||||
consts::user::TOTP_DIGITS,
|
||||
consts::user::TOTP_TOLERANCE,
|
||||
consts::user::TOTP_VALIDITY_DURATION_IN_SECONDS,
|
||||
secret,
|
||||
Some(consts::user::TOTP_ISSUER_NAME.to_string()),
|
||||
email.expose().expose(),
|
||||
)
|
||||
.change_context(UserErrors::InternalServerError)
|
||||
}
|
||||
|
||||
52
crates/router/src/utils/user/two_factor_auth.rs
Normal file
52
crates/router/src/utils/user/two_factor_auth.rs
Normal file
@ -0,0 +1,52 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use common_utils::pii;
|
||||
use error_stack::ResultExt;
|
||||
use masking::ExposeInterface;
|
||||
use redis_interface::RedisConnectionPool;
|
||||
use totp_rs::{Algorithm, TOTP};
|
||||
|
||||
use crate::{
|
||||
consts,
|
||||
core::errors::{UserErrors, UserResult},
|
||||
routes::AppState,
|
||||
};
|
||||
|
||||
pub fn generate_default_totp(
|
||||
email: pii::Email,
|
||||
secret: Option<masking::Secret<String>>,
|
||||
) -> UserResult<TOTP> {
|
||||
let secret = secret
|
||||
.map(|sec| totp_rs::Secret::Encoded(sec.expose()))
|
||||
.unwrap_or_else(totp_rs::Secret::generate_secret)
|
||||
.to_bytes()
|
||||
.change_context(UserErrors::InternalServerError)?;
|
||||
|
||||
TOTP::new(
|
||||
Algorithm::SHA1,
|
||||
consts::user::TOTP_DIGITS,
|
||||
consts::user::TOTP_TOLERANCE,
|
||||
consts::user::TOTP_VALIDITY_DURATION_IN_SECONDS,
|
||||
secret,
|
||||
Some(consts::user::TOTP_ISSUER_NAME.to_string()),
|
||||
email.expose().expose(),
|
||||
)
|
||||
.change_context(UserErrors::InternalServerError)
|
||||
}
|
||||
|
||||
pub async fn check_totp_in_redis(state: &AppState, user_id: &str) -> UserResult<bool> {
|
||||
let redis_conn = get_redis_connection(state)?;
|
||||
let key = format!("{}{}", consts::user::TOTP_PREFIX, user_id);
|
||||
redis_conn
|
||||
.exists::<()>(&key)
|
||||
.await
|
||||
.change_context(UserErrors::InternalServerError)
|
||||
}
|
||||
|
||||
fn get_redis_connection(state: &AppState) -> UserResult<Arc<RedisConnectionPool>> {
|
||||
state
|
||||
.store
|
||||
.get_redis_conn()
|
||||
.change_context(UserErrors::InternalServerError)
|
||||
.attach_printable("Failed to get redis connection")
|
||||
}
|
||||
Reference in New Issue
Block a user