Skip to content

Commit

Permalink
feat: Add Sync Run API Token Type
Browse files Browse the repository at this point in the history
  • Loading branch information
erezrokah committed Jan 23, 2024
1 parent bcf1462 commit a610d0f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
11 changes: 9 additions & 2 deletions auth/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/url"
"os"
"strings"
"time"

"github.com/cloudquery/cloudquery-api-go/config"
Expand All @@ -18,6 +19,7 @@ const (
EnvVarCloudQueryAPIKey = "CLOUDQUERY_API_KEY"
ExpiryBuffer = 60 * time.Second
tokenFilePath = "cloudquery/token"
syncRunAPIKeyPrefix = "cqsr_"
)

type tokenResponse struct {
Expand All @@ -36,6 +38,7 @@ const (
Undefined TokenType = iota
BearerToken
APIKey
SyncRunAPIKey
)

var UndefinedToken = Token{Type: Undefined, Value: ""}
Expand Down Expand Up @@ -66,8 +69,9 @@ func NewTokenClient() *TokenClient {
// GetToken returns the ID token
// If CLOUDQUERY_API_KEY is set, it returns that value, otherwise it returns an ID token generated from the refresh token.
func (tc *TokenClient) GetToken() (Token, error) {
if tc.GetTokenType() == APIKey {
return Token{Type: APIKey, Value: os.Getenv(EnvVarCloudQueryAPIKey)}, nil
tokenType := tc.GetTokenType()
if tokenType != BearerToken {
return Token{Type: tokenType, Value: os.Getenv(EnvVarCloudQueryAPIKey)}, nil
}

// If the token is not expired, return it
Expand Down Expand Up @@ -104,6 +108,9 @@ func (tc *TokenClient) GetToken() (Token, error) {
// GetTokenType returns the type of token that will be returned by GetToken
func (tc *TokenClient) GetTokenType() TokenType {
if token := os.Getenv(EnvVarCloudQueryAPIKey); token != "" {
if strings.HasPrefix(token, syncRunAPIKeyPrefix) {
return SyncRunAPIKey
}
return APIKey
}
return BearerToken
Expand Down
8 changes: 8 additions & 0 deletions auth/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ func TestTokenClient_APIKeyTokenType(t *testing.T) {
assert.Equal(t, APIKey, tc.GetTokenType())
}

func TestTokenClient_SyncRunAPIKeyTokenType(t *testing.T) {
t.Setenv(EnvVarCloudQueryAPIKey, "cqsr_my_token")

tc := NewTokenClient()

assert.Equal(t, SyncRunAPIKey, tc.GetTokenType())
}

func overrideEnvironmentVariable(t *testing.T, key, value string) func() {
originalValue := os.Getenv(key)
resetFn := func() {
Expand Down

0 comments on commit a610d0f

Please sign in to comment.