-
Notifications
You must be signed in to change notification settings - Fork 420
-
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
}
}
Beta Was this translation helpful? Give feedback.
All reactions
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
-
You should be able to first create a new parser with jwt.New and jwt.WithLeeway and then use that with request.WithParser
Beta Was this translation helpful? Give feedback.
All reactions
-
@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
}
Beta Was this translation helpful? Give feedback.
All reactions
-
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
}
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 2
-
@oxisto, thank you so much!
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
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
Beta Was this translation helpful? Give feedback.
All reactions
-
It seems to you need to pass a pointer to your claims struct WithClaims(&jwt.RegisteredClaims{}, although I am not 100 % sure why.
Beta Was this translation helpful? Give feedback.
All reactions
-
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)?
Beta Was this translation helpful? Give feedback.
All reactions
-
Shouldn't change func
WithClaims(claims jwt.Claims)tofunc 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.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1