From 0cf1b8827a993f77195949d2daff450a7bf59cd1 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 11 Feb 2024 16:26:54 -0500 Subject: [PATCH] refactor: just try to parse and check expire without verification --- api/oauth.go | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/api/oauth.go b/api/oauth.go index 19b706a..bdccd6a 100644 --- a/api/oauth.go +++ b/api/oauth.go @@ -57,30 +57,28 @@ func (o oauth) loadToken(config *oauth2.Config) { } if o.token != nil { - parsedToken, err := jwt.Parse(o.cfg.Oauth.Token, func(token *jwt.Token) (interface{}, error) { - return nil, nil - }) + valid := false + parseToken, _, err := new(jwt.Parser).ParseUnverified(o.cfg.Oauth.Token, jwt.MapClaims{}) if err != nil { - o.logger.Fatal("Error parsing token", zap.Error(err)) - } - - valid := false - - if claims, ok := parsedToken.Claims.(jwt.MapClaims); ok && parsedToken.Valid { - if exp, ok := claims["exp"].(float64); ok { - expirationTime := time.Unix(int64(exp), 0) - if time.Now().Before(expirationTime) { - valid = true - o.token.Expiry = expirationTime + o.logger.Error("Error parsing token", zap.Error(err)) + } else { + // Assert the token's claims to the desired type (MapClaims in this case) + if claims, ok := parseToken.Claims.(jwt.MapClaims); ok { + if exp, ok := claims["exp"].(float64); ok { + expirationTime := time.Unix(int64(exp), 0) + if time.Now().Before(expirationTime) { + valid = true + o.token.Expiry = expirationTime + } } } - } - if valid { - token = o.token - } else { - o.logger.Info("Token is expired, ignoring") + if valid { + token = o.token + } else { + o.logger.Info("Token is expired, ignoring") + } } }