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

Prompt if username is empty on login #132

Merged
merged 1 commit into from
Nov 20, 2020
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
10 changes: 7 additions & 3 deletions internal/commands/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,19 @@ const (

func newLoginCmd(streams command.Streams, store credentials.Store, hubClient *hub.Client) *cobra.Command {
cmd := &cobra.Command{
Use: loginName + " USERNAME",
Use: loginName + " [USERNAME]",
Short: "Login to the Hub",
Args: cli.ExactArgs(1),
Args: cli.RequiresMaxArgs(1),
DisableFlagsInUseLine: true,
PreRun: func(cmd *cobra.Command, args []string) {
metrics.Send("root", loginName)
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := login.RunLogin(cmd.Context(), streams, hubClient, store, args[0]); err != nil {
username := ""
if len(args) > 0 {
username = args[0]
}
if err := login.RunLogin(cmd.Context(), streams, hubClient, store, username); err != nil {
return err
}
fmt.Fprintln(streams.Out(), ansi.Info("Login Succeeded"))
Expand Down
42 changes: 26 additions & 16 deletions internal/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ import (
)

// RunLogin logs the user and asks for the 2FA code if needed
func RunLogin(ctx context.Context, streams command.Streams, hubClient *hub.Client, store credentials.Store, username string) error {
func RunLogin(ctx context.Context, streams command.Streams, hubClient *hub.Client, store credentials.Store, candidateUsername string) error {
username := candidateUsername
if username == "" {
var err error
if username, err = readClearText(ctx, streams, "Username: "); err != nil {
return err
}
}
password, err := readPassword(streams)
if err != nil {
return err
Expand All @@ -62,24 +69,27 @@ func RunLogin(ctx context.Context, streams command.Streams, hubClient *hub.Clien
// VerifyTwoFactorCode run 2FA login
func VerifyTwoFactorCode(ctx context.Context, streams command.Streams, hubClient *hub.Client, username string, password string) (string, string, error) {
return hubClient.Login(username, password, func() (string, error) {
userIn := make(chan string, 1)
go func() {
fmt.Fprint(streams.Out(), ansi.Info("2FA required, please provide the 6 digit code: "))
reader := bufio.NewReader(streams.In())
input, _ := reader.ReadString('\n')
userIn <- strings.TrimSpace(input)
}()
input := ""
select {
case <-ctx.Done():
return "", errors.New("canceled")
case input = <-userIn:
}

return input, nil
return readClearText(ctx, streams, "2FA required, please provide the 6 digit code: ")
})
}

func readClearText(ctx context.Context, streams command.Streams, prompt string) (string, error) {
userIn := make(chan string, 1)
go func() {
fmt.Fprint(streams.Out(), ansi.Info(prompt))
reader := bufio.NewReader(streams.In())
input, _ := reader.ReadString('\n')
userIn <- strings.TrimSpace(input)
}()
input := ""
select {
case <-ctx.Done():
return "", errors.New("canceled")
case input = <-userIn:
}
return input, nil
}

func readPassword(streams command.Streams) (string, error) {
in := streams.In()
// On Windows, force the use of the regular OS stdin stream. Fixes #14336/#14210
Expand Down