补充测试用例

This commit is contained in:
2025-09-13 19:48:13 +08:00
parent ec2595a751
commit bd22e452d3
9 changed files with 353 additions and 117 deletions

View File

@@ -1,6 +1,7 @@
package token
import (
"fmt"
"time"
"github.com/golang-jwt/jwt/v5"
@@ -52,9 +53,16 @@ func (s *tokenService) ParseToken(tokenString string) (*Claims, error) {
return s.secret, nil
})
// 优先检查解析过程中是否发生错误
if err != nil {
return nil, err
}
// 只有当 token 对象有效时,才尝试获取 Claims 并验证
if claims, ok := token.Claims.(*Claims); ok && token.Valid {
return claims, nil
}
return nil, err
// 如果 token 无效(例如,过期但没有返回错误,或者 Claims 类型不匹配),则返回一个通用错误
return nil, fmt.Errorf("token is invalid")
}