| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- using System.Linq;
- using Application.Features.Config.Get;
- using SharedKernel.Results;
- namespace Application.Helpers;
- public static class PasswordPolicyValidator
- {
- public static Result Validate(string password, Response.AccountConfigDto config)
- {
- if (string.IsNullOrWhiteSpace(password))
- {
- return Result.Failure(Error.Problem("Auth.PasswordRequired", "비밀번호는 필수입니다."));
- }
- // 최소 길이 검증 (config 없으면 기본 6자)
- var minLength = config.PasswordMinLength ?? 6;
- if (password.Length < minLength)
- {
- return Result.Failure(Error.Problem("Auth.PasswordTooShort", $"비밀번호는 {minLength}자 이상이어야 합니다."));
- }
- // 대문자 검증
- if (config.PasswordUppercaseLength is > 0)
- {
- var uppercaseCount = password.Count(char.IsUpper);
- if (uppercaseCount < config.PasswordUppercaseLength)
- {
- return Result.Failure(Error.Problem("Auth.PasswordUppercase", $"비밀번호에 영문 대문자가 {config.PasswordUppercaseLength}자 이상 포함되어야 합니다."));
- }
- }
- // 숫자 검증
- if (config.PasswordNumbersLength is > 0)
- {
- var numberCount = password.Count(char.IsDigit);
- if (numberCount < config.PasswordNumbersLength)
- {
- return Result.Failure(Error.Problem("Auth.PasswordNumbers", $"비밀번호에 숫자가 {config.PasswordNumbersLength}자 이상 포함되어야 합니다."));
- }
- }
- // 특수문자 검증
- if (config.PasswordSpecialcharsLength is > 0)
- {
- var specialCount = password.Count(c => !char.IsLetterOrDigit(c));
- if (specialCount < config.PasswordSpecialcharsLength)
- {
- return Result.Failure(Error.Problem("Auth.PasswordSpecialChars", $"비밀번호에 특수문자가 {config.PasswordSpecialcharsLength}자 이상 포함되어야 합니다."));
- }
- }
- return Result.Success();
- }
- }
|