| | | 1 | | using AsiBackbone.AspNetCore.CapabilityGrants; |
| | | 2 | | using AsiBackbone.AspNetCore.Endpoints; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection; |
| | | 4 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 5 | | using Microsoft.Extensions.Options; |
| | | 6 | | |
| | | 7 | | namespace 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> |
| | | 16 | | public 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 | | { |
| | 48 | 41 | | ArgumentNullException.ThrowIfNull(services); |
| | 48 | 42 | | 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. |
| | 2439 | 46 | | foreach (ServiceDescriptor descriptor in services) |
| | | 47 | | { |
| | 1173 | 48 | | if (descriptor.ServiceType == typeof(IEndpointCapabilityGrantValidator) |
| | 1173 | 49 | | && descriptor.ImplementationType != typeof(SignedCapabilityGrantEndpointValidator)) |
| | | 50 | | { |
| | 1 | 51 | | throw new InvalidOperationException( |
| | 1 | 52 | | "An IEndpointCapabilityGrantValidator is already registered. Register either signed capability grant |
| | | 53 | | } |
| | | 54 | | } |
| | | 55 | | |
| | 46 | 56 | | _ = services.AddOptions<SignedCapabilityGrantOptions>() |
| | 46 | 57 | | .Configure(configure) |
| | 46 | 58 | | .Validate(static options => |
| | 46 | 59 | | { |
| | 46 | 60 | | try |
| | 46 | 61 | | { |
| | 46 | 62 | | options.Validate(); |
| | 46 | 63 | | return true; |
| | 46 | 64 | | } |
| | 46 | 65 | | catch (InvalidOperationException) |
| | 46 | 66 | | { |
| | 46 | 67 | | return false; |
| | 46 | 68 | | } |
| | 46 | 69 | | }, "Signed capability grant options must set an issuer, an audience, and valid limits.") |
| | 46 | 70 | | .ValidateOnStart(); |
| | | 71 | | |
| | 46 | 72 | | _ = services.AddLogging(); |
| | 46 | 73 | | services.TryAddScoped<IEndpointCapabilityGrantValidator, SignedCapabilityGrantEndpointValidator>(); |
| | | 74 | | |
| | 46 | 75 | | 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 | | { |
| | 41 | 90 | | ArgumentNullException.ThrowIfNull(services); |
| | | 91 | | |
| | 41 | 92 | | OptionsBuilder<FormFieldCapabilityGrantBindingOptions> optionsBuilder = |
| | 41 | 93 | | services.AddOptions<FormFieldCapabilityGrantBindingOptions>(); |
| | | 94 | | |
| | 41 | 95 | | if (configure is not null) |
| | | 96 | | { |
| | 0 | 97 | | _ = optionsBuilder.Configure(configure); |
| | | 98 | | } |
| | | 99 | | |
| | 41 | 100 | | services.TryAddScoped<ICapabilityGrantRequestBindingProvider, FormFieldCapabilityGrantRequestBindingProvider>(); |
| | | 101 | | |
| | 41 | 102 | | return services; |
| | | 103 | | } |
| | | 104 | | } |