// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1 using System.Collections.Frozen; using System.Text.RegularExpressions; namespace Progmasoft.Progmaweb.Api.Accounts; /// Rules for account names. /// /// A name is 8 to 128 ASCII letters or digits and begins with an upper-case letter. Names are unique without regard /// to case, so two names that differ only in case cannot both exist, while each keeps the spelling its owner chose. /// The same name is the ViGet publisher name. /// internal static partial class AccountNamePolicy { /// Fewest characters an account name may have. public const int MinimumLength = 8; /// Most characters an account name may have. public const int MaximumLength = 128; /// Names nobody may register, in their lookup form. // These names identify Progmasoft itself and the initial administrative identity. They are provisioned only by // trusted operations tooling, never by the public registration endpoint. private static readonly FrozenSet ReservedNames = new[] { "LEITWOLF", "PROGMASOFT" }.ToFrozenSet(StringComparer.Ordinal); /// Matches a name of the allowed length, alphabet and first letter. /// The compiled expression. [GeneratedRegex("^[A-Z][A-Za-z0-9]{7,127}$", RegexOptions.CultureInvariant)] private static partial Regex CanonicalPattern(); /// Validates an account name and returns its canonical spelling. /// The name as entered; surrounding white space is ignored. /// The trimmed name. It is meaningful only when the method returns . /// Message for the user when the name is refused; empty otherwise. /// when the name satisfies every rule and is not reserved. public static bool TryNormalize(string? value, out string canonical, out string error) { canonical = value?.Trim() ?? string.Empty; if (canonical.Length is < MinimumLength or > MaximumLength) { error = $"Account names must contain {MinimumLength}–{MaximumLength} characters."; return false; } if (!CanonicalPattern().IsMatch(canonical)) { error = "Account names must begin with an uppercase ASCII letter and contain only ASCII letters or digits."; return false; } if (ReservedNames.Contains(NormalizeForLookup(canonical))) { error = "That account name is reserved."; return false; } error = string.Empty; return true; } /// /// Validates a reserved name for the operations command that provisions it: the same rules as /// , except that the name must be one of the reserved names. /// /// The name as entered; surrounding white space is ignored. /// The trimmed name. It is meaningful only when the method returns . /// Message for the operator when the name is refused; empty otherwise. /// when the name is a well-formed reserved name. public static bool TryNormalizeReserved(string? value, out string canonical, out string error) { canonical = value?.Trim() ?? string.Empty; if (!CanonicalPattern().IsMatch(canonical)) { error = "Account names must begin with an uppercase ASCII letter and contain only ASCII letters or digits."; return false; } if (!ReservedNames.Contains(NormalizeForLookup(canonical))) { error = $"'{canonical}' is not a reserved name; it is registered through the account site."; return false; } error = string.Empty; return true; } /// Returns the key under which a name is compared for uniqueness. /// A name that accepted. /// The name in upper case. public static string NormalizeForLookup(string canonical) => canonical.ToUpperInvariant(); }