golang-jwt / jwt

Go implementation of JSON Web Tokens (JWT).

Home Page:https://golang-jwt.github.io/jwt/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

v5.0.0/request/request.go: with WithLeeway support?

Grabber opened this issue · comments

Is it possible to use WithLeeway with request.ParseFromRequest as with jwt.ParseWithClaims?
-> https://github.com/golang-jwt/jwt/blob/v5.0.0/request/request.go

func WithLeeway(leeway time.Duration) ParserOption {
	return func(p *Parser) {
		p.validator.leeway = leeway
	}
}
func WithClaims(claims jwt.Claims) ParseFromRequestOption {
	return func(p *fromRequestParser) {
		p.claims = claims
	}
}

You should be able to first create a new parser with jwt.New and jwt.WithLeeway and then use that with request.WithParser

@oxisto, that you!
Is the following snippet correct?

func VerifyJWToken(r *http.Request, secretKey []byte) (*jwt.RegisteredClaims, error) {
  token, err := request.ParseFromRequest(r, request.AuthorizationHeaderExtractor, func(token *jwt.Token) (interface{}, error) {
    return secretKey, nil
  }, request.WithClaims(jwt.RegisteredClaims{}), request.WithParser(jwt.NewParser(jwt.WithLeeway(5 * time.Second))))

  if err == nil {
    if claims, ok := token.Claims.(*jwt.RegisteredClaims); ok && token.Valid {
      return claims, nil
    }
  }

  return nil, err
}