refactor: just try to parse and check expire without verification

This commit is contained in:
Derrick Hammer 2024-02-11 16:26:54 -05:00
parent 4b429e6d59
commit 0cf1b8827a
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 17 additions and 19 deletions

View File

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