From 9abcff9d30fbef3f6602cc1a04e22b9794c917fc Mon Sep 17 00:00:00 2001 From: songzhibin97 <49082129+songzhibin97@users.noreply.github.com> Date: Sat, 21 May 2022 16:24:21 +0800 Subject: [PATCH] Update jwt.go (#2016) --- middleware/auth/jwt/jwt.go | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/middleware/auth/jwt/jwt.go b/middleware/auth/jwt/jwt.go index 60dad2db2..2c65c209c 100644 --- a/middleware/auth/jwt/jwt.go +++ b/middleware/auth/jwt/jwt.go @@ -104,19 +104,22 @@ func Server(keyFunc jwt.Keyfunc, opts ...Option) middleware.Middleware { tokenInfo, err = jwt.Parse(jwtToken, keyFunc) } if err != nil { - if ve, ok := err.(*jwt.ValidationError); ok { - if ve.Errors&jwt.ValidationErrorMalformed != 0 { - return nil, ErrTokenInvalid - } else if ve.Errors&(jwt.ValidationErrorExpired|jwt.ValidationErrorNotValidYet) != 0 { - return nil, ErrTokenExpired - } else { - return nil, ErrTokenParseFail - } + ve, ok := err.(*jwt.ValidationError) + if !ok { + return nil, errors.Unauthorized(reason, err.Error()) } - return nil, errors.Unauthorized(reason, err.Error()) - } else if !tokenInfo.Valid { + if ve.Errors&jwt.ValidationErrorMalformed != 0 { + return nil, ErrTokenInvalid + } + if ve.Errors&(jwt.ValidationErrorExpired|jwt.ValidationErrorNotValidYet) != 0 { + return nil, ErrTokenExpired + } + return nil, ErrTokenParseFail + } + if !tokenInfo.Valid { return nil, ErrTokenInvalid - } else if tokenInfo.Method != o.signingMethod { + } + if tokenInfo.Method != o.signingMethod { return nil, ErrUnSupportSigningMethod } ctx = NewContext(ctx, tokenInfo.Claims)