fix: 修复多个安全缺陷和功能问题
- 移除加密相关代码(服务端和所有客户端) - 修复 JWT 验证未检查签名算法的问题 - 修复重置密码使用不安全随机数的问题 - 实现 Logout 服务端失效机制 - 修复历史版本对比 diff 颜色标记错误 - 修复占位符解析循环引用风险 - 添加用户禁用状态检查 - 更新 bug 清单文档
This commit is contained in:
@@ -2,6 +2,7 @@ package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"kenaito-conf/server/internal/dao"
|
||||
"kenaito-conf/server/internal/model"
|
||||
"strconv"
|
||||
@@ -47,12 +48,14 @@ func (s *AuthService) Login(userName, password string) (string, *model.LoginUser
|
||||
s.loadPermissions(userInfo, user.Id)
|
||||
|
||||
// generate JWT
|
||||
now := time.Now()
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||
"userId": user.Id,
|
||||
"name": user.Name,
|
||||
"type": user.Type,
|
||||
"groupId": user.GroupId,
|
||||
"exp": time.Now().Add(time.Duration(s.expireHrs) * time.Hour).Unix(),
|
||||
"iat": now.Unix(),
|
||||
"exp": now.Add(time.Duration(s.expireHrs) * time.Hour).Unix(),
|
||||
})
|
||||
tokenString, err := token.SignedString([]byte(s.jwtSecret))
|
||||
if err != nil {
|
||||
@@ -64,6 +67,9 @@ func (s *AuthService) Login(userName, password string) (string, *model.LoginUser
|
||||
|
||||
func (s *AuthService) ValidateToken(tokenString string) (*model.LoginUserInfo, error) {
|
||||
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
return []byte(s.jwtSecret), nil
|
||||
})
|
||||
if err != nil {
|
||||
@@ -82,6 +88,19 @@ func (s *AuthService) ValidateToken(tokenString string) (*model.LoginUserInfo, e
|
||||
return nil, errors.New("user not found")
|
||||
}
|
||||
|
||||
// 检查用户是否已被禁用
|
||||
if !user.Enabled {
|
||||
return nil, errors.New("user is disabled")
|
||||
}
|
||||
|
||||
// 检查 token 是否在登出后签发
|
||||
if user.LastLogoutAt != nil {
|
||||
iat := int64(claims["iat"].(float64))
|
||||
if iat <= user.LastLogoutAt.Unix() {
|
||||
return nil, errors.New("token is invalid after logout")
|
||||
}
|
||||
}
|
||||
|
||||
userInfo := &model.LoginUserInfo{
|
||||
Id: strconv.Itoa(user.Id),
|
||||
Name: user.Name,
|
||||
|
||||
Reference in New Issue
Block a user