-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
cmd_account_lockdown.go
182 lines (160 loc) · 4.62 KB
/
cmd_account_lockdown.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
// Copyright 2018 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"errors"
"fmt"
"text/tabwriter"
"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"
)
type CmdAccountLockdown struct {
libkb.Contextified
SetLockdownMode *bool
History bool
Force bool
}
func NewCmdAccountLockdown(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
cmd := &CmdAccountLockdown{
Contextified: libkb.NewContextified(g),
}
flags := []cli.Flag{
cli.BoolFlag{
Name: "enable",
Usage: "Enable account lockdown mode.",
},
cli.BoolFlag{
Name: "disable",
Usage: "Disable account lockdown mode.",
},
cli.BoolFlag{
Name: "history",
Usage: "Print history of lockdown mode changes.",
},
cli.BoolFlag{
Name: "force, f",
Usage: "Don't prompt.",
},
}
return cli.Command{
Name: "lockdown",
Usage: "Manage account lockdown mode",
Flags: flags,
Action: func(c *cli.Context) {
cl.ChooseCommand(cmd, "lockdown", c)
},
Description: `When lockdown mode is enabled for an account, some operations are
blocked for website sessions, including (but not limited to):
- account delete or reset,
- posting signatures,
- changing password or email address,
- changing profile information or profile picture.
These actions are still possible using the Keybase client.`,
}
}
func (c *CmdAccountLockdown) ParseArgv(ctx *cli.Context) error {
if len(ctx.Args()) > 0 {
args0 := ctx.Args()[0]
if args0 == "enable" || args0 == "disable" {
return fmt.Errorf("did you mean: keybase account lockdown --%s", args0)
}
return errors.New("this command does not take any positional arguments, only flags")
}
enable := ctx.Bool("enable")
disable := ctx.Bool("disable")
if enable && disable {
return errors.New("Both --enable and --disable flags are passed which is invalid")
} else if enable {
val := true
c.SetLockdownMode = &val
} else if disable {
val := false
c.SetLockdownMode = &val
}
c.History = ctx.Bool("history")
c.Force = ctx.Bool("force")
return nil
}
func (c *CmdAccountLockdown) Run() error {
cli, err := GetAccountClient(c.G())
if err != nil {
return err
}
tui := c.G().UI.GetTerminalUI()
enabledGreen := func() string { return ColorString(c.G(), "green", "enabled") }
disabledYellow := func() string { return ColorString(c.G(), "yellow", "disabled") }
if c.SetLockdownMode != nil {
res, err := cli.GetLockdownMode(context.Background(), 0)
if err != nil {
return err
}
if res.Status == *c.SetLockdownMode {
if res.Status {
_, _ = tui.PrintfUnescaped("Lockdown mode is already %s. Nothing to do.\n", enabledGreen())
} else {
_, _ = tui.PrintfUnescaped("Lockdown mode is already %s. Nothing to do.\n", disabledYellow())
}
return nil
}
if !c.Force {
var prompt string
if *c.SetLockdownMode {
prompt = fmt.Sprintf("Do you want to %s lockdown mode?", ColorString(c.G(), "green", "ENABLE"))
} else {
prompt = fmt.Sprintf("Do you want to %s lockdown mode?", ColorString(c.G(), "red", "DISABLE"))
}
ok, err := tui.PromptYesNo(PromptDescriptorChangeLockdownMode, prompt, libkb.PromptDefaultNo)
if err != nil {
return err
}
if !ok {
c.G().Log.CDebugf(context.TODO(), "CmdAccountLockdown: user aborted via prompt")
return NotConfirmedError{}
}
}
err = cli.SetLockdownMode(context.Background(), keybase1.SetLockdownModeArg{
Enabled: *c.SetLockdownMode,
})
if err != nil {
return err
}
} else {
fmt.Fprintf(tui.ErrorWriter(), "Learn more about lockdown mode: `%s`\n", ColorString(c.G(), "bold", "keybase account lockdown -h"))
}
res, err := cli.GetLockdownMode(context.Background(), 0)
if err != nil {
return err
}
tui.Printf("Lockdown mode is: ")
if res.Status {
_, _ = tui.PrintfUnescaped("%s\n", enabledGreen())
} else {
_, _ = tui.PrintfUnescaped("%s\n", disabledYellow())
}
if c.History {
tabw := new(tabwriter.Writer)
tabw.Init(tui.OutputWriter(), 0, 8, 4, ' ', 0)
fmt.Fprintf(tabw, "Changed to:\tChange time:\tDevice:\n")
for _, v := range res.History {
var status string
if v.Status {
status = "enabled"
} else {
status = "disabled"
}
fmt.Fprintf(tabw, "%s\t%s\t%s (%s)\n", status, keybase1.FormatTime(v.CreationTime), v.DeviceName, v.DeviceID)
}
tabw.Flush()
}
return nil
}
func (c *CmdAccountLockdown) GetUsage() libkb.Usage {
return libkb.Usage{
API: true,
KbKeyring: true,
Config: true,
}
}