jwt icon indicating copy to clipboard operation
jwt copied to clipboard

It seems that the default Claims may lose the type when the number is too large

Open sixwaaaay opened this issue 3 years ago • 0 comments

I use jwt.MapClaims to store the kv ("e", time.Now().Unix() + (a int64 value) ),and when I deserialize it to jwt.MapClaims , use m["e"].(int64), it panics and says that interface conversion: interface {} is float64, not int64.

sixwaaaay avatar Jun 06 '22 12:06 sixwaaaay

I have the same problem - if you log the value, it is logged as float64 for some reason. why does it try to parse as float?

OlegGulevskyy avatar Sep 19 '22 20:09 OlegGulevskyy

This is sort of a duplicate of #230. The problem is that a JWT is a JSON and JSON does not have integer values. In fact all JSON numbers are float values. You can try to work around with jwt.Parse(..., jwt.WithJSONNumber()) and then access it as json.Number. However, this will still have limitations for large numbers.

A better alternative, especially for time and date values is the jwt.RegisteredClaims which has a built-in type for time values.

oxisto avatar Sep 20 '22 07:09 oxisto

Thanks for the response @oxisto Yeah I figured that Registered Claims are better suited for this use case, so will go with them.

OlegGulevskyy avatar Sep 20 '22 07:09 OlegGulevskyy