@@ -16,6 +16,11 @@ func (h *AuthTokenHandler) CheckAndRefreshAuthToken(apiHandler apihandler.APIHan
16
16
const maxConsecutiveRefreshAttempts = 10
17
17
refreshAttempts := 0
18
18
19
+ if h .isTokenValid (tokenRefreshBufferPeriod ) {
20
+ h .Logger .Info ("Authentication token is valid" , zap .Bool ("IsTokenValid" , true ))
21
+ return true , nil
22
+ }
23
+
19
24
for ! h .isTokenValid (tokenRefreshBufferPeriod ) {
20
25
h .Logger .Debug ("Token found to be invalid or close to expiry, handling token acquisition or refresh." )
21
26
if err := h .obtainNewToken (apiHandler , httpClient , clientCredentials ); err != nil {
@@ -34,11 +39,6 @@ func (h *AuthTokenHandler) CheckAndRefreshAuthToken(apiHandler apihandler.APIHan
34
39
}
35
40
}
36
41
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
-
42
42
isValid := h .isTokenValid (tokenRefreshBufferPeriod )
43
43
h .Logger .Info ("Authentication token status check completed" , zap .Bool ("IsTokenValid" , isValid ))
44
44
return isValid , nil
@@ -56,18 +56,27 @@ func (h *AuthTokenHandler) isTokenValid(tokenRefreshBufferPeriod time.Duration)
56
56
// It handles different authentication methods based on the AuthMethod setting.
57
57
func (h * AuthTokenHandler ) obtainNewToken (apiHandler apihandler.APIHandler , httpClient * http.Client , clientCredentials ClientCredentials ) error {
58
58
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
+ }
67
70
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
70
78
}
79
+
71
80
return err
72
81
}
73
82
0 commit comments