| | | 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 production-oriented managed-key signing with a host-owned managed-key client factory. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// The production-oriented registration fails closed by default because <see cref="ManagedKeySigningOptions.ReturnU |
| | | 16 | | /// defaults to <see langword="false" />. |
| | | 17 | | /// </remarks> |
| | | 18 | | public static IServiceCollection AddAsiBackboneManagedKeySigning( |
| | | 19 | | this IServiceCollection services, |
| | | 20 | | Action<ManagedKeySigningOptions> configure, |
| | | 21 | | Func<IServiceProvider, IManagedKeySigningClient> clientFactory) |
| | | 22 | | { |
| | 12 | 23 | | ArgumentNullException.ThrowIfNull(services); |
| | 10 | 24 | | ArgumentNullException.ThrowIfNull(configure); |
| | 8 | 25 | | ArgumentNullException.ThrowIfNull(clientFactory); |
| | | 26 | | |
| | 6 | 27 | | ManagedKeySigningOptions options = new(); |
| | 6 | 28 | | configure(options); |
| | 6 | 29 | | options.Validate(); |
| | | 30 | | |
| | 6 | 31 | | return AddManagedKeySigningCore(services, options, clientFactory); |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Adds production-oriented managed-key signing using an already-registered <see cref="IManagedKeySigningClient" /> |
| | | 36 | | /// </summary> |
| | | 37 | | /// <remarks> |
| | | 38 | | /// The production-oriented registration fails closed by default because <see cref="ManagedKeySigningOptions.ReturnU |
| | | 39 | | /// defaults to <see langword="false" />. |
| | | 40 | | /// </remarks> |
| | | 41 | | public static IServiceCollection AddAsiBackboneManagedKeySigning( |
| | | 42 | | this IServiceCollection services, |
| | | 43 | | Action<ManagedKeySigningOptions> configure) |
| | | 44 | | { |
| | 10 | 45 | | ArgumentNullException.ThrowIfNull(services); |
| | 8 | 46 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 47 | | |
| | 6 | 48 | | ManagedKeySigningOptions options = new(); |
| | 6 | 49 | | configure(options); |
| | 6 | 50 | | options.Validate(); |
| | | 51 | | |
| | 4 | 52 | | return AddManagedKeySigningCore(services, options); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Adds local-validation managed-key signing with a host-owned managed-key client factory. |
| | | 57 | | /// </summary> |
| | | 58 | | /// <remarks> |
| | | 59 | | /// This helper explicitly sets <see cref="ManagedKeySigningOptions.ReturnUnsignedOnFailure" /> to <see langword="tr |
| | | 60 | | /// so samples, tests, and diagnostics can inspect unsigned failure metadata. Do not use this helper as the default |
| | | 61 | | /// production registration unless host policy explicitly routes unsigned failure metadata. |
| | | 62 | | /// </remarks> |
| | | 63 | | public static IServiceCollection AddAsiBackboneManagedKeySigningForLocalValidation( |
| | | 64 | | this IServiceCollection services, |
| | | 65 | | Action<ManagedKeySigningOptions> configure, |
| | | 66 | | Func<IServiceProvider, IManagedKeySigningClient> clientFactory) |
| | | 67 | | { |
| | 10 | 68 | | ArgumentNullException.ThrowIfNull(services); |
| | 8 | 69 | | ArgumentNullException.ThrowIfNull(configure); |
| | 6 | 70 | | ArgumentNullException.ThrowIfNull(clientFactory); |
| | | 71 | | |
| | 4 | 72 | | ManagedKeySigningOptions options = new(); |
| | 4 | 73 | | configure(options); |
| | 4 | 74 | | options.ReturnUnsignedOnFailure = true; |
| | 4 | 75 | | options.Validate(); |
| | | 76 | | |
| | 4 | 77 | | return AddManagedKeySigningCore(services, options, clientFactory); |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Adds local-validation managed-key signing using an already-registered <see cref="IManagedKeySigningClient" />. |
| | | 82 | | /// </summary> |
| | | 83 | | /// <remarks> |
| | | 84 | | /// This helper explicitly sets <see cref="ManagedKeySigningOptions.ReturnUnsignedOnFailure" /> to <see langword="tr |
| | | 85 | | /// so samples, tests, and diagnostics can inspect unsigned failure metadata. Do not use this helper as the default |
| | | 86 | | /// production registration unless host policy explicitly routes unsigned failure metadata. |
| | | 87 | | /// </remarks> |
| | | 88 | | public static IServiceCollection AddAsiBackboneManagedKeySigningForLocalValidation( |
| | | 89 | | this IServiceCollection services, |
| | | 90 | | Action<ManagedKeySigningOptions> configure) |
| | | 91 | | { |
| | 6 | 92 | | ArgumentNullException.ThrowIfNull(services); |
| | 4 | 93 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 94 | | |
| | 2 | 95 | | ManagedKeySigningOptions options = new(); |
| | 2 | 96 | | configure(options); |
| | 2 | 97 | | options.ReturnUnsignedOnFailure = true; |
| | 2 | 98 | | options.Validate(); |
| | | 99 | | |
| | 2 | 100 | | return AddManagedKeySigningCore(services, options); |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | private static IServiceCollection AddManagedKeySigningCore( |
| | | 104 | | IServiceCollection services, |
| | | 105 | | ManagedKeySigningOptions options, |
| | | 106 | | Func<IServiceProvider, IManagedKeySigningClient> clientFactory) |
| | | 107 | | { |
| | 10 | 108 | | _ = services.AddSingleton(options); |
| | 10 | 109 | | _ = services.AddSingleton(clientFactory); |
| | 10 | 110 | | _ = services.AddSingleton<ManagedKeySigningService>(); |
| | 10 | 111 | | _ = services.AddSingleton<IAsiBackboneSigningService>(serviceProvider => |
| | 20 | 112 | | serviceProvider.GetRequiredService<ManagedKeySigningService>()); |
| | | 113 | | |
| | 10 | 114 | | return services; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | private static IServiceCollection AddManagedKeySigningCore( |
| | | 118 | | IServiceCollection services, |
| | | 119 | | ManagedKeySigningOptions options) |
| | | 120 | | { |
| | 6 | 121 | | _ = services.AddSingleton(options); |
| | 6 | 122 | | _ = services.AddSingleton<ManagedKeySigningService>(); |
| | 6 | 123 | | _ = services.AddSingleton<IAsiBackboneSigningService>(serviceProvider => |
| | 12 | 124 | | serviceProvider.GetRequiredService<ManagedKeySigningService>()); |
| | | 125 | | |
| | 6 | 126 | | return services; |
| | | 127 | | } |
| | | 128 | | } |