DotBased/DotBased.AspNet.Authority/AuthorityProviderExtensions.cs

57 lines
2.4 KiB
C#
Raw Normal View History

2024-12-25 22:50:04 +01:00
using DotBased.AspNet.Authority.Crypto;
using DotBased.AspNet.Authority.Managers;
using DotBased.AspNet.Authority.Models.Options;
2024-12-25 22:50:04 +01:00
using DotBased.AspNet.Authority.Validators;
2024-12-21 15:30:17 +01:00
using Microsoft.Extensions.DependencyInjection;
2024-12-25 22:50:04 +01:00
using Microsoft.Extensions.DependencyInjection.Extensions;
2024-12-21 15:30:17 +01:00
namespace DotBased.AspNet.Authority;
public static class AuthorityProviderExtensions
{
2024-12-25 22:50:04 +01:00
public static AuthorityBuilder AddAuthority(this IServiceCollection services, Action<AuthorityOptions>? optionsAction = null)
2024-12-21 15:30:17 +01:00
{
2024-12-25 22:50:04 +01:00
if (optionsAction != null)
{
services.AddOptions();
services.Configure<AuthorityOptions>(optionsAction);
}
2024-12-26 20:01:57 +01:00
2024-12-25 22:50:04 +01:00
services.TryAddScoped<ICryptographer, Cryptographer>();
services.TryAddScoped<IPasswordHasher, PasswordHasher>();
services.TryAddScoped<IPasswordValidator, PasswordOptionsValidator>();
services.TryAddScoped<IPasswordValidator, PasswordEqualsValidator>();
services.TryAddScoped<IUserValidator, UserValidator>();
2024-12-25 22:50:04 +01:00
/*services.TryAddScoped<IEmailVerifier, EmailVerifier>();
services.TryAddScoped<IPhoneNumberVerifier, PhoneNumberVerifier>();
services.TryAddScoped<IUserVerifier, UserVerifier>();*/
services.TryAddScoped<AuthorityManager>();
2024-12-21 15:30:17 +01:00
return new AuthorityBuilder(services);
}
2024-12-25 22:50:04 +01:00
public static AuthorityBuilder AddAuthorityRepository<TRepository>(this AuthorityBuilder authorityBuilder) where TRepository : class
2024-12-21 15:30:17 +01:00
{
return authorityBuilder;
}
2024-12-23 15:59:24 +01:00
public static AuthorityBuilder MapAuthorityEndpoints(this AuthorityBuilder builder)
{
return builder;
}
2024-12-26 20:01:57 +01:00
private static Type GetBaseGenericArgumentType<TModel>(Type baseType)
{
var userGenericBaseTypeDefinition = typeof(TModel).BaseType?.GetGenericTypeDefinition();
if (userGenericBaseTypeDefinition != null && userGenericBaseTypeDefinition == baseType)
{
var userBaseGenericArguments = userGenericBaseTypeDefinition.GetGenericArguments();
if (userBaseGenericArguments.Length <= 0)
{
throw new ArgumentException("Base implementation does not have the required generic argument.", nameof(TModel));
}
return userBaseGenericArguments[0];
}
throw new ArgumentException($"Given object {typeof(TModel).Name} does not have the base implementation type of: {baseType.Name}", nameof(TModel));
}
2024-12-21 15:30:17 +01:00
}