Skip to content

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

@G-Codeline

Description

@G-Codeline

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

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions