fixup!: remove check for ENV token validity in login

I made an incorrect assumption, we can check if
a flag/env token is valid on login and create a new
token if the provided one is invalid.

However, on subsequent requests the invalid ENV token
will still take priority over the valid token stored
on disk. Therefore, this behavior does not work.
This commit is contained in:
elasticspoon 2024-04-26 17:23:14 -04:00
parent f0616ea5e5
commit 341feddf1b
2 changed files with 2 additions and 93 deletions

View File

@ -289,18 +289,9 @@ func (r *RootCmd) login() *serpent.Command {
// a session token.
key, err := client.CreateAPIKey(ctx, codersdk.Me)
if err != nil {
_, err = cliui.Prompt(inv, cliui.PromptOptions{
Text: fmt.Sprintf("Failed to authenticate with provided token %q. Login normally?", sessionToken),
IsConfirm: true,
Default: cliui.ConfirmYes,
})
if err != nil {
return xerrors.Errorf("create api key: %w", err)
}
sessionToken = ""
} else {
sessionToken = key.Key
return xerrors.Errorf("create api key: %w", err)
}
sessionToken = key.Key
}
// Check for existing session token on disk, and validate user data.

View File

@ -293,35 +293,6 @@ func TestLogin(t *testing.T) {
<-doneChan
})
t.Run("AuthenticatedUserInvalidEnvToken", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
coderdtest.CreateFirstUser(t, client)
inv, root := clitest.New(t, "login", "--no-open")
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t).Attach(inv)
invalidToken := "an-invalid-token"
inv.Environ.Set("CODER_SESSION_TOKEN", invalidToken)
doneChan := make(chan struct{})
go func() {
defer close(doneChan)
err := inv.Run()
assert.NoError(t, err)
}()
pty.ExpectMatch(fmt.Sprintf("Failed to authenticate with provided token %q. Login normally?", invalidToken))
pty.WriteLine("yes")
pty.ExpectMatch("Are you sure you want to log in again?")
pty.WriteLine("yes")
pty.ExpectMatch("Paste your token here:")
pty.WriteLine(client.SessionToken())
pty.ExpectMatch("Welcome to Coder")
<-doneChan
})
t.Run("ExistingUserExpiredSessionToken", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
@ -346,32 +317,6 @@ func TestLogin(t *testing.T) {
<-doneChan
})
t.Run("ExistingUserInvalidEnvToken", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
coderdtest.CreateFirstUser(t, client)
inv, _ := clitest.New(t, "login", "--no-open", client.URL.String())
pty := ptytest.New(t).Attach(inv)
invalidToken := "an-invalid-token"
inv.Environ.Set("CODER_SESSION_TOKEN", invalidToken)
doneChan := make(chan struct{})
go func() {
defer close(doneChan)
err := inv.Run()
assert.NoError(t, err)
}()
pty.ExpectMatch(fmt.Sprintf("Failed to authenticate with provided token %q. Login normally?", invalidToken))
pty.WriteLine("yes")
pty.ExpectMatch("Paste your token here:")
pty.WriteLine(client.SessionToken())
pty.ExpectMatch("Welcome to Coder")
<-doneChan
})
t.Run("ExistingUserRelogin", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
@ -452,33 +397,6 @@ func TestLogin(t *testing.T) {
require.Equal(t, client.SessionToken(), sessionFile)
})
t.Run("AuthenticatedUserTokenFlagInvalid", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
coderdtest.CreateFirstUser(t, client)
invalidToken := "an-invalid-token"
inv, root := clitest.New(t, "login", client.URL.String(), "--no-open", "--token", invalidToken)
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t).Attach(inv)
doneChan := make(chan struct{})
go func() {
defer close(doneChan)
err := inv.Run()
assert.NoError(t, err)
}()
pty.ExpectMatch(fmt.Sprintf("Failed to authenticate with provided token %q. Login normally?", invalidToken))
pty.WriteLine("yes")
pty.ExpectMatch("Are you sure you want to log in again?")
pty.WriteLine("yes")
pty.ExpectMatch("Paste your token here:")
pty.WriteLine(client.SessionToken())
pty.ExpectMatch("Welcome to Coder")
<-doneChan
})
// TokenFlag should generate a new session token and store it in the session file.
t.Run("TokenFlag", func(t *testing.T) {
t.Parallel()