Skip to content

Bug fix: #198

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

Merged
merged 2 commits into from
May 24, 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
15 changes: 14 additions & 1 deletion authenticationhandler/tokenmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,25 @@ import (
// CheckAndRefreshAuthToken checks the token's validity and refreshes it if necessary.
// It returns true if the token is valid post any required operations and false with an error otherwise.
func (h *AuthTokenHandler) CheckAndRefreshAuthToken(apiHandler apihandler.APIHandler, httpClient *http.Client, clientCredentials ClientCredentials, tokenRefreshBufferPeriod time.Duration) (bool, error) {
if !h.isTokenValid(tokenRefreshBufferPeriod) {
const maxConsecutiveRefreshAttempts = 10
refreshAttempts := 0

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 {
h.Logger.Error("Failed to obtain new token", zap.Error(err))
return false, err
}

refreshAttempts++
if refreshAttempts >= maxConsecutiveRefreshAttempts {
return false, fmt.Errorf(
"exceeded maximum consecutive token refresh attempts (%d): access token lifetime (%s) is likely too short compared to the buffer period (%s) configured for token refresh",
maxConsecutiveRefreshAttempts,
h.Expires.Sub(time.Now()).String(), // Access token lifetime
tokenRefreshBufferPeriod.String(), // Configured buffer period
)
}
}

if err := h.refreshTokenIfNeeded(apiHandler, httpClient, clientCredentials, tokenRefreshBufferPeriod); err != nil {
Expand Down
Loading