Skip to content

fix: reprompt for matching passwords on mismatch #2758

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

Merged
merged 1 commit into from
Jul 1, 2022
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
43 changes: 23 additions & 20 deletions cli/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,26 +131,29 @@ func login() *cobra.Command {
}

if password == "" {
password, err = cliui.Prompt(cmd, cliui.PromptOptions{
Text: "Enter a " + cliui.Styles.Field.Render("password") + ":",
Secret: true,
Validate: cliui.ValidateNotEmpty,
})
if err != nil {
return xerrors.Errorf("specify password prompt: %w", err)
}
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
Text: "Confirm " + cliui.Styles.Field.Render("password") + ":",
Secret: true,
Validate: func(s string) error {
if s != password {
return xerrors.Errorf("Passwords do not match")
}
return nil
},
})
if err != nil {
return xerrors.Errorf("confirm password prompt: %w", err)
var matching bool

for !matching {
password, err = cliui.Prompt(cmd, cliui.PromptOptions{
Text: "Enter a " + cliui.Styles.Field.Render("password") + ":",
Secret: true,
Validate: cliui.ValidateNotEmpty,
})
if err != nil {
return xerrors.Errorf("specify password prompt: %w", err)
}
confirm, err := cliui.Prompt(cmd, cliui.PromptOptions{
Text: "Confirm " + cliui.Styles.Field.Render("password") + ":",
Secret: true,
})
if err != nil {
return xerrors.Errorf("confirm password prompt: %w", err)
}

matching = confirm == password
if !matching {
_, _ = fmt.Fprintln(cmd.OutOrStdout(), cliui.Styles.Error.Render("Passwords do not match"))
}
}
}

Expand Down
13 changes: 10 additions & 3 deletions cli/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/pty/ptytest"
)
Expand Down Expand Up @@ -92,7 +93,7 @@ func TestLogin(t *testing.T) {
go func() {
defer close(doneChan)
err := root.ExecuteContext(ctx)
assert.ErrorIs(t, err, context.Canceled)
assert.NoError(t, err)
}()

matches := []string{
Expand All @@ -108,9 +109,15 @@ func TestLogin(t *testing.T) {
pty.ExpectMatch(match)
pty.WriteLine(value)
}

// Validate that we reprompt for matching passwords.
pty.ExpectMatch("Passwords do not match")
pty.ExpectMatch("password") // Re-prompt password.
cancel()
pty.ExpectMatch("Enter a " + cliui.Styles.Field.Render("password"))

pty.WriteLine("pass")
pty.ExpectMatch("Confirm")
pty.WriteLine("pass")
pty.ExpectMatch("Welcome to Coder")
<-doneChan
})

Expand Down