u
This commit is contained in:
1 parent
8408cf779f
commit
25a9e41025
58 files changed
+2079
-6308
No files matched your search
@@ -1,37 +1,91 @@
|
||||
package jwtx
|
||||
|
||||
import (
|
||||
"cn/oneao/base-go/config"
|
||||
"cn/oneao/base-go/conf"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
)
|
||||
|
||||
// CustomClaims 自定义 payload,可扩展其他信息
|
||||
type CustomClaims struct {
|
||||
UserID int64 `json:"user_id"`
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
// TokenResult 用于返回验证结果
|
||||
type TokenResult struct {
|
||||
Claims *CustomClaims
|
||||
IsValid bool
|
||||
IsExpired bool
|
||||
}
|
||||
|
||||
// CreateAccessToken 创建 Access Token,只返回 token 和 error
|
||||
type jwtManager struct {
|
||||
secret string
|
||||
accessExpiry time.Duration
|
||||
refreshExpiry time.Duration
|
||||
}
|
||||
|
||||
var (
|
||||
manager *jwtManager
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
// InitJwt 初始化全局 JWT 配置,只能调用一次
|
||||
func InitJwt() {
|
||||
once.Do(func() {
|
||||
manager = &jwtManager{
|
||||
secret: conf.GetConf().JWT.Secret,
|
||||
accessExpiry: conf.GetConf().JWT.AccessExpiry,
|
||||
refreshExpiry: conf.GetConf().JWT.RefreshExpiry,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// CreateAccessToken 全局函数创建 Access Token
|
||||
func CreateAccessToken(userID int64) (string, error) {
|
||||
return createToken(userID, config.App.JWT.AccessExpiry)
|
||||
return manager.createToken(userID, manager.accessExpiry)
|
||||
}
|
||||
|
||||
// CreateRefreshToken 创建 Refresh Token,只返回 token 和 error
|
||||
// CreateRefreshToken 全局函数创建 Refresh Token
|
||||
func CreateRefreshToken(userID int64) (string, error) {
|
||||
return createToken(userID, config.App.JWT.RefreshExpiry)
|
||||
return manager.createToken(userID, manager.refreshExpiry)
|
||||
}
|
||||
|
||||
// 内部方法,根据过期时间生成 token
|
||||
func createToken(userID int64, expiry time.Duration) (string, error) {
|
||||
// VerifyToken 全局函数验证 Token
|
||||
func VerifyToken(tokenString string) TokenResult {
|
||||
result := TokenResult{IsValid: false, IsExpired: false}
|
||||
|
||||
if tokenString == "" || manager == nil {
|
||||
return result
|
||||
}
|
||||
|
||||
token, err := jwt.ParseWithClaims(tokenString, &CustomClaims{}, func(token *jwt.Token) (interface{}, error) {
|
||||
return []byte(manager.secret), nil
|
||||
})
|
||||
|
||||
if token == nil {
|
||||
return result
|
||||
}
|
||||
|
||||
claims, ok := token.Claims.(*CustomClaims)
|
||||
if !ok {
|
||||
return result
|
||||
}
|
||||
|
||||
if err != nil || !token.Valid {
|
||||
return result
|
||||
}
|
||||
|
||||
result.Claims = claims
|
||||
result.IsValid = true
|
||||
if claims.ExpiresAt != nil && time.Now().After(claims.ExpiresAt.Time) {
|
||||
result.IsExpired = true
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// 内部方法,生成 Token
|
||||
func (j *jwtManager) createToken(userID int64, expiry time.Duration) (string, error) {
|
||||
claims := &CustomClaims{
|
||||
UserID: userID,
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
@@ -41,65 +95,13 @@ func createToken(userID int64, expiry time.Duration) (string, error) {
|
||||
|
||||
switch {
|
||||
case expiry > 0:
|
||||
expireAt := time.Now().Add(expiry)
|
||||
claims.ExpiresAt = jwt.NewNumericDate(expireAt)
|
||||
claims.ExpiresAt = jwt.NewNumericDate(time.Now().Add(expiry))
|
||||
case expiry == -1:
|
||||
claims.ExpiresAt = nil // 永不过期
|
||||
default:
|
||||
expireAt := time.Now().Add(time.Hour)
|
||||
claims.ExpiresAt = jwt.NewNumericDate(expireAt)
|
||||
claims.ExpiresAt = jwt.NewNumericDate(time.Now().Add(time.Hour))
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
signed, err := token.SignedString([]byte(config.App.JWT.Secret))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return signed, nil
|
||||
}
|
||||
|
||||
// VerifyToken 验证 token 并返回结构化结果
|
||||
func VerifyToken(tokenString string) TokenResult {
|
||||
result := TokenResult{
|
||||
IsValid: false,
|
||||
IsExpired: false,
|
||||
}
|
||||
|
||||
// 🧩 1. token 为空
|
||||
if tokenString == "" {
|
||||
return result
|
||||
}
|
||||
|
||||
// 🧩 2. 解析 token
|
||||
token, err := jwt.ParseWithClaims(tokenString, &CustomClaims{}, func(token *jwt.Token) (interface{}, error) {
|
||||
return []byte(config.App.JWT.Secret), nil
|
||||
})
|
||||
|
||||
// 🧩 3. token 为 nil(解析异常)
|
||||
if token == nil {
|
||||
return result
|
||||
}
|
||||
|
||||
// 🧩 4. 提取 claims 并验证
|
||||
claims, ok := token.Claims.(*CustomClaims)
|
||||
if !ok {
|
||||
return result
|
||||
}
|
||||
|
||||
// 🧩 5. 解析失败或签名无效
|
||||
if err != nil || !token.Valid {
|
||||
return result
|
||||
}
|
||||
|
||||
// ✅ 6. 填充结果
|
||||
result.Claims = claims
|
||||
result.IsValid = true
|
||||
|
||||
// 🧩 7. 检查是否过期
|
||||
if claims.ExpiresAt != nil && time.Now().After(claims.ExpiresAt.Time) {
|
||||
result.IsExpired = true
|
||||
}
|
||||
|
||||
return result
|
||||
return token.SignedString([]byte(j.secret))
|
||||
}
|
||||
Reference in new issue
Block a user