[ADD] Pwd validator, reworked classes
This commit is contained in:
@@ -3,7 +3,7 @@ using DotBased.AspNet.Authority.Services;
|
||||
|
||||
namespace DotBased.AspNet.Authority.Validators;
|
||||
|
||||
public interface IPasswordValidator<TUser>
|
||||
public interface IPasswordValidator<TUser> where TUser : class
|
||||
{
|
||||
public Task<ValidationResult> ValidatePasswordAsync(AuthorityUserManager<TUser> userManager, string password);
|
||||
public Task<ValidationResult> ValidatePasswordAsync(AuthorityUserManager<TUser> userManager, TUser user, string password);
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
using DotBased.AspNet.Authority.Models.Authority;
|
||||
using DotBased.AspNet.Authority.Models.Validation;
|
||||
using DotBased.AspNet.Authority.Services;
|
||||
|
||||
namespace DotBased.AspNet.Authority.Validators;
|
||||
|
||||
public class PasswordEqualsValidator<TUser> : IPasswordValidator<TUser> where TUser : class
|
||||
{
|
||||
private const string ValidatorId = "Authority.Validator.Password.Equals";
|
||||
private const string ValidationBase = "Authority.Validation.Password";
|
||||
public async Task<ValidationResult> ValidatePasswordAsync(AuthorityUserManager<TUser> userManager, TUser user, string password)
|
||||
{
|
||||
if (user == null || user is not AuthorityUserBase authorityUser)
|
||||
{
|
||||
throw new ArgumentException("Invalid user given!", nameof(user));
|
||||
}
|
||||
|
||||
List<ValidationError> errors = [];
|
||||
var hashedPassword = await userManager.PasswordHasher.HashPasswordAsync(password);
|
||||
if (authorityUser.PasswordHash != null && authorityUser.PasswordHash.Equals(hashedPassword, StringComparison.Ordinal))
|
||||
{
|
||||
errors.Add(new ValidationError(ValidatorId, $"{ValidationBase}.InUse", "User uses this password already!"));
|
||||
}
|
||||
|
||||
return errors.Count > 0 ? ValidationResult.Failed(errors) : ValidationResult.Ok();
|
||||
}
|
||||
}
|
@@ -8,12 +8,12 @@ namespace DotBased.AspNet.Authority.Validators;
|
||||
/// Validates the password against the options that is configured.
|
||||
/// </summary>
|
||||
/// <typeparam name="TUser">The user model used.</typeparam>
|
||||
public class PasswordOptionsValidator<TUser> : IPasswordValidator<TUser>
|
||||
public class PasswordOptionsValidator<TUser> : IPasswordValidator<TUser> where TUser : class
|
||||
{
|
||||
private const string ValidatorId = "Authority.Validator.Password.Options";
|
||||
private const string ValidationBase = "Authority.Validation.Password";
|
||||
|
||||
public async Task<ValidationResult> ValidatePasswordAsync(AuthorityUserManager<TUser> userManager, string password)
|
||||
public async Task<ValidationResult> ValidatePasswordAsync(AuthorityUserManager<TUser> userManager, TUser user, string password)
|
||||
{
|
||||
if (userManager == null)
|
||||
{
|
||||
|
Reference in New Issue
Block a user