Skip to content

Commit f1bcc50

Browse files
authored
feat: Add Sync Run API Token Type (#109)
1 parent bcf1462 commit f1bcc50

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

auth/token.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"net/url"
99
"os"
10+
"strings"
1011
"time"
1112

1213
"github.com/cloudquery/cloudquery-api-go/config"
@@ -18,6 +19,7 @@ const (
1819
EnvVarCloudQueryAPIKey = "CLOUDQUERY_API_KEY"
1920
ExpiryBuffer = 60 * time.Second
2021
tokenFilePath = "cloudquery/token"
22+
syncRunAPIKeyPrefix = "cqsr_"
2123
)
2224

2325
type tokenResponse struct {
@@ -36,6 +38,7 @@ const (
3638
Undefined TokenType = iota
3739
BearerToken
3840
APIKey
41+
SyncRunAPIKey
3942
)
4043

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

7377
// If the token is not expired, return it
@@ -104,6 +108,9 @@ func (tc *TokenClient) GetToken() (Token, error) {
104108
// GetTokenType returns the type of token that will be returned by GetToken
105109
func (tc *TokenClient) GetTokenType() TokenType {
106110
if token := os.Getenv(EnvVarCloudQueryAPIKey); token != "" {
111+
if strings.HasPrefix(token, syncRunAPIKeyPrefix) {
112+
return SyncRunAPIKey
113+
}
107114
return APIKey
108115
}
109116
return BearerToken

auth/token_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ func TestTokenClient_APIKeyTokenType(t *testing.T) {
118118
assert.Equal(t, APIKey, tc.GetTokenType())
119119
}
120120

121+
func TestTokenClient_SyncRunAPIKeyTokenType(t *testing.T) {
122+
t.Setenv(EnvVarCloudQueryAPIKey, "cqsr_my_token")
123+
124+
tc := NewTokenClient()
125+
126+
assert.Equal(t, SyncRunAPIKey, tc.GetTokenType())
127+
}
128+
121129
func overrideEnvironmentVariable(t *testing.T, key, value string) func() {
122130
originalValue := os.Getenv(key)
123131
resetFn := func() {

0 commit comments

Comments
 (0)