Skip to content

Commit 9157462

Browse files
committed
Update authentication method names and function calls in the authentication package
1 parent bf8ab79 commit 9157462

File tree

9 files changed

+90
-67
lines changed

9 files changed

+90
-67
lines changed

authenticationhandler/auth_token_management.go

Lines changed: 0 additions & 58 deletions
This file was deleted.

authenticationhandler/auth_handler.go renamed to authenticationhandler/authenticationhandler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// authenticationhandler/auth_handler.go
1+
// authenticationhandler/authenticationhandler.go
22

33
package authenticationhandler
44

authenticationhandler/auth_bearer_token.go renamed to authenticationhandler/basicauthentication.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// authenticationhandler/auth_bearer_token.go
1+
// authenticationhandler/basicauthentication.go
22
/* The http_client_auth package focuses on authentication mechanisms for an HTTP client.
33
It provides structures and methods for handling both basic and bearer token based authentication */
44

authenticationhandler/auth_oauth2.go renamed to authenticationhandler/oauth2.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
// authenticationhandler/auth_oauth.go
1+
// authenticationhandler/oauth2.go
22

33
/* The http_client_auth package focuses on authentication mechanisms for an HTTP client.
4-
It provides structures and methods for handling OAuth-based authentication
5-
*/
4+
It provides structures and methods for handling OAuth-based authentication */
65

76
package authenticationhandler
87

authenticationhandler/tokenmanager.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
}

authenticationhandler/auth_validation.go renamed to authenticationhandler/validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// authenticationhandler/auth_validation.go
1+
// authenticationhandler/validation.go
22

33
package authenticationhandler
44

httpclient/multipartrequest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (c *Client) DoMultipartRequest(method, endpoint string, fields map[string]s
4848
ClientSecret: c.clientConfig.Auth.ClientSecret,
4949
}
5050

51-
valid, err := c.AuthTokenHandler.ValidAuthTokenCheck(c.APIHandler, c.httpClient, clientCredentials, c.clientConfig.ClientOptions.TokenRefreshBufferPeriod)
51+
valid, err := c.AuthTokenHandler.CheckAndRefreshAuthToken(c.APIHandler, c.httpClient, clientCredentials, c.clientConfig.ClientOptions.TokenRefreshBufferPeriod)
5252
if err != nil || !valid {
5353
return nil, err
5454
}

httpclient/request.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in
123123
ClientSecret: c.clientConfig.Auth.ClientSecret,
124124
}
125125

126-
valid, err := c.AuthTokenHandler.ValidAuthTokenCheck(c.APIHandler, c.httpClient, clientCredentials, c.clientConfig.ClientOptions.TokenRefreshBufferPeriod)
126+
valid, err := c.AuthTokenHandler.CheckAndRefreshAuthToken(c.APIHandler, c.httpClient, clientCredentials, c.clientConfig.ClientOptions.TokenRefreshBufferPeriod)
127127
if err != nil || !valid {
128128
return nil, err
129129
}
@@ -286,7 +286,7 @@ func (c *Client) executeRequest(method, endpoint string, body, out interface{})
286286
ClientSecret: c.clientConfig.Auth.ClientSecret,
287287
}
288288

289-
valid, err := c.AuthTokenHandler.ValidAuthTokenCheck(c.APIHandler, c.httpClient, clientCredentials, c.clientConfig.ClientOptions.TokenRefreshBufferPeriod)
289+
valid, err := c.AuthTokenHandler.CheckAndRefreshAuthToken(c.APIHandler, c.httpClient, clientCredentials, c.clientConfig.ClientOptions.TokenRefreshBufferPeriod)
290290
if err != nil || !valid {
291291
return nil, err
292292
}

0 commit comments

Comments
 (0)