-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
191 lines (168 loc) · 4.07 KB
/
main.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
package main
import (
"fmt"
"os"
"time"
"sort"
"strings"
"encoding/base32"
"strconv"
"github.com/atotto/clipboard"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
var errNoAccountFound = errors.New("no account exists")
var errInvalidOption = errors.New("invalid option, check 'tfat help'")
var errInvalidBase32 = errors.New("the key is not a valid base32 encoding")
var runInLoop bool
func checkResult(err error, errMsg string) {
if err != nil {
fmt.Println(errMsg + ": " + err.Error())
os.Exit(1)
}
}
func addOrModifyAccount(name, secret string) error {
if len(name) == 0 || len(secret) == 0 {
checkResult(errInvalidOption, "Error")
}
if _, err := base32.StdEncoding.WithPadding(base32.NoPadding).DecodeString(secret); err != nil {
checkResult(errInvalidBase32, "Error")
}
config, err := readConfig()
checkResult(err, "Open data failed")
if config.IsNew {
err = initPassword(&config)
checkResult(err, "Failed to add account")
}
config.Accounts[name] = strings.ToUpper(secret)
saveConfig(&config)
fmt.Printf("Add %s successfully!\n", name)
return nil
}
func deleteAccount(name string) {
config, err := readConfig()
checkResult(err, "Open data failed")
if _, ok := config.Accounts[name]; ok {
delete(config.Accounts, name)
err = saveConfig(&config)
checkResult(err, "Failed to delete account")
fmt.Println("Delete success!")
} else {
fmt.Printf("Account %s is not found", name)
}
}
func changePassword() {
config, err := readConfig()
checkResult(err, "Open data failed")
err = initPassword(&config)
checkResult(err, "Failed to change password")
saveConfig(&config)
fmt.Println("Change password successfully!")
}
func getCode() {
config, err := readConfig()
checkResult(err, "Open data failed")
var names []string
idx := 1
for k := range config.Accounts {
names = append(names, k)
fmt.Printf("%d. %s\n", idx, k)
idx++
}
if len(names) <= 0 {
checkResult(errNoAccountFound, "Failed to get code")
} else if len(names) > 1 {
for true {
idx, err = strconv.Atoi(getUserInput("select", false))
if err == nil && idx >= 1 && idx <= len(names) {
break
}
fmt.Printf("Number should be in 1..%d\n", len(names))
}
} else {
idx = 1
}
keyStr := config.Accounts[names[idx-1]]
var lastCode uint32
for true {
pwd, err := OneTimePassword(keyStr)
checkResult(err, "Failed to get code")
secondsRemaining := 30 - (time.Now().Unix() % 30)
fmt.Printf("\r%06d (remain %ds) ", pwd, secondsRemaining)
if lastCode != pwd {
lastCode = pwd
clipboard.WriteAll(fmt.Sprintf("%06d", pwd))
}
if !runInLoop {
fmt.Println("")
break
}
time.Sleep(1 * time.Second)
}
}
func main() {
app := cli.NewApp()
app.Name = "Two Factor Authentication(2FA) Tool"
app.Usage = "Help to generate 2FA code"
app.Version = "0.0.1"
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "loop, l",
Usage: "generate code in loop",
Destination: &runInLoop,
},
}
app.Commands = []cli.Command{
{
Name: "add",
UsageText: "tfat add <NAME> <KEY>",
Usage: "Add a new account",
Action: func(c *cli.Context) error {
if len(c.Args()) < 2 {
checkResult(errInvalidOption, "Error")
}
account := c.Args().First()
secret := c.Args()[1]
addOrModifyAccount(account, secret)
return nil
},
},
{
Name: "delete",
UsageText: "tfat delete <NAME>",
Usage: "Delete an account",
Action: func(c *cli.Context) error {
if len(c.Args()) < 1 {
checkResult(errInvalidOption, "Error")
}
deleteAccount(c.Args().First())
return nil
},
}, {
Name: "password",
UsageText: "tfat password <PASSWORD>",
Usage: "Change password",
Action: func(c *cli.Context) error {
changePassword()
return nil
},
},
}
app.Action = func(c *cli.Context) error {
getCode()
return nil
}
sort.Sort(cli.FlagsByName(app.Flags))
cli.HelpFlag = cli.BoolFlag{
Name: "help, h",
Usage: "Show this help",
}
cli.VersionFlag = cli.BoolFlag{
Name: "version, v",
Usage: "Print version",
}
err := app.Run(os.Args)
if err != nil {
checkResult(err, "Error")
}
}