Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

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

Closed
Grabber opened this issue Oct 15, 2023 · 2 comments
Closed

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

Grabber opened this issue Oct 15, 2023 · 2 comments

Comments

@Grabber
Copy link

Grabber commented Oct 15, 2023

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
	}
}
@oxisto
Copy link
Collaborator

oxisto commented Oct 15, 2023

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

@Grabber
Copy link
Author

Grabber commented Oct 16, 2023

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

@golang-jwt golang-jwt locked and limited conversation to collaborators Oct 16, 2023
@oxisto oxisto converted this issue into discussion #358 Oct 16, 2023
@oxisto oxisto linked a pull request Nov 8, 2023 that will close this issue

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants