u
This commit is contained in:
1 parent
7971c527af
commit
b8a6b15355
27 files changed
+480
-399
No files matched your search
@@ -61,19 +61,42 @@ func createToken(userID int64, expiry time.Duration) (string, error) {
|
||||
|
||||
// VerifyToken 验证 token 并返回结构化结果
|
||||
func VerifyToken(tokenString string) TokenResult {
|
||||
result := TokenResult{IsValid: false, IsExpired: false}
|
||||
result := TokenResult{
|
||||
IsValid: false,
|
||||
IsExpired: false,
|
||||
}
|
||||
|
||||
token, _ := jwt.ParseWithClaims(tokenString, &CustomClaims{}, func(token *jwt.Token) (interface{}, error) {
|
||||
// 🧩 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
|
||||
})
|
||||
|
||||
claims, ok := token.Claims.(*CustomClaims)
|
||||
if !ok || !token.Valid {
|
||||
return result // token 无效
|
||||
// 🧩 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
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user