Skip to content

Commit

Permalink
Custom jwt errors (labstack#999)
Browse files Browse the repository at this point in the history
* Custom error for jwt
* New field `inner` in HTTPError to store error from external dependency

Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr authored Aug 31, 2017
1 parent cec7629 commit 7dfec7e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go
go:
- 1.7.x
- 1.8.x
- 1.9.x
- tip
install:
- make dependency
Expand Down
17 changes: 10 additions & 7 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type (
HTTPError struct {
Code int
Message interface{}
Inner error // Stores the error returned by an external dependency
}

// MiddlewareFunc defines a function to process middleware.
Expand Down Expand Up @@ -321,6 +322,9 @@ func (e *Echo) DefaultHTTPErrorHandler(err error, c Context) {
msg = he.Message
} else if e.Debug {
msg = err.Error()
if he.Inner != nil {
msg = fmt.Sprintf("%v, %v", err, he.Inner)
}
} else {
msg = http.StatusText(code)
}
Expand All @@ -330,16 +334,15 @@ func (e *Echo) DefaultHTTPErrorHandler(err error, c Context) {

if !c.Response().Committed {
if c.Request().Method == HEAD { // Issue #608
if err := c.NoContent(code); err != nil {
goto ERROR
}
err = c.NoContent(code)
} else {
if err := c.JSON(code, msg); err != nil {
goto ERROR
}
err = c.JSON(code, msg)
}
if err != nil {
e.Logger.Error(err)
}
}
ERROR:

e.Logger.Error(err)
}

Expand Down
18 changes: 12 additions & 6 deletions middleware/jwt.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package middleware

import (
"errors"
"fmt"
"net/http"
"reflect"
Expand Down Expand Up @@ -57,6 +56,11 @@ const (
AlgorithmHS256 = "HS256"
)

// Errors
var (
ErrJWTInvalid = echo.NewHTTPError(http.StatusBadRequest, "Missing or invalid jwt")
)

var (
// DefaultJWTConfig is the default JWT auth middleware config.
DefaultJWTConfig = JWTConfig{
Expand Down Expand Up @@ -134,7 +138,7 @@ 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
Expand All @@ -150,7 +154,9 @@ func JWTWithConfig(config JWTConfig) echo.MiddlewareFunc {
c.Set(config.ContextKey, token)
return next(c)
}
return echo.ErrUnauthorized
he := echo.NewHTTPError(http.StatusUnauthorized, "Invalid or expired jwt")
he.Inner = err
return he
}
}
}
Expand All @@ -163,7 +169,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 "", ErrJWTInvalid
}
}

Expand All @@ -172,7 +178,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 "", ErrJWTInvalid
}
return token, nil
}
Expand All @@ -183,7 +189,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 "", ErrJWTInvalid
}
return cookie.Value, nil
}
Expand Down

0 comments on commit 7dfec7e

Please sign in to comment.