Implement password change functionality

This commit is contained in:
Nadja Reitzenstein
2022-07-11 12:27:51 +02:00
parent 1fc13405e8
commit 70c94feced
2 changed files with 39 additions and 11 deletions

View File

@ -27,6 +27,12 @@ pub struct User {
pub userdata: UserData,
}
fn hash_pw(pw: &[u8]) -> argon2::Result<String> {
let config = argon2::Config::default();
let salt: [u8; 16] = rand::random();
argon2::hash_encoded(pw, &salt, &config)
}
impl User {
pub fn check_password(&self, pwd: &[u8]) -> miette::Result<bool> {
if let Some(ref encoded) = self.userdata.passwd {
@ -39,9 +45,7 @@ impl User {
}
pub fn new_with_plain_pw(username: &str, password: impl AsRef<[u8]>) -> Self {
let config = argon2::Config::default();
let salt: [u8; 16] = rand::random();
let hash = argon2::hash_encoded(password.as_ref(), &salt, &config)
let hash = hash_pw(password.as_ref())
.expect(&format!("Failed to hash password for {}: ", username));
tracing::debug!("Hashed pw for {} to {}", username, hash);
@ -53,6 +57,13 @@ impl User {
},
}
}
pub fn set_pw(&mut self, password: impl AsRef<[u8]>) {
self.userdata.passwd = Some(hash_pw(password.as_ref()).expect(&format!(
"failed to update hashed password for {}",
&self.id
)));
}
}
#[derive(