From bbf626d15a85593c2923dede9a365458881716c3 Mon Sep 17 00:00:00 2001 From: Wesley Hershberger Date: Wed, 18 Dec 2024 16:14:12 -0600 Subject: [PATCH] cmd/microcloud: Allow multiple spaces between passphrase elements Prevents an accidental double-tap of the spacebar from rejecting the entire passphrase. Allows this: `a b c d` Signed-off-by: Wesley Hershberger --- cmd/microcloud/ask.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/cmd/microcloud/ask.go b/cmd/microcloud/ask.go index 7e0879a0..280a8540 100644 --- a/cmd/microcloud/ask.go +++ b/cmd/microcloud/ask.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "net" + "slices" "sort" "strings" "time" @@ -1485,14 +1486,17 @@ func (c *initConfig) shortFingerprint(fingerprint string) (string, error) { func (c *initConfig) askPassphrase(s *service.Handler) (string, error) { format := func(password string) (string, error) { - trimmed := strings.TrimSpace(password) - passwordSplit := strings.SplitN(trimmed, " ", 5) + passwordSplit := strings.Split(password, " ") - if len(passwordSplit) != 4 { + passwordClean := slices.DeleteFunc(passwordSplit, func(element string) bool { + return element == "" + }) + + if len(passwordClean) != 4 { return "", fmt.Errorf("Passphrase has to contain exactly four elements") } - return trimmed, nil + return strings.Join(passwordClean, " "), nil } validator := func(password string) error {