This repository has been archived by the owner on Dec 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
otp.go
284 lines (226 loc) · 5.98 KB
/
otp.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
package main
import (
"errors"
"fmt"
"net/url"
"strconv"
"strings"
"github.com/davecgh/go-spew/spew"
"github.com/fatih/color"
"github.com/hgfischer/go-otp"
"github.com/urfave/cli"
)
type tOTP struct {
Type string `json:"type"` //totp or hotp
Issuer string `json:"issuer"` //identifies the issuer
User string `json:"user"` //idefifies a user if given
Secret string `json:"secret"` //shared secret base32 encoded
Label string `json:"Label"` //label for the account
Description string `json:"description"` //optional description for the token usage
Length uint8 `json:"length"` //number of digits/characters to return
Counter uint64 `json:"counter"` //counter for HOTPs
Base32 bool `json:"base32"` //true if secret is base32 encoded
}
func AddOTP(c *cli.Context) {
var (
err error
otp tOTP
uri string
)
// Using flags for setup
if c.NumFlags() == 0 {
color.Red("::Missing arguments")
}
if c.String("uri") == "" {
color.Red("::Missing URI value")
return
} else {
uri = c.String("uri")
}
if c.String("description") != "" {
otp.Description = c.String("description")
}
if c.String("label") != "" {
otp.Label = c.String("label")
}
err = parseURI(&uri, &otp)
if err != nil {
return
}
if Config.Debug {
spew.Dump(otp)
}
// Final check if anything is fishy
err = otp.Verify()
if err != nil {
return
}
Config.Tokens = append(Config.Tokens, &otp)
// Save new OTP to config
err = Config.SaveTokens()
if err != nil {
return
}
fmt.Printf("Successfully added new OTP called '%s'\n", otp.Label)
}
func GetOTP(c *cli.Context) {
var (
label string
i int
totp otp.TOTP
hotp otp.HOTP
)
label = c.Args().Get(0)
if label == "" {
color.Red("::Label argument is missing")
return
}
for i = range Config.Tokens {
if label == Config.Tokens[i].Label {
if Config.Tokens[i].Type == "totp" {
totp = otp.TOTP{Secret: Config.Tokens[i].Secret, IsBase32Secret: Config.Tokens[i].Base32}
fmt.Printf("%s\n", totp.Get())
} else {
hotp = otp.HOTP{Secret: Config.Tokens[i].Secret, IsBase32Secret: Config.Tokens[i].Base32}
fmt.Printf("%s\n", hotp.Get())
}
}
}
}
func ListOTP(c *cli.Context) {
var (
i int
)
fmt.Printf("-------------------------------------------------------------------------------------------------------\n")
fmt.Printf("| %-20s| %-20s| %-20s| %-30s|\n", "Label", "Issuer", "User", "Description")
fmt.Printf("|----------------------|----------------------|----------------------|--------------------------------|\n")
for i = range Config.Tokens {
fmt.Printf("| %-20s| %-20s| %-20s| %-30s|\n", Config.Tokens[i].Label, Config.Tokens[i].Issuer, Config.Tokens[i].User, Config.Tokens[i].Description)
}
fmt.Printf("-------------------------------------------------------------------------------------------------------\n")
}
func DeleteOTP(c *cli.Context) {
var (
label string
i int
resp string
)
label = c.Args().Get(0)
if label == "" {
color.Red("::Label argument is missing")
return
}
for i = range Config.Tokens {
if label == Config.Tokens[i].Label {
if !c.Bool("force") {
// Prompt user for confirmation
for true {
fmt.Printf("Are you sure you want to delete OTP with label '%s'? [y/n]: ", label)
fmt.Scanln(&resp)
if resp == "n" {
return
}
if resp == "y" {
break
}
}
}
Config.Tokens[i] = Config.Tokens[len(Config.Tokens)-1]
Config.Tokens = Config.Tokens[:len(Config.Tokens)-1]
_ = Config.SaveTokens()
fmt.Printf("Successfully deleted OTP with label '%s'\n", label)
break
}
}
}
func parseURI(uri *string, otp *tOTP) error {
var (
err error
data *url.URL
values url.Values
tmpName []string
tmpLabel string
)
data, err = url.Parse(*uri)
if err != nil {
color.Red("::Could not parse otpauth URI")
return err
}
values, err = url.ParseQuery(data.RawQuery)
if err != nil {
color.Red("::Could not parse otpauth URI query")
return err
}
// TODO: proper base32 detection
// most likely the secret is base32 encoded
otp.Base32 = true
otp.Type = data.Host
tmpLabel = data.Path[1:] //trim slash
tmpName = strings.Split(tmpLabel, ":")
if len(tmpName) > 1 {
// Actually split the string.
if otp.Label == "" {
// Only use the given label if none has been provided by the user.
otp.Label = tmpName[0]
}
otp.User = tmpName[1]
}
if values["issuer"] != nil {
// Issuer isn't mandatory thus checking if it is set or not.
otp.Issuer = values["issuer"][0]
}
// Though it should be set everytime, better check for secret really existing
if values["secret"] != nil {
otp.Secret = values["secret"][0]
} else {
color.Red("::Missing shared secret")
return err
}
// Since we have the verify function we skip error checking here
// so we have all error messages at one place.
if values["digits"] != nil {
// reusing counter since we've allocated that memory anyway
otp.Counter, _ = strconv.ParseUint(values["digits"][0], 10, 8)
otp.Length = uint8(otp.Counter)
otp.Counter = 0
}
if values["counter"] != nil {
otp.Counter, _ = strconv.ParseUint(values["counter"][0], 10, 64)
}
return nil
}
// Verify checks if a given tOTP has all necessary variables set.
func (otp *tOTP) Verify() error {
var (
i int
errorExists bool
)
if otp.Label == "" {
color.Red("::Label is missing")
errorExists = true
}
if otp.Secret == "" {
color.Red("::Secret is missing")
errorExists = true
}
if otp.Length == 0 {
color.Red("::Length isn't set")
errorExists = true
}
if otp.Type == "hotp" && otp.Counter == 0 {
color.Red("::Counter isn't set")
errorExists = true
}
// Also check if a config for a specific label already exists.
for i = range Config.Tokens {
if Config.Tokens[i].Label == otp.Label {
color.Red("::An OTP with label '%s' already exists, aborted\n", otp.Label)
errorExists = true
break
}
}
if errorExists {
return errors.New("")
}
return nil
}