Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/apps/api/Accounts/AccountNamePolicy.cs
blob: 7dce6c59ebb9c3821e2722324dbf0f28327f512e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// 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;

/// <summary>Rules for account names.</summary>
/// <remarks>
/// 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.
/// </remarks>
internal static partial class AccountNamePolicy
{
    /// <summary>Fewest characters an account name may have.</summary>
    public const int MinimumLength = 8;
    /// <summary>Most characters an account name may have.</summary>
    public const int MaximumLength = 128;

    /// <summary>Names nobody may register, in their lookup form.</summary>
    // 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<string> ReservedNames =
        new[] { "LEITWOLF", "PROGMASOFT" }.ToFrozenSet(StringComparer.Ordinal);

    /// <summary>Matches a name of the allowed length, alphabet and first letter.</summary>
    /// <returns>The compiled expression.</returns>
    [GeneratedRegex("^[A-Z][A-Za-z0-9]{7,127}$", RegexOptions.CultureInvariant)]
    private static partial Regex CanonicalPattern();

    /// <summary>Validates an account name and returns its canonical spelling.</summary>
    /// <param name="value">The name as entered; surrounding white space is ignored.</param>
    /// <param name="canonical">The trimmed name. It is meaningful only when the method returns <see langword="true"/>.</param>
    /// <param name="error">Message for the user when the name is refused; empty otherwise.</param>
    /// <returns><see langword="true"/> when the name satisfies every rule and is not reserved.</returns>
    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;
    }

    /// <summary>
    /// Validates a reserved name for the operations command that provisions it: the same rules as
    /// <see cref="TryNormalize"/>, except that the name must be one of the reserved names.
    /// </summary>
    /// <param name="value">The name as entered; surrounding white space is ignored.</param>
    /// <param name="canonical">The trimmed name. It is meaningful only when the method returns <see langword="true"/>.</param>
    /// <param name="error">Message for the operator when the name is refused; empty otherwise.</param>
    /// <returns><see langword="true"/> when the name is a well-formed reserved name.</returns>
    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;
    }

    /// <summary>Returns the key under which a name is compared for uniqueness.</summary>
    /// <param name="canonical">A name that <see cref="TryNormalize"/> accepted.</param>
    /// <returns>The name in upper case.</returns>
    public static string NormalizeForLookup(string canonical) => canonical.ToUpperInvariant();
}