Update jwt.go (#2016)

pull/2021/head
songzhibin97 3 years ago committed by GitHub
parent 1dfac59204
commit 9e66ac2f5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 25
      middleware/auth/jwt/jwt.go

@ -104,19 +104,22 @@ func Server(keyFunc jwt.Keyfunc, opts ...Option) middleware.Middleware {
tokenInfo, err = jwt.Parse(jwtToken, keyFunc) tokenInfo, err = jwt.Parse(jwtToken, keyFunc)
} }
if err != nil { if err != nil {
if ve, ok := err.(*jwt.ValidationError); ok { ve, ok := err.(*jwt.ValidationError)
if ve.Errors&jwt.ValidationErrorMalformed != 0 { if !ok {
return nil, ErrTokenInvalid return nil, errors.Unauthorized(reason, err.Error())
} else if ve.Errors&(jwt.ValidationErrorExpired|jwt.ValidationErrorNotValidYet) != 0 {
return nil, ErrTokenExpired
} else {
return nil, ErrTokenParseFail
}
} }
return nil, errors.Unauthorized(reason, err.Error()) if ve.Errors&jwt.ValidationErrorMalformed != 0 {
} else if !tokenInfo.Valid { return nil, ErrTokenInvalid
}
if ve.Errors&(jwt.ValidationErrorExpired|jwt.ValidationErrorNotValidYet) != 0 {
return nil, ErrTokenExpired
}
return nil, ErrTokenParseFail
}
if !tokenInfo.Valid {
return nil, ErrTokenInvalid return nil, ErrTokenInvalid
} else if tokenInfo.Method != o.signingMethod { }
if tokenInfo.Method != o.signingMethod {
return nil, ErrUnSupportSigningMethod return nil, ErrUnSupportSigningMethod
} }
ctx = NewContext(ctx, tokenInfo.Claims) ctx = NewContext(ctx, tokenInfo.Claims)

Loading…
Cancel
Save