2023-01-14 16:29:52 +08:00
|
|
|
|
package req
|
2020-09-01 10:34:11 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"errors"
|
2021-03-24 17:18:39 +08:00
|
|
|
|
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/biz"
|
|
|
|
|
|
"mayfly-go/pkg/config"
|
2022-07-27 15:36:56 +08:00
|
|
|
|
"mayfly-go/pkg/global"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/model"
|
2023-07-21 17:07:04 +08:00
|
|
|
|
"mayfly-go/pkg/utils/stringx"
|
2020-09-01 10:34:11 +08:00
|
|
|
|
"time"
|
2021-01-08 15:37:32 +08:00
|
|
|
|
|
2023-05-24 12:32:17 +08:00
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
2020-09-01 10:34:11 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2023-07-31 17:34:32 +08:00
|
|
|
|
// 初始化jwt key与expire time等
|
2022-08-10 19:46:17 +08:00
|
|
|
|
func InitTokenConfig() {
|
2023-07-31 17:34:32 +08:00
|
|
|
|
if ExpTime == 0 {
|
|
|
|
|
|
JwtKey = config.Conf.Jwt.Key
|
|
|
|
|
|
ExpTime = config.Conf.Jwt.ExpireTime
|
|
|
|
|
|
|
|
|
|
|
|
// 如果配置文件中的jwt key为空,则随机生成字符串
|
|
|
|
|
|
if JwtKey == "" {
|
|
|
|
|
|
JwtKey = stringx.Rand(32)
|
|
|
|
|
|
global.Log.Infof("config.yml未配置jwt.key, 随机生成key为: %s", JwtKey)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-08-10 19:46:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
|
JwtKey string
|
|
|
|
|
|
ExpTime uint64
|
2020-09-01 10:34:11 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 创建用户token
|
|
|
|
|
|
func CreateToken(userId uint64, username string) string {
|
2023-07-31 17:34:32 +08:00
|
|
|
|
InitTokenConfig()
|
|
|
|
|
|
|
2020-09-01 10:34:11 +08:00
|
|
|
|
// 带权限创建令牌
|
|
|
|
|
|
// 设置有效期,过期需要重新登录获取token
|
|
|
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
|
|
|
|
|
"id": userId,
|
|
|
|
|
|
"username": username,
|
2021-07-28 18:03:19 +08:00
|
|
|
|
"exp": time.Now().Add(time.Minute * time.Duration(ExpTime)).Unix(),
|
2020-09-01 10:34:11 +08:00
|
|
|
|
})
|
2022-08-10 19:46:17 +08:00
|
|
|
|
|
2020-09-01 10:34:11 +08:00
|
|
|
|
// 使用自定义字符串加密 and get the complete encoded token as a string
|
|
|
|
|
|
tokenString, err := token.SignedString([]byte(JwtKey))
|
2023-07-31 17:34:32 +08:00
|
|
|
|
biz.ErrIsNilAppendErr(err, "token创建失败: %s")
|
2020-09-01 10:34:11 +08:00
|
|
|
|
return tokenString
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 解析token,并返回登录者账号信息
|
2021-04-16 15:10:07 +08:00
|
|
|
|
func ParseToken(tokenStr string) (*model.LoginAccount, error) {
|
2020-09-01 10:34:11 +08:00
|
|
|
|
if tokenStr == "" {
|
|
|
|
|
|
return nil, errors.New("token error")
|
|
|
|
|
|
}
|
2023-07-31 17:34:32 +08:00
|
|
|
|
InitTokenConfig()
|
|
|
|
|
|
|
2020-09-01 10:34:11 +08:00
|
|
|
|
// Parse token
|
2023-06-01 12:31:32 +08:00
|
|
|
|
token, err := jwt.Parse(tokenStr, func(token *jwt.Token) (any, error) {
|
2020-09-01 10:34:11 +08:00
|
|
|
|
return []byte(JwtKey), nil
|
|
|
|
|
|
})
|
|
|
|
|
|
if err != nil || token == nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
i := token.Claims.(jwt.MapClaims)
|
2021-04-16 15:10:07 +08:00
|
|
|
|
return &model.LoginAccount{Id: uint64(i["id"].(float64)), Username: i["username"].(string)}, nil
|
2020-09-01 10:34:11 +08:00
|
|
|
|
}
|