| | | 1 | | using AsiBackbone.Core.Signing; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | |
| | | 4 | | namespace AsiBackbone.Signing.ManagedKey; |
| | | 5 | | |
| | | 6 | | /// <summary>Registers public-key governance signature verification.</summary> |
| | | 7 | | public static class KeySignatureVerificationServiceCollectionExtensions |
| | | 8 | | { |
| | | 9 | | /// <summary>Registers configured public keys and the public-key verifier.</summary> |
| | | 10 | | public static IServiceCollection AddAsiBackboneKeySignatureVerification( |
| | | 11 | | this IServiceCollection services, |
| | | 12 | | Action<KeySignatureVerificationOptions> configure) |
| | | 13 | | { |
| | 5 | 14 | | ArgumentNullException.ThrowIfNull(services); |
| | 5 | 15 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 16 | | |
| | 5 | 17 | | KeySignatureVerificationOptions options = new(); |
| | 5 | 18 | | configure(options); |
| | 5 | 19 | | options.Validate(); |
| | 5 | 20 | | _ = services.AddSingleton(options); |
| | 5 | 21 | | _ = services.AddSingleton<IGovernanceVerificationKeyResolver, ConfiguredGovernanceVerificationKeyResolver>(); |
| | 5 | 22 | | return AddVerifier(services); |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary>Registers a host key resolver and the public-key verifier.</summary> |
| | | 26 | | public static IServiceCollection AddAsiBackboneKeySignatureVerification<TResolver>( |
| | | 27 | | this IServiceCollection services, |
| | | 28 | | Action<KeySignatureVerificationOptions>? configure = null) |
| | | 29 | | where TResolver : class, IGovernanceVerificationKeyResolver |
| | | 30 | | { |
| | 0 | 31 | | ArgumentNullException.ThrowIfNull(services); |
| | 0 | 32 | | KeySignatureVerificationOptions options = new(); |
| | 0 | 33 | | configure?.Invoke(options); |
| | 0 | 34 | | options.Validate(); |
| | 0 | 35 | | _ = services.AddSingleton(options); |
| | 0 | 36 | | _ = services.AddSingleton<IGovernanceVerificationKeyResolver, TResolver>(); |
| | 0 | 37 | | return AddVerifier(services); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | private static IServiceCollection AddVerifier(IServiceCollection services) |
| | | 41 | | { |
| | 5 | 42 | | _ = services.AddSingleton<PublicKeySignatureVerificationService>(); |
| | 5 | 43 | | _ = services.AddSingleton<IGovernanceSignatureVerificationService>(provider => |
| | 5 | 44 | | provider.GetRequiredService<PublicKeySignatureVerificationService>()); |
| | 5 | 45 | | return services; |
| | | 46 | | } |
| | | 47 | | |
| | 5 | 48 | | private sealed class ConfiguredGovernanceVerificationKeyResolver(KeySignatureVerificationOptions options) |
| | | 49 | | : IGovernanceVerificationKeyResolver |
| | | 50 | | { |
| | | 51 | | public ValueTask<bool> IsKnownKeyAsync(string keyId, CancellationToken cancellationToken = default) |
| | | 52 | | { |
| | 2 | 53 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 54 | | return ValueTask.FromResult(options.Keys.Any(candidate => |
| | 2 | 55 | | string.Equals(candidate.KeyId, keyId, StringComparison.Ordinal))); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | public ValueTask<GovernanceVerificationKey?> ResolveAsync( |
| | | 59 | | string keyId, |
| | | 60 | | string keyVersion, |
| | | 61 | | CancellationToken cancellationToken = default) |
| | | 62 | | { |
| | 7 | 63 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 7 | 64 | | GovernanceVerificationKey? key = options.Keys.SingleOrDefault(candidate => |
| | 7 | 65 | | string.Equals(candidate.KeyId, keyId, StringComparison.Ordinal) |
| | 7 | 66 | | && string.Equals(candidate.KeyVersion, keyVersion, StringComparison.Ordinal)); |
| | 7 | 67 | | return ValueTask.FromResult(key); |
| | | 68 | | } |
| | | 69 | | } |
| | | 70 | | } |