< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.Claims.ClaimPolicyBuilder
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Claims/ClaimPolicyBuilder.cs
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 74
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
AddPolicy(...)100%44100%
AddPolicy(...)100%44100%
AddPolicy(...)100%11100%
get_Policies()100%11100%
Build()100%11100%

File(s)

/home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Claims/ClaimPolicyBuilder.cs

#LineLine coverage
 1namespace Kestrun.Claims;
 2
 3
 4/// <summary>
 5/// Builder for defining claim-based authorization policies.
 6/// </summary>
 7public sealed class ClaimPolicyBuilder
 8{
 119    private readonly Dictionary<string, ClaimRule> _policies = new(StringComparer.OrdinalIgnoreCase);
 10
 11    /// <summary>
 12    /// Adds a new policy with a required claim rule.
 13    /// </summary>
 14    /// <param name="policyName">The name of the policy.</param>
 15    /// <param name="claimType">The required claim type.</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, params string[] allowedValues)
 19    {
 1120        ArgumentException.ThrowIfNullOrWhiteSpace(policyName);
 921        ArgumentException.ThrowIfNullOrWhiteSpace(claimType);
 722        if (allowedValues is null || allowedValues.Length == 0)
 23        {
 224            throw new ArgumentException("At least one allowed value must be specified.", nameof(allowedValues));
 25        }
 26
 527        _policies[policyName] = new ClaimRule(claimType, allowedValues);
 528        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="allowedValues">Allowed values for the claim.</param>
 37    /// <returns>The current builder instance.</returns>
 38    public ClaimPolicyBuilder AddPolicy(string policyName, UserIdentityClaim claimType, params string[] allowedValues)
 39    {
 440        ArgumentException.ThrowIfNullOrWhiteSpace(policyName);
 441        if (allowedValues is null || allowedValues.Length == 0)
 42        {
 243            throw new ArgumentException("At least one allowed value must be specified.", nameof(allowedValues));
 44        }
 45
 246        _policies[policyName] = new ClaimRule(claimType.ToClaimUri(), allowedValues);
 247        return this;
 48    }
 49    /// <summary>
 50    /// Adds a prebuilt claim rule under a policy name.
 51    /// </summary>
 52    public ClaimPolicyBuilder AddPolicy(string policyName, ClaimRule rule)
 53    {
 454        ArgumentException.ThrowIfNullOrWhiteSpace(policyName);
 255        ArgumentNullException.ThrowIfNull(rule);
 56
 157        _policies[policyName] = rule;
 158        return this;
 59    }
 60
 61    /// <summary>
 62    /// Gets the dictionary of all configured policies.
 63    /// </summary>
 764    public IReadOnlyDictionary<string, ClaimRule> Policies => _policies;
 65
 66    /// <summary>
 67    /// Builds the configuration object.
 68    /// </summary>
 169    public ClaimPolicyConfig Build() => new()
 170    {
 171        Policies = new Dictionary<string, ClaimRule>(_policies, StringComparer.OrdinalIgnoreCase)
 172    };
 73}
 74