< Summary

Information
Class: AsiBackbone.AspNetCore.DependencyInjection.AsiBackboneSignedCapabilityGrantServiceCollectionExtensions
Assembly: AsiBackbone.AspNetCore
File(s): /home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.AspNetCore/DependencyInjection/AsiBackboneSignedCapabilityGrantServiceCollectionExtensions.cs
Line coverage
96%
Covered lines: 31
Uncovered lines: 1
Coverable lines: 32
Total lines: 104
Line coverage: 96.8%
Branch coverage
87%
Covered branches: 7
Total branches: 8
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddAsiBackboneSignedCapabilityGrants(...)100%66100%
AddAsiBackboneFormFieldCapabilityGrantBinding(...)50%2285.71%

File(s)

/home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.AspNetCore/DependencyInjection/AsiBackboneSignedCapabilityGrantServiceCollectionExtensions.cs

#LineLine coverage
 1using AsiBackbone.AspNetCore.CapabilityGrants;
 2using AsiBackbone.AspNetCore.Endpoints;
 3using Microsoft.Extensions.DependencyInjection;
 4using Microsoft.Extensions.DependencyInjection.Extensions;
 5using Microsoft.Extensions.Options;
 6
 7namespace AsiBackbone.AspNetCore.DependencyInjection;
 8
 9/// <summary>
 10/// Provides opt-in registration for signed capability grant endpoint validation.
 11/// </summary>
 12/// <remarks>
 13/// Nothing here is registered by <see cref="AsiBackboneAspNetCoreServiceCollectionExtensions.AddAsiBackboneAspNetCore(I
 14/// Hosts that keep a claim-backed <see cref="IEndpointCapabilityGrantValidator" /> see no change.
 15/// </remarks>
 16public static class AsiBackboneSignedCapabilityGrantServiceCollectionExtensions
 17{
 18    /// <summary>
 19    /// Registers <see cref="SignedCapabilityGrantEndpointValidator" /> as the endpoint capability grant validator.
 20    /// </summary>
 21    /// <remarks>
 22    /// <para>
 23    /// The host must also register an <see cref="Core.Signing.IGovernanceSignatureVerificationService" />, an
 24    /// <see cref="Core.CapabilityGrants.ICapabilityGrantUseStore" />, and an <see cref="ICapabilityGrantRequestBindingP
 25    /// for example with <see cref="AddAsiBackboneFormFieldCapabilityGrantBinding" />. Any of them missing denies every
 26    /// request that requires a capability grant.
 27    /// </para>
 28    /// <para>
 29    /// Option validation runs through the Microsoft.Extensions.Options pipeline and at host startup.
 30    /// </para>
 31    /// </remarks>
 32    /// <param name="services">The service collection to add services to.</param>
 33    /// <param name="configure">The options configuration callback, which must set the issuer and audience.</param>
 34    /// <returns>The same service collection so calls can be chained.</returns>
 35    /// <exception cref="ArgumentNullException"><paramref name="services" /> or <paramref name="configure" /> is <see la
 36    /// <exception cref="InvalidOperationException">Another <see cref="IEndpointCapabilityGrantValidator" /> is already 
 37    public static IServiceCollection AddAsiBackboneSignedCapabilityGrants(
 38        this IServiceCollection services,
 39        Action<SignedCapabilityGrantOptions> configure)
 40    {
 4841        ArgumentNullException.ThrowIfNull(services);
 4842        ArgumentNullException.ThrowIfNull(configure);
 43
 44        // Two validators would leave the effective one to registration order, which is the kind of choice a host should
 45        // make explicitly, so a conflicting registration is refused.
 243946        foreach (ServiceDescriptor descriptor in services)
 47        {
 117348            if (descriptor.ServiceType == typeof(IEndpointCapabilityGrantValidator)
 117349                && descriptor.ImplementationType != typeof(SignedCapabilityGrantEndpointValidator))
 50            {
 151                throw new InvalidOperationException(
 152                    "An IEndpointCapabilityGrantValidator is already registered. Register either signed capability grant
 53            }
 54        }
 55
 4656        _ = services.AddOptions<SignedCapabilityGrantOptions>()
 4657            .Configure(configure)
 4658            .Validate(static options =>
 4659            {
 4660                try
 4661                {
 4662                    options.Validate();
 4663                    return true;
 4664                }
 4665                catch (InvalidOperationException)
 4666                {
 4667                    return false;
 4668                }
 4669            }, "Signed capability grant options must set an issuer, an audience, and valid limits.")
 4670            .ValidateOnStart();
 71
 4672        _ = services.AddLogging();
 4673        services.TryAddScoped<IEndpointCapabilityGrantValidator, SignedCapabilityGrantEndpointValidator>();
 74
 4675        return services;
 76    }
 77
 78    /// <summary>
 79    /// Registers <see cref="FormFieldCapabilityGrantRequestBindingProvider" /> as the capability grant request-binding
 80    /// provider.
 81    /// </summary>
 82    /// <param name="services">The service collection to add services to.</param>
 83    /// <param name="configure">An optional callback that configures the excluded field names.</param>
 84    /// <returns>The same service collection so calls can be chained.</returns>
 85    /// <exception cref="ArgumentNullException"><paramref name="services" /> is <see langword="null" />.</exception>
 86    public static IServiceCollection AddAsiBackboneFormFieldCapabilityGrantBinding(
 87        this IServiceCollection services,
 88        Action<FormFieldCapabilityGrantBindingOptions>? configure = null)
 89    {
 4190        ArgumentNullException.ThrowIfNull(services);
 91
 4192        OptionsBuilder<FormFieldCapabilityGrantBindingOptions> optionsBuilder =
 4193            services.AddOptions<FormFieldCapabilityGrantBindingOptions>();
 94
 4195        if (configure is not null)
 96        {
 097            _ = optionsBuilder.Configure(configure);
 98        }
 99
 41100        services.TryAddScoped<ICapabilityGrantRequestBindingProvider, FormFieldCapabilityGrantRequestBindingProvider>();
 101
 41102        return services;
 103    }
 104}