Refactor environment module: Rename enviroment to environment and consolidate environment configuration management
- Updated all references from `enviroment` to `environment` across the codebase. - Removed the old `enviroment` module and replaced it with a new `environment` module that includes centralized configuration management. - Enhanced OTP generation to include secure hashing and expiration handling. - Improved CSRF token generation and validation with better error handling. - Cleaned up logging statements in various modules for clarity and consistency. - Updated response formatting to include versioning from Cargo.toml. - Removed unused mock test module from utils.
This commit is contained in:
@@ -3,13 +3,14 @@ use super::UserCacheSchema;
|
||||
use imphnen_entities::{PermissionsQueryDto, RolesDetailQueryDto, UsersDetailQueryDto};
|
||||
use crate::ResourceEnum;
|
||||
use anyhow::{Result, anyhow, bail};
|
||||
use chrono::{Duration, Utc};
|
||||
use chrono::Utc;
|
||||
use surrealdb::sql::Thing;
|
||||
use tracing::instrument;
|
||||
use tracing::info;
|
||||
use async_trait::async_trait;
|
||||
use imphnen_libs::AuthRepositoryTrait;
|
||||
use imphnen_libs::SurrealMemClient;
|
||||
use imphnen_utils::generate_otp::OtpData;
|
||||
|
||||
|
||||
pub struct AuthRepository {
|
||||
@@ -162,15 +163,13 @@ impl AuthRepository {
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(self, email, otp), err)]
|
||||
pub async fn query_store_otp(&self, email: String, otp: u32) -> Result<String> {
|
||||
let expires_at = Utc::now() + Duration::seconds(300);
|
||||
pub async fn query_store_otp(&self, email: String, otp: OtpData) -> Result<String> {
|
||||
let table: String = ResourceEnum::OtpCache.to_string();
|
||||
info!(query = %format!("CREATE {}:{}", table, email), "Executing SurrealDB query");
|
||||
let record: Option<AuthOtpSchema> = self
|
||||
.db
|
||||
.create((table.as_str(), email.as_str()))
|
||||
.content(AuthOtpSchema { otp, expires_at })
|
||||
.content(AuthOtpSchema { otp: otp.code, hash: otp.hash, expires_at: otp.expires_at })
|
||||
.await?;
|
||||
match record {
|
||||
Some(_) => Ok("Success store otp".to_string()),
|
||||
|
||||
@@ -4,5 +4,6 @@ use serde::{Deserialize, Serialize};
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct AuthOtpSchema {
|
||||
pub otp: u32,
|
||||
pub hash: String,
|
||||
pub expires_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::pin::Pin;
|
||||
use std::future::Future;
|
||||
use imphnen_utils as generate_otp;
|
||||
use imphnen_libs::enviroment;
|
||||
use imphnen_libs::environment;
|
||||
use super::{
|
||||
AuthLoginRequestDto, AuthLoginResponsetDto, AuthNewPasswordRequestDto,
|
||||
AuthRefreshTokenRequestDto, AuthRegisterRequestDto, AuthRepository,
|
||||
@@ -310,9 +310,9 @@ impl AuthServiceTrait for AuthService {
|
||||
phone_number: payload.phone_number,
|
||||
};
|
||||
let otp = generate_otp::OtpManager::generate_otp();
|
||||
match auth_repo.query_store_otp(new_user.email.clone(), otp).await {
|
||||
match auth_repo.query_store_otp(new_user.email.clone(), otp.clone()).await {
|
||||
Ok(_) => {
|
||||
let message = format!("your otp code is {otp}");
|
||||
let message = format!("your otp code is {}", otp.code);
|
||||
if let Err(err_send) =
|
||||
send_email(&new_user.email, "OTP Verification", &message)
|
||||
{
|
||||
@@ -383,7 +383,7 @@ impl AuthServiceTrait for AuthService {
|
||||
let auth_repo = AuthRepository::new(state.surrealdb_mem.clone());
|
||||
let _ = auth_repo.query_get_stored_otp(payload.email.clone()).await;
|
||||
let otp = generate_otp::OtpManager::generate_otp();
|
||||
let message = format!("Your OTP code is {otp}");
|
||||
let message = format!("Your OTP code is {}", otp.code);
|
||||
match auth_repo.query_store_otp(payload.email.clone(), otp).await {
|
||||
Ok(_) => match send_email(&payload.email, "OTP Verification", &message) {
|
||||
Ok(_) => common_response(StatusCode::OK, "OTP resent successfully"),
|
||||
@@ -477,7 +477,7 @@ impl AuthServiceTrait for AuthService {
|
||||
}
|
||||
};
|
||||
|
||||
let env = &enviroment::ENV;
|
||||
let env = &environment::ENV;
|
||||
let fe_url = env.fe_url.clone();
|
||||
let message = format!(
|
||||
"You have requested a password reset. Please click the link below to continue: {fe_url}/auth/reset-password?token={token}"
|
||||
|
||||
@@ -7,7 +7,7 @@ use axum::{
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::ToSchema;
|
||||
use std::sync::Arc;
|
||||
use imphnen_libs::enviroment::ENV; // Import ENV
|
||||
use imphnen_libs::environment::ENV; // Import ENV
|
||||
|
||||
use crate::v1::auth::google::google_oauth_service::{AuthRequest, GoogleOauthService, GoogleOauthServiceImpl};
|
||||
use imphnen_entities::error_dto::error::Error;
|
||||
|
||||
@@ -12,7 +12,7 @@ use oauth2::TokenResponse;
|
||||
use tracing::{info, error};
|
||||
|
||||
use imphnen_entities::error_dto::error::Error;
|
||||
use imphnen_libs::{jsonwebtoken::{encode_access_token, encode_refresh_token}, enviroment::Env, AppState};
|
||||
use imphnen_libs::{jsonwebtoken::{encode_access_token, encode_refresh_token}, environment::Env, AppState};
|
||||
use imphnen_utils::{generate_oauth_csrf_token, validate_oauth_csrf_token, validate_csrf_token};
|
||||
use crate::v1::auth::TokenDto;
|
||||
use crate::v1::auth::auth_service::AuthServiceTrait;
|
||||
|
||||
@@ -79,7 +79,7 @@ impl TeamsService {
|
||||
}
|
||||
|
||||
async fn generate_invitation_token() -> String {
|
||||
format!("team_{}_{}", Uuid::new_v4(), OtpManager::generate_otp())
|
||||
format!("team_{}_{}", Uuid::new_v4(), OtpManager::generate_otp().code)
|
||||
}
|
||||
|
||||
async fn get_user_info_with_privacy(
|
||||
|
||||
Reference in New Issue
Block a user