-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (59 loc) · 1.37 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
package main
import (
"fmt"
"os"
"path"
"github.com/99designs/keyring"
"github.com/atotto/clipboard"
)
func GetPassword() (string, error) {
keyring, err := keyring.Open(keyring.Config{
ServiceName: "2FA",
KeychainTrustApplication: true,
})
if err != nil {
return "", err
}
keys, err := keyring.Keys()
if err != nil || len(keys) < 1 {
return "", err
}
entry, err := keyring.Get(keys[0])
if err != nil {
return "", err
}
return string(entry.Data), nil
}
func main() {
password, err := GetPassword()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get the encryption password: %s\n", err)
return
}
home, _ := os.UserHomeDir()
filename := path.Join(home, ".2fa", "otp_accounts.json.aes")
accounts, err := ReadDatabase(filename, password)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open your database: %s\n", err)
return
}
if len(os.Args) < 2 {
fmt.Println("Available accounts:")
for _, entry := range accounts {
fmt.Printf("* %s\n", entry.Label)
}
return
}
label := os.Args[1]
entry := FindAccountByLabel(accounts, label)
if entry == nil {
fmt.Fprintf(os.Stderr, "No matching account found\n")
return
}
otp := Totp(entry.Secret, entry.Period, entry.Digits)
fmt.Printf("2FA code for %s: %s\n", entry.Label, otp)
err = clipboard.WriteAll(otp)
if err == nil {
fmt.Println("(copied to clipboard)")
}
}