| | | 1 | | using Anichron.API.Settings; |
| | | 2 | | using Microsoft.Extensions.Options; |
| | | 3 | | using System.Net.Mail; |
| | | 4 | | |
| | | 5 | | namespace Anichron.API.Services; |
| | | 6 | | |
| | | 7 | | public interface IRegistrationValidator |
| | | 8 | | { |
| | | 9 | | IIdentityRejection? ValidateIdentity(string username, string email); |
| | | 10 | | Task<IRegisterError?> ValidateAsync(string username, string email, string password, CancellationToken ct); |
| | | 11 | | Task<PasswordRejected?> ValidatePasswordAsync(string password, CancellationToken ct); |
| | | 12 | | } |
| | | 13 | | |
| | 73 | 14 | | public sealed class RegistrationValidator( |
| | 73 | 15 | | IOptions<PasswordPolicy> passwordPolicyOptions, |
| | 73 | 16 | | IOptions<UsernamePolicy> usernamePolicyOptions, |
| | 73 | 17 | | IPwnedPasswordClient pwnedClient) : IRegistrationValidator |
| | | 18 | | { |
| | 73 | 19 | | private readonly PasswordPolicy passwordPolicy = passwordPolicyOptions.Value; |
| | 73 | 20 | | private readonly UsernamePolicy usernamePolicy = usernamePolicyOptions.Value; |
| | | 21 | | |
| | | 22 | | public IIdentityRejection? ValidateIdentity(string username, string email) |
| | 37 | 23 | | { |
| | 37 | 24 | | if (username.Length < usernamePolicy.MinLength |
| | 37 | 25 | | || username.Length > usernamePolicy.MaxLength |
| | 37 | 26 | | || !UsernamePolicy.AllowedCharacters().IsMatch(username)) |
| | 12 | 27 | | { |
| | 12 | 28 | | return new InvalidUsername(); |
| | | 29 | | } |
| | | 30 | | |
| | 25 | 31 | | if (!IsValidEmail(email)) |
| | 10 | 32 | | return new InvalidEmail(); |
| | | 33 | | |
| | 15 | 34 | | return null; |
| | 37 | 35 | | } |
| | | 36 | | |
| | | 37 | | public async Task<IRegisterError?> ValidateAsync(string username, string email, string password, CancellationToken c |
| | 25 | 38 | | => ValidateIdentity(username, email) ?? (IRegisterError?)await ValidatePasswordAsync(password, ct); |
| | | 39 | | |
| | | 40 | | public async Task<PasswordRejected?> ValidatePasswordAsync(string password, CancellationToken ct) |
| | 40 | 41 | | { |
| | 40 | 42 | | if (password.Length < passwordPolicy.MinLength) |
| | 6 | 43 | | return new PasswordTooShort(); |
| | | 44 | | |
| | 34 | 45 | | if (password.Length > passwordPolicy.MaxLength) |
| | 4 | 46 | | return new PasswordTooLong(); |
| | | 47 | | |
| | | 48 | | // IDE0046 suppressed: collapsing an async condition into a ternary reduces readability |
| | | 49 | | #pragma warning disable IDE0046 |
| | 30 | 50 | | if (passwordPolicy.CheckPwnedPasswords && await pwnedClient.IsPwnedAsync(password, ct)) |
| | | 51 | | #pragma warning restore IDE0046 |
| | 4 | 52 | | return new PasswordPwned(); |
| | | 53 | | |
| | 24 | 54 | | return null; |
| | 38 | 55 | | } |
| | | 56 | | |
| | | 57 | | private static bool IsValidEmail(string email) |
| | 25 | 58 | | { |
| | 25 | 59 | | if (email.Length > AppDefaults.Email.MaxLength) |
| | 1 | 60 | | return false; |
| | | 61 | | try |
| | 24 | 62 | | { |
| | 24 | 63 | | var mailAddress = new MailAddress(email.Trim()); |
| | 16 | 64 | | return mailAddress.Address.Equals(email.Trim(), StringComparison.OrdinalIgnoreCase); |
| | | 65 | | } |
| | 8 | 66 | | catch (Exception ex) when (ex is FormatException or ArgumentException) |
| | 8 | 67 | | { |
| | 8 | 68 | | return false; |
| | | 69 | | } |
| | 25 | 70 | | } |
| | | 71 | | } |