80 lines
4.2 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.Security.Claims;
namespace DigitalData.Core.Security
{
public static class DIExtensions
{
private static IServiceCollection AddParamsConfigureOptions<TParams>(this IServiceCollection services) where TParams : RSAFactoryParams
=> services.AddSingleton<IConfigureOptions<TParams>, ParamsConfigureOptions<TParams>>();
private static IServiceCollection AddAsymCryptService(this IServiceCollection services) => services
.AddParamsConfigureOptions<AsymCryptParams>()
.AddAutoMapper(typeof(MappingProfile).Assembly)
.AddSingleton<IAsymCryptHandler, AsymCryptHandler>();
/// <summary>
/// Registers a custom asym crypt service with specified parameters from the given configuration section.
/// </summary>
/// <param name="services"></param>
/// <param name="section"></param>
/// <returns>The updated <see cref="IServiceCollection"/> with the RSA Factory registered.</returns>
public static IServiceCollection AddAsymCryptService(this IServiceCollection services, IConfigurationSection section) => services
.Configure<AsymCryptParams>(section)
.AddAsymCryptService();
/// <summary>
/// Registers an asym crypt service with the specified parameters from the given instance.
/// </summary>
/// <param name="services"></param>
/// <returns>The updated <see cref="IServiceCollection"/> with the RSA Factory registered.</returns>
public static IServiceCollection AddAsymCryptService(this IServiceCollection services, AsymCryptParams? asymCryptParams = null) => services
.AddSingleton(Options.Create(asymCryptParams ?? new()))
.AddAsymCryptService();
/// <summary>
/// Registers a custom RSA Factory with specified parameters from the given configuration section.
/// </summary>
/// <param name="services"></param>
/// <param name="section"></param>
/// <returns>The updated <see cref="IServiceCollection"/> with the RSA Factory registered.</returns>
public static IServiceCollection AddRSAFactory(this IServiceCollection services, IConfigurationSection section) => services
.AddParamsConfigureOptions<RSAFactoryParams>()
.Configure<RSAFactoryParams>(section)
.AddSingleton<IRSAFactory, RSAFactory<RSAFactoryParams>>();
private static IServiceCollection AddClaimDescriptor<TPrincipal>(this IServiceCollection services,
Func<TPrincipal, IDictionary<string, object>>? claimsMapper = null,
Func<TPrincipal, ClaimsIdentity>? subjectMapper = null)
{
var descriptor = new ClaimDescriptor<TPrincipal>
{
CreateClaims = claimsMapper,
CreateSubject = subjectMapper
};
return services.AddSingleton(sp => Options.Create(descriptor));
}
public static IServiceCollection AddTokenParams(this IServiceCollection services, IConfiguration configuration)
=> services.Configure<TokenParams>(configuration);
public static IServiceCollection AddTokenParams(this IServiceCollection services, TokenParams tokenParams)
=> services.AddSingleton(Options.Create(tokenParams));
public static IServiceCollection AddTokenParams(this IServiceCollection services, params TokenDescription[] descriptions)
=> services.AddSingleton(Options.Create<TokenParams>(new() { Descriptions = descriptions }));
public static IServiceCollection AddJwtSignatureHandler<TPrincipal>(this IServiceCollection services,
Func<TPrincipal, IDictionary<string, object>>? claimsMapper = null,
Func<TPrincipal, ClaimsIdentity>? subjectMapper = null)
=> services
.AddClaimDescriptor(claimsMapper: claimsMapper, subjectMapper: subjectMapper)
.AddSingleton<IJwtSignatureHandler<TPrincipal>, JwtSignatureHandler<TPrincipal>>();
}
}