use crate::crypto::password::{hash_password, verify_password};
use crate::error::RustAuthError;
use crate::options::{DeploymentMode, EmailPasswordOptions, PasswordOptions, RustAuthOptions};
pub fn fast_hash_password(password: &str) -> Result<String, RustAuthError> {
Ok(format!("test-password:{password}"))
}
pub fn fast_verify_password(hash: &str, password: &str) -> Result<bool, RustAuthError> {
Ok(hash == format!("test-password:{password}"))
}
pub fn real_password_options() -> PasswordOptions {
PasswordOptions::default()
.hash_password(hash_password)
.verify_password(verify_password)
}
pub fn with_integration_test_defaults(mut options: RustAuthOptions) -> RustAuthOptions {
if options.mode != DeploymentMode::Production {
options.mode = DeploymentMode::Development;
}
if !options.email_password.enabled {
options.email_password = EmailPasswordOptions::new().enabled(true);
}
apply_fast_password_defaults(options)
}
pub fn apply_fast_password_defaults(mut options: RustAuthOptions) -> RustAuthOptions {
if options.password.hash_password.is_none() {
options.password = options
.password
.hash_password(fast_hash_password)
.verify_password(fast_verify_password);
}
options
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fast_password_callbacks_roundtrip() -> Result<(), RustAuthError> {
let hash = fast_hash_password("secret123")?;
assert!(fast_verify_password(&hash, "secret123")?);
assert!(!fast_verify_password(&hash, "wrong")?);
Ok(())
}
}