Skip to content

Commit

Permalink
feat: return token in login command [IDE-458] (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
bastiandoetsch authored Jul 30, 2024
1 parent ce0f92f commit 1194a9d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
3 changes: 2 additions & 1 deletion application/server/execute_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,13 @@ func Test_loginCommand_StartsAuthentication(t *testing.T) {
params := lsp.ExecuteCommandParams{Command: types.LoginCommand}

// Act
_, err = loc.Client.Call(ctx, "workspace/executeCommand", params)
tokenResponse, err := loc.Client.Call(ctx, "workspace/executeCommand", params)
if err != nil {
t.Fatal(err)
}

// Assert
assert.NotEmpty(t, tokenResponse.ResultString())
assert.True(t, fakeAuthenticationProvider.IsAuthenticated)
assert.Eventually(t, func() bool { return len(jsonRPCRecorder.Notifications()) > 0 }, 5*time.Second, 50*time.Millisecond)
assert.Equal(t, 1, len(jsonRPCRecorder.FindNotificationsByMethod("$/snyk.hasAuthenticated")))
Expand Down
33 changes: 22 additions & 11 deletions application/server/parallelization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"os"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -47,17 +48,27 @@ func Test_Concurrent_CLI_Runs(t *testing.T) {
successfulScans := map[string]scanParamsTuple{}

var workspaceFolders []types.WorkspaceFolder
wg := sync.WaitGroup{}
mu := sync.Mutex{}
for i := 0; i < 10; i++ {
dir := t.TempDir()
repo, err := testutil.SetupCustomTestRepo(t, dir, nodejsGoof, "", c.Logger())
require.NoError(t, err)
folder := types.WorkspaceFolder{
Name: fmt.Sprintf("Test Repo %d", i),
Uri: uri.PathToUri(repo),
}
workspaceFolders = append(workspaceFolders, folder)
successfulScans[repo] = scanParamsTuple{}
intermediateIndex := i
wg.Add(1)
go func() {
defer wg.Done()
dir := t.TempDir()
repo, err := testutil.SetupCustomTestRepo(t, dir, nodejsGoof, "", c.Logger())
require.NoError(t, err)
folder := types.WorkspaceFolder{
Name: fmt.Sprintf("Test Repo %d", intermediateIndex),
Uri: uri.PathToUri(repo),
}
mu.Lock()
workspaceFolders = append(workspaceFolders, folder)
successfulScans[repo] = scanParamsTuple{}
mu.Unlock()
}()
}
wg.Wait()

clientParams := types.InitializeParams{
WorkspaceFolders: workspaceFolders,
Expand All @@ -71,8 +82,8 @@ func Test_Concurrent_CLI_Runs(t *testing.T) {
},
}

lspClient.Call(context.Background(), "initialize", clientParams)
lspClient.Call(context.Background(), "initialized", nil)
_, _ = lspClient.Call(context.Background(), "initialize", clientParams)
_, _ = lspClient.Call(context.Background(), "initialized", nil)

// check if all scan params were sent
assert.Eventuallyf(t, func() bool {
Expand Down
1 change: 1 addition & 0 deletions domain/ide/command/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (cmd *loginCommand) Execute(ctx context.Context) (any, error) {
cmd.logger.Debug().Str("method", "loginCommand.Execute").
Str("hashed token", util.Hash([]byte(token))[0:16]).
Msgf("authentication successful, received token")
return token, nil
}
return nil, err
}
12 changes: 3 additions & 9 deletions infrastructure/authentication/auth_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package authentication

import (
"context"
"errors"
"fmt"

"golang.org/x/oauth2"

Expand All @@ -42,17 +40,13 @@ func Token(c *config.Config, errorReporter error_reporting.ErrorReporter) Authen

// Default authentication configures an OAuth2 authenticator,
// the auth service parameter is needed, as the oauth2 provider needs a callback function
func Default(c *config.Config, errorReporter error_reporting.ErrorReporter, authenticationService AuthenticationService) AuthenticationProvider {
func Default(c *config.Config, authenticationService AuthenticationService) AuthenticationProvider {
conf := c.Engine().GetConfiguration()
conf.Set(configuration.FF_OAUTH_AUTH_FLOW_ENABLED, true)
conf.Unset(configuration.AUTHENTICATION_TOKEN)
credentialsUpdateCallback := func(_ string, value any) {
newToken, ok := value.(string)
if !ok {
msg := fmt.Sprintf("Failed to cast creds of type %T to string", value)
errorReporter.CaptureError(errors.New(msg))
return
}
// an empty struct marks an empty token, so we stay with empty string if the cast fails
newToken, _ := value.(string)
go authenticationService.UpdateCredentials(newToken, true)
}

Expand Down
2 changes: 1 addition & 1 deletion infrastructure/authentication/auth_service_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (a *AuthenticationServiceImpl) ConfigureProviders(c *config.Config) {
authProviderChange = true
}

p = Default(c, a.errorReporter, a)
p = Default(c, a)
a.SetProvider(p)
case types.TokenAuthentication:
// if err == nil, previous token was oauth2. So we had a provider change
Expand Down

0 comments on commit 1194a9d

Please sign in to comment.