fix(middleware): JWT 密钥未配置时返回 500 而非 401
密钥未配置属服务端配置错误,此前与 token 过期一样返回 401,把排查方向引向前端与刷新逻辑,是 P0-1 难以定位的直接原因。 改为识别 ErrSecretNotConfigured 后返回 500 server auth misconfigured 并打 FATAL 日志;JWTAuth、SoftJWTAuth 与 SSE 鉴权三处同步。
This commit is contained in:
@@ -34,6 +34,15 @@ func JWTAuth(accountRepo *account.AccountRepository, cache *rediscache.Client) g
|
||||
|
||||
claims, err := auth.ParseToken(tokenString)
|
||||
if err != nil {
|
||||
// 服务端未正确配置密钥:这是 500 级故障,必须打 FATAL 日志,
|
||||
// 绝不能伪装成 401,否则会出现静默误诊。
|
||||
if errors.Is(err, auth.ErrSecretNotConfigured) {
|
||||
log.Printf("FATAL: JWT secret not configured; %s %s rejected",
|
||||
c.Request.Method, c.Request.URL.Path)
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError,
|
||||
gin.H{"error": "server auth misconfigured"})
|
||||
return
|
||||
}
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid or expired token"})
|
||||
return
|
||||
}
|
||||
@@ -59,6 +68,15 @@ func SoftJWTAuth(accountRepo *account.AccountRepository, cache *rediscache.Clien
|
||||
|
||||
claims, err := auth.ParseToken(tokenString)
|
||||
if err != nil {
|
||||
// 服务端未正确配置密钥:这是 500 级故障,必须打 FATAL 日志,
|
||||
// 绝不能伪装成 401,否则会出现静默误诊。
|
||||
if errors.Is(err, auth.ErrSecretNotConfigured) {
|
||||
log.Printf("FATAL: JWT secret not configured; %s %s rejected",
|
||||
c.Request.Method, c.Request.URL.Path)
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError,
|
||||
gin.H{"error": "server auth misconfigured"})
|
||||
return
|
||||
}
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid or expired token"})
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user