Skip to content

Conversation

paemuri
Copy link

@paemuri paemuri commented Aug 23, 2021

I know this commit leads to huge changes, so I'll understand any kind of opinion or feedback.

Also, thanks for this awesome project! :)

@aldas
Copy link
Contributor

aldas commented Aug 23, 2021

Probably will not happen before v5.

Meanwhile you can use JWTConfig.ParseTokenFunc callback for whatever library and version you choose. Documentation has example for that: https://echo.labstack.com/middleware/jwt/#custom-configuration

import (
"github.com/golang-jwt/jwt/v4"
)

...
...
...

signingKey := []byte("secret")

config := middleware.JWTConfig{
  TokenLookup: "query:token",
  ParseTokenFunc: func(auth string, c echo.Context) (interface{}, error) {
    keyFunc := func(t *jwt.Token) (interface{}, error) {
      if t.Method.Alg() != "HS256" {
        return nil, fmt.Errorf("unexpected jwt signing method=%v", t.Header["alg"])
      }
      return signingKey, nil
    }

    // claims are of type `jwt.MapClaims` when token is created with `jwt.Parse`
    token, err := jwt.Parse(auth, keyFunc)
    if err != nil {
      return nil, err
    }
    if !token.Valid {
      return nil, errors.New("invalid token")
    }
    return token, nil
  },
}

e.Use(middleware.JWTWithConfig(config))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants