Skip to content

Exposing KeyFunc #1303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions middleware/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"reflect"
"strings"

"github.com/dgrijalva/jwt-go"
jwt "github.com/dgrijalva/jwt-go"
"github.com/labstack/echo/v4"
)

Expand Down Expand Up @@ -55,7 +55,7 @@ type (
// Optional. Default value "Bearer".
AuthScheme string

keyFunc jwt.Keyfunc
KeyFunc jwt.Keyfunc
}

// JWTSuccessHandler defines a function which is executed for a valid token.
Expand Down Expand Up @@ -110,9 +110,13 @@ func JWTWithConfig(config JWTConfig) echo.MiddlewareFunc {
if config.Skipper == nil {
config.Skipper = DefaultJWTConfig.Skipper
}
if config.SigningKey == nil {
if config.SigningKey == nil && config.KeyFunc == nil {
panic("echo: jwt middleware requires signing key")
}

if config.SigningKey != nil && config.KeyFunc == nil {
config.KeyFunc = config.staticKeyFetch
}
if config.SigningMethod == "" {
config.SigningMethod = DefaultJWTConfig.SigningMethod
}
Expand All @@ -128,13 +132,6 @@ func JWTWithConfig(config JWTConfig) echo.MiddlewareFunc {
if config.AuthScheme == "" {
config.AuthScheme = DefaultJWTConfig.AuthScheme
}
config.keyFunc = func(t *jwt.Token) (interface{}, error) {
// Check the signing method
if t.Method.Alg() != config.SigningMethod {
return nil, fmt.Errorf("unexpected jwt signing method=%v", t.Header["alg"])
}
return config.SigningKey, nil
}

// Initialize
parts := strings.Split(config.TokenLookup, ":")
Expand Down Expand Up @@ -166,11 +163,11 @@ func JWTWithConfig(config JWTConfig) echo.MiddlewareFunc {
token := new(jwt.Token)
// Issue #647, #656
if _, ok := config.Claims.(jwt.MapClaims); ok {
token, err = jwt.Parse(auth, config.keyFunc)
token, err = jwt.Parse(auth, config.KeyFunc)
} else {
t := reflect.ValueOf(config.Claims).Type().Elem()
claims := reflect.New(t).Interface().(jwt.Claims)
token, err = jwt.ParseWithClaims(auth, claims, config.keyFunc)
token, err = jwt.ParseWithClaims(auth, claims, config.KeyFunc)
}
if err == nil && token.Valid {
// Store user information from token into context.
Expand Down Expand Up @@ -225,3 +222,12 @@ func jwtFromCookie(name string) jwtExtractor {
return cookie.Value, nil
}
}

// staticKeyFetch
func (config *JWTConfig) staticKeyFetch(t *jwt.Token) (interface{}, error) {
// Check the signing method
if t.Method.Alg() != config.SigningMethod {
return nil, fmt.Errorf("unexpected jwt signing method=%v", t.Header["alg"])
}
return config.SigningKey, nil
}
10 changes: 9 additions & 1 deletion middleware/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http/httptest"
"testing"

"github.com/dgrijalva/jwt-go"
jwt "github.com/dgrijalva/jwt-go"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -184,6 +184,14 @@ func TestJWT(t *testing.T) {
expErrCode: http.StatusBadRequest,
info: "Empty cookie",
},
{
config: JWTConfig{
SigningKey: validKey,
SigningMethod: "RS256",
},
expErrCode: http.StatusBadRequest,
info: "Invalid algorithm",
},
} {
if tc.reqURL == "" {
tc.reqURL = "/"
Expand Down