1- // http_token_management .go
1+ // http_client_auth_token_management .go
22package http_client
33
44import (
5+ "fmt"
56 "time"
67)
78
@@ -11,51 +12,42 @@ type TokenResponse struct {
1112 Expires time.Time `json:"expires"`
1213}
1314
14- // ValidAuthToken checks if the current token is valid and not close to expiry.
15+ // ValidAuthTokenCheck checks if the current token is valid and not close to expiry.
1516// If the token is invalid, it tries to refresh it.
16- func ( c * Client ) ValidAuthTokenCheck () bool {
17-
17+ // It returns a boolean indicating the validity of the token and an error if there's a failure.
18+ func ( c * Client ) ValidAuthTokenCheck () ( bool , error ) {
1819 // If token doesn't exist
1920 if c .Token == "" {
2021 if c .BearerTokenAuthCredentials .Username != "" && c .BearerTokenAuthCredentials .Password != "" {
2122 err := c .ObtainToken ()
2223 if err != nil {
23- return false
24+ return false , fmt . Errorf ( "failed to obtain bearer token: %w" , err )
2425 }
2526 } else if c .OAuthCredentials .ClientID != "" && c .OAuthCredentials .ClientSecret != "" {
2627 err := c .ObtainOAuthToken (c .OAuthCredentials )
2728 if err != nil {
28- return false
29+ return false , fmt . Errorf ( "failed to obtain OAuth token: %w" , err )
2930 }
3031 } else {
31- c .logger .Error ("No valid credentials provided. Unable to obtain a token." )
32- return false
32+ return false , fmt .Errorf ("no valid credentials provided. Unable to obtain a token" )
3333 }
3434 }
3535
3636 // If token exists and is close to expiry or already expired
3737 if time .Until (c .Expiry ) < c .config .TokenRefreshBufferPeriod {
38- if c .config .DebugMode {
39- c .logger .Debug ("Token is not valid or is close to expiry" , "Expiry" , c .Expiry )
40- }
41-
4238 var err error
4339 if c .BearerTokenAuthCredentials .Username != "" && c .BearerTokenAuthCredentials .Password != "" {
4440 err = c .RefreshToken ()
4541 } else if c .OAuthCredentials .ClientID != "" && c .OAuthCredentials .ClientSecret != "" {
4642 err = c .RefreshOAuthToken ()
4743 } else {
48- c .logger .Error ("Unknown auth method" , "AuthMethod" , c .authMethod )
49- return false
44+ return false , fmt .Errorf ("unknown auth method: %s" , c .authMethod )
5045 }
5146
5247 if err != nil {
53- return false
48+ return false , fmt . Errorf ( "failed to refresh token: %w" , err )
5449 }
5550 }
5651
57- if c .config .DebugMode {
58- c .logger .Debug ("Token is valid" , "Expiry" , c .Expiry )
59- }
60- return true
52+ return true , nil
6153}
0 commit comments