internal/plugin,cmd/age: implement confirm protocol verb

This commit is contained in:
Filippo Valsorda
2022-05-24 15:56:22 +02:00
parent 349ed5ed3f
commit 5a0da177e9
3 changed files with 117 additions and 0 deletions
+2
View File
@@ -37,6 +37,7 @@ func parseRecipient(arg string) (age.Recipient, error) {
}
r.DisplayMessage = pluginDisplayMessage(r.Name())
r.RequestValue = pluginRequestSecret(r.Name())
r.Confirm = pluginConfirm(r.Name())
return r, nil
case strings.HasPrefix(arg, "age1"):
return age.ParseX25519Recipient(arg)
@@ -210,6 +211,7 @@ func parseIdentity(s string) (age.Identity, error) {
}
i.DisplayMessage = pluginDisplayMessage(i.Name())
i.RequestValue = pluginRequestSecret(i.Name())
i.Confirm = pluginConfirm(i.Name())
return i, nil
case strings.HasPrefix(s, "AGE-SECRET-KEY-1"):
return age.ParseX25519Identity(s)
+28
View File
@@ -18,6 +18,7 @@ import (
"log"
"os"
"runtime"
"strings"
"golang.org/x/term"
)
@@ -111,3 +112,30 @@ func pluginRequestSecret(name string) func(string, bool) (string, error) {
return string(secret), nil
}
}
func pluginConfirm(name string) func(msg, yes, no string) (bool, error) {
return func(message, yes, no string) (bool, error) {
if no != "" {
message += fmt.Sprintf(" (1 for %q, 2 for %q)", yes, no)
selection, err := readSecret(message)
if err != nil {
return false, fmt.Errorf("could not read value for age-plugin-%s: %v", name, err)
}
switch strings.TrimSpace(string(selection)) {
case "1":
return true, nil
case "2":
return false, nil
default:
return false, fmt.Errorf("invalid selection %q", selection)
}
} else {
message += fmt.Sprintf(" (press enter for %q)", yes)
_, err := readSecret(message)
if err != nil {
return false, fmt.Errorf("could not read value for age-plugin-%s: %v", name, err)
}
return true, nil
}
}
}