forked from keybase/client
/
cmd_signup.go
576 lines (502 loc) · 14.1 KB
/
cmd_signup.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"fmt"
"os"
"golang.org/x/net/context"
"github.com/keybase/cli"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
)
func NewCmdSignup(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
cmd := cli.Command{
Name: "signup",
Usage: "Signup for a new account",
Action: func(c *cli.Context) {
cl.ChooseCommand(NewCmdSignupRunner(g), "signup", c)
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "c, invite-code",
Usage: "Specify an invite code.",
},
cli.StringFlag{
Name: "email",
Usage: "Specify an account email.",
},
cli.StringFlag{
Name: "username",
Usage: "Specify a username.",
},
cli.BoolFlag{
Name: "no-email",
Usage: "Do not signup with email.",
},
cli.BoolFlag{
Name: "set-password",
Usage: "Ask for password (optional by default).",
},
cli.BoolFlag{
Name: "force",
Usage: "(dangerous) Ignore any reasons not to signup right now",
},
},
}
cmd.Flags = append(cmd.Flags, restrictedSignupFlags...)
return cmd
}
type PromptFields struct {
email, code, username, passphraseRetry, deviceName *Field
}
func (pf PromptFields) ToList() []*Field {
fields := []*Field{pf.email}
if pf.code.Defval == "" {
fields = append(fields, pf.code)
}
fields = append(fields, pf.username, pf.passphraseRetry, pf.deviceName)
return fields
}
type CmdSignup struct {
libkb.Contextified
fields *PromptFields
prompter *Prompter
scli keybase1.SignupClient
ccli keybase1.ConfigClient
code string
requestedInvite bool
fullname string
notes string
passphrase string
storeSecret bool
defaultEmail string
defaultUsername string
defaultPassphrase string
doPromptPassphrase bool
noEmail bool
randomPassphrase bool
defaultDevice string
doPrompt bool
skipMail bool
genPGP bool
genPaper bool
force bool
// Test option to not call to requestInvitationCode for bypassing
// invitation code.
noInvitationCodeBypass bool
}
func NewCmdSignupRunner(g *libkb.GlobalContext) *CmdSignup {
return &CmdSignup{
Contextified: libkb.NewContextified(g),
doPrompt: true,
}
}
func (s *CmdSignup) SetTest() {
s.skipMail = true
s.genPaper = true
// Signup test users with passwords by default.
s.doPromptPassphrase = true
}
func (s *CmdSignup) SetTestWithPaper(b bool) {
s.skipMail = true
s.genPaper = b
// Signup test users with passwords by default.
s.doPromptPassphrase = true
}
func (s *CmdSignup) SetNoPassphrasePrompt() {
// Do not prompt for passphrase, for testing.
s.doPromptPassphrase = false
}
func (s *CmdSignup) SetNoInvitationCodeBypass() {
// This will result in deterministic invitation code prompt unless it's
// been provided via command argument or env var. Otherwise prompt is
// affected by whether API server allows us to skip invite code (see
// requestInvitationCode).
// Used in tests. If the test is checking prompts that occurred, it should
// use this to avoid invitation code bypass behaviour that may differ
// between testing environments.
s.noInvitationCodeBypass = true
}
func (s *CmdSignup) SetNoEmail() {
s.noEmail = true
}
func (s *CmdSignup) ParseArgv(ctx *cli.Context) (err error) {
nargs := len(ctx.Args())
s.code = ctx.String("invite-code")
if s.code == "" {
// For development convenience.
s.code = os.Getenv("KEYBASE_INVITATION_CODE")
}
s.defaultUsername = ctx.String("username")
s.defaultPassphrase = ctx.String("passphrase")
s.defaultDevice = ctx.String("device")
if s.defaultDevice == "" {
s.defaultDevice = "home computer"
}
// If using prompter mode (non-batch), we do not ask for password by
// default and user is signing up in no-passphrase mode - that is unless
// --set-password flag is used. Only then we are prompting for password.
s.doPromptPassphrase = ctx.Bool("set-password")
s.defaultEmail = ctx.String("email")
s.noEmail = ctx.Bool("no-email")
if (s.defaultEmail != "") && s.noEmail {
return fmt.Errorf("cannot pass --no-email and non-empty --email")
}
s.force = ctx.Bool("force")
if ctx.Bool("batch") {
s.fields = &PromptFields{
email: &Field{Value: &s.defaultEmail},
code: &Field{Value: &s.code},
username: &Field{Value: &s.defaultUsername},
deviceName: &Field{Value: &s.defaultDevice},
passphraseRetry: &Field{},
}
s.passphrase = s.defaultPassphrase
s.genPGP = ctx.Bool("pgp")
s.genPaper = !ctx.Bool("skip-paperkey")
s.doPrompt = false
s.storeSecret = true
s.randomPassphrase = ctx.Bool("no-passphrase")
if s.randomPassphrase && s.defaultPassphrase != "" {
return fmt.Errorf("cannot pass both --no-passphrase and --passphrase")
}
} else {
s.doPrompt = true
}
if nargs != 0 {
err = BadArgsError{"Signup doesn't take arguments."}
}
return err
}
func (s *CmdSignup) successMessage() error {
username := s.fields.username.GetValue()
msg := fmt.Sprintf(`
Welcome to keybase.io!
- you are now logged in as %s
- your profile on keybase is https://keybase.io/%s
- type 'keybase help' for more instructions
Found a bug? Please report it with %skeybase log send%s
Enjoy!
`, username, username, "`", "`")
return s.G().UI.GetTerminalUI().Output(msg)
}
func (s *CmdSignup) Run() (err error) {
s.G().Log.Debug("| Client mode")
if err = s.initClient(); err != nil {
return err
}
if !s.force {
if err = s.checkRegistered(); err != nil {
return err
}
}
if s.code == "" && !s.noInvitationCodeBypass {
// Eat the error here - we prompt the user in that case
_ = s.requestInvitationCode()
}
if err = s.trySignup(); err != nil {
if _, cce := err.(CleanCancelError); cce {
s.requestedInvite = true
return s.requestInvite()
}
return err
}
return s.successMessage()
}
func (s *CmdSignup) checkRegistered() (err error) {
s.G().Log.Debug("+ clientModeSignupEngine::CheckRegistered")
defer s.G().Log.Debug("- clientModeSignupEngine::CheckRegistered -> %s", libkb.ErrToOk(err))
var rres keybase1.CurrentStatus
if rres, err = s.ccli.GetCurrentStatus(context.TODO(), 0); err != nil {
return err
}
if !rres.Registered {
return
}
if !s.doPrompt {
return nil
}
prompt := "Already registered; do you want to reregister?"
if rereg, err := s.G().UI.GetTerminalUI().PromptYesNo(PromptDescriptorReregister, prompt, libkb.PromptDefaultNo); err != nil {
return err
} else if !rereg {
return NotConfirmedError{}
}
return nil
}
func (s *CmdSignup) prompt() (err error) {
s.G().Log.Debug("+ prompt")
defer func() {
s.G().Log.Debug("- prompt -> %s", libkb.ErrToOk(err))
}()
if !s.doPrompt {
return nil
}
if s.prompter == nil {
s.MakePrompter()
}
if err = s.prompter.Run(); err != nil {
s.G().Log.Debug("| Prompter failed\n")
return
}
if s.doPromptPassphrase {
f := s.fields.passphraseRetry
if f.Disabled || libkb.IsYes(f.GetValue()) {
var res keybase1.GetPassphraseRes
res, err = PromptPassphrase(s.G())
if err != nil {
return
}
s.passphrase = res.Passphrase
s.storeSecret = res.StoreSecret
}
} else {
s.randomPassphrase = true
}
return
}
func (s *CmdSignup) trySignup() (err error) {
retry := true
for retry && err == nil {
if err = s.prompt(); err == nil {
retry, err = s.runEngine()
}
}
return err
}
func (s *CmdSignup) runEngine() (retry bool, err error) {
if s.randomPassphrase && s.passphrase != "" {
return false, fmt.Errorf("Requested random passphrase but passphrase was provided")
}
rarg := keybase1.SignupArg{
Username: s.fields.username.GetValue(),
InviteCode: s.fields.code.GetValue(),
Passphrase: s.passphrase,
RandomPw: s.randomPassphrase,
StoreSecret: true,
DeviceName: s.fields.deviceName.GetValue(),
DeviceType: keybase1.DeviceType_DESKTOP,
SkipMail: s.skipMail,
GenPGPBatch: s.genPGP,
GenPaper: s.genPaper,
}
if s.fields.email != nil {
email := s.fields.email.GetValue()
if email != "" {
rarg.Email = email
}
}
res, err := s.scli.Signup(context.TODO(), rarg)
if err == nil {
return false, nil
}
s.G().Log.Debug("error: %q, type: %T", err, err)
// check to see if the error is a join engine run result:
if res.PassphraseOk {
s.fields.passphraseRetry.Disabled = false
}
if !res.PostOk {
retry, err = s.handlePostError(err)
}
return retry, err
}
func (s *CmdSignup) requestInvitePromptForOk() (err error) {
prompt := "Would you like to be added to the invite request list?"
var invite bool
if invite, err = s.G().UI.GetTerminalUI().PromptYesNo(PromptDescriptorInviteOK, prompt, libkb.PromptDefaultYes); err != nil {
return err
}
if !invite {
return NotConfirmedError{}
}
return nil
}
func (s *CmdSignup) requestInvitePromptForData() error {
fullname := &Field{
Name: "fullname",
Prompt: "Your name",
PromptDescriptor: PromptDescriptorSignupFullName,
}
notes := &Field{
Name: "notes",
Prompt: "Any comments for the team",
PromptDescriptor: PromptDescriptorSignupNotes,
}
fields := []*Field{fullname, notes}
prompter := NewPrompter(fields, s.G().UI.GetTerminalUI())
if err := prompter.Run(); err != nil {
return err
}
s.fullname = fullname.GetValue()
s.notes = notes.GetValue()
return nil
}
func (s *CmdSignup) requestInvite() error {
if err := s.requestInvitePromptForOk(); err != nil {
return err
}
if err := s.requestInvitePromptForData(); err != nil {
return err
}
return s.postInviteRequest()
}
func (s *CmdSignup) MakePrompter() {
code := &Field{
Defval: s.code,
Name: "code",
Prompt: "Your invite code",
Checker: &libkb.CheckInviteCode,
PromptDescriptor: PromptDescriptorSignupCode,
}
if len(s.code) == 0 {
code.Prompt += " (leave blank if you don't have one)"
code.Thrower = func(k, v string) error {
if len(v) == 0 {
return CleanCancelError{}
}
return nil
}
} else {
// we omit this prompt if populated
code.Value = &s.code
}
passphraseRetry := &Field{
Defval: "n",
Disabled: true,
Name: "passphraseRetry",
Checker: &libkb.CheckYesNo,
Prompt: "Re-enter passphrase",
PromptDescriptor: PromptDescriptorSignupReenterPassphrase,
}
email := &Field{
Disabled: s.noEmail,
Defval: s.defaultEmail,
Name: "email",
Prompt: "Your email address",
Checker: &libkb.CheckEmail,
PromptDescriptor: PromptDescriptorSignupEmail,
}
username := &Field{
Defval: s.defaultUsername,
Name: "username",
Prompt: "Your desired username",
Checker: &libkb.CheckUsername,
PromptDescriptor: PromptDescriptorSignupUsername,
}
deviceName := &Field{
Defval: s.defaultDevice,
Name: "devname",
Prompt: "A public name for this device",
Checker: &libkb.CheckDeviceName,
PromptDescriptor: PromptDescriptorSignupDevice,
}
s.fields = &PromptFields{
email: email,
code: code,
username: username,
passphraseRetry: passphraseRetry,
deviceName: deviceName,
}
s.prompter = NewPrompter(s.fields.ToList(), s.G().UI.GetTerminalUI())
}
func (s *CmdSignup) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
GpgKeyring: true,
KbKeyring: true,
API: true,
}
}
func (s *CmdSignup) initClient() error {
var err error
if s.scli, err = GetSignupClient(s.G()); err != nil {
return err
}
if s.ccli, err = GetConfigClient(s.G()); err != nil {
return err
}
protocols := []rpc.Protocol{
NewSecretUIProtocol(s.G()),
}
if s.doPrompt {
protocols = append(protocols, NewGPGUIProtocol(s.G()))
protocols = append(protocols, NewLoginUIProtocol(s.G()))
} else {
gpgUI := s.G().UI.GetGPGUI().(GPGUI)
gpgUI.noPrompt = true
protocols = append(protocols, keybase1.GpgUiProtocol(gpgUI))
loginUI := s.G().UI.GetLoginUI().(LoginUI)
loginUI.noPrompt = true
protocols = append(protocols, keybase1.LoginUiProtocol(loginUI))
}
return RegisterProtocolsWithContext(protocols, s.G())
}
func (s *CmdSignup) postInviteRequest() (err error) {
rarg := keybase1.InviteRequestArg{
Email: s.fields.email.GetValue(),
Fullname: s.fullname,
Notes: s.notes,
}
err = s.scli.InviteRequest(context.TODO(), rarg)
if err == nil {
s.G().Log.Info("Success! You're on our list, thanks for your interest.")
}
return
}
func (s *CmdSignup) requestInvitationCode() error {
code, err := s.scli.GetInvitationCode(context.TODO(), 0)
if err != nil {
s.G().Log.Debug("Error getting new code: %v", err)
} else {
s.G().Log.Debug("Success! You got new code %s", code)
}
s.code = code
return err
}
func (s *CmdSignup) handlePostError(inerr error) (retry bool, err error) {
retry = false
err = inerr
if ase, ok := inerr.(libkb.AppStatusError); ok {
switch ase.Name {
case "BAD_SIGNUP_EMAIL_TAKEN":
v := s.fields.email.Clear()
s.G().Log.Errorf("Email address '%s' already taken", v)
retry = true
err = nil
case "BAD_SIGNUP_USERNAME_RESERVED":
v := s.fields.username.Clear()
s.G().Log.Errorf("Username '%s' is reserved! Please email admin@keybase.io for more info.", v)
retry = true
err = nil
case "BAD_SIGNUP_USERNAME_DELETED":
v := s.fields.username.Clear()
s.G().Log.Errorf("Username '%s' has been deleted.", v)
retry = true
err = nil
case "BAD_SIGNUP_USERNAME_TAKEN", "BAD_SIGNUP_TEAM_NAME":
v := s.fields.username.Clear()
s.G().Log.Errorf("Username '%s' already taken", v)
retry = true
err = nil
case "INPUT_ERROR":
if ase.IsBadField("username") {
v := s.fields.username.Clear()
s.G().Log.Errorf("Username '%s' rejected by server", v)
retry = true
err = nil
}
case "BAD_INVITATION_CODE":
v := s.fields.code.Clear()
s.G().Log.Errorf("Bad invitation code '%s' given", v)
retry = true
err = nil
}
}
if !s.doPrompt {
err = inerr
retry = false
}
return
}