Skip to content

Commit

Permalink
Merge pull request #19 from deploymenttheory/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
ShocOne authored Nov 1, 2023
2 parents 343dbaa + 9325fe1 commit c1f3103
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 467 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// http_token_management.go
// http_client_auth_token_management.go
package http_client

import (
"fmt"
"time"
)

Expand All @@ -11,51 +12,42 @@ type TokenResponse struct {
Expires time.Time `json:"expires"`
}

// ValidAuthToken checks if the current token is valid and not close to expiry.
// ValidAuthTokenCheck checks if the current token is valid and not close to expiry.
// If the token is invalid, it tries to refresh it.
func (c *Client) ValidAuthTokenCheck() bool {

// It returns a boolean indicating the validity of the token and an error if there's a failure.
func (c *Client) ValidAuthTokenCheck() (bool, error) {
// If token doesn't exist
if c.Token == "" {
if c.BearerTokenAuthCredentials.Username != "" && c.BearerTokenAuthCredentials.Password != "" {
err := c.ObtainToken()
if err != nil {
return false
return false, fmt.Errorf("failed to obtain bearer token: %w", err)
}
} else if c.OAuthCredentials.ClientID != "" && c.OAuthCredentials.ClientSecret != "" {
err := c.ObtainOAuthToken(c.OAuthCredentials)
if err != nil {
return false
return false, fmt.Errorf("failed to obtain OAuth token: %w", err)
}
} else {
c.logger.Error("No valid credentials provided. Unable to obtain a token.")
return false
return false, fmt.Errorf("no valid credentials provided. Unable to obtain a token")
}
}

// If token exists and is close to expiry or already expired
if time.Until(c.Expiry) < c.config.TokenRefreshBufferPeriod {
if c.config.DebugMode {
c.logger.Debug("Token is not valid or is close to expiry", "Expiry", c.Expiry)
}

var err error
if c.BearerTokenAuthCredentials.Username != "" && c.BearerTokenAuthCredentials.Password != "" {
err = c.RefreshToken()
} else if c.OAuthCredentials.ClientID != "" && c.OAuthCredentials.ClientSecret != "" {
err = c.RefreshOAuthToken()
} else {
c.logger.Error("Unknown auth method", "AuthMethod", c.authMethod)
return false
return false, fmt.Errorf("unknown auth method: %s", c.authMethod)
}

if err != nil {
return false
return false, fmt.Errorf("failed to refresh token: %w", err)
}
}

if c.config.DebugMode {
c.logger.Debug("Token is valid", "Expiry", c.Expiry)
}
return true
return true, nil
}
8 changes: 3 additions & 5 deletions sdk/http_client/http_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ import (

func (c *Client) DoRequest(method, endpoint string, body, out interface{}) (*http.Response, error) {
// Auth Token validation check
if !c.ValidAuthTokenCheck() {
if c.config.DebugMode {
c.logger.Debug("Failed to validate or refresh token.")
}
return nil, fmt.Errorf("failed to validate or refresh token. Stopping")
valid, err := c.ValidAuthTokenCheck()
if err != nil || !valid {
return nil, fmt.Errorf("validity of the authentication token failed with error: %w", err)
}

// Acquire a token for concurrency management with a timeout and measure its acquisition time
Expand Down
188 changes: 0 additions & 188 deletions sdk/http_client/http_request.go.back

This file was deleted.

Loading

0 comments on commit c1f3103

Please sign in to comment.