完善证书管理与初始化流程
This commit is contained in:
1 parent
07636abe44
commit
c74372284a
84 files changed
+10151
-3502
No files matched your search
@@ -0,0 +1,39 @@
|
||||
package passwordpolicy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
const (
|
||||
MinLength = 8
|
||||
MaxLength = 128
|
||||
)
|
||||
|
||||
func Validate(password, username string) error {
|
||||
length := utf8.RuneCountInString(password)
|
||||
if length < MinLength {
|
||||
return fmt.Errorf("超级管理员密码至少需要 %d 个字符", MinLength)
|
||||
}
|
||||
if length > MaxLength {
|
||||
return fmt.Errorf("超级管理员密码最多允许 %d 个字符", MaxLength)
|
||||
}
|
||||
if strings.EqualFold(password, strings.TrimSpace(username)) {
|
||||
return fmt.Errorf("超级管理员密码不能与用户名相同")
|
||||
}
|
||||
hasLetter := false
|
||||
hasDigit := false
|
||||
for _, value := range password {
|
||||
if unicode.IsControl(value) {
|
||||
return fmt.Errorf("超级管理员密码不能包含控制字符")
|
||||
}
|
||||
hasLetter = hasLetter || unicode.IsLetter(value)
|
||||
hasDigit = hasDigit || unicode.IsDigit(value)
|
||||
}
|
||||
if !hasLetter || !hasDigit {
|
||||
return fmt.Errorf("超级管理员密码必须同时包含字母和数字")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package passwordpolicy
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValidate(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
password string
|
||||
username string
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "合法密码", password: "Password123", username: "admin"},
|
||||
{name: "支持中文字符", password: "安全密码2026", username: "admin"},
|
||||
{name: "长度不足", password: "Pass123", username: "admin", wantErr: true},
|
||||
{name: "缺少数字", password: "PasswordOnly", username: "admin", wantErr: true},
|
||||
{name: "缺少字母", password: "123456789", username: "admin", wantErr: true},
|
||||
{name: "等于用户名", password: "Admin123", username: "Admin123", wantErr: true},
|
||||
{name: "包含控制字符", password: "Password1\n", username: "admin", wantErr: true},
|
||||
{name: "长度超限", password: "Password1" + strings.Repeat("a", MaxLength), username: "admin", wantErr: true},
|
||||
}
|
||||
for _, test := range cases {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
err := Validate(test.password, test.username)
|
||||
if (err != nil) != test.wantErr {
|
||||
t.Fatalf("密码校验结果不正确:err=%v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user