Skip to content

Commit ff7a485

Browse files
authored
feat(remoteoauth): Add WithToken option (#1898)
instead of `WithAccessToken`, which needed explicit default values and didn't support refresh tokens in non-cloud mode. --------- Co-authored-by: Kemal Hadimli <[email protected]>
1 parent 4201fa6 commit ff7a485

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

helpers/remoteoauth/token_test.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,24 @@ import (
99

1010
"github.com/google/uuid"
1111
"github.com/stretchr/testify/require"
12+
"golang.org/x/oauth2"
1213
)
1314

1415
const testAPIKey = "test-key"
1516

1617
func TestLocalTokenAccess(t *testing.T) {
18+
r := require.New(t)
19+
_, cloud := os.LookupEnv("CQ_CLOUD")
20+
r.False(cloud, "CQ_CLOUD should not be set")
21+
tok, err := NewTokenSource(WithToken(oauth2.Token{AccessToken: "token", TokenType: "bearer"}))
22+
r.NoError(err)
23+
tk, err := tok.Token()
24+
r.NoError(err)
25+
r.True(tk.Valid())
26+
r.Equal("token", tk.AccessToken)
27+
}
28+
29+
func TestLocalTokenAccessWithDeprecatedTokenOpt(t *testing.T) {
1730
r := require.New(t)
1831
_, cloud := os.LookupEnv("CQ_CLOUD")
1932
r.False(cloud, "CQ_CLOUD should not be set")
@@ -41,7 +54,7 @@ func TestFirstLocalTokenAccess(t *testing.T) {
4154
"_CQ_CONNECTOR_ID": connID,
4255
})
4356
r := require.New(t)
44-
tok, err := NewTokenSource(WithAccessToken("token", "bearer", time.Time{}))
57+
tok, err := NewTokenSource(WithToken(oauth2.Token{AccessToken: "token"}))
4558
r.NoError(err)
4659
tk, err := tok.Token()
4760
r.NoError(err)
@@ -63,7 +76,7 @@ func TestInvalidAPIKeyTokenAccess(t *testing.T) {
6376
"_CQ_CONNECTOR_ID": connID,
6477
})
6578
r := require.New(t)
66-
tok, err := NewTokenSource(WithAccessToken("token", "bearer", time.Time{}))
79+
tok, err := NewTokenSource(WithToken(oauth2.Token{AccessToken: "token"}))
6780
r.NoError(err)
6881
tk, err := tok.Token()
6982
r.Nil(tk)
@@ -110,7 +123,7 @@ func TestTestConnectionTokenAccess(t *testing.T) {
110123
"_CQ_CONNECTOR_ID": connID,
111124
})
112125
r := require.New(t)
113-
tok, err := NewTokenSource(WithAccessToken("token", "bearer", time.Time{}))
126+
tok, err := NewTokenSource(WithToken(oauth2.Token{AccessToken: "token"}))
114127
r.NoError(err)
115128
tk, err := tok.Token()
116129
r.NoError(err)

helpers/remoteoauth/tokenoptions.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99

1010
type TokenSourceOption func(source *tokenSource)
1111

12+
// WithAccessToken sets the access token, token type and expiry time for the token source.
13+
// Deprecated: Use WithToken instead.
1214
func WithAccessToken(token, tokenType string, expiry time.Time) TokenSourceOption {
1315
return func(t *tokenSource) {
1416
t.currentToken = oauth2.Token{
@@ -19,6 +21,14 @@ func WithAccessToken(token, tokenType string, expiry time.Time) TokenSourceOptio
1921
}
2022
}
2123

24+
// WithToken sets the default token for the token source.
25+
func WithToken(token oauth2.Token) TokenSourceOption {
26+
return func(t *tokenSource) {
27+
t.currentToken = token
28+
}
29+
}
30+
31+
// WithDefaultContext sets the default context for the token source, used when creating a new token request.
2232
func WithDefaultContext(ctx context.Context) TokenSourceOption {
2333
return func(t *tokenSource) {
2434
t.defaultContext = ctx

0 commit comments

Comments
 (0)