Skip to content

Commit

Permalink
login: Prompt if username is empty
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Crone <[email protected]>
  • Loading branch information
chris-crone committed Nov 20, 2020
1 parent 2f39b6e commit 1d998be
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
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.Fprintf(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

0 comments on commit 1d998be

Please sign in to comment.