This commit is contained in:
Mateusz Gruszczyński committed 2026-11-04 08:55:53 +01:00
1 parent cdca612a59
commit eb25f8de4a
56 files changed
+7848 -4345

No files matched your search

+28
View File
@@ -99,6 +99,34 @@ pub fn validate_new_password(config: &AppConfig, password: &str) -> AppResult<()
Ok(())
}
/// Generate a random password that satisfies the configured policy
/// (PostfixAdmin `generate_password()`).
pub fn generate_password(config: &AppConfig) -> String {
const LETTERS: &[u8] = b"abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
const DIGITS: &[u8] = b"23456789";
let length = config
.password
.min_length
.max(config.password.min_letters + config.password.min_digits)
.max(12);
let mut rng = rand::rng();
let mut pick = |set: &[u8]| set[(rng.next_u32() as usize) % set.len()] as char;
let mut chars: Vec<char> = Vec::with_capacity(length);
for _ in 0..config.password.min_digits.max(2) {
chars.push(pick(DIGITS));
}
while chars.len() < length {
chars.push(pick(LETTERS));
}
// Fisher-Yates shuffle so the digits are not always in front.
let mut rng = rand::rng();
for i in (1..chars.len()).rev() {
let j = (rng.next_u32() as usize) % (i + 1);
chars.swap(i, j);
}
chars.into_iter().collect()
}
/// Verify an existing PostfixAdmin hash. The format stored in the database wins
/// over the currently configured format whenever it identifies itself.
pub async fn verify(config: &AppConfig, password: &str, stored: &str, username: &str) -> bool {