forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clihelper.go
297 lines (259 loc) · 7.07 KB
/
clihelper.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package action
import (
"bufio"
"context"
"fmt"
"os"
"sort"
"strconv"
"strings"
"github.com/justwatchcom/gopass/utils/ctxutil"
"github.com/justwatchcom/gopass/utils/out"
"github.com/pkg/errors"
)
const (
maxTries = 42
)
// ConfirmRecipients asks the user to confirm a given set of recipients
func (s *Action) ConfirmRecipients(ctx context.Context, name string, recipients []string) ([]string, error) {
if ctxutil.IsNoConfirm(ctx) || !ctxutil.IsInteractive(ctx) || ctxutil.IsAlwaysYes(ctx) {
return recipients, nil
}
sort.Strings(recipients)
fmt.Printf("gopass: Encrypting %s for these recipients:\n", name)
for _, r := range recipients {
// check for context cancelation
select {
case <-ctx.Done():
return nil, errors.New("user aborted")
default:
}
kl, err := s.gpg.FindPublicKeys(ctx, r)
if err != nil {
out.Red(ctx, "Failed to read public key for '%s': %s", name, err)
continue
}
if len(kl) < 1 {
fmt.Println("key not found", r)
continue
}
fmt.Printf(" - %s\n", kl[0].OneLine())
}
fmt.Println("")
yes, err := s.askForBool(ctx, "Do you want to continue?", true)
if err != nil {
return recipients, errors.Wrapf(err, "failed to read user input")
}
if yes {
return recipients, nil
}
return recipients, errors.New("user aborted")
}
// AskForConfirmation asks a yes/no question until the user
// replies yes or no
func (s *Action) AskForConfirmation(ctx context.Context, text string) bool {
if ctxutil.IsAlwaysYes(ctx) {
return true
}
for i := 0; i < maxTries; i++ {
if choice, err := s.askForBool(ctx, text, false); err == nil {
return choice
}
}
return false
}
// askForBool ask for a bool (yes or no) exactly once.
// The empty answer uses the specified default, any other answer
// is an error.
func (s *Action) askForBool(ctx context.Context, text string, def bool) (bool, error) {
if ctxutil.IsAlwaysYes(ctx) {
return def, nil
}
choices := "y/N"
if def {
choices = "Y/n"
}
str, err := s.askForString(ctx, text, choices)
if err != nil {
return false, errors.Wrapf(err, "failed to read user input")
}
switch str {
case "Y/n":
return true, nil
case "y/N":
return false, nil
}
str = strings.ToLower(string(str[0]))
switch str {
case "y":
return true, nil
case "n":
return false, nil
default:
return false, errors.Errorf("Unknown answer: %s", str)
}
}
// askForString asks for a string once, using the default if the
// anser is empty. Errors are only returned on I/O errors
func (s *Action) askForString(ctx context.Context, text, def string) (string, error) {
if ctxutil.IsAlwaysYes(ctx) {
return def, nil
}
// check for context cancelation
select {
case <-ctx.Done():
return def, errors.New("user aborted")
default:
}
reader := bufio.NewReader(os.Stdin)
fmt.Printf("%s [%s]: ", text, def)
input, err := reader.ReadString('\n')
if err != nil {
return "", errors.Wrapf(err, "failed to read user input")
}
input = strings.TrimSpace(input)
if input == "" {
input = def
}
return input, nil
}
// askForInt asks for an valid interger once. If the input
// can not be converted to an int it returns an error
func (s *Action) askForInt(ctx context.Context, text string, def int) (int, error) {
if ctxutil.IsAlwaysYes(ctx) {
return def, nil
}
str, err := s.askForString(ctx, text, strconv.Itoa(def))
if err != nil {
return 0, err
}
intVal, err := strconv.Atoi(str)
if err != nil {
return 0, errors.Wrapf(err, "failed to convert to number")
}
return intVal, nil
}
// askForPassword prompts for a password twice until both match
func (s *Action) askForPassword(ctx context.Context, name string, askFn func(context.Context, string) (string, error)) (string, error) {
if !ctxutil.IsInteractive(ctx) {
return "", errors.New("impossible without terminal")
}
if ctxutil.IsAlwaysYes(ctx) {
return "", nil
}
if askFn == nil {
askFn = s.promptPass
}
for i := 0; i < maxTries; i++ {
// check for context cancelation
select {
case <-ctx.Done():
return "", errors.New("user aborted")
default:
}
pass, err := askFn(ctx, fmt.Sprintf("Enter password for %s", name))
if err != nil {
return "", err
}
passAgain, err := askFn(ctx, fmt.Sprintf("Retype password for %s", name))
if err != nil {
return "", err
}
if pass == passAgain {
return strings.TrimSpace(pass), nil
}
fmt.Println("Error: the entered password do not match")
}
return "", errors.New("no valid user input")
}
// AskForKeyImport asks for permissions to import the named key
func (s *Action) AskForKeyImport(ctx context.Context, key string) bool {
if ctxutil.IsAlwaysYes(ctx) {
return true
}
if !ctxutil.IsInteractive(ctx) {
return false
}
ok, err := s.askForBool(ctx, fmt.Sprintf("Do you want to import the public key '%s' into your keyring?", key), false)
if err != nil {
return false
}
return ok
}
// askforPrivateKey promts the user to select from a list of private keys
func (s *Action) askForPrivateKey(ctx context.Context, prompt string) (string, error) {
if !ctxutil.IsInteractive(ctx) {
return "", errors.New("no interaction without terminal")
}
kl, err := s.gpg.ListPrivateKeys(ctx)
if err != nil {
return "", err
}
kl = kl.UseableKeys()
if len(kl) < 1 {
return "", errors.New("No useable private keys found")
}
for i := 0; i < maxTries; i++ {
if ctxutil.IsAlwaysYes(ctx) {
return kl[0].Fingerprint, nil
}
// check for context cancelation
select {
case <-ctx.Done():
return "", errors.New("user aborted")
default:
}
fmt.Println(prompt)
for i, k := range kl {
fmt.Printf("[%d] %s\n", i, k.OneLine())
}
iv, err := s.askForInt(ctx, fmt.Sprintf("Please enter the number of a key (0-%d)", len(kl)-1), 0)
if err != nil {
continue
}
if iv >= 0 && iv < len(kl) {
return kl[iv].Fingerprint, nil
}
}
return "", errors.New("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 (s *Action) askForGitConfigUser(ctx context.Context) (string, string, error) {
var useCurrent bool
keyList, err := s.gpg.ListPrivateKeys(ctx)
if err != nil {
return "", "", err
}
keyList = keyList.UseableKeys()
if len(keyList) < 1 {
return "", "", errors.New("No usable private keys found")
}
for _, key := range keyList {
for _, identity := range key.Identities {
if !ctxutil.IsTerminal(ctx) || ctxutil.IsAlwaysYes(ctx) {
return identity.Name, identity.Email, nil
}
// check for context cancelation
select {
case <-ctx.Done():
return "", "", errors.New("user aborted")
default:
}
useCurrent, err = s.askForBool(
ctx,
fmt.Sprintf("Use %s (%s) for password store git config?", identity.Name, identity.Email), true)
if err != nil {
return "", "", err
}
if useCurrent {
return identity.Name, identity.Email, nil
}
}
}
return "", "", nil
}