Skip to content
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

Can I set jwt.MapClaims "exp" to millisecond precision #113

Closed
G-Codeline opened this issue Oct 20, 2021 · 3 comments
Closed

Can I set jwt.MapClaims "exp" to millisecond precision #113

G-Codeline opened this issue Oct 20, 2021 · 3 comments

Comments

@G-Codeline
Copy link

I need to set the valid time of token to 100ms, but I got "Token is expired" err. is there any way to make it?

func TestJWTRsaMS(t *testing.T) {
privateKeyByte, _ := ioutil.ReadFile("private.key")
privateKey, _ := jwt.ParseRSAPrivateKeyFromPEM(privateKeyByte)

jwt.TimePrecision = time.Millisecond

publicKeyByte, _ := ioutil.ReadFile("private.key.pub")
publicKey, _ := jwt.ParseRSAPublicKeyFromPEM(publicKeyByte)

token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{
	"exp": time.Now().Add(time.Millisecond*time.Duration(100)).UnixNano() / 1e6,
	"iat": time.Now().UnixNano() / 1e6,
	"subject": map[string]string{
		"a": "123",
		"b": "321",
	},
})
tokenStr, err := token.SignedString(privateKey)
if err != nil {
	t.Logf("token.SignedString err:%s", err)
	return
}
_, err = jwt.Parse(tokenStr, func(token *jwt.Token) (interface{}, error) {
	return publicKey, nil
})
if err != nil {
	t.Logf("%s", err)
}

}

@oxisto
Copy link
Collaborator

oxisto commented Oct 31, 2021

There was some issue with parsing the exp value from MapClaims, especially when using large numbers or potentially sub-second numbers. This was fixed in #112. Can you give it another go with the current master, where #112 is now merged in.

@Waterdrips
Copy link
Member

Closing due to inactivity, feel free to reopen if the above response does not fix the issue

@elyshow
Copy link

elyshow commented Jul 22, 2022

I have the same problem. Is there any good solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants