lockbox

password manager
Log | Files | Refs | README | LICENSE

commit 0017cfb9c929d1f93f0ae4e54a3a9894907bc481
parent 3aacfd2318b54b9cb38bb2affef1aaeee40c0da9
Author: Sean Enck <sean@ttypty.com>
Date:   Sat, 16 Jul 2022 16:36:52 -0400

moving stdin password handling to stdin area

Diffstat:
Mcmd/lb/main.go | 36++++++++++--------------------------
Minternal/inputs/stdin.go | 28+++++++++++++++++++++++++---
2 files changed, 35 insertions(+), 29 deletions(-)

diff --git a/cmd/lb/main.go b/cmd/lb/main.go @@ -29,14 +29,6 @@ func getEntry(fs store.FileSystem, args []string, idx int) string { return fs.NewPath(args[idx]) } -func getExecutable() string { - exe, err := os.Executable() - if err != nil { - misc.Die("unable to get exe", err) - } - return exe -} - func main() { args := os.Args if len(args) < 2 { @@ -93,24 +85,11 @@ func main() { } } } - var password string - if !options.Multi && !isPipe { - input, err := inputs.ConfirmInputsMatch("password") - if err != nil { - misc.Die("password input failed", err) - } - password = input - } else { - input, err := inputs.Stdin(false) - if err != nil { - misc.Die("failed to read stdin", err) - } - password = input - } - if password == "" { - misc.Die("empty password provided", errors.New("password can NOT be empty")) + password, err := inputs.GetUserInputPassword(isPipe, options.Multi) + if err != nil { + misc.Die("invalid input", err) } - if err := encrypt.ToFile(entry, []byte(password)); err != nil { + if err := encrypt.ToFile(entry, password); err != nil { misc.Die("unable to encrypt object", err) } fmt.Println("") @@ -158,11 +137,16 @@ func main() { return } clipboard := platform.Clipboard{} + exe := "" if !opts.Show { clipboard, err = platform.NewClipboard() if err != nil { misc.Die("unable to get clipboard", err) } + exe, err = os.Executable() + if err != nil { + misc.Die("unable to get executable", err) + } } for _, obj := range dumpData { if opts.Show { @@ -172,7 +156,7 @@ func main() { fmt.Println(obj.Value) continue } - clipboard.CopyTo(obj.Value, getExecutable()) + clipboard.CopyTo(obj.Value, exe) } case "clear": if err := subcommands.ClearClipboardCallback(); err != nil { diff --git a/internal/inputs/stdin.go b/internal/inputs/stdin.go @@ -3,6 +3,7 @@ package inputs import ( "bufio" "bytes" + "errors" "fmt" "os" "strings" @@ -38,8 +39,29 @@ func termEcho(on bool) { } } -// ConfirmInputsMatch will get 2 inputs and confirm they are the same. -func ConfirmInputsMatch(object string) (string, error) { +// GetUserInputPassword will read the user's input from stdin via multiple means. +func GetUserInputPassword(piping, multiLine bool) ([]byte, error) { + var password string + if !multiLine && !piping { + input, err := confirmInputsMatch("password") + if err != nil { + return nil, err + } + password = input + } else { + input, err := Stdin(false) + if err != nil { + return nil, err + } + password = input + } + if password == "" { + return nil, errors.New("password can NOT be empty") + } + return []byte(password), nil +} + +func confirmInputsMatch(object string) (string, error) { termEcho(false) defer func() { termEcho(true) @@ -60,7 +82,7 @@ func ConfirmInputsMatch(object string) (string, error) { return first, nil } -// Stdin will retrieve stdin data. +// Stdin will get one (or more) lines of stdin as string. func Stdin(one bool) (string, error) { b, err := getStdin(one) if err != nil {