feat: simplify Sense password policy (#42)
This commit is contained in:
@@ -2,28 +2,14 @@ package identity
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func ValidatePassword(username, password string) error {
|
||||
func ValidatePassword(_ string, password string) error {
|
||||
if len([]rune(password)) < 6 {
|
||||
return fmt.Errorf("密码至少需要 6 个字符")
|
||||
}
|
||||
var upper, lower, digit bool
|
||||
for _, value := range password {
|
||||
upper = upper || unicode.IsUpper(value)
|
||||
lower = lower || unicode.IsLower(value)
|
||||
digit = digit || unicode.IsDigit(value)
|
||||
}
|
||||
if !upper || !lower || !digit {
|
||||
return fmt.Errorf("密码必须同时包含大写字母、小写字母和数字")
|
||||
}
|
||||
if username != "" && strings.Contains(strings.ToLower(password), strings.ToLower(username)) {
|
||||
return fmt.Errorf("密码不能包含用户名")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -12,12 +12,10 @@ func TestValidatePasswordLengthAndComplexity(t *testing.T) {
|
||||
password string
|
||||
want string
|
||||
}{
|
||||
{name: "six characters", username: "operator", password: "Abc123"},
|
||||
{name: "six lowercase characters", username: "operator", password: "abcdef"},
|
||||
{name: "contains username", username: "admin", password: "admin123"},
|
||||
{name: "six unicode characters", username: "operator", password: "密码密码密码"},
|
||||
{name: "five characters", username: "operator", password: "Ab123", want: "至少需要 6 个字符"},
|
||||
{name: "missing uppercase", username: "operator", password: "abc123", want: "大写字母、小写字母和数字"},
|
||||
{name: "missing lowercase", username: "operator", password: "ABC123", want: "大写字母、小写字母和数字"},
|
||||
{name: "missing digit", username: "operator", password: "Abcdef", want: "大写字母、小写字母和数字"},
|
||||
{name: "contains username", username: "admin", password: "MyAdmin1", want: "不能包含用户名"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
|
||||
Reference in New Issue
Block a user