| | | 1 | | namespace Kestrun.Claims; |
| | | 2 | | |
| | | 3 | | /// <summary>Represents one claim must equal rule.</summary> |
| | | 4 | | /// <remarks> |
| | | 5 | | /// This is used to define authorization policies that require a specific claim type |
| | | 6 | | /// with specific allowed values. |
| | | 7 | | /// It is typically used in conjunction with <see cref="ClaimPolicyConfig"/> to define |
| | | 8 | | /// multiple policies. |
| | | 9 | | /// </remarks> |
| | | 10 | | public sealed record ClaimRule |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// The claim type required by this rule. |
| | | 14 | | /// </summary> |
| | 17 | 15 | | public string ClaimType { get; } |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Description of the claim rule. |
| | | 19 | | /// </summary> |
| | 27 | 20 | | public string? Description { get; set; } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Allowed values for the claim. Exposed as a read-only sequence. |
| | | 24 | | /// </summary> |
| | 17 | 25 | | public IReadOnlyList<string> AllowedValues { get; } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Constructs a rule from a claim type and one or more allowed values. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="claimType">The claim type required by this rule.</param> |
| | | 31 | | /// <param name="description">Description of the claim rule.</param> |
| | | 32 | | /// <param name="allowedValues">Allowed values for the claim.</param> |
| | 27 | 33 | | public ClaimRule(string claimType, string? description, params string[] allowedValues) |
| | | 34 | | { |
| | 27 | 35 | | ClaimType = claimType ?? throw new ArgumentNullException(nameof(claimType)); |
| | 27 | 36 | | Description = description; |
| | | 37 | | // Make a defensive copy to avoid exposing caller-owned mutable arrays. |
| | 27 | 38 | | AllowedValues = (allowedValues is null) ? Array.Empty<string>() : Array.AsReadOnly((string[])allowedValues.Clone |
| | 27 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Constructs a rule from a claim type and an explicit read-only list of values. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <param name="claimType">The claim type required by this rule.</param> |
| | | 45 | | /// <param name="description">Description of the claim rule.</param> |
| | | 46 | | /// <param name="allowedValues">Allowed values for the claim.</param> |
| | 0 | 47 | | public ClaimRule(string claimType, string? description, IReadOnlyList<string> allowedValues) |
| | | 48 | | { |
| | 0 | 49 | | ClaimType = claimType ?? throw new ArgumentNullException(nameof(claimType)); |
| | 0 | 50 | | Description = description; |
| | 0 | 51 | | AllowedValues = allowedValues ?? []; |
| | 0 | 52 | | } |
| | | 53 | | /// <summary> |
| | | 54 | | /// Constructs a rule from a claim type and one or more allowed values without description. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name="claimType">The claim type required by this rule.</param> |
| | | 57 | | /// <param name="allowedValues">Allowed values for the claim.</param> |
| | | 58 | | public ClaimRule(string claimType, IReadOnlyList<string> allowedValues) |
| | 0 | 59 | | : this(claimType, null, allowedValues) { } |
| | | 60 | | } |