Skip to content

Commit df8cee1

Browse files
authored
Merge pull request #198 from deploymenttheory/dev
Bug fix:
2 parents 61bd56c + 166e0e1 commit df8cee1

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

authenticationhandler/tokenmanager.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,25 @@ import (
1313
// CheckAndRefreshAuthToken checks the token's validity and refreshes it if necessary.
1414
// It returns true if the token is valid post any required operations and false with an error otherwise.
1515
func (h *AuthTokenHandler) CheckAndRefreshAuthToken(apiHandler apihandler.APIHandler, httpClient *http.Client, clientCredentials ClientCredentials, tokenRefreshBufferPeriod time.Duration) (bool, error) {
16-
if !h.isTokenValid(tokenRefreshBufferPeriod) {
16+
const maxConsecutiveRefreshAttempts = 10
17+
refreshAttempts := 0
18+
19+
for !h.isTokenValid(tokenRefreshBufferPeriod) {
1720
h.Logger.Debug("Token found to be invalid or close to expiry, handling token acquisition or refresh.")
1821
if err := h.obtainNewToken(apiHandler, httpClient, clientCredentials); err != nil {
1922
h.Logger.Error("Failed to obtain new token", zap.Error(err))
2023
return false, err
2124
}
25+
26+
refreshAttempts++
27+
if refreshAttempts >= maxConsecutiveRefreshAttempts {
28+
return false, fmt.Errorf(
29+
"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",
30+
maxConsecutiveRefreshAttempts,
31+
h.Expires.Sub(time.Now()).String(), // Access token lifetime
32+
tokenRefreshBufferPeriod.String(), // Configured buffer period
33+
)
34+
}
2235
}
2336

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

0 commit comments

Comments
 (0)