-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (65 loc) · 1.61 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
package main
import (
"encoding/base32"
"fmt"
"io/ioutil"
"net/url"
dgoogauth "github.com/dgryski/dgoogauth"
qr "rsc.io/qr"
)
const (
qrFilename = "./tmp/qr.png"
)
func main() {
secret := []byte{'n', '.', 'v', 't', 'o', 'r', 'u', 's', 'h', 'i', 'n', 0xDE, 0xAD, 0xBE, 0xEF}
secretBase32 := base32.StdEncoding.EncodeToString(secret)
account := "[email protected]"
issuer := "SmartWorld.team"
URL, err := url.Parse("otpauth://totp")
if err != nil {
panic(err)
}
URL.Path += "/" + url.PathEscape(issuer) + ":" + url.PathEscape(account)
params := url.Values{}
params.Add("secret", secretBase32)
params.Add("issuer", issuer)
URL.RawQuery = params.Encode()
fmt.Printf("URL is %s\n", URL.String())
code, err := qr.Encode(URL.String(), qr.Q)
if err != nil {
panic(err)
}
b := code.PNG()
err = ioutil.WriteFile(qrFilename, b, 0600)
if err != nil {
panic(err)
}
fmt.Printf("QR code is in %s. Please scan it into Google Authenticator app.\n", qrFilename)
// The OTPConfig gets modified by otpc.Authenticate() to prevent passcode replay, etc.,
// so allocate it once and reuse it for multiple calls.
otpc := &dgoogauth.OTPConfig{
Secret: secretBase32,
WindowSize: 3,
HotpCounter: 0,
// UTC: true,
}
for {
var token string
fmt.Printf("Please enter the token value (or q or quit to quit): ")
fmt.Scanln(&token)
if token == "q" || token == "quit" {
break
}
val, err := otpc.Authenticate(token)
if err != nil {
fmt.Println(err)
continue
}
if !val {
fmt.Println("Sorry, Not Authenticated")
continue
}
fmt.Println("Authenticated!")
}
return
}