Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(remoteoauth): Add WithToken option #1898

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions helpers/remoteoauth/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,24 @@ import (

"github.com/google/uuid"
"github.com/stretchr/testify/require"
"golang.org/x/oauth2"
)

const testAPIKey = "test-key"

func TestLocalTokenAccess(t *testing.T) {
r := require.New(t)
_, cloud := os.LookupEnv("CQ_CLOUD")
r.False(cloud, "CQ_CLOUD should not be set")
tok, err := NewTokenSource(WithToken(oauth2.Token{AccessToken: "token", TokenType: "bearer"}))
r.NoError(err)
tk, err := tok.Token()
r.NoError(err)
r.True(tk.Valid())
r.Equal("token", tk.AccessToken)
}

func TestLocalTokenAccessWithDeprecatedTokenOpt(t *testing.T) {
r := require.New(t)
_, cloud := os.LookupEnv("CQ_CLOUD")
r.False(cloud, "CQ_CLOUD should not be set")
Expand Down Expand Up @@ -41,7 +54,7 @@ func TestFirstLocalTokenAccess(t *testing.T) {
"_CQ_CONNECTOR_ID": connID,
})
r := require.New(t)
tok, err := NewTokenSource(WithAccessToken("token", "bearer", time.Time{}))
tok, err := NewTokenSource(WithToken(oauth2.Token{AccessToken: "token"}))
r.NoError(err)
tk, err := tok.Token()
r.NoError(err)
Expand All @@ -63,7 +76,7 @@ func TestInvalidAPIKeyTokenAccess(t *testing.T) {
"_CQ_CONNECTOR_ID": connID,
})
r := require.New(t)
tok, err := NewTokenSource(WithAccessToken("token", "bearer", time.Time{}))
tok, err := NewTokenSource(WithToken(oauth2.Token{AccessToken: "token"}))
r.NoError(err)
tk, err := tok.Token()
r.Nil(tk)
Expand Down Expand Up @@ -110,7 +123,7 @@ func TestTestConnectionTokenAccess(t *testing.T) {
"_CQ_CONNECTOR_ID": connID,
})
r := require.New(t)
tok, err := NewTokenSource(WithAccessToken("token", "bearer", time.Time{}))
tok, err := NewTokenSource(WithToken(oauth2.Token{AccessToken: "token"}))
r.NoError(err)
tk, err := tok.Token()
r.NoError(err)
Expand Down
10 changes: 10 additions & 0 deletions helpers/remoteoauth/tokenoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

type TokenSourceOption func(source *tokenSource)

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

// WithToken sets the default token for the token source.
func WithToken(token oauth2.Token) TokenSourceOption {
return func(t *tokenSource) {
t.currentToken = token
}
}

// WithDefaultContext sets the default context for the token source, used when creating a new token request.
func WithDefaultContext(ctx context.Context) TokenSourceOption {
return func(t *tokenSource) {
t.defaultContext = ctx
Expand Down
Loading