Skip to content

Commit 016747b

Browse files
authored
Merge pull request #113 from deploymenttheory/dev
Refactor authentication credentials handling
2 parents 2bac524 + 7f313e8 commit 016747b

File tree

4 files changed

+42
-42
lines changed

4 files changed

+42
-42
lines changed

httpclient/httpclient_auth_bearer_token.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ import (
1414
"go.uber.org/zap"
1515
)
1616

17-
// BearerTokenAuthCredentials represents the username and password for basic authentication.
18-
type BearerTokenAuthCredentials struct {
19-
Username string
20-
Password string
21-
}
22-
23-
// SetBearerTokenAuthCredentials sets the BearerTokenAuthCredentials (Username and Password)
24-
// for the client instance. These credentials are used for obtaining and refreshing
25-
// bearer tokens for authentication.
26-
func (c *Client) SetBearerTokenAuthCredentials(credentials BearerTokenAuthCredentials) {
27-
c.BearerTokenAuthCredentials = credentials
28-
}
17+
// // BearerTokenAuthCredentials represents the username and password for basic authentication.
18+
// type BearerTokenAuthCredentials struct {
19+
// Username string
20+
// Password string
21+
// }
22+
23+
// // SetBearerTokenAuthCredentials sets the BearerTokenAuthCredentials (Username and Password)
24+
// // for the client instance. These credentials are used for obtaining and refreshing
25+
// // bearer tokens for authentication.
26+
// func (c *Client) SetBearerTokenAuthCredentials(credentials BearerTokenAuthCredentials) {
27+
// c.BearerTokenAuthCredentials = credentials
28+
// }
2929

3030
// ObtainToken fetches and sets an authentication token using the stored basic authentication credentials.
3131
func (c *Client) ObtainToken(log logger.Logger) error {
@@ -36,14 +36,14 @@ func (c *Client) ObtainToken(log logger.Logger) error {
3636
// Construct the full authentication endpoint URL
3737
authenticationEndpoint := c.APIHandler.ConstructAPIAuthEndpoint(c.InstanceName, bearerTokenEndpoint, c.Logger)
3838

39-
log.Debug("Attempting to obtain token for user", zap.String("Username", c.BearerTokenAuthCredentials.Username))
39+
log.Debug("Attempting to obtain token for user", zap.String("Username", c.clientConfig.Auth.Username))
4040

4141
req, err := http.NewRequest("POST", authenticationEndpoint, nil)
4242
if err != nil {
4343
log.LogError("authentication_request_creation_error", "POST", authenticationEndpoint, 0, "", err, "Failed to create new request for token")
4444
return err
4545
}
46-
req.SetBasicAuth(c.BearerTokenAuthCredentials.Username, c.BearerTokenAuthCredentials.Password)
46+
req.SetBasicAuth(c.clientConfig.Auth.Username, c.clientConfig.Auth.Password)
4747

4848
resp, err := c.httpClient.Do(req)
4949
if err != nil {

httpclient/httpclient_auth_oauth.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,18 @@ type OAuthResponse struct {
2626
Error string `json:"error,omitempty"`
2727
}
2828

29-
// OAuthCredentials contains the client ID and client secret required for OAuth authentication.
30-
type OAuthCredentials struct {
31-
ClientID string
32-
ClientSecret string
33-
}
34-
35-
// SetOAuthCredentials sets the OAuth credentials (Client ID and Client Secret)
36-
// for the client instance. These credentials are used for obtaining and refreshing
37-
// OAuth tokens for authentication.
38-
func (c *Client) SetOAuthCredentials(credentials OAuthCredentials) {
39-
c.OAuthCredentials = credentials
40-
}
29+
// // OAuthCredentials contains the client ID and client secret required for OAuth authentication.
30+
// type OAuthCredentials struct {
31+
// ClientID string
32+
// ClientSecret string
33+
// }
34+
35+
// // SetOAuthCredentials sets the OAuth credentials (Client ID and Client Secret)
36+
// // for the client instance. These credentials are used for obtaining and refreshing
37+
// // OAuth tokens for authentication.
38+
// func (c *Client) SetOAuthCredentials(credentials OAuthCredentials) {
39+
// c.OAuthCredentials = credentials
40+
// }
4141

4242
// ObtainOAuthToken fetches an OAuth access token using the provided OAuthCredentials (Client ID and Client Secret).
4343
// It updates the client's Token and Expiry fields with the obtained values.

httpclient/httpclient_auth_token_management.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ func (c *Client) ValidAuthTokenCheck() (bool, error) {
3939

4040
if time.Until(c.Expiry) < c.clientConfig.ClientOptions.TokenRefreshBufferPeriod {
4141
var err error
42-
if c.BearerTokenAuthCredentials.Username != "" && c.BearerTokenAuthCredentials.Password != "" {
42+
if c.clientConfig.Auth.Username != "" && c.clientConfig.Auth.Password != "" {
4343
err = c.RefreshToken(log)
44-
} else if c.OAuthCredentials.ClientID != "" && c.OAuthCredentials.ClientSecret != "" {
44+
} else if c.clientConfig.Auth.ClientID != "" && c.clientConfig.Auth.ClientSecret != "" {
4545
err = c.ObtainOAuthToken(c.clientConfig.Auth)
4646
} else {
4747
return false, log.Error("Unknown auth method", zap.String("authMethod", c.AuthMethod))

httpclient/httpclient_client.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ import (
1919

2020
// Client represents an HTTP client to interact with a specific API.
2121
type Client struct {
22-
APIHandler APIHandler // APIHandler interface used to define which API handler to use
23-
InstanceName string // Website Instance name without the root domain
24-
AuthMethod string // Specifies the authentication method: "bearer" or "oauth"
25-
Token string // Authentication Token
26-
OverrideBaseDomain string // Base domain override used when the default in the api handler isn't suitable
27-
OAuthCredentials OAuthCredentials // ClientID / Client Secret
28-
BearerTokenAuthCredentials BearerTokenAuthCredentials // Username and Password for Basic Authentication
29-
Expiry time.Time // Expiry time set for the auth token
30-
httpClient *http.Client
31-
tokenLock sync.Mutex
32-
clientConfig ClientConfig
33-
Logger logger.Logger
34-
ConcurrencyMgr *ConcurrencyManager
35-
PerfMetrics PerformanceMetrics
22+
APIHandler APIHandler // APIHandler interface used to define which API handler to use
23+
InstanceName string // Website Instance name without the root domain
24+
AuthMethod string // Specifies the authentication method: "bearer" or "oauth"
25+
Token string // Authentication Token
26+
OverrideBaseDomain string // Base domain override used when the default in the api handler isn't suitable
27+
//OAuthCredentials OAuthCredentials // ClientID / Client Secret
28+
//BearerTokenAuthCredentials BearerTokenAuthCredentials // Username and Password for Basic Authentication
29+
Expiry time.Time // Expiry time set for the auth token
30+
httpClient *http.Client
31+
tokenLock sync.Mutex
32+
clientConfig ClientConfig
33+
Logger logger.Logger
34+
ConcurrencyMgr *ConcurrencyManager
35+
PerfMetrics PerformanceMetrics
3636
}
3737

3838
// Config holds configuration options for the HTTP Client.

0 commit comments

Comments
 (0)