| | | 1 | | namespace Kestrun.Claims; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Builder for defining claim-based authorization policies. |
| | | 5 | | /// </summary> |
| | | 6 | | public sealed class ClaimPolicyBuilder |
| | | 7 | | { |
| | 15 | 8 | | private readonly Dictionary<string, ClaimRule> _policies = new(StringComparer.OrdinalIgnoreCase); |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Adds a new policy with a required claim rule. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="policyName">The name of the policy.</param> |
| | | 14 | | /// <param name="claimType">The required claim type.</param> |
| | | 15 | | /// <param name="description">Description of the claim rule.</param> |
| | | 16 | | /// <param name="allowedValues">Allowed values for the claim.</param> |
| | | 17 | | /// <returns>The current builder instance.</returns> |
| | | 18 | | public ClaimPolicyBuilder AddPolicy(string policyName, string claimType, string description, params string[] allowed |
| | | 19 | | { |
| | 17 | 20 | | ArgumentException.ThrowIfNullOrWhiteSpace(policyName); |
| | 15 | 21 | | ArgumentException.ThrowIfNullOrWhiteSpace(claimType); |
| | 13 | 22 | | if (allowedValues is null || allowedValues.Length == 0) |
| | | 23 | | { |
| | 2 | 24 | | throw new ArgumentException("At least one allowed value must be specified.", nameof(allowedValues)); |
| | | 25 | | } |
| | | 26 | | |
| | 11 | 27 | | _policies[policyName] = new ClaimRule(claimType, description, allowedValues); |
| | 11 | 28 | | return this; |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Adds a new policy with a required claim rule using a <see cref="UserIdentityClaim"/>. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="policyName">The name of the policy.</param> |
| | | 35 | | /// <param name="claimType">The required <see cref="UserIdentityClaim"/> type.</param> |
| | | 36 | | /// <param name="description">Description of the claim rule.</param> |
| | | 37 | | /// <param name="allowedValues">Allowed values for the claim.</param> |
| | | 38 | | /// <returns>The current builder instance.</returns> |
| | | 39 | | public ClaimPolicyBuilder AddPolicy(string policyName, UserIdentityClaim claimType, string? description, params stri |
| | | 40 | | { |
| | 4 | 41 | | ArgumentException.ThrowIfNullOrWhiteSpace(policyName); |
| | 4 | 42 | | if (allowedValues is null || allowedValues.Length == 0) |
| | | 43 | | { |
| | 2 | 44 | | throw new ArgumentException("At least one allowed value must be specified.", nameof(allowedValues)); |
| | | 45 | | } |
| | | 46 | | |
| | 2 | 47 | | _policies[policyName] = new ClaimRule(claimType.ToClaimUri(), description, allowedValues); |
| | 2 | 48 | | return this; |
| | | 49 | | } |
| | | 50 | | /// <summary> |
| | | 51 | | /// Adds a prebuilt claim rule under a policy name. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="policyName">The name of the policy.</param> |
| | | 54 | | /// <param name="rule">The claim rule to associate with the policy.</param> |
| | | 55 | | /// <param name="description">Description of the claim rule.</param> |
| | | 56 | | /// <returns>The current builder instance.</returns> |
| | | 57 | | public ClaimPolicyBuilder AddPolicy(string policyName, ClaimRule rule, string? description = null) |
| | | 58 | | { |
| | 4 | 59 | | ArgumentException.ThrowIfNullOrWhiteSpace(policyName); |
| | 2 | 60 | | ArgumentNullException.ThrowIfNull(rule); |
| | 1 | 61 | | if (description is not null) |
| | | 62 | | { |
| | 0 | 63 | | rule.Description = description; |
| | | 64 | | } |
| | 1 | 65 | | _policies[policyName] = rule; |
| | 1 | 66 | | return this; |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Gets the dictionary of all configured policies. |
| | | 71 | | /// </summary> |
| | 8 | 72 | | public IReadOnlyDictionary<string, ClaimRule> Policies => _policies; |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Builds the configuration object. |
| | | 76 | | /// </summary> |
| | 4 | 77 | | public ClaimPolicyConfig Build() => new() |
| | 4 | 78 | | { |
| | 4 | 79 | | Policies = new Dictionary<string, ClaimRule>(_policies, StringComparer.OrdinalIgnoreCase) |
| | 4 | 80 | | }; |
| | | 81 | | /// <summary> |
| | | 82 | | /// Returns a string representation of the builder. |
| | | 83 | | /// </summary> |
| | | 84 | | /// <returns></returns> |
| | 0 | 85 | | public override string ToString() => $"ClaimPolicyBuilder: {_policies.Count} policies defined."; |
| | | 86 | | /// <summary> |
| | | 87 | | /// Clears all defined policies from the builder. |
| | | 88 | | /// </summary> |
| | 0 | 89 | | public void Clear() => _policies.Clear(); |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Creates a new instance of the <see cref="ClaimPolicyBuilder"/>. |
| | | 93 | | /// </summary> |
| | | 94 | | /// <returns>A new instance of <see cref="ClaimPolicyBuilder"/>.</returns> |
| | 0 | 95 | | public static ClaimPolicyBuilder Create() => new(); |
| | | 96 | | } |