From f2bc6802be74c688212fa0e81e08f0e59b180f73 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Thu, 31 Aug 2017 10:16:34 -0700 Subject: [PATCH] Exposed JWT error Signed-off-by: Vishal Rana --- middleware/jwt.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/middleware/jwt.go b/middleware/jwt.go index 7ee72712..47d885b0 100644 --- a/middleware/jwt.go +++ b/middleware/jwt.go @@ -58,7 +58,8 @@ const ( // Errors var ( - ErrJWTInvalid = echo.NewHTTPError(http.StatusBadRequest, "Missing or invalid jwt") + ErrJWTMissing = echo.NewHTTPError(http.StatusBadRequest, "Missing or malformed jwt") + ErrJWTInvalid = echo.NewHTTPError(http.StatusUnauthorized, "Invalid or expired jwt") ) var ( @@ -154,9 +155,11 @@ func JWTWithConfig(config JWTConfig) echo.MiddlewareFunc { c.Set(config.ContextKey, token) return next(c) } - he := echo.NewHTTPError(http.StatusUnauthorized, "Invalid or expired jwt") - he.Inner = err - return he + return &echo.HTTPError{ + Code: ErrJWTInvalid.Code, + Message: ErrJWTInvalid.Message, + Inner: err, + } } } } @@ -169,7 +172,7 @@ func jwtFromHeader(header string, authScheme string) jwtExtractor { if len(auth) > l+1 && auth[:l] == authScheme { return auth[l+1:], nil } - return "", ErrJWTInvalid + return "", ErrJWTMissing } } @@ -178,7 +181,7 @@ func jwtFromQuery(param string) jwtExtractor { return func(c echo.Context) (string, error) { token := c.QueryParam(param) if token == "" { - return "", ErrJWTInvalid + return "", ErrJWTMissing } return token, nil } @@ -189,7 +192,7 @@ func jwtFromCookie(name string) jwtExtractor { return func(c echo.Context) (string, error) { cookie, err := c.Cookie(name) if err != nil { - return "", ErrJWTInvalid + return "", ErrJWTMissing } return cookie.Value, nil }