Files
git.stella-ops.org/src/__Libraries/StellaOps.Cryptography.DependencyInjection/CryptoPluginServiceCollectionExtensions.cs
2026-02-04 19:59:20 +02:00

54 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using StellaOps.Cryptography;
using StellaOps.Cryptography.PluginLoader;
namespace StellaOps.Cryptography.DependencyInjection;
/// <summary>
/// DI extension methods for configuration-driven crypto plugin loading.
/// </summary>
public static partial class CryptoPluginServiceCollectionExtensions
{
/// <summary>
/// Registers crypto providers using configuration-driven plugin loading.
/// Replaces hardcoded provider registrations with dynamic plugin loader.
/// </summary>
/// <param name="services">Service collection.</param>
/// <param name="configuration">Application configuration.</param>
/// <param name="configurePlugins">Optional plugin configuration action.</param>
/// <returns>The service collection.</returns>
public static IServiceCollection AddStellaOpsCryptoWithPlugins(
this IServiceCollection services,
IConfiguration configuration,
Action<CryptoPluginConfiguration>? configurePlugins = null)
{
ArgumentNullException.ThrowIfNull(services);
ArgumentNullException.ThrowIfNull(configuration);
CryptoComplianceOptionsRegistration.Register(services, bindFromConfiguration: false);
services.Configure<CryptoComplianceOptions>(options =>
{
configuration.GetSection(CryptoComplianceOptions.SectionKey).Bind(options);
});
services.Configure<CryptoPluginConfiguration>(options =>
{
configuration.GetSection("StellaOps:Crypto:Plugins").Bind(options);
configurePlugins?.Invoke(options);
});
services.TryAddSingleton<ICryptoComplianceService, CryptoComplianceService>();
services.TryAddSingleton<ICryptoHash, DefaultCryptoHash>();
services.TryAddSingleton<ICryptoHmac, DefaultCryptoHmac>();
services.TryAddSingleton<IReadOnlyList<ICryptoProvider>, CryptoPluginProviderList>();
services.TryAddSingleton<IEnumerable<ICryptoProvider>, CryptoPluginProviderEnumerable>();
services.TryAddSingleton<ICryptoProviderRegistry, CryptoPluginProviderRegistry>();
return services;
}
}