dgrijalva/jwt-go

token is expired by 445101h17m58s

MunDeuksoo opened this issue · 2 comments

Hi
I looked up the document at the address below to use JWT.
https://godoc.org/github.com/dgrijalva/jwt-go#example-NewWithClaims--CustomClaimsType
I used ParseWithClaims and an error message like the title above occurred. No matter how much time you change, the same problem occurs. I'd like you to tell me the cause.

mySigningKey := []byte("AllYourBase")

	type MyCustomClaims struct {
		Foo string `json:"foo"`
		jwt.StandardClaims
	}
		etime := 5 * time.Minute

	// Create the Claims
	claims := &MyCustomClaims{
		"bar",
		jwt.StandardClaims{
			ExpiresAt: etime.Milliseconds(),
		},
	}

	t := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
	ss, err := t.SignedString(mySigningKey)
	fmt.Printf("%v %v", ss, err)

	tokenString := ss

		token, err := jwt.ParseWithClaims(tokenString, &MyCustomClaims{}, func(token *jwt.Token) (interface{}, error) {
			return []byte("AllYourBase"), nil
		})

		if claims, ok := token.Claims.(*MyCustomClaims); ok && token.Valid {
			fmt.Printf("%v %v", claims.Foo, claims.StandardClaims.ExpiresAt)
		} else {
			fmt.Println(claims.Foo,claims)
		}

You need to use unix time if you want the lib to validate the token.

now := time.Now()
etime := now.Add(5 * time.Minute).Unix()

Thank you. I mistake to convert time expression unix.
Example is Complete.