use super::defaults::*;
use crate::Identifier;
use crate::Validatable;
use crate::error::IggyError;
use secrecy::{ExposeSecret, SecretString};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ChangePassword {
#[serde(skip)]
pub user_id: Identifier,
#[serde(serialize_with = "crate::utils::serde_secret::serialize_secret")]
pub current_password: SecretString,
#[serde(serialize_with = "crate::utils::serde_secret::serialize_secret")]
pub new_password: SecretString,
}
impl Default for ChangePassword {
fn default() -> Self {
ChangePassword {
user_id: Identifier::default(),
current_password: SecretString::from("secret"),
new_password: SecretString::from("topsecret"),
}
}
}
impl Validatable<IggyError> for ChangePassword {
fn validate(&self) -> Result<(), IggyError> {
let current_password = self.current_password.expose_secret();
if current_password.is_empty()
|| current_password.len() > MAX_PASSWORD_LENGTH
|| current_password.len() < MIN_PASSWORD_LENGTH
{
return Err(IggyError::InvalidPassword);
}
let new_password = self.new_password.expose_secret();
if new_password.is_empty()
|| new_password.len() > MAX_PASSWORD_LENGTH
|| new_password.len() < MIN_PASSWORD_LENGTH
{
return Err(IggyError::InvalidPassword);
}
Ok(())
}
}