-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
464 lines (386 loc) · 10.8 KB
/
common.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//
// Copyright © 2020 alexj@backpocket.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"bufio"
"encoding/json"
"fmt"
"github.com/alexj212/authy/totp"
"github.com/alexzorin/authy"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh/terminal"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
const (
configFileName = ".authy.json"
cacheFileName = ".authycache.json"
)
var verbose bool
var (
// Version version tag
Version = "v0.1"
// BuildDate date string of when build was performed filled in by -X compile flag
BuildDate string
// LatestCommit date string of when build was performed filled in by -X compile flag
LatestCommit string
// BuildNumber date string of when build was performed filled in by -X compile flag
BuildNumber string
// BuiltOnIP date string of when build was performed filled in by -X compile flag
BuiltOnIP string
// BuiltOnOs date string of when build was performed filled in by -X compile flag
BuiltOnOs string
// RuntimeVer date string of when build was performed filled in by -X compile flag
RuntimeVer string
)
// DeviceRegistration authy account details
type DeviceRegistration struct {
UserID uint64 `json:"user_id,omitempty"`
DeviceID uint64 `json:"device_id,omitempty"`
Seed string `json:"seed,omitempty"`
APIKey string `json:"api_key,omitempty"`
MainPassword string `json:"main_password,omitempty"`
}
// Token save in cache
type Token struct {
Name string `json:"name"`
OriginalName string `json:"original_name"`
Digital int `json:"digital"`
Secret string `json:"secret"`
Period int `json:"period"`
}
// Tokens type for results of search etc
type Tokens []*Token
// String return token name of results index
func (ts Tokens) String(i int) string {
if len(ts[i].Name) > len(ts[i].OriginalName) {
return ts[i].Name
}
return ts[i].OriginalName
}
// Len - number of Token results
func (ts Tokens) Len() int { return len(ts) }
func loadCachedTokens() ([]*Token, error) {
fpath, err := ConfigPath(cacheFileName)
if err != nil {
return nil, err
}
f, err := os.Open(fpath)
if err != nil {
return nil, err
}
defer f.Close()
tokens := []*Token{}
err = json.NewDecoder(f).Decode(tokens)
if err != nil {
return nil, err
}
if verbose {
fmt.Printf("Loaded cached providers from %v\n", fpath)
}
return tokens, err
}
func saveTokens(tks []*Token) error {
regrPath, err := ConfigPath(cacheFileName)
if err != nil {
return err
}
f, err := os.OpenFile(regrPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer f.Close()
err = json.NewEncoder(f).Encode(tks)
if err != nil {
fmt.Printf("Error saving tokens: %v\n", err)
return err
}
if verbose {
fmt.Printf("Saved tokens to file: %v\n", regrPath)
}
return nil
}
func getTokensFromAuthyServer(devInfo *DeviceRegistration) ([]*Token, error) {
client, err := authy.NewClient()
if err != nil {
log.Fatalf("Create authy API client failed %+v", err)
}
apps, err := client.QueryAuthenticatorApps(nil, devInfo.UserID, devInfo.DeviceID, devInfo.Seed)
if err != nil {
log.Fatalf("Fetch authenticator apps failed %+v", err)
}
if !apps.Success {
log.Fatalf("Fetch authenticator apps failed %+v", apps)
}
tokensResponse, err := client.QueryAuthenticatorTokens(nil, devInfo.UserID, devInfo.DeviceID, devInfo.Seed)
if err != nil {
log.Fatalf("Fetch authenticator tokens failed %+v", err)
}
if !tokensResponse.Success {
log.Fatalf("Fetch authenticator tokens failed %+v", tokensResponse)
}
if len(devInfo.MainPassword) == 0 {
fmt.Print("\nPlease input Authy main password: ")
pp, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
log.Fatalf("Get password failed %+v", err)
}
devInfo.MainPassword = strings.TrimSpace(string(pp))
SaveDeviceInfo(devInfo)
}
tks := []*Token{}
for _, v := range tokensResponse.AuthenticatorTokens {
secret, err := v.Decrypt(devInfo.MainPassword)
if err != nil {
devInfo.MainPassword = ""
SaveDeviceInfo(devInfo)
log.Fatalf("Decrypt token failed %+v", err)
}
if verbose {
fmt.Printf("AuthenticatorTokens: %v\n", v.Name)
}
tks = append(tks, &Token{
Name: v.Name,
OriginalName: v.OriginalName,
Digital: v.Digits,
Secret: secret,
})
}
for _, v := range apps.AuthenticatorApps {
secret, err := v.Token()
if err != nil {
log.Fatal("Get secret from app failed", err)
}
if verbose {
fmt.Printf("AuthenticatorApps: %v\n", v.Name)
}
tks = append(tks, &Token{
Name: v.Name,
Digital: v.Digits,
Secret: secret,
Period: 10,
})
}
saveTokens(tks)
return tks, nil
}
func findToken(tokens []*Token, tokenName string) (*Token, error) {
var tk *Token
for _, v := range tokens {
if tokenName == v.Name {
tk = v
break
}
}
if tk == nil {
err := errors.Errorf("unable to find token: %s", tokenName)
return nil, err
}
return tk, nil
}
// GetTotpCode return code and # of seconds left in current code
func (tk *Token) GetTotpCode() (string, int) {
codes := totp.GetTotpCode(tk.Secret, tk.Digital)
challenge := totp.GetChallenge()
secsLeft := 30 - int(time.Now().Unix()-challenge*30)
code := codes[1]
return code, secsLeft
}
// SaveDeviceInfo ..
func SaveDeviceInfo(devInfo *DeviceRegistration) error {
regrPath, err := ConfigPath(configFileName)
if err != nil {
return err
}
f, err := os.OpenFile(regrPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer f.Close()
err = json.NewEncoder(f).Encode(devInfo)
if verbose {
fmt.Printf("Save device info to file: %s\n", regrPath)
}
return nil
}
// LoadExistingDeviceInfo ,,,
func LoadExistingDeviceInfo() (*DeviceRegistration, error) {
devPath, err := ConfigPath(configFileName)
if err != nil {
log.Println("Get device info file path failed", err)
os.Exit(1)
}
f, err := os.Open(devPath)
if err != nil {
return nil, err
}
defer f.Close()
devInfo := &DeviceRegistration{}
err = json.NewDecoder(f).Decode(devInfo)
if err != nil {
return nil, err
}
if verbose {
fmt.Printf("Loaded device info from file: %s\n", configFileName)
}
return devInfo, nil
}
// ConfigPath get config file path
func ConfigPath(fname string) (string, error) {
devPath, err := homedir.Dir()
if err != nil {
return "", err
}
return filepath.Join(devPath, fname), nil
}
func promptAccountInfo() (phoneCC int, mobile string, err error) {
var (
sc = bufio.NewScanner(os.Stdin)
)
if len(countrycode) == 0 {
fmt.Print("\nWhat is your phone number's country code? (digits only, e.g. 1): ")
if !sc.Scan() {
err = errors.New("Please provide a phone country code, e.g. 1")
log.Println(err)
return
}
countrycode = sc.Text()
}
phoneCC, err = strconv.Atoi(strings.TrimSpace(countrycode))
if err != nil {
log.Println("Invalid country code. Parse country code failed", err)
return
}
if len(mobile) == 0 {
fmt.Print("\nWhat is your phone number? (digits only): ")
if !sc.Scan() {
err = errors.New("Please provide a phone number, e.g. 1232211")
log.Println(err)
return
}
mobile = sc.Text()
}
mobile = strings.TrimSpace(mobile)
return
}
func newRegistrationDevice() (*DeviceRegistration, error) {
phoneCC, mobile, err := promptAccountInfo()
if err != nil {
return nil, err
}
client, err := authy.NewClient()
if err != nil {
log.Println("New authy client failed", err)
return nil, err
}
userStatus, err := client.QueryUser(nil, phoneCC, mobile)
if err != nil {
log.Println("Query user failed", err)
return nil, err
}
if !userStatus.IsActiveUser() {
err = errors.New("There doesn't seem to be an Authy account attached to that phone number")
log.Println(err)
return nil, err
}
// Begin a device registration using Authy app push notification
regStart, err := client.RequestDeviceRegistration(nil, userStatus.AuthyID, authy.ViaMethodPush)
if err != nil {
log.Println("Start register device failed", err)
return nil, err
}
if !regStart.Success {
err = fmt.Errorf("Authy did not accept the device registration request: %+v", regStart)
log.Println(err)
return nil, err
}
var regPIN string
timeout := time.Now().Add(5 * time.Minute)
for {
if timeout.Before(time.Now()) {
err = errors.New("Gave up waiting for user to respond to Authy device registration request")
log.Println(err)
return nil, err
}
log.Printf("Checking device registration status (%s until we give up)", time.Until(timeout).Truncate(time.Second))
regStatus, err1 := client.CheckDeviceRegistration(nil, userStatus.AuthyID, regStart.RequestID)
if err1 != nil {
err = err1
log.Println(err)
return nil, err
}
if regStatus.Status == "accepted" {
regPIN = regStatus.PIN
break
} else if regStatus.Status != "pending" {
err = fmt.Errorf("Invalid status while waiting for device registration: %s", regStatus.Status)
log.Println(err)
return nil, err
}
time.Sleep(5 * time.Second)
}
regComplete, err := client.CompleteDeviceRegistration(nil, userStatus.AuthyID, regPIN)
if err != nil {
log.Println(err)
return nil, err
}
if regComplete.Device.SecretSeed == "" {
err = errors.New("Something went wrong completing the device registration")
log.Println(err)
return nil, err
}
devInfo := &DeviceRegistration{
UserID: regComplete.AuthyID,
DeviceID: regComplete.Device.ID,
Seed: regComplete.Device.SecretSeed,
APIKey: regComplete.Device.APIKey,
}
err = SaveDeviceInfo(devInfo)
if err != nil {
log.Println("Save device info failed", err)
}
return devInfo, nil
}
// Initialize initialize device info and cached tokens
func Initialize() (*DeviceRegistration, []*Token, error) {
devInfo, err := LoadExistingDeviceInfo()
if err != nil && os.IsNotExist(err) {
devInfo, err = newRegistrationDevice()
if err != nil {
log.Printf("Registration Device failed error: %+v\n", err)
return nil, nil, err
}
log.Println("Register device success!!!")
log.Printf("Your device info: %+v\n", devInfo)
} else if err != nil {
log.Printf("Load device info failed %v", err)
return nil, nil, err
}
tokens, err := loadCachedTokens()
if err != nil {
tokens, err = getTokensFromAuthyServer(devInfo)
if err != nil {
fmt.Printf("error getTokensFromAuthyServer: %v\n", err)
return nil, nil, err
}
}
fmt.Printf("\nLoaded %d auth tokens from authy server\n\n", len(tokens))
return devInfo, tokens, nil
}