Improve and expand kdf_upgrade (#7855)

Co-authored-by: Timshel <timshel@users.noreply.github.com>
This commit is contained in:
TimshelandTimshel authored and GitHub committed 2026-11-23 01:42:58 +01:00
1 parent c6e7da4b5c
commit 010c269e37
12 files changed
+68 -45

No files matched your search

+30
View File
@@ -2,6 +2,7 @@ use chrono::{NaiveDateTime, TimeDelta, Utc};
use derive_more::{AsRef, Deref, Display, From};
use diesel::prelude::*;
use serde_json::Value;
use std::cmp;
use crate::{
CONFIG,
@@ -248,6 +249,7 @@ impl User {
allow_next_route: Option<Vec<String>>,
conn: &DbConn,
) -> EmptyResult {
self.password_iterations = cmp::max(self.password_iterations, CONFIG.password_iterations());
self.password_hash =
crypto::hash_password(password.as_bytes(), &self.salt, self.password_iterations.cast_unsigned());
@@ -477,6 +479,34 @@ impl User {
}
}
pub async fn check_persist_kdf_upgrade(&mut self, pwd_hash: &str, conn: &DbConn) -> EmptyResult {
if self.password_iterations < CONFIG.password_iterations() {
let old_iteration = self.password_iterations;
let old_hash = self.password_hash.clone();
self.set_password(pwd_hash, None, false, None, conn).await?;
conn.run(move |conn| {
diesel::update(
users::table
.filter(users::uuid.eq(&self.uuid))
.filter(users::password_iterations.eq(old_iteration))
.filter(users::password_hash.eq(old_hash)),
)
.set((
users::updated_at.eq(Utc::now().naive_utc()),
users::password_iterations.eq(&self.password_iterations),
users::password_hash.eq(&self.password_hash),
))
.execute(conn)
.map_res("Error updating user KDF iterations")
})
.await
} else {
Ok(())
}
}
pub async fn delete(&self, conn: &DbConn) -> EmptyResult {
for member in Membership::find_confirmed_by_user(&self.uuid, conn).await {
if member.atype == MembershipType::Owner