// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1 namespace Progmasoft.Progmaweb.Api.Accounts; /// Rules for passwords. /// /// Only length is constrained, so long passphrases are welcome. The upper bound keeps hashing cost bounded for a /// request that an anonymous caller can send. /// internal static class PasswordPolicy { /// Fewest characters a password may have. public const int MinimumLength = 12; /// Most characters a password may have. public const int MaximumLength = 256; /// Validates a new password. /// The password in plain text. /// Message for the user when the password is refused; empty otherwise. /// when the password may be used. public static bool TryValidate(string? password, out string error) { if (password is null || password.Length < MinimumLength) { error = $"Passwords must contain at least {MinimumLength} characters."; return false; } if (password.Length > MaximumLength) { error = $"Passwords cannot exceed {MaximumLength} characters."; return false; } if (password.Contains('\0')) { error = "Passwords cannot contain a null character."; return false; } error = string.Empty; return true; } }