|
| 1 | +// authenticationhandler/tokenmanager.go |
| 2 | +package authenticationhandler |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/deploymenttheory/go-api-http-client/apiintegrations/apihandler" |
| 10 | + "go.uber.org/zap" |
| 11 | +) |
| 12 | + |
| 13 | +// CheckAndRefreshAuthToken checks the token's validity and refreshes it if necessary. |
| 14 | +// It returns true if the token is valid post any required operations and false with an error otherwise. |
| 15 | +func (h *AuthTokenHandler) CheckAndRefreshAuthToken(apiHandler apihandler.APIHandler, httpClient *http.Client, clientCredentials ClientCredentials, tokenRefreshBufferPeriod time.Duration) (bool, error) { |
| 16 | + if !h.isTokenValid(tokenRefreshBufferPeriod) { |
| 17 | + h.Logger.Debug("Token found to be invalid or close to expiry, handling token acquisition or refresh.") |
| 18 | + if err := h.obtainNewToken(apiHandler, httpClient, clientCredentials); err != nil { |
| 19 | + h.Logger.Error("Failed to obtain new token", zap.Error(err)) |
| 20 | + return false, err |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + if err := h.refreshTokenIfNeeded(apiHandler, httpClient, clientCredentials, tokenRefreshBufferPeriod); err != nil { |
| 25 | + h.Logger.Error("Failed to refresh token", zap.Error(err)) |
| 26 | + return false, err |
| 27 | + } |
| 28 | + |
| 29 | + isValid := h.isTokenValid(tokenRefreshBufferPeriod) |
| 30 | + h.Logger.Info("Token validation status post check", zap.Bool("IsValid", isValid)) |
| 31 | + return isValid, nil |
| 32 | +} |
| 33 | + |
| 34 | +// isTokenValid checks if the current token is non-empty and not about to expire. |
| 35 | +// It considers a token valid if it exists and the time until its expiration is greater than the provided buffer period. |
| 36 | +func (h *AuthTokenHandler) isTokenValid(tokenRefreshBufferPeriod time.Duration) bool { |
| 37 | + isValid := h.Token != "" && time.Until(h.Expires) >= tokenRefreshBufferPeriod |
| 38 | + h.Logger.Debug("Checking token validity", zap.Bool("IsValid", isValid), zap.Duration("TimeUntilExpiry", time.Until(h.Expires))) |
| 39 | + return isValid |
| 40 | +} |
| 41 | + |
| 42 | +// obtainNewToken acquires a new token using the credentials provided. |
| 43 | +// It handles different authentication methods based on the AuthMethod setting. |
| 44 | +func (h *AuthTokenHandler) obtainNewToken(apiHandler apihandler.APIHandler, httpClient *http.Client, clientCredentials ClientCredentials) error { |
| 45 | + var err error |
| 46 | + if h.AuthMethod == "basicauth" { |
| 47 | + err = h.BasicAuthTokenAcquisition(apiHandler, httpClient, clientCredentials.Username, clientCredentials.Password) |
| 48 | + } else if h.AuthMethod == "oauth2" { |
| 49 | + err = h.OAuth2TokenAcquisition(apiHandler, httpClient, clientCredentials.ClientID, clientCredentials.ClientSecret) |
| 50 | + } else { |
| 51 | + err = fmt.Errorf("no valid credentials provided. Unable to obtain a token") |
| 52 | + h.Logger.Error("Authentication method not supported", zap.String("AuthMethod", h.AuthMethod)) |
| 53 | + } |
| 54 | + |
| 55 | + if err != nil { |
| 56 | + h.Logger.Error("Failed to obtain new token", zap.Error(err)) |
| 57 | + } |
| 58 | + return err |
| 59 | +} |
| 60 | + |
| 61 | +// refreshTokenIfNeeded refreshes the token if it's close to expiration. |
| 62 | +// This function decides on the method based on the credentials type available. |
| 63 | +func (h *AuthTokenHandler) refreshTokenIfNeeded(apiHandler apihandler.APIHandler, httpClient *http.Client, clientCredentials ClientCredentials, tokenRefreshBufferPeriod time.Duration) error { |
| 64 | + if time.Until(h.Expires) < tokenRefreshBufferPeriod { |
| 65 | + h.Logger.Info("Token is close to expiry and will be refreshed", zap.Duration("TimeUntilExpiry", time.Until(h.Expires))) |
| 66 | + var err error |
| 67 | + if clientCredentials.Username != "" && clientCredentials.Password != "" { |
| 68 | + err = h.RefreshBearerToken(apiHandler, httpClient) |
| 69 | + } else if clientCredentials.ClientID != "" && clientCredentials.ClientSecret != "" { |
| 70 | + err = h.OAuth2TokenAcquisition(apiHandler, httpClient, clientCredentials.ClientID, clientCredentials.ClientSecret) |
| 71 | + } else { |
| 72 | + err = fmt.Errorf("unknown auth method") |
| 73 | + h.Logger.Error("Failed to determine authentication method for token refresh", zap.String("AuthMethod", h.AuthMethod)) |
| 74 | + } |
| 75 | + |
| 76 | + if err != nil { |
| 77 | + h.Logger.Error("Failed to refresh token", zap.Error(err)) |
| 78 | + return err |
| 79 | + } |
| 80 | + } |
| 81 | + return nil |
| 82 | +} |
0 commit comments