Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Refactor token acquisition and refresh logic in TokenManager #199

Merged
merged 1 commit into from
May 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions authenticationhandler/tokenmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
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 {
Expand All @@ -34,11 +39,6 @@
}
}

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
Expand All @@ -56,18 +56,27 @@
// 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))

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
}

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))

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
time.Sleep(backoff)
backoff *= 2
}

return err
}

Expand Down
Loading