mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
refactor: implement password validation in dto; remove redundant validation code in auth controller
This commit is contained in:
@@ -36,11 +36,7 @@ namespace SharpbinV3.Server.Controllers
|
|||||||
var existingEmailUser = await _userService.GetByEmail(request.Email);
|
var existingEmailUser = await _userService.GetByEmail(request.Email);
|
||||||
if (existingEmailUser != null)
|
if (existingEmailUser != null)
|
||||||
return Conflict(new { message = "Email already in use." });
|
return Conflict(new { message = "Email already in use." });
|
||||||
if (!IsValidEmail(request.Email))
|
|
||||||
return BadRequest(new { message = "Invalid email." });
|
|
||||||
}
|
}
|
||||||
if (!IsValidPassword(request.Password))
|
|
||||||
return BadRequest(new { message = "Password should have at least 8 characters, including uppercase, lowercase, and digits." });
|
|
||||||
if (!string.IsNullOrEmpty(request.DisplayName) && request.DisplayName.Length > 26)
|
if (!string.IsNullOrEmpty(request.DisplayName) && request.DisplayName.Length > 26)
|
||||||
return BadRequest(new { message = "Display name should not exceed 26 characters." });
|
return BadRequest(new { message = "Display name should not exceed 26 characters." });
|
||||||
|
|
||||||
@@ -112,31 +108,5 @@ namespace SharpbinV3.Server.Controllers
|
|||||||
registration_enabled = authSettings.Registration_Enabled
|
registration_enabled = authSettings.Registration_Enabled
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static bool IsValidPassword(string password)
|
|
||||||
{
|
|
||||||
if (password.Length < 6)
|
|
||||||
return false;
|
|
||||||
if (!password.Any(char.IsUpper))
|
|
||||||
return false;
|
|
||||||
if (!password.Any(char.IsLower))
|
|
||||||
return false;
|
|
||||||
if (!password.Any(char.IsDigit))
|
|
||||||
return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
private static bool IsValidEmail(string email)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var addr = new System.Net.Mail.MailAddress(email);
|
|
||||||
return addr.Address == email;
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ namespace SharpbinV3.Server.DTOs
|
|||||||
var results = new List<ValidationResult>();
|
var results = new List<ValidationResult>();
|
||||||
if (!ValidateUsername().IsMatch(Username))
|
if (!ValidateUsername().IsMatch(Username))
|
||||||
results.Add(new ValidationResult("Username can only contain letters, numbers, underscores, and dots.", [nameof(Username)]));
|
results.Add(new ValidationResult("Username can only contain letters, numbers, underscores, and dots.", [nameof(Username)]));
|
||||||
|
if (!ValidatePassword().IsMatch(Password))
|
||||||
|
results.Add(new ValidationResult("Password should have at least 8 characters, including uppercase, lowercase, and digits.", [nameof(Password)]));
|
||||||
|
|
||||||
if (DisplayName != null)
|
if (DisplayName != null)
|
||||||
{
|
{
|
||||||
@@ -42,5 +44,7 @@ namespace SharpbinV3.Server.DTOs
|
|||||||
|
|
||||||
[GeneratedRegex("^[A-Za-z0-9_.]+$")]
|
[GeneratedRegex("^[A-Za-z0-9_.]+$")]
|
||||||
private static partial Regex ValidateUsername();
|
private static partial Regex ValidateUsername();
|
||||||
|
[GeneratedRegex("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).{8,}$")]
|
||||||
|
public static partial Regex ValidatePassword();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user