diff --git a/DotBased.AspNet.Authority/AuthorityProviderExtensions.cs b/DotBased.AspNet.Authority/AuthorityProviderExtensions.cs index 88c02f2..9018d7a 100644 --- a/DotBased.AspNet.Authority/AuthorityProviderExtensions.cs +++ b/DotBased.AspNet.Authority/AuthorityProviderExtensions.cs @@ -26,9 +26,6 @@ public static class AuthorityProviderExtensions services.TryAddScoped(); services.TryAddScoped();*/ services.TryAddScoped(); - services.TryAddScoped(); - services.TryAddScoped(); - services.TryAddScoped(); return new AuthorityBuilder(services); } diff --git a/DotBased.AspNet.Authority/Managers/AuthorityGroupManager.cs b/DotBased.AspNet.Authority/Managers/AuthorityGroupManager.cs index 7da7e73..22e70cb 100644 --- a/DotBased.AspNet.Authority/Managers/AuthorityGroupManager.cs +++ b/DotBased.AspNet.Authority/Managers/AuthorityGroupManager.cs @@ -1,6 +1,6 @@ namespace DotBased.AspNet.Authority.Managers; -public class AuthorityGroupManager +public partial class AuthorityManager { } \ No newline at end of file diff --git a/DotBased.AspNet.Authority/Managers/AuthorityManager.cs b/DotBased.AspNet.Authority/Managers/AuthorityManager.cs index b80e1e3..4b78e03 100644 --- a/DotBased.AspNet.Authority/Managers/AuthorityManager.cs +++ b/DotBased.AspNet.Authority/Managers/AuthorityManager.cs @@ -2,22 +2,28 @@ using System.Reflection; using DotBased.AspNet.Authority.Attributes; using DotBased.AspNet.Authority.Crypto; using DotBased.AspNet.Authority.Models.Options; +using DotBased.AspNet.Authority.Repositories; +using DotBased.AspNet.Authority.Validators; using DotBased.Logging; using Microsoft.Extensions.Options; namespace DotBased.AspNet.Authority.Managers; -public class AuthorityManager +public partial class AuthorityManager { public AuthorityManager( IOptions options, IServiceProvider services, - ICryptographer cryptographer) + ICryptographer cryptographer, + IUserRepository userRepository, + IPasswordHasher passwordHasher) { _logger = LogService.RegisterLogger(); - Options = options.Value ?? new AuthorityOptions(); + Options = options.Value; Services = services; Cryptographer = cryptographer; + UserRepository = userRepository; + PasswordHasher = passwordHasher; } private readonly ILogger _logger; @@ -25,6 +31,13 @@ public class AuthorityManager public IServiceProvider Services { get; } public AuthorityOptions Options { get; } public ICryptographer Cryptographer { get; } + + public IUserRepository UserRepository { get; } + + public IPasswordHasher PasswordHasher { get; } + + public IEnumerable PasswordValidators { get; } = []; + public IEnumerable UserValidators { get; } = []; public long GenerateVersion() => DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); @@ -38,12 +51,7 @@ public class AuthorityManager /// The class with the properties to protect. public async Task HandlePropertyProtection(TModel data, bool protection) { - var props = GetProtectedPropertiesValues(data); - if (Cryptographer == null) - { - _logger.Warning("No cryptographer specified! Cannot encrypt/decrypt properties."); - return; - } + var props = GetProtectedPropertiesValues(data); if (props.Count == 0) { return; diff --git a/DotBased.AspNet.Authority/Managers/AuthorityRoleManager.cs b/DotBased.AspNet.Authority/Managers/AuthorityRoleManager.cs index 420b639..22e70cb 100644 --- a/DotBased.AspNet.Authority/Managers/AuthorityRoleManager.cs +++ b/DotBased.AspNet.Authority/Managers/AuthorityRoleManager.cs @@ -1,6 +1,6 @@ namespace DotBased.AspNet.Authority.Managers; -public class AuthorityRoleManager +public partial class AuthorityManager { } \ No newline at end of file diff --git a/DotBased.AspNet.Authority/Managers/AuthorityUserManager.cs b/DotBased.AspNet.Authority/Managers/AuthorityUserManager.cs index 6cc0974..94feb6f 100644 --- a/DotBased.AspNet.Authority/Managers/AuthorityUserManager.cs +++ b/DotBased.AspNet.Authority/Managers/AuthorityUserManager.cs @@ -1,41 +1,11 @@ -using DotBased.AspNet.Authority.Crypto; using DotBased.AspNet.Authority.Models; using DotBased.AspNet.Authority.Models.Authority; using DotBased.AspNet.Authority.Models.Validation; -using DotBased.AspNet.Authority.Repositories; -using DotBased.AspNet.Authority.Validators; -using DotBased.Logging; namespace DotBased.AspNet.Authority.Managers; -public class AuthorityUserManager +public partial class AuthorityManager { - public AuthorityUserManager( - AuthorityManager manager, - IUserRepository userRepository, - IPasswordHasher passwordHasher, - IEnumerable? passwordValidators, - IEnumerable? userValidators) - { - _logger = LogService.RegisterLogger(); - AuthorityManager = manager; - UserRepository = userRepository; - PasswordHasher = passwordHasher; - if (passwordValidators != null) - PasswordValidators = passwordValidators; - if (userValidators != null) - UserValidators = userValidators; - } - - private readonly ILogger _logger; - public AuthorityManager AuthorityManager { get; } - public IUserRepository UserRepository { get; } - - public IPasswordHasher PasswordHasher { get; } - - public IEnumerable PasswordValidators { get; } = []; - public IEnumerable UserValidators { get; } = []; - public async Task ValidatePasswordAsync(AuthorityUser user, string password) { List errors = []; @@ -81,7 +51,7 @@ public class AuthorityUserManager } user.PasswordHash = await PasswordHasher.HashPasswordAsync(password); - user.SecurityVersion = AuthorityManager.GenerateVersion(); + user.SecurityVersion = GenerateVersion(); var updateResult = await UserRepository.UpdateUserAsync(user); return updateResult == null ? AuthorityResult.Error("Failed to save updates!") : AuthorityResult.Ok(updateResult); @@ -99,8 +69,8 @@ public class AuthorityUserManager return AuthorityResult.Failed(errors, ResultFailReason.Validation); } - userModel.Version = AuthorityManager.GenerateVersion(); - userModel.SecurityVersion = AuthorityManager.GenerateVersion(); + userModel.Version = GenerateVersion(); + userModel.SecurityVersion = GenerateVersion(); var hashedPassword = await PasswordHasher.HashPasswordAsync(password); userModel.PasswordHash = hashedPassword; diff --git a/DotBased.AspNet.Authority/Validators/IPasswordValidator.cs b/DotBased.AspNet.Authority/Validators/IPasswordValidator.cs index ff7c770..1208a21 100644 --- a/DotBased.AspNet.Authority/Validators/IPasswordValidator.cs +++ b/DotBased.AspNet.Authority/Validators/IPasswordValidator.cs @@ -6,5 +6,5 @@ namespace DotBased.AspNet.Authority.Validators; public interface IPasswordValidator { - public Task ValidatePasswordAsync(AuthorityUserManager userManager, AuthorityUser user, string password); + public Task ValidatePasswordAsync(AuthorityManager manager, AuthorityUser user, string password); } \ No newline at end of file diff --git a/DotBased.AspNet.Authority/Validators/IUserValidator.cs b/DotBased.AspNet.Authority/Validators/IUserValidator.cs index da07405..ef07650 100644 --- a/DotBased.AspNet.Authority/Validators/IUserValidator.cs +++ b/DotBased.AspNet.Authority/Validators/IUserValidator.cs @@ -6,5 +6,5 @@ namespace DotBased.AspNet.Authority.Validators; public interface IUserValidator { - public Task ValidateUserAsync(AuthorityUserManager manager, AuthorityUser user); + public Task ValidateUserAsync(AuthorityManager manager, AuthorityUser user); } \ No newline at end of file diff --git a/DotBased.AspNet.Authority/Validators/PasswordEqualsValidator.cs b/DotBased.AspNet.Authority/Validators/PasswordEqualsValidator.cs index a95c55d..8278e13 100644 --- a/DotBased.AspNet.Authority/Validators/PasswordEqualsValidator.cs +++ b/DotBased.AspNet.Authority/Validators/PasswordEqualsValidator.cs @@ -8,7 +8,7 @@ public class PasswordEqualsValidator : IPasswordValidator { private const string ValidatorId = "Authority.Validator.Password.Equals"; private const string ValidationBase = "Authority.Validation.Password"; - public async Task ValidatePasswordAsync(AuthorityUserManager userManager, AuthorityUser user, string password) + public async Task ValidatePasswordAsync(AuthorityManager userManager, AuthorityUser user, string password) { List errors = []; var hashedPassword = await userManager.PasswordHasher.HashPasswordAsync(password); diff --git a/DotBased.AspNet.Authority/Validators/PasswordOptionsValidator.cs b/DotBased.AspNet.Authority/Validators/PasswordOptionsValidator.cs index bd38b25..41cab40 100644 --- a/DotBased.AspNet.Authority/Validators/PasswordOptionsValidator.cs +++ b/DotBased.AspNet.Authority/Validators/PasswordOptionsValidator.cs @@ -8,19 +8,18 @@ namespace DotBased.AspNet.Authority.Validators; /// /// Validates the password against the options that is configured. /// -/// The user model used. public class PasswordOptionsValidator : IPasswordValidator { private const string ValidatorId = "Authority.Validator.Password.Options"; private const string ValidationBase = "Authority.Validation.Password"; - public async Task ValidatePasswordAsync(AuthorityUserManager userManager, AuthorityUser user, string password) + public async Task ValidatePasswordAsync(AuthorityManager userManager, AuthorityUser user, string password) { if (userManager == null) { throw new ArgumentNullException(nameof(userManager), "User manager is not provided!"); } - var passwordOptions = userManager.AuthorityManager.Options.Password; + var passwordOptions = userManager.Options.Password; var errors = new List(); if (password.IsNullOrEmpty() || password.Length < passwordOptions.RequiredLength) diff --git a/DotBased.AspNet.Authority/Validators/UserValidator.cs b/DotBased.AspNet.Authority/Validators/UserValidator.cs index 317327f..1b914bc 100644 --- a/DotBased.AspNet.Authority/Validators/UserValidator.cs +++ b/DotBased.AspNet.Authority/Validators/UserValidator.cs @@ -10,11 +10,11 @@ public class UserValidator : IUserValidator private const string ValidatorId = "Authority.Validator.User"; private const string ValidationBase = "Authority.Validation.User"; - public async Task ValidateUserAsync(AuthorityUserManager manager, AuthorityUser user) + public async Task ValidateUserAsync(AuthorityManager manager, AuthorityUser user) { List errors = []; - var userOptions = manager.AuthorityManager.Options.User; + var userOptions = manager.Options.User; if (userOptions.RequireUniqueEmail) {