| | | 1 | | using AsiBackbone.Core.Signing; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | |
| | | 4 | | namespace AsiBackbone.Signing.ManagedKey; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides dependency injection registration helpers for managed-key signing. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class ManagedKeySigningServiceCollectionExtensions |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Adds managed-key signing with a host-owned managed-key client factory. |
| | | 13 | | /// </summary> |
| | | 14 | | public static IServiceCollection AddAsiBackboneManagedKeySigning( |
| | | 15 | | this IServiceCollection services, |
| | | 16 | | Action<ManagedKeySigningOptions> configure, |
| | | 17 | | Func<IServiceProvider, IManagedKeySigningClient> clientFactory) |
| | | 18 | | { |
| | 2 | 19 | | ArgumentNullException.ThrowIfNull(services); |
| | 2 | 20 | | ArgumentNullException.ThrowIfNull(configure); |
| | 2 | 21 | | ArgumentNullException.ThrowIfNull(clientFactory); |
| | | 22 | | |
| | 2 | 23 | | ManagedKeySigningOptions options = new(); |
| | 2 | 24 | | configure(options); |
| | 2 | 25 | | options.Validate(); |
| | | 26 | | |
| | 2 | 27 | | _ = services.AddSingleton(options); |
| | 2 | 28 | | _ = services.AddSingleton(clientFactory); |
| | 2 | 29 | | _ = services.AddSingleton<ManagedKeySigningService>(); |
| | 2 | 30 | | _ = services.AddSingleton<IAsiBackboneSigningService>(serviceProvider => |
| | 4 | 31 | | serviceProvider.GetRequiredService<ManagedKeySigningService>()); |
| | | 32 | | |
| | 2 | 33 | | return services; |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Adds managed-key signing using an already-registered <see cref="IManagedKeySigningClient" />. |
| | | 38 | | /// </summary> |
| | | 39 | | public static IServiceCollection AddAsiBackboneManagedKeySigning( |
| | | 40 | | this IServiceCollection services, |
| | | 41 | | Action<ManagedKeySigningOptions> configure) |
| | | 42 | | { |
| | 0 | 43 | | ArgumentNullException.ThrowIfNull(services); |
| | 0 | 44 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 45 | | |
| | 0 | 46 | | ManagedKeySigningOptions options = new(); |
| | 0 | 47 | | configure(options); |
| | 0 | 48 | | options.Validate(); |
| | | 49 | | |
| | 0 | 50 | | _ = services.AddSingleton(options); |
| | 0 | 51 | | _ = services.AddSingleton<ManagedKeySigningService>(); |
| | 0 | 52 | | _ = services.AddSingleton<IAsiBackboneSigningService>(serviceProvider => |
| | 0 | 53 | | serviceProvider.GetRequiredService<ManagedKeySigningService>()); |
| | | 54 | | |
| | 0 | 55 | | return services; |
| | | 56 | | } |
| | | 57 | | } |