-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
136 lines (116 loc) · 2.5 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
package main
import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"encoding/base32"
"encoding/binary"
"flag"
"fmt"
homedir "github.com/mitchellh/go-homedir"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"strconv"
"strings"
"time"
)
var (
configFilePath string
configBytes []byte
config = []ConfigurationItem{}
home string
issuers []string
err error
)
func main() {
home, err = homedir.Dir()
if err != nil {
panic(err)
}
configFilePath = os.Getenv("TWO_FA_CONFIG")
if configFilePath == "" {
configFilePath = home + "/.2faconfig.yaml"
}
flag.StringVar(&configFilePath, "config", configFilePath, "path to secret file")
flag.Parse()
configBytes, err = ioutil.ReadFile(configFilePath)
if err != nil {
fmt.Println("configuration file read error")
panic(err)
}
err = yaml.Unmarshal(configBytes, &config)
if err != nil {
panic(err)
}
for _, n := range config {
issuers = append(issuers, n.Issuer)
}
count := flag.NArg()
if count == 0 {
fmt.Println(issuers)
flag.Usage()
// flag.PrintDefaults()
os.Exit(1)
}
if count != 1 {
fmt.Println(issuers)
flag.Usage()
// flag.PrintDefaults()
os.Exit(1)
}
Issuer, err := getIssuerFromFlags()
if err != nil {
panic(err)
}
for _, n := range config {
if strings.ToLower(Issuer) == strings.ToLower(n.Issuer) {
fmt.Println(getTOTPToken(n.Secret))
}
}
}
func getIssuerFromFlags() (string, error) {
for _, val := range flag.Args() {
return val, nil
}
return "", fmt.Errorf("why you appear here?")
}
type ConfigurationItem struct {
Issuer string
Secret string
}
// below code was founded here: https://github.com/tilaklodha/google-authenticator/blob/master/google_authenticator.go
func getTOTPToken(secret string) string {
interval := time.Now().Unix() / 30
return getHOTPToken(secret, interval)
}
func getHOTPToken(secret string, interval int64) string {
key, err := base32.StdEncoding.DecodeString(strings.ToUpper(secret))
if err != nil {
panic(err)
}
bs := make([]byte, 8)
binary.BigEndian.PutUint64(bs, uint64(interval))
hash := hmac.New(sha1.New, key)
hash.Write(bs)
h := hash.Sum(nil)
o := (h[19] & 15)
var header uint32
r := bytes.NewReader(h[o : o+4])
err = binary.Read(r, binary.BigEndian, &header)
if err != nil {
panic(err)
}
h12 := (int(header) & 0x7fffffff) % 1000000
otp := strconv.Itoa(int(h12))
return prefixWithZeros(otp)
}
func prefixWithZeros(otp string) string {
if len(otp) == 6 {
return otp
}
for i := (6 - len(otp)); i > 0; i-- {
otp = "0" + otp
}
return otp
}