Skip to content

Update JWT middleware #2079

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
AngangGuo opened this issue Jan 24, 2022 · 6 comments
Closed

Update JWT middleware #2079

AngangGuo opened this issue Jan 24, 2022 · 6 comments

Comments

@AngangGuo
Copy link

JWT has updated to v4.2.0 as for now. There're some improvement in v4 - Go module support, use RegisteredClaims instead of StandardClaims, etc.
Do you have any plan to update the JWT middleware to use this new version?

@aldas
Copy link
Contributor

aldas commented Jan 24, 2022

I am not sure if there will be switch off from jwt.MapClaims any time soon. This is because if you have default settings and have set token into context and get it from other middleware or handler you probably cast it into that type and changing that breaks things.

JWTConfig.ParseTokenFunc allows to you create your own token parsing function and use any version or library you like.

@AngangGuo
Copy link
Author

Will there be any conflict if I use v4.2.0 in my application and the middleware use v3.x?

@aldas
Copy link
Contributor

aldas commented Jan 24, 2022

If you create your own JWTConfig.ParseTokenFunc (copy implementation from here) and use imports from "github.com/golang-jwt/jwt/v4" you should be fine.

I think minimal implementation is:

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
  },
}

@AngangGuo
Copy link
Author

Got it. Great thanks.

@re-Tick
Copy link

re-Tick commented Mar 23, 2022

Hey but what if when we wants to use a custom claims which is embedding jwt.Standardclaims in it. Then, echo middleware's JWTConfig is showing type error of Claims field.

@re-Tick
Copy link

re-Tick commented Mar 23, 2022

Also since jwt.Token have Claims as field therefore jwt.KeyFunc signature is also changed and throwing type error. I think you should consider updating jwt version in echo jwt middleware.

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

3 participants