Skip to content
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

Is there any plan to upgrade golang-jwt to v4? #2222

Closed
pancpp opened this issue Jul 15, 2022 · 2 comments
Closed

Is there any plan to upgrade golang-jwt to v4? #2222

pancpp opened this issue Jul 15, 2022 · 2 comments

Comments

@pancpp
Copy link

pancpp commented Jul 15, 2022

As per the title, is there any plan to upgrade the jwt middleware to use github.com/golang-jwt/jwt/v4?

@aldas
Copy link
Contributor

aldas commented Jul 15, 2022

No at the moment. In v5 are are removing direct dependency from golang-jwt/jwt library from core middleware and have version of JWT middleware in echo-contrib using golang-jwt/jwt

Upgrading JWT to golang-jwt/jwt/v4 is breaking change and it is a quite sneaky change as Echo would start to use v4 structs and insert them into context but your middlewares/handler (whereever you are checking claims) are still importing v3 struct and if you do not have tests your requests will panic when you do the cast. So it would create havoc for unaware users bumping Echo version to latest.

If you really want jwt/v4 then you can create ParseTokenFunc implementation that uses v4 instead of v3:

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))

@pancpp
Copy link
Author

pancpp commented Jul 17, 2022

@aldas that explanation makes good sense and I really appreciate your example! Close the issue.

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

No branches or pull requests

2 participants