完善证书管理与初始化流程
This commit is contained in:
1 parent
07636abe44
commit
c74372284a
84 files changed
+10151
-3502
No files matched your search
@@ -0,0 +1,37 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
const dummyPasswordHash = "$2a$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy"
|
||||
|
||||
type AdminAccount struct {
|
||||
Username string
|
||||
Email string
|
||||
Role string
|
||||
}
|
||||
|
||||
// AuthenticateAdmin 统一执行用户名或邮箱登录,并只返回后端确认的真实权限。
|
||||
func AuthenticateAdmin(identifier, password, username, email, passwordHash string) (AdminAccount, error) {
|
||||
matched := IsMatch(identifier, username, email)
|
||||
if !matched || strings.TrimSpace(password) == "" || len([]byte(password)) > 256 {
|
||||
_ = bcrypt.CompareHashAndPassword([]byte(dummyPasswordHash), []byte(password))
|
||||
return AdminAccount{}, ErrInvalidCredentials
|
||||
}
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(passwordHash), []byte(password)); err != nil {
|
||||
return AdminAccount{}, ErrInvalidCredentials
|
||||
}
|
||||
return AdminAccount{
|
||||
Username: strings.TrimSpace(username),
|
||||
Email: strings.TrimSpace(email),
|
||||
Role: "super_admin",
|
||||
}, nil
|
||||
}
|
||||
|
||||
func IsInvalidCredentials(err error) bool {
|
||||
return errors.Is(err, ErrInvalidCredentials)
|
||||
}
|
||||
Reference in new issue
Block a user