-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
cmd_login.go
307 lines (259 loc) · 8.48 KB
/
cmd_login.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"errors"
"fmt"
"os"
"strings"
"time"
"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 NewCmdLogin(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
flags := []cli.Flag{
cli.BoolFlag{
Name: "s, switch",
Usage: "switch out the current user for another",
},
cli.StringFlag{
Name: "paperkey",
Usage: "DANGEROUS: automatically provision using this paper key",
},
cli.StringFlag{
Name: "devicename",
Usage: "Device name used in automated provisioning",
},
}
cmd := cli.Command{
Name: "login",
ArgumentHelp: "[username]",
Usage: "Establish a session with the keybase server",
Description: `"keybase login" allows you to authenticate your local service against
the keybase server. By default this runs an interactive flow, but
you can automate this if your service has never been logged into
a particular account before and the account has a paper key - in order
to do so, pass the username as an argument, your desired unique device
name as the "-devicename" flag and pass the paper key as the standard
input. Alternatively, these parameters can be passed as "KEYBASE_PAPERKEY"
and "KEYBASE_DEVICENAME" environment variables.`,
Action: func(c *cli.Context) {
cl.ChooseCommand(NewCmdLoginRunner(g), "login", c)
},
Flags: flags,
}
// Note we'll only be able to set this via mode via Environment variable
// since it's too early to check command-line setting of it.
if g.Env.GetRunMode() == libkb.DevelRunMode {
cmd.Flags = append(cmd.Flags, cli.BoolFlag{
Name: "emulate-gui",
Usage: "emulate GUI signing and fork GPG from the service",
})
}
return cmd
}
type CmdLogin struct {
libkb.Contextified
Username string
doUserSwitch bool
PaperKey string
DeviceName string
clientType keybase1.ClientType
cancel func()
done chan struct{}
SessionID int
}
func NewCmdLoginRunner(g *libkb.GlobalContext) *CmdLogin {
return &CmdLogin{
Contextified: libkb.NewContextified(g),
clientType: keybase1.ClientType_CLI,
done: make(chan struct{}, 1),
}
}
func (c *CmdLogin) Run() error {
protocols := []rpc.Protocol{
NewProvisionUIProtocol(c.G(), libkb.KexRoleProvisionee),
NewLoginUIProtocol(c.G()),
NewSecretUIProtocol(c.G()),
NewGPGUIProtocol(c.G()),
}
if err := RegisterProtocolsWithContext(protocols, c.G()); err != nil {
return err
}
client, err := GetLoginClient(c.G())
if err != nil {
return err
}
// TODO: it would be nice to move this up a level and have keybase/main.go create
// a context and pass it to Command.Run(), then it can handle cancel itself
// instead of using Command.Cancel().
ctx, cancel := context.WithCancel(context.Background())
c.cancel = cancel
defer func() {
c.cancel()
c.cancel = nil
}()
var paperKey string
if c.DeviceName != "" {
paperKey, err = c.getPaperKey()
if err != nil {
return err
}
}
err = client.Login(ctx,
keybase1.LoginArg{
Username: c.Username,
DeviceType: keybase1.DeviceTypeV2_DESKTOP,
ClientType: c.clientType,
SessionID: c.SessionID,
DoUserSwitch: c.doUserSwitch,
PaperKey: paperKey,
DeviceName: c.DeviceName,
})
c.done <- struct{}{}
// Provide explicit error messages for these cases.
switch x := err.(type) {
case libkb.NoSyncedPGPKeyError:
err = c.errNoSyncedKey()
case libkb.PassphraseError:
err = c.errPassphrase()
case libkb.NoMatchingGPGKeysError:
err = c.errNoMatchingGPGKeys(x.Fingerprints)
case libkb.DeviceAlreadyProvisionedError:
err = c.errDeviceAlreadyProvisioned()
case libkb.ProvisionUnavailableError:
err = c.errProvisionUnavailable()
case libkb.GPGUnavailableError:
err = c.errGPGUnavailable()
case libkb.NotFoundError:
err = c.errNotFound()
}
return err
}
func (c *CmdLogin) ParseArgv(ctx *cli.Context) error {
nargs := len(ctx.Args())
if nargs > 1 {
return errors.New("Invalid arguments.")
}
if nargs == 1 {
c.Username = ctx.Args()[0]
checker := libkb.CheckUsername
if !checker.F(c.Username) {
return fmt.Errorf("Invalid username. Valid usernames are: %s", checker.Hint)
}
}
c.doUserSwitch = ctx.Bool("switch")
c.PaperKey = c.getOption(ctx, "paperkey")
c.DeviceName = c.getOption(ctx, "devicename")
return nil
}
func (c *CmdLogin) getOption(ctx *cli.Context, s string) string {
v := ctx.String(s)
if len(v) > 0 {
return v
}
envVarName := fmt.Sprintf("KEYBASE_%s", strings.ToUpper(strings.ReplaceAll(s, "-", "_")))
v = os.Getenv(envVarName)
if len(v) > 0 {
return v
}
return ""
}
func (c *CmdLogin) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
KbKeyring: true,
API: true,
}
}
func (c *CmdLogin) Cancel() error {
c.G().Log.Debug("received request to cancel running login command")
if c.cancel != nil {
c.G().Log.Debug("command cancel function exists, calling it")
c.cancel()
// In go-framed-msgpack-rpc, dispatch.handleCall() starts a goroutine to check the context being
// canceled.
// So, need to wait here for call to actually finish in order for the cancel message to make it
// to the daemon.
select {
case <-c.done:
c.G().Log.Debug("command finished, cancel complete")
case <-time.After(5 * time.Second):
c.G().Log.Debug("timed out waiting for command to finish")
}
}
return nil
}
func (c *CmdLogin) getPaperKey() (ret string, err error) {
if len(c.PaperKey) > 0 {
return c.PaperKey, nil
}
ret, err = c.G().UI.GetTerminalUI().PromptPasswordMaybeScripted(PromptDescriptorPaperKey, "paper key: ")
return ret, err
}
func (c *CmdLogin) errNoSyncedKey() error {
return errors.New(`in Login
Sorry, your account is already established with a PGP public key, but this
utility cannot access the corresponding private key. You need to prove
you're you. We suggest one of the following:
- install GPG and put your PGP private key on this machine and try again
- reset your account and start fresh: https://keybase.io/#account-reset
- go back and provision with another device or paper key
`)
}
func (c *CmdLogin) errPassphrase() error {
return errors.New(`in Login
The server rejected your login attempt.
If you'd like to reset your passphrase, go to https://keybase.io/#password-reset
`)
}
func (c *CmdLogin) errNoMatchingGPGKeys(fingerprints []string) error {
plural := len(fingerprints) > 1
first := "Sorry, your account is already established with a PGP public key, but this\nutility cannot find the corresponding private key on this machine."
pre := "This is the fingerprint of the PGP key in your account:"
if plural {
first = "Sorry, your account is already established with PGP public keys, but this\nutility cannot find a corresponding private key on this machine."
pre = "These are the fingerprints of the PGP keys in your account:"
}
fpsIndent := make([]string, len(fingerprints))
for i, fp := range fingerprints {
fpsIndent[i] = " " + fp
}
after := `You need to prove you're you. We suggest one of the following:
- put one of the PGP private keys listed above on this machine and try again
- reset your account and start fresh: https://keybase.io/#account-reset
`
out := first + "\n" + pre + "\n\n" + strings.Join(fpsIndent, "\n") + "\n\n" + after
return errors.New(out)
}
func (c *CmdLogin) errDeviceAlreadyProvisioned() error {
return errors.New(`in Login
You have already provisioned this device. Please use 'keybase login [username]'
to log in.
`)
}
func (c *CmdLogin) errProvisionUnavailable() error {
return errors.New(`in Login
The only way to provision this device is with access to one of your existing
devices. You can try again later, or if you have lost access to all your
existing devices you can reset your account and start fresh.
If you'd like to reset your account: https://keybase.io/#account-reset
`)
}
func (c *CmdLogin) errGPGUnavailable() error {
return errors.New(`in Login
Sorry, your account is already established with a PGP public key, but this
utility cannot access the corresponding private key. You need to prove
you're you. We suggest one of the following:
- install GPG and put your PGP private key on this machine and try again
- reset your account and start fresh: https://keybase.io/#account-reset
`)
}
func (c *CmdLogin) errNotFound() error {
return errors.New(`in Login
This username doesn't exist.`)
}