diff options
author | Wim <wim@42.be> | 2017-12-07 23:00:56 +0100 |
---|---|---|
committer | Wim <wim@42.be> | 2017-12-07 23:00:56 +0100 |
commit | 5c919e6bffbf5dbe057623323effdbed2e82189e (patch) | |
tree | 8e0130ea7387ac1a38c8ceb5b85de1f43435bbe0 /vendor/github.com/labstack/echo/middleware/jwt.go | |
parent | 434393d1c3ade598bd38c98eefe7db93be4c9b27 (diff) | |
download | matterbridge-msglm-5c919e6bffbf5dbe057623323effdbed2e82189e.tar.gz matterbridge-msglm-5c919e6bffbf5dbe057623323effdbed2e82189e.tar.bz2 matterbridge-msglm-5c919e6bffbf5dbe057623323effdbed2e82189e.zip |
Update vendor labstack/echo
Diffstat (limited to 'vendor/github.com/labstack/echo/middleware/jwt.go')
-rw-r--r-- | vendor/github.com/labstack/echo/middleware/jwt.go | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/vendor/github.com/labstack/echo/middleware/jwt.go b/vendor/github.com/labstack/echo/middleware/jwt.go index 5d2072e7..47d885b0 100644 --- a/vendor/github.com/labstack/echo/middleware/jwt.go +++ b/vendor/github.com/labstack/echo/middleware/jwt.go @@ -1,7 +1,6 @@ package middleware import ( - "errors" "fmt" "net/http" "reflect" @@ -57,6 +56,12 @@ const ( AlgorithmHS256 = "HS256" ) +// Errors +var ( + ErrJWTMissing = echo.NewHTTPError(http.StatusBadRequest, "Missing or malformed jwt") + ErrJWTInvalid = echo.NewHTTPError(http.StatusUnauthorized, "Invalid or expired jwt") +) + var ( // DefaultJWTConfig is the default JWT auth middleware config. DefaultJWTConfig = JWTConfig{ @@ -77,7 +82,7 @@ var ( // // See: https://jwt.io/introduction // See `JWTConfig.TokenLookup` -func JWT(key []byte) echo.MiddlewareFunc { +func JWT(key interface{}) echo.MiddlewareFunc { c := DefaultJWTConfig c.SigningKey = key return JWTWithConfig(c) @@ -134,14 +139,15 @@ func JWTWithConfig(config JWTConfig) echo.MiddlewareFunc { auth, err := extractor(c) if err != nil { - return echo.NewHTTPError(http.StatusBadRequest, err.Error()) + return err } token := new(jwt.Token) // Issue #647, #656 if _, ok := config.Claims.(jwt.MapClaims); ok { token, err = jwt.Parse(auth, config.keyFunc) } else { - claims := reflect.ValueOf(config.Claims).Interface().(jwt.Claims) + t := reflect.ValueOf(config.Claims).Type().Elem() + claims := reflect.New(t).Interface().(jwt.Claims) token, err = jwt.ParseWithClaims(auth, claims, config.keyFunc) } if err == nil && token.Valid { @@ -149,7 +155,11 @@ func JWTWithConfig(config JWTConfig) echo.MiddlewareFunc { c.Set(config.ContextKey, token) return next(c) } - return echo.ErrUnauthorized + return &echo.HTTPError{ + Code: ErrJWTInvalid.Code, + Message: ErrJWTInvalid.Message, + Inner: err, + } } } } @@ -162,7 +172,7 @@ func jwtFromHeader(header string, authScheme string) jwtExtractor { if len(auth) > l+1 && auth[:l] == authScheme { return auth[l+1:], nil } - return "", errors.New("Missing or invalid jwt in the request header") + return "", ErrJWTMissing } } @@ -171,7 +181,7 @@ func jwtFromQuery(param string) jwtExtractor { return func(c echo.Context) (string, error) { token := c.QueryParam(param) if token == "" { - return "", errors.New("Missing jwt in the query string") + return "", ErrJWTMissing } return token, nil } @@ -182,7 +192,7 @@ func jwtFromCookie(name string) jwtExtractor { return func(c echo.Context) (string, error) { cookie, err := c.Cookie(name) if err != nil { - return "", errors.New("Missing jwt in the cookie") + return "", ErrJWTMissing } return cookie.Value, nil } |