feat: add independent Bell authentication and RBAC (#12)

This commit is contained in:
QiuSW
2026-08-12 17:44:31 +08:00
parent a5d846a437
commit a5fc8f5490
28 changed files with 840 additions and 22 deletions
+41
View File
@@ -0,0 +1,41 @@
package auth
import (
"errors"
"fmt"
"unicode"
"unicode/utf8"
"golang.org/x/crypto/bcrypt"
)
func validatePassword(value string) error {
if utf8.RuneCountInString(value) < 12 {
return errors.New("密码至少需要 12 个字符")
}
var lower, upper, digit bool
for _, r := range value {
lower = lower || unicode.IsLower(r)
upper = upper || unicode.IsUpper(r)
digit = digit || unicode.IsDigit(r)
}
if !lower || !upper || !digit {
return errors.New("密码必须同时包含大写字母、小写字母和数字")
}
return nil
}
func hashPassword(value string) (string, error) {
if err := validatePassword(value); err != nil {
return "", err
}
hash, err := bcrypt.GenerateFromPassword([]byte(value), bcrypt.DefaultCost)
if err != nil {
return "", fmt.Errorf("hash password: %w", err)
}
return string(hash), nil
}
func passwordMatches(hash, value string) bool {
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(value)) == nil
}