Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Answered by oxisto
Grabber asked this question in Q&A
Discussion options

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 must be logged in to vote

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

Replies: 3 comments 6 replies

Comment options

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

You must be logged in to vote
0 replies
Answer selected by oxisto
Comment options

@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
}
You must be logged in to vote
6 replies
Comment options

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, errors.New("unable to claim token")
 }
 return nil, err
}
Comment options

@oxisto, thank you so much!

Comment options

@oxisto

Sorry about asking. Do you have any idea WHY the parser is failing to extract the "Authorization: Bearer " from the request?

curl -vvv -X GET http://127.0.0.1:3333/testing -H "Accept: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzZW5zZWUiLCJzdWIiOiJzb21lYm9keSIsImF1ZCI6WyJzb21lYm9keV9lbHNlIl0sImV4cCI6MTY5NzU3MTY4MiwibmJmIjoxNjk3NDg1MjgyLCJpYXQiOjE2OTc0ODUyODIsImp0aSI6InBpcG9jYSJ9.IcrKi0HYSXxxcIQnin3Gbn2KACNtG9msHgc-lclxIiI"
token is malformed: could not JSON decode claim: json: cannot unmarshal object into Go value of type jwt.Claims
Comment options

It seems to you need to pass a pointer to your claims struct WithClaims(&jwt.RegisteredClaims{}, although I am not 100 % sure why.

Comment options

type fromRequestParser struct {
	req *http.Request
	extractor Extractor
	claims jwt.Claims
	parser *jwt.Parser
}
type ParseFromRequestOption func(*fromRequestParser)
// Parse with custom claims
func WithClaims(claims jwt.Claims) ParseFromRequestOption {
	return func(p *fromRequestParser) {
		p.claims = claims
	}
}
// Parse using a custom parser
func WithParser(parser *jwt.Parser) ParseFromRequestOption {
	return func(p *fromRequestParser) {
		p.parser = parser
	}
}

Shouldn't change func WithClaims(claims jwt.Claims) to func WithClaims(claims *jwt.Claims)?

Comment options

Shouldn't change func WithClaims(claims jwt.Claims) to func WithClaims(claims *jwt.Claims)?

No, because jwt.Claims is an interface. Unfortunately, the jwt.Claims interface accept both the non-pointer type jwt.RegisteredClaims as well as the pointer type *jwt.RegisteredClaims, but only the pointer type seems to work in all the cases.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #357 on October 16, 2023 05:46.

AltStyle によって変換されたページ (->オリジナル) /