2025-01-03 00:14:12 +01:00
|
|
|
using DotBased.AspNet.Authority.Managers;
|
2024-12-26 20:01:57 +01:00
|
|
|
using DotBased.AspNet.Authority.Models.Authority;
|
|
|
|
using DotBased.AspNet.Authority.Models.Validation;
|
|
|
|
|
|
|
|
namespace DotBased.AspNet.Authority.Validators;
|
|
|
|
|
2025-01-08 15:55:00 +01:00
|
|
|
public class PasswordEqualsValidator : IPasswordValidator
|
2024-12-26 20:01:57 +01:00
|
|
|
{
|
|
|
|
private const string ValidatorId = "Authority.Validator.Password.Equals";
|
|
|
|
private const string ValidationBase = "Authority.Validation.Password";
|
2025-01-08 16:06:25 +01:00
|
|
|
public async Task<ValidationResult> ValidatePasswordAsync(AuthorityManager userManager, AuthorityUser user, string password)
|
2024-12-26 20:01:57 +01:00
|
|
|
{
|
|
|
|
List<ValidationError> errors = [];
|
|
|
|
var hashedPassword = await userManager.PasswordHasher.HashPasswordAsync(password);
|
2025-01-08 15:55:00 +01:00
|
|
|
if (user.PasswordHash != null && user.PasswordHash.Equals(hashedPassword, StringComparison.Ordinal))
|
2024-12-26 20:01:57 +01:00
|
|
|
{
|
|
|
|
errors.Add(new ValidationError(ValidatorId, $"{ValidationBase}.InUse", "User uses this password already!"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.Count > 0 ? ValidationResult.Failed(errors) : ValidationResult.Ok();
|
|
|
|
}
|
|
|
|
}
|