优化
This commit is contained in:
+17
-10
@@ -3,10 +3,13 @@ package biz
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
type SecurityConfig struct {
|
||||
@@ -90,33 +93,37 @@ func (uc *SecurityUsecase) Current(ctx context.Context) (*SecurityConfig, error)
|
||||
}
|
||||
|
||||
func (uc *SecurityUsecase) ValidatePassword(value *SecurityConfig, password string) error {
|
||||
if len([]rune(password)) < value.PwdMinLength {
|
||||
return errors.New("密码长度不足")
|
||||
if value.PwdMinLength > 0 && utf8.RuneCountInString(password) < value.PwdMinLength {
|
||||
return fmt.Errorf("密码长度不能少于 %d 位", value.PwdMinLength)
|
||||
}
|
||||
hasUpper, hasLower, hasDigit, hasSpecial := false, false, false, false
|
||||
for _, ch := range password {
|
||||
switch {
|
||||
case ch >= 'A' && ch <= 'Z':
|
||||
case unicode.IsUpper(ch):
|
||||
hasUpper = true
|
||||
case ch >= 'a' && ch <= 'z':
|
||||
case unicode.IsLower(ch):
|
||||
hasLower = true
|
||||
case ch >= '0' && ch <= '9':
|
||||
case unicode.IsDigit(ch):
|
||||
hasDigit = true
|
||||
default:
|
||||
case unicode.IsPunct(ch) || unicode.IsSymbol(ch):
|
||||
hasSpecial = true
|
||||
}
|
||||
}
|
||||
missing := make([]string, 0, 4)
|
||||
if value.PwdRequireUpper && !hasUpper {
|
||||
return errors.New("密码必须包含大写字母")
|
||||
missing = append(missing, "大写字母")
|
||||
}
|
||||
if value.PwdRequireLower && !hasLower {
|
||||
return errors.New("密码必须包含小写字母")
|
||||
missing = append(missing, "小写字母")
|
||||
}
|
||||
if value.PwdRequireDigit && !hasDigit {
|
||||
return errors.New("密码必须包含数字")
|
||||
missing = append(missing, "数字")
|
||||
}
|
||||
if value.PwdRequireSpecial && !hasSpecial {
|
||||
return errors.New("密码必须包含特殊字符")
|
||||
missing = append(missing, "特殊字符")
|
||||
}
|
||||
if len(missing) > 0 {
|
||||
return fmt.Errorf("密码必须包含%s", strings.Join(missing, "、"))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user