Files
neo-blog/pkg/utils/json_web_token.go

53 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package utils
import (
"github.com/golang-jwt/jwt/v5"
"github.com/snowykami/neo-blog/pkg/constant"
"time"
)
type jwtUtils struct{}
var Jwt = jwtUtils{}
type Claims struct {
jwt.RegisteredClaims
UserID uint `json:"user_id"`
SessionKey string `json:"session_key"` // 会话ID仅在有状态Token中使用
Stateful bool `json:"stateful"` // 是否为有状态Token
}
// NewClaims 创建一个新的Claims实例对于无状态
func (j *jwtUtils) NewClaims(userID uint, sessionKey string, stateful bool, duration time.Duration) *Claims {
return &Claims{
UserID: userID,
SessionKey: sessionKey,
Stateful: stateful,
RegisteredClaims: jwt.RegisteredClaims{
IssuedAt: jwt.NewNumericDate(time.Now()),
ExpiresAt: jwt.NewNumericDate(time.Now().Add(duration)),
},
}
}
// ToString 将Claims转换为JWT字符串
func (c *Claims) ToString() (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, c)
return token.SignedString([]byte(Env.Get(constant.EnvKeyJwtSecrete, "default_jwt_secret")))
}
// ParseJsonWebTokenWithoutState 解析JWT令牌不对有状态的Token进行状态检查
func (j *jwtUtils) ParseJsonWebTokenWithoutState(tokenString string) (*Claims, error) {
claims := &Claims{}
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (any, error) {
return []byte(Env.Get(constant.EnvKeyJwtSecrete, "default_jwt_secret")), nil
})
if err != nil {
return nil, err
}
if !token.Valid {
return nil, jwt.ErrSignatureInvalid
}
return claims, nil
}