From 9d030f38b7cd77d2e293ef667cbc927f5b79d2f2 Mon Sep 17 00:00:00 2001 From: Eugene Bujak Date: Fri, 12 Oct 2018 19:40:43 +0300 Subject: [PATCH 1/2] If running from terminal, ask for username/password if config file does not exists --- app.go | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 1 deletion(-) diff --git a/app.go b/app.go index 608f1590..222f9d9c 100644 --- a/app.go +++ b/app.go @@ -1,6 +1,7 @@ package main import ( + "bufio" "fmt" "log" "net" @@ -10,6 +11,7 @@ import ( "strconv" "github.com/gobuffalo/packr" + "golang.org/x/crypto/ssh/terminal" ) // VersionString will be set through ldflags, contains current version @@ -96,8 +98,14 @@ func main() { if configFilename != nil { config.ourConfigFilename = *configFilename } + + err := askUsernamePasswordIfPossible() + if err != nil { + log.Fatal(err) + } + // parse from config file - err := parseConfig() + err = parseConfig() if err != nil { log.Fatal(err) } @@ -130,3 +138,93 @@ func main() { log.Println("Go to " + URL) log.Fatal(http.ListenAndServe(address, nil)) } + +func getInput() (string, error) { + scanner := bufio.NewScanner(os.Stdin) + scanner.Scan() + text := scanner.Text() + err := scanner.Err() + return text, err +} + +func promptAndGet(prompt string) (string, error) { + for { + fmt.Printf(prompt) + input, err := getInput() + if err != nil { + log.Printf("Failed to get input, aborting: %s", err) + return "", err + } + if len(input) != 0 { + return input, nil + } + // try again + } + return "", nil +} + +func promptAndGetPassword(prompt string) (string, error) { + for { + fmt.Printf(prompt) + password, err := terminal.ReadPassword(int(os.Stdin.Fd())) + fmt.Printf("\n") + if err != nil { + log.Printf("Failed to get input, aborting: %s", err) + return "", err + } + if len(password) != 0 { + return string(password), nil + } + // try again + } +} + +func askUsernamePasswordIfPossible() error { + configfile := filepath.Join(config.ourBinaryDir, config.ourConfigFilename) + _, err := os.Stat(configfile) + if !os.IsNotExist(err) { + // do nothing, file exists + trace("File %s exists, won't ask for password", configfile) + return nil + } + + if !terminal.IsTerminal(int(os.Stdin.Fd())) { + return nil // do nothing + } + trace("stdin is a terminal") + if !terminal.IsTerminal(int(os.Stdout.Fd())) { + return nil // do nothing + } + trace("stdout is a terminal") + fmt.Printf("Would you like to set user/password for the web interface authentication (yes/no)?\n") + yesno, err := promptAndGet("Please type 'yes' or 'no': ") + if err != nil { + return err + } + if yesno[0] != 'y' && yesno[0] != 'Y' { + trace("User didn't want password, exiting") + return nil + } + username, err := promptAndGet("Please enter the username: ") + if err != nil { + return err + } + + password, err := promptAndGetPassword("Please enter the password: ") + if err != nil { + return err + } + + password2, err := promptAndGetPassword("Please enter password again: ") + if err != nil { + return err + } + if password2 != password { + fmt.Printf("Passwords do not match! Aborting\n") + os.Exit(1) + } + + config.AuthName = username + config.AuthPass = password + return nil +} From d2e5692694e4537038434b56020f4e6c9e782955 Mon Sep 17 00:00:00 2001 From: Eugene Bujak Date: Fri, 12 Oct 2018 19:49:08 +0300 Subject: [PATCH 2/2] Remove debug logging added by previous commit. Closes #363. --- app.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app.go b/app.go index 222f9d9c..d47db95c 100644 --- a/app.go +++ b/app.go @@ -187,22 +187,18 @@ func askUsernamePasswordIfPossible() error { trace("File %s exists, won't ask for password", configfile) return nil } - if !terminal.IsTerminal(int(os.Stdin.Fd())) { return nil // do nothing } - trace("stdin is a terminal") if !terminal.IsTerminal(int(os.Stdout.Fd())) { return nil // do nothing } - trace("stdout is a terminal") fmt.Printf("Would you like to set user/password for the web interface authentication (yes/no)?\n") yesno, err := promptAndGet("Please type 'yes' or 'no': ") if err != nil { return err } if yesno[0] != 'y' && yesno[0] != 'Y' { - trace("User didn't want password, exiting") return nil } username, err := promptAndGet("Please enter the username: ")