goldwarden-vaultwarden-bitw.../cli/agent/systemauth/pinentry/go-pinentry.go

84 lines
1.8 KiB
Go
Raw Normal View History

2024-06-01 15:18:11 +02:00
//go:build freebsd || linux || darwin
2023-09-12 02:54:46 +02:00
package pinentry
2023-07-17 03:23:26 +02:00
import (
"errors"
2024-06-01 15:18:11 +02:00
"runtime"
2023-07-17 03:23:26 +02:00
"github.com/twpayne/go-pinentry"
)
func getBinaryClientOption() (clientOption pinentry.ClientOption) {
2024-06-01 15:18:11 +02:00
binaryClientOption := pinentry.WithBinaryNameFromGnuPGAgentConf()
if runtime.GOOS == "darwin" {
binaryClientOption = pinentry.WithBinaryName("pinentry-mac")
}
return binaryClientOption
}
func getPassword(title string, description string) (string, error) {
binaryClientOption := getBinaryClientOption()
2024-06-01 15:18:11 +02:00
2023-07-17 03:23:26 +02:00
client, err := pinentry.NewClient(
2024-06-01 15:18:11 +02:00
binaryClientOption,
2023-07-17 03:23:26 +02:00
pinentry.WithGPGTTY(),
pinentry.WithTitle(title),
pinentry.WithDesc(description),
pinentry.WithPrompt(title),
)
log.Info("Asking for pin |%s|%s|", title, description)
if err != nil {
return "", err
}
defer client.Close()
switch pin, fromCache, err := client.GetPIN(); {
case pinentry.IsCancelled(err):
log.Info("Cancelled")
return "", errors.New("Cancelled")
case err != nil:
return "", err
case fromCache:
log.Info("Got pin from cache")
return pin, nil
default:
log.Info("Got pin from user")
return pin, nil
}
}
2024-02-09 00:24:28 +01:00
func getApproval(title string, description string) (bool, error) {
2023-08-21 18:37:34 +02:00
if systemAuthDisabled {
2023-08-21 13:52:06 +02:00
return true, nil
}
binaryClientOption := getBinaryClientOption()
2023-07-17 03:23:26 +02:00
client, err := pinentry.NewClient(
binaryClientOption,
2023-07-17 03:23:26 +02:00
pinentry.WithGPGTTY(),
pinentry.WithTitle(title),
pinentry.WithDesc(description),
pinentry.WithPrompt(title),
)
log.Info("Asking for approval |%s|%s|", title, description)
if err != nil {
return false, err
}
defer client.Close()
switch _, err := client.Confirm("Confirm"); {
case pinentry.IsCancelled(err):
log.Info("Cancelled")
return false, errors.New("Cancelled")
case err != nil:
return false, err
default:
log.Info("Got approval from user")
return true, nil
}
}