- SetAsDefault-Parameter hinzugefügt, um nicht-generische IRSAFactory im Falle einer Konfiguration über appsettings registrieren zu können.
76 lines
4.4 KiB
C#
76 lines
4.4 KiB
C#
using DigitalData.Core.Abstractions.Security;
|
|
using DigitalData.Core.Security.Config;
|
|
using DigitalData.Core.Security.Cryptographer;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Options;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace DigitalData.Core.Security
|
|
{
|
|
public static class DIExtensions
|
|
{
|
|
public static JsonSerializerOptions AddCryptographerConverter(this JsonSerializerOptions options)
|
|
{
|
|
if (!options.Converters.OfType<HashAlgorithmNameConverter>().Any())
|
|
options.Converters.Add(new HashAlgorithmNameConverter());
|
|
|
|
if (!options.Converters.OfType<JsonStringEnumConverter>().Any())
|
|
options.Converters.Add(new JsonStringEnumConverter());
|
|
return options;
|
|
}
|
|
|
|
private static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services) where TAsymCryptParams : AsymCryptParams
|
|
=> services.AddScoped<IAsymCryptService<TAsymCryptParams>, AsymCryptService<TAsymCryptParams>>();
|
|
|
|
public static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, IConfigurationSection section)
|
|
where TAsymCryptParams : AsymCryptParams
|
|
=> services.Configure<TAsymCryptParams>(section).AddAsymCryptService<TAsymCryptParams>();
|
|
|
|
public static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, TAsymCryptParams param)
|
|
where TAsymCryptParams : AsymCryptParams
|
|
=> services.AddSingleton(Options.Create(param)).AddAsymCryptService<TAsymCryptParams>();
|
|
|
|
/// <summary>
|
|
/// Registers default RSA Factory instance with default params
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <returns>The updated <see cref="IServiceCollection"/> with the RSA Factory registered.</returns>
|
|
public static IServiceCollection AddRSAFactory(this IServiceCollection services) => services.AddScoped(_ => Instance.RSAFactory);
|
|
|
|
/// <summary>
|
|
/// Registers a custom RSA Factory instance with specified parameters from the given configuration section.
|
|
/// </summary>
|
|
/// <typeparam name="TRSAFactoryParams"></typeparam>
|
|
/// <param name="services"></param>
|
|
/// <param name="section"></param>
|
|
/// <param name="setAsDefault">If true, the factory is registered as the default <see cref="IRSAFactory"/>. Otherwise, it is registered as <see cref="IRSAFactory{TRSAFactoryParams}"/>.</param>
|
|
/// <returns>The updated <see cref="IServiceCollection"/> with the RSA Factory registered.</returns>
|
|
public static IServiceCollection AddRSAFactory<TRSAFactoryParams>(this IServiceCollection services, IConfigurationSection section, bool setAsDefault = false)
|
|
where TRSAFactoryParams : RSAFactoryParams
|
|
{
|
|
services.Configure<TRSAFactoryParams>(section);
|
|
return setAsDefault
|
|
? services.AddSingleton<IRSAFactory, RSAFactory<TRSAFactoryParams>>()
|
|
: services.AddSingleton<IRSAFactory<TRSAFactoryParams>, RSAFactory<TRSAFactoryParams>>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Registers an RSA Factory with the specified parameters from the given instance. Optionally, sets it as the default factory.
|
|
/// </summary>
|
|
/// <typeparam name="TRSAFactoryParams">The type of the RSA factory parameters.</typeparam>
|
|
/// <param name="services"></param>
|
|
/// <param name="rsaParams"></param>
|
|
/// <param name="setAsDefault">If true, the factory is registered as the default <see cref="IRSAFactory"/>. Otherwise, it is registered as <see cref="IRSAFactory{TRSAFactoryParams}"/>.</param>
|
|
/// <returns>The updated <see cref="IServiceCollection"/> with the RSA Factory registered.</returns>
|
|
public static IServiceCollection AddRSAFactory<TRSAFactoryParams>(this IServiceCollection services, TRSAFactoryParams rsaParams, bool setAsDefault = false)
|
|
where TRSAFactoryParams : RSAFactoryParams
|
|
{
|
|
services.AddSingleton(Options.Create(rsaParams));
|
|
return setAsDefault
|
|
? services.AddSingleton<IRSAFactory, RSAFactory<TRSAFactoryParams>>()
|
|
: services.AddSingleton<IRSAFactory<TRSAFactoryParams>, RSAFactory<TRSAFactoryParams>>();
|
|
}
|
|
}
|
|
} |