@@ -16,6 +16,11 @@ func (h *AuthTokenHandler) CheckAndRefreshAuthToken(apiHandler apihandler.APIHan
1616 const maxConsecutiveRefreshAttempts = 10
1717 refreshAttempts := 0
1818
19+ if h .isTokenValid (tokenRefreshBufferPeriod ) {
20+ h .Logger .Info ("Authentication token is valid" , zap .Bool ("IsTokenValid" , true ))
21+ return true , nil
22+ }
23+
1924 for ! h .isTokenValid (tokenRefreshBufferPeriod ) {
2025 h .Logger .Debug ("Token found to be invalid or close to expiry, handling token acquisition or refresh." )
2126 if err := h .obtainNewToken (apiHandler , httpClient , clientCredentials ); err != nil {
@@ -34,11 +39,6 @@ func (h *AuthTokenHandler) CheckAndRefreshAuthToken(apiHandler apihandler.APIHan
3439 }
3540 }
3641
37- if err := h .refreshTokenIfNeeded (apiHandler , httpClient , clientCredentials , tokenRefreshBufferPeriod ); err != nil {
38- h .Logger .Error ("Failed to refresh token" , zap .Error (err ))
39- return false , err
40- }
41-
4242 isValid := h .isTokenValid (tokenRefreshBufferPeriod )
4343 h .Logger .Info ("Authentication token status check completed" , zap .Bool ("IsTokenValid" , isValid ))
4444 return isValid , nil
@@ -56,18 +56,27 @@ func (h *AuthTokenHandler) isTokenValid(tokenRefreshBufferPeriod time.Duration)
5656// It handles different authentication methods based on the AuthMethod setting.
5757func (h * AuthTokenHandler ) obtainNewToken (apiHandler apihandler.APIHandler , httpClient * http.Client , clientCredentials ClientCredentials ) error {
5858 var err error
59- if h .AuthMethod == "basicauth" {
60- err = h .BasicAuthTokenAcquisition (apiHandler , httpClient , clientCredentials .Username , clientCredentials .Password )
61- } else if h .AuthMethod == "oauth2" {
62- err = h .OAuth2TokenAcquisition (apiHandler , httpClient , clientCredentials .ClientID , clientCredentials .ClientSecret )
63- } else {
64- err = fmt .Errorf ("no valid credentials provided. Unable to obtain a token" )
65- h .Logger .Error ("Authentication method not supported" , zap .String ("AuthMethod" , h .AuthMethod ))
66- }
59+ backoff := time .Millisecond * 100
60+
61+ for attempts := 0 ; attempts < 5 ; attempts ++ {
62+ if h .AuthMethod == "basicauth" {
63+ err = h .BasicAuthTokenAcquisition (apiHandler , httpClient , clientCredentials .Username , clientCredentials .Password )
64+ } else if h .AuthMethod == "oauth2" {
65+ err = h .OAuth2TokenAcquisition (apiHandler , httpClient , clientCredentials .ClientID , clientCredentials .ClientSecret )
66+ } else {
67+ err = fmt .Errorf ("no valid credentials provided. Unable to obtain a token" )
68+ h .Logger .Error ("Authentication method not supported" , zap .String ("AuthMethod" , h .AuthMethod ))
69+ }
6770
68- if err != nil {
69- h .Logger .Error ("Failed to obtain new token" , zap .Error (err ))
71+ if err == nil {
72+ break
73+ }
74+
75+ h .Logger .Error ("Failed to obtain new token, retrying..." , zap .Error (err ), zap .Int ("attempt" , attempts + 1 ))
76+ time .Sleep (backoff )
77+ backoff *= 2
7078 }
79+
7180 return err
7281}
7382
0 commit comments