I'd like to parse the expiration date (exp
) from a JSON Web Token (JWT) without verifying it. I've tried the following script (in an attempt to follow How to parse unix timestamp to time.Time):
package main
import (
"fmt"
"log"
"strconv"
"time"
jwt "github.com/dgrijalva/jwt-go"
)
func main() {
tokenString := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
token, _, err := new(jwt.Parser).ParseUnverified(tokenString, jwt.MapClaims{})
if err != nil {
log.Fatal(err)
}
if claims, ok := token.Claims.(jwt.MapClaims); ok {
fmt.Println(claims["exp"])
i, err := strconv.ParseInt(claims["exp"].(string), 10, 64)
if err != nil {
log.Fatal(err)
}
tm := time.Unix(i, 0)
fmt.Println(tm)
}
}
However, I get this error:
> go run main.go
<nil>
panic: interface conversion: interface {} is nil, not string
goroutine 1 [running]:
main.main()
/Users/kurt/go/src/github.com/khpeek/mygoproject/jwt_validation/main.go:23 +0x34a
exit status 2
Is there a way I could get a string for exp
out of the jwt
library? How could I make this give me the expiration date (or the iat
) of the JWT?