using System;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using StellaOps.Cryptography;
using StellaOps.Cryptography.PluginLoader;
namespace StellaOps.Cryptography.DependencyInjection;
public static partial class CryptoServiceCollectionExtensions
{
///
/// Registers crypto services using configuration-driven plugin loading.
/// This is the recommended method for production deployments with regional compliance requirements.
///
/// Service collection.
/// Configuration root.
/// Optional custom plugin directory path. Defaults to application base directory.
/// The service collection.
public static IServiceCollection AddStellaOpsCryptoFromConfiguration(
this IServiceCollection services,
IConfiguration configuration,
string? pluginDirectory = null)
{
ArgumentNullException.ThrowIfNull(services);
ArgumentNullException.ThrowIfNull(configuration);
var pluginConfig = new CryptoPluginConfiguration();
configuration.GetSection("StellaOps:Crypto:Plugins").Bind(pluginConfig);
var complianceConfig = new CryptoComplianceConfiguration();
configuration.GetSection("StellaOps:Crypto:Compliance").Bind(complianceConfig);
pluginConfig.Compliance = complianceConfig;
services.AddSingleton(pluginConfig);
services.TryAddSingleton, CryptoPluginConfigurationOptions>();
services.TryAddSingleton(new CryptoPluginDirectoryOptions(pluginDirectory));
CryptoComplianceOptionsRegistration.Register(services, bindFromConfiguration: false);
services.Configure(options =>
{
configuration.GetSection(CryptoComplianceOptions.SectionKey).Bind(options);
});
services.TryAddSingleton();
services.TryAddSingleton, CryptoPluginProviderList>();
services.TryAddSingleton, CryptoPluginProviderEnumerable>();
services.TryAddSingleton();
return services;
}
///
/// Registers crypto services using configuration-driven plugin loading with explicit compliance profile.
///
/// Service collection.
/// Configuration root.
/// Compliance profile identifier (e.g., "gost", "fips", "eidas", "sm").
/// Enable strict compliance validation.
/// Optional custom plugin directory path.
/// The service collection.
public static IServiceCollection AddStellaOpsCryptoFromConfiguration(
this IServiceCollection services,
IConfiguration configuration,
string complianceProfileId,
bool strictValidation = true,
string? pluginDirectory = null)
{
ArgumentNullException.ThrowIfNull(services);
ArgumentNullException.ThrowIfNull(configuration);
ArgumentNullException.ThrowIfNull(complianceProfileId);
services.AddStellaOpsCryptoFromConfiguration(configuration, pluginDirectory);
services.Configure(options =>
{
options.ProfileId = complianceProfileId;
options.StrictValidation = strictValidation;
});
return services;
}
}