it took me an embarrising long time to learn that the jwt tokens encode the expiration time inside of them
(token string)
| 86 | |
| 87 | // it took me an embarrising long time to learn that the jwt tokens encode the expiration time inside of them |
| 88 | func GetJWTTokenExpirationUnix(token string) (*float64, error) { |
| 89 | // Split the JWT token into its components |
| 90 | parts := strings.Split(token, ".") |
| 91 | if len(parts) != 3 { |
| 92 | return nil, errors.New("invalid JWT token") |
| 93 | } |
| 94 | |
| 95 | // Decode the payload (2nd part of the JWT) |
| 96 | payload, err := base64.RawURLEncoding.DecodeString(parts[1]) |
| 97 | if err != nil { |
| 98 | return nil, err |
| 99 | } |
| 100 | |
| 101 | // Parse the payload into a map |
| 102 | var claims map[string]interface{} |
| 103 | err = json.Unmarshal(payload, &claims) |
| 104 | if err != nil { |
| 105 | return nil, err |
| 106 | } |
| 107 | |
| 108 | // Extract the `exp` claim |
| 109 | exp, ok := claims["exp"].(float64) |
| 110 | if !ok { |
| 111 | return nil, errors.New("expiration time not found in token") |
| 112 | } |
| 113 | return &exp, nil |
| 114 | } |
| 115 | |
| 116 | // Base64URLEncode encodes the given string using base64 and returns the result as a URL-safe string |
| 117 | func Base64URLEncode(input string) string { |
no outgoing calls
no test coverage detected