using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Svrnty.CQRS.Altcha.Abstractions; namespace Svrnty.CQRS.Altcha.Grpc; public static class ServiceCollectionExtensions { /// /// Registers the gRPC-backed and /// . Configure the endpoint /// and optional service-auth token provider via the /// delegate. /// /// /// /// services.AddSvrntyAltcha(); /// services.AddSvrntyAltchaGrpcVerifier(opts => /// { /// opts.Endpoint = "http://altcha:9090"; /// opts.TokenProvider = async ct => await tokenIssuer.GetTokenAsync("altcha", ct); /// }); /// /// public static IServiceCollection AddSvrntyAltchaGrpcVerifier( this IServiceCollection services, Action configure) { services.Configure(configure); RegisterCore(services); return services; } /// /// Binds from a configuration section /// (typically "Altcha:Grpc") and registers the gRPC verifier /// and challenge provider. /// public static IServiceCollection AddSvrntyAltchaGrpcVerifier( this IServiceCollection services, IConfiguration configuration) { services.Configure(configuration); RegisterCore(services); return services; } private static void RegisterCore(IServiceCollection services) { services.AddGrpcClient((sp, client) => { var opts = sp.GetRequiredService>().Value; if (string.IsNullOrWhiteSpace(opts.Endpoint)) throw new InvalidOperationException( "Altcha gRPC endpoint not configured. Set AltchaGrpcOptions.Endpoint " + "(e.g. http://altcha:9090)."); client.Address = new Uri(opts.Endpoint); }); services.TryAddSingleton(); services.TryAddSingleton(); } }