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:
@@ -1,13 +1,45 @@
|
||||
//! OTP generation utilities with time-based expiration and secure hashing.
|
||||
//!
|
||||
//! This module provides functionality to generate one-time passwords (OTPs) with
|
||||
//! a 5-minute expiration time and SHA256 hashing for secure storage and validation,
|
||||
//! preventing replay attacks.
|
||||
|
||||
use rand::{Rng, rng};
|
||||
use sha2::{Sha256, Digest};
|
||||
use chrono::{DateTime, Utc, Duration};
|
||||
|
||||
/// Represents an OTP with its code, hashed value and expiration time
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct OtpData {
|
||||
pub code: u32,
|
||||
pub hash: String,
|
||||
pub expires_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
pub struct OtpManager;
|
||||
|
||||
impl OtpManager {
|
||||
pub fn generate_otp() -> u32 {
|
||||
rng().random_range(100_000..1_000_000)
|
||||
}
|
||||
/// Generates a new OTP with a 5-minute expiration and SHA256 hash for secure storage
|
||||
pub fn generate_otp() -> OtpData {
|
||||
let code = rng().random_range(100_000..1_000_000);
|
||||
let otp_str = code.to_string();
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(otp_str.as_bytes());
|
||||
let hash = format!("{:x}", hasher.finalize());
|
||||
let expires_at = Utc::now() + Duration::minutes(5);
|
||||
OtpData { code, hash, expires_at }
|
||||
}
|
||||
|
||||
pub fn validate_otp(stored_otp: u32, user_otp: u32) -> bool {
|
||||
stored_otp == user_otp
|
||||
}
|
||||
/// Validates the user-provided OTP against the stored OTP data
|
||||
/// Checks both hash match and expiration
|
||||
pub fn validate_otp(stored: &OtpData, user_otp: u32) -> bool {
|
||||
if Utc::now() > stored.expires_at {
|
||||
return false;
|
||||
}
|
||||
let user_otp_str = user_otp.to_string();
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(user_otp_str.as_bytes());
|
||||
let user_hash = format!("{:x}", hasher.finalize());
|
||||
user_hash == stored.hash
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user