forked from gopasspw/gopass
/
recipients.go
163 lines (142 loc) · 3.81 KB
/
recipients.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package cui
import (
"context"
"fmt"
"io"
"os"
"sort"
"strconv"
"github.com/gopasspw/gopass/internal/backend"
"github.com/gopasspw/gopass/internal/backend/crypto/gpg"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/termio"
)
var (
// Stdin is exported for tests
Stdin io.Reader = os.Stdin
// Stdout is exported for tests
Stdout io.Writer = os.Stdout
// Stderr is exported for tests
Stderr io.Writer = os.Stderr
)
const (
maxTries = 42
)
// AskForPrivateKey prompts the user to select from a list of private keys
func AskForPrivateKey(ctx context.Context, crypto backend.Crypto, prompt string) (string, error) {
if !ctxutil.IsInteractive(ctx) {
return "", fmt.Errorf("can not select private key without terminal")
}
if crypto == nil {
return "", fmt.Errorf("can not select private key without valid crypto backend")
}
kl, err := crypto.ListIdentities(gpg.WithAlwaysTrust(ctx, false))
if err != nil {
return "", err
}
if len(kl) < 1 {
return "", fmt.Errorf("no useable private keys found. make sure you have valid private keys with sufficient trust")
}
fmtStr := "[%" + strconv.Itoa(int(len(kl)/10)+1) + "d] %s - %s\n"
for i := 0; i < maxTries; i++ {
if !ctxutil.IsTerminal(ctx) {
return kl[0], nil
}
// check for context cancelation
select {
case <-ctx.Done():
return "", fmt.Errorf("user aborted")
default:
}
fmt.Fprintln(Stdout, prompt)
for i, k := range kl {
fmt.Fprintf(Stdout, fmtStr, i, crypto.Name(), crypto.FormatKey(ctx, k, ""))
}
iv, err := termio.AskForInt(ctx, fmt.Sprintf("Please enter the number of a key (0-%d, [q]uit)", len(kl)-1), 0)
if err != nil {
if err.Error() == "user aborted" {
return "", err
}
continue
}
if iv >= 0 && iv < len(kl) {
return kl[iv], nil
}
}
return "", fmt.Errorf("no valid user input")
}
// AskForGitConfigUser will iterate over GPG private key identities and prompt
// the user for selecting one identity whose name and email address will be used as
// git config user.name and git config user.email, respectively.
// On error or no selection, name and email will be empty.
// If s.isTerm is false (i.e., the user cannot be prompted), however,
// the first identity's name/email pair found is returned.
func AskForGitConfigUser(ctx context.Context, crypto backend.Crypto) (string, string, error) {
var useCurrent bool
if crypto == nil {
return "", "", fmt.Errorf("crypto not available")
}
keyList, err := crypto.ListIdentities(ctx)
if err != nil {
return "", "", err
}
if len(keyList) < 1 {
return "", "", fmt.Errorf("no usable private keys found")
}
for _, key := range keyList {
// check for context cancelation
select {
case <-ctx.Done():
return "", "", fmt.Errorf("user aborted")
default:
}
name := crypto.FormatKey(ctx, key, "{{ .Identity.Name }}")
email := crypto.FormatKey(ctx, key, "{{ .Identity.Email }}")
if name == "" && email == "" {
continue
}
useCurrent, err = termio.AskForBool(
ctx,
fmt.Sprintf("Use %s (%s) for password store git config?", name, email),
true,
)
if err != nil {
return "", "", err
}
if useCurrent {
return name, email, nil
}
}
return "", "", nil
}
type mountPointer interface {
MountPoints() []string
}
func sorted(s []string) []string {
sort.Strings(s)
return s
}
// AskForStore shows a store / mount point selection
func AskForStore(ctx context.Context, s mountPointer) string {
if !ctxutil.IsInteractive(ctx) {
return ""
}
stores := []string{"<root>"}
stores = append(stores, sorted(s.MountPoints())...)
if len(stores) < 2 {
return ""
}
act, sel := GetSelection(ctx, "Please select the store you would like to use", stores)
switch act {
case "default":
fallthrough
case "show":
store := stores[sel]
if store == "<root>" {
store = ""
}
return store
default:
return "" // root store
}
}