From d4365bfe986bd9e95bd4a789f5ed56be52448784 Mon Sep 17 00:00:00 2001 From: ShocOne <62835948+ShocOne@users.noreply.github.com> Date: Sat, 25 May 2024 18:20:32 +0100 Subject: [PATCH] chore: Refactor token acquisition and refresh logic in TokenManager --- authenticationhandler/tokenmanager.go | 39 ++++++++++++++++----------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/authenticationhandler/tokenmanager.go b/authenticationhandler/tokenmanager.go index 0b45e44..4944d10 100644 --- a/authenticationhandler/tokenmanager.go +++ b/authenticationhandler/tokenmanager.go @@ -16,6 +16,11 @@ func (h *AuthTokenHandler) CheckAndRefreshAuthToken(apiHandler apihandler.APIHan const maxConsecutiveRefreshAttempts = 10 refreshAttempts := 0 + if h.isTokenValid(tokenRefreshBufferPeriod) { + h.Logger.Info("Authentication token is valid", zap.Bool("IsTokenValid", true)) + return true, nil + } + for !h.isTokenValid(tokenRefreshBufferPeriod) { h.Logger.Debug("Token found to be invalid or close to expiry, handling token acquisition or refresh.") if err := h.obtainNewToken(apiHandler, httpClient, clientCredentials); err != nil { @@ -34,11 +39,6 @@ func (h *AuthTokenHandler) CheckAndRefreshAuthToken(apiHandler apihandler.APIHan } } - if err := h.refreshTokenIfNeeded(apiHandler, httpClient, clientCredentials, tokenRefreshBufferPeriod); err != nil { - h.Logger.Error("Failed to refresh token", zap.Error(err)) - return false, err - } - isValid := h.isTokenValid(tokenRefreshBufferPeriod) h.Logger.Info("Authentication token status check completed", zap.Bool("IsTokenValid", isValid)) return isValid, nil @@ -56,18 +56,27 @@ func (h *AuthTokenHandler) isTokenValid(tokenRefreshBufferPeriod time.Duration) // It handles different authentication methods based on the AuthMethod setting. func (h *AuthTokenHandler) obtainNewToken(apiHandler apihandler.APIHandler, httpClient *http.Client, clientCredentials ClientCredentials) error { var err error - if h.AuthMethod == "basicauth" { - err = h.BasicAuthTokenAcquisition(apiHandler, httpClient, clientCredentials.Username, clientCredentials.Password) - } else if h.AuthMethod == "oauth2" { - err = h.OAuth2TokenAcquisition(apiHandler, httpClient, clientCredentials.ClientID, clientCredentials.ClientSecret) - } else { - err = fmt.Errorf("no valid credentials provided. Unable to obtain a token") - h.Logger.Error("Authentication method not supported", zap.String("AuthMethod", h.AuthMethod)) - } + backoff := time.Millisecond * 100 + + for attempts := 0; attempts < 5; attempts++ { + if h.AuthMethod == "basicauth" { + err = h.BasicAuthTokenAcquisition(apiHandler, httpClient, clientCredentials.Username, clientCredentials.Password) + } else if h.AuthMethod == "oauth2" { + err = h.OAuth2TokenAcquisition(apiHandler, httpClient, clientCredentials.ClientID, clientCredentials.ClientSecret) + } else { + err = fmt.Errorf("no valid credentials provided. Unable to obtain a token") + h.Logger.Error("Authentication method not supported", zap.String("AuthMethod", h.AuthMethod)) + } - if err != nil { - h.Logger.Error("Failed to obtain new token", zap.Error(err)) + if err == nil { + break + } + + h.Logger.Error("Failed to obtain new token, retrying...", zap.Error(err), zap.Int("attempt", attempts+1)) + time.Sleep(backoff) + backoff *= 2 } + return err }