fix(api): relax account password validation
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/infra"
|
||||
@@ -16,6 +17,14 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// AccountPasswordMinLength 是账号密码允许的最低字符数。
|
||||
const AccountPasswordMinLength = 6
|
||||
|
||||
// IsValidAccountPassword 仅校验账号密码的最低字符长度。
|
||||
func IsValidAccountPassword(password string) bool {
|
||||
return utf8.RuneCountInString(password) >= AccountPasswordMinLength
|
||||
}
|
||||
|
||||
// PasswordHash creates the shared password representation used by account modules.
|
||||
func PasswordHash(password string) (string, error) {
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
|
||||
24
backend/api/internal/logic/common/password_test.go
Normal file
24
backend/api/internal/logic/common/password_test.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package common
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestIsValidAccountPassword(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
password string
|
||||
want bool
|
||||
}{
|
||||
{name: "five characters", password: "12345", want: false},
|
||||
{name: "six digits", password: "123456", want: true},
|
||||
{name: "six letters without complexity", password: "abcdef", want: true},
|
||||
{name: "six unicode characters", password: "密码密码密码", want: true},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if got := IsValidAccountPassword(test.password); got != test.want {
|
||||
t.Fatalf("IsValidAccountPassword(%q) = %v, want %v", test.password, got, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user