-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
cmd_device_remove.go
198 lines (175 loc) · 4.97 KB
/
cmd_device_remove.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"errors"
"fmt"
"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"
"golang.org/x/net/context"
)
type CmdDeviceRemove struct {
idOrName string
forceSelf bool
forceLast bool
libkb.Contextified
}
func (c *CmdDeviceRemove) SetIDOrName(s string) {
c.idOrName = s
}
func (c *CmdDeviceRemove) ParseArgv(ctx *cli.Context) error {
if len(ctx.Args()) != 1 {
return fmt.Errorf("Device remove only takes one argument: the device ID or name.")
}
c.idOrName = ctx.Args()[0]
c.forceSelf = ctx.Bool("force-self")
c.forceLast = ctx.Bool("force-last")
return nil
}
func (c *CmdDeviceRemove) confirmDelete(id keybase1.DeviceID) error {
if c.forceSelf || c.forceLast {
return nil
}
rkcli, err := GetRekeyClient(c.G())
if err != nil {
return err
}
arg := keybase1.GetRevokeWarningArg{TargetDevice: id}
res, err := rkcli.GetRevokeWarning(context.TODO(), arg)
if err != nil {
return err
}
if len(res.EndangeredTLFs) == 0 {
return nil
}
tui := c.G().UI.GetTerminalUI()
if tui == nil {
return errors.New("Need a terminal UI to prompt for TLF data loss override")
}
out := "Are you sure you want to delete this device? If you do, you can lose KBFS access to:\n"
n := len(res.EndangeredTLFs)
for i, tlf := range res.EndangeredTLFs {
if i == 10 && n > 11 {
out += fmt.Sprintf(" .... and %d others\n", (n - 10))
break
}
out += fmt.Sprintf(" * %s\n", tlf.Name)
}
_ = tui.OutputDesc(OutputDescriptorEndageredTLFs, out)
ok, err := tui.PromptYesNo(PromptDescriptorDeviceRevoke, "Go ahead anyway?", libkb.PromptDefaultNo)
if err != nil {
return err
}
if !ok {
return errors.New("not confirmed")
}
return nil
}
func (c *CmdDeviceRemove) Run() (err error) {
protocols := []rpc.Protocol{
NewSecretUIProtocol(c.G()),
}
if err = RegisterProtocolsWithContext(protocols, c.G()); err != nil {
return err
}
var id keybase1.DeviceID
id, err = keybase1.DeviceIDFromString(c.idOrName)
if err != nil {
id, err = c.lookup(c.idOrName)
if err != nil {
return err
}
}
if err = c.confirmDelete(id); err != nil {
return err
}
cli, err := GetRevokeClient(c.G())
if err != nil {
return err
}
err = cli.RevokeDevice(context.TODO(), keybase1.RevokeDeviceArg{
ForceSelf: c.forceSelf,
ForceLast: c.forceLast,
DeviceID: id,
})
ui := c.G().UI.GetTerminalUI()
if ui == nil {
return err
}
switch e := err.(type) {
case libkb.RevokeCurrentDeviceError:
_ = ui.Output("You tried to remove this device. If you are sure you want to\n")
_ = ui.Output("remove the current device, then run\n\n")
_ = ui.Output("\tkeybase device remove --force-self <device id or name>\n\n")
case libkb.RevokeLastDeviceError:
if e.NoPassphrase {
_ = ui.Output("Because your account does not have a passphrase, you cannot\n")
_ = ui.Output("revoke your last device. Set a passphrase first with\n\n")
_ = ui.Output("\tkeybase passphrase set\n\n")
} else {
_ = ui.Output("You tried to remove the last device in your account. If you are\n")
_ = ui.Output("sure you want to remove it, then run\n\n")
_ = ui.Output("\tkeybase device remove --force-last <device id or name>\n\n")
_ = ui.Output("Your account will be automatically reset afterward.\n\n")
}
case libkb.RevokeLastDevicePGPError:
_ = ui.Output("You tried to remove the last device in your account. Because\n")
_ = ui.Output("you also have a PGP key, you cannot do this.\n\n")
_ = ui.Output("You can reset your account here: https://keybase.io/#account-reset\n\n")
default:
return err
}
return nil
}
func (c *CmdDeviceRemove) lookup(name string) (keybase1.DeviceID, error) {
cli, err := GetDeviceClient(c.G())
if err != nil {
return "", err
}
devs, err := cli.DeviceList(context.TODO(), 0)
if err != nil {
return "", err
}
for _, dev := range devs {
if dev.Name == name {
return dev.DeviceID, nil
}
}
return "", fmt.Errorf("Invalid Device ID or Unknown Device Name")
}
func NewCmdDeviceRemove(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "remove",
ArgumentHelp: "<id|name>",
Usage: "Remove a device",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "force-self",
Usage: "Force removal of the current device.",
},
cli.BoolFlag{
Name: "force-last",
Usage: "Force removal of the last device in your account.",
},
},
Action: func(c *cli.Context) {
cl.ChooseCommand(NewCmdDeviceRemoveRunner(g), "remove", c)
},
}
}
func NewCmdDeviceRemoveRunner(g *libkb.GlobalContext) *CmdDeviceRemove {
return &CmdDeviceRemove{
Contextified: libkb.NewContextified(g),
}
}
func (c *CmdDeviceRemove) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
GpgKeyring: true,
KbKeyring: true,
API: true,
}
}