-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
277 lines (244 loc) · 7.48 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
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
//go:generate abigen --sol=./web3/contracts/demo/SADemo.sol --pkg=demo --out=./web3/contracts/demo/SADemo.go
package main
import (
"bufio"
"context"
"embed"
"errors"
"flag"
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/joho/godotenv"
"github.com/refitor/rslog"
"github.com/refitor/selfAuth/auth"
"github.com/refitor/selfAuth/web3"
"github.com/refitor/selfAuth/web3/contracts/demo"
"github.com/skip2/go-qrcode"
)
const (
c_host = ""
c_env_file = ".env"
c_email_host = ""
c_email_user = ""
c_email_pwd = ""
c_email_push = ""
c_address_wallet = ""
)
//go:embed web
var memfs embed.FS
type rsFS struct {
fs embed.FS
}
var (
v_selfauth_account = ""
v_selfauth_secret = ""
port = flag.String("port", "9999", "--port=9999")
bDeploy = flag.Bool("deploy", false, "--deploy=true")
v_event *web3.SelfAuthEvent
v_client *ethclient.Client
v_session *demo.SADemoSession
v_myenv = make(map[string]string, 0)
)
func main() {
flag.Parse()
runForInit()
runForAuth()
go runForWeb()
go runForMonitor()
for {
fmt.Printf(
"Pick an option:\n" + "" +
"1. Trigger authRequest.\n" +
"2. Check VerifyCount.\n" +
"3. Exit.\n",
)
// Reads a single UTF-8 character (rune)
// from STDIN and switches to case.
switch readStringStdin() {
case "1":
runForAuthRequest("trigger")
break
case "2":
runForAuthRequest("verified")
break
case "3":
fmt.Println("Bye!")
return
default:
fmt.Println("Invalid option. Please try again.")
break
}
}
}
func runForInit() {
loadEnv(c_env_file)
auth.InitEmail(c_email_host, c_email_user, c_email_pwd)
client, err := web3.Init(v_myenv["GATEWAY"], func(s string) string {
return v_myenv[s]
})
fatalCheck(err)
v_client = client
session, err := demo.SADemoInit(context.Background(), v_client, func(s string) string {
if s == "CONTRACTADDR" {
if *bDeploy {
return ""
}
return v_myenv["CONTRACTADDR"]
} else if *bDeploy && s == "operate" {
return "CONTRACTNEW"
}
return v_myenv[s]
})
fatalCheck(err)
v_session = session
if *bDeploy {
updateEnvFile("CONTRACTADDR", demo.SADemoAddress(), c_env_file)
fmt.Printf("contract address: %s\n", demo.SADemoAddress())
os.Exit(0)
}
}
func runForWeb() {
http.Handle("/", http.FileServer(&rsFS{memfs}))
http.HandleFunc("/api/auth/verify", webVerify)
log.Printf("selfAuth ListenAndServe at %s......\n", *port)
log.Println(http.ListenAndServe(":"+*port, nil))
}
func (p rsFS) Open(name string) (http.File, error) {
if name == "/" {
return http.FS(p.fs).Open("web")
}
if _, err := p.fs.Open("web/" + strings.TrimPrefix(name, "/")); err == nil {
return http.FS(p.fs).Open("web/" + strings.TrimPrefix(name, "/"))
}
return nil, errors.New("permission denied")
}
func webVerify(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
httpResponse(w, r, []byte(err.Error()), "text/plain")
return
}
code := r.PostForm.Get("code")
authID := r.PostForm.Get("authID")
rslog.Infof("receive selfAuth verify successed, authID: %s, code: %s", authID, code)
if authID == c_address_wallet {
if ok, err := auth.NewGoogleAuth().VerifyCode(v_selfauth_secret, code); err != nil {
httpResponse(w, r, []byte(err.Error()), "text/plain")
return
} else if !ok {
httpResponse(w, r, []byte("selfAuth verify failed"), "text/plain")
return
}
} else {
httpResponse(w, r, []byte("permission denied"), "text/plain")
return
}
if _, err := runForAuthResponse(); err != nil {
httpResponse(w, r, []byte(err.Error()), "text/plain")
return
} else {
httpResponse(w, r, []byte("verified successful"), "text/plain")
}
}
func runForAuth() {
frunes := []rune(c_address_wallet)
googleAuth := auth.NewGoogleAuth()
v_selfauth_secret = googleAuth.GetSecret()
v_selfauth_account = string(frunes[:4]) + string(frunes[len(frunes)-2:])
fmt.Println("\nPlease scan the QR code to add an account through google authenticator on your mobile phone:")
fmt.Println("")
renderQRCode(googleAuth.GetQrcode(v_selfauth_account, v_selfauth_secret))
}
func runForAuthRequest(operate string) {
if operate == "trigger" {
tx, err := v_session.Trigger(common.HexToAddress(c_address_wallet))
fatalCheck(err)
rslog.Infof("send trigger to contract successed: %s, TX: %s", c_address_wallet, tx.Hash().String())
} else if operate == "verified" {
verifyCount, err := v_session.Verified(common.HexToAddress(c_address_wallet))
fatalCheck(err)
rslog.Infof("send verifyCount to contract successed: %s, verified: %v", c_address_wallet, verifyCount.Int64())
}
}
func runForAuthResponse() (result string, err error) {
if respTx, respErr := web3.AuthResponse(v_client, v_myenv["CONTRACTADDR"], c_address_wallet, v_event.Params); respErr != nil {
rslog.Errorf("send response to contract failed, contract: %s, authID: %s, detail: %s", v_myenv["CONTRACTADDR"], c_address_wallet, respErr.Error())
err = respErr
} else {
rslog.Infof("send response to contract successed, contract: %s, authID: %s, respTx: %s", v_myenv["CONTRACTADDR"], c_address_wallet, respTx)
result = fmt.Sprintf("https://kovan.etherscan.io/tx/%s", respTx)
}
return
}
func runForMonitor() {
rslog.Infof("start monitor for demo %s", v_myenv["CONTRACTADDR"])
web3.WatchContract(v_client, v_myenv["CONTRACTADDR"], func(l types.Log) error {
v_event = web3.GetEvent(l)
contractAddr := l.Address.String()
authAddr := web3.GetEventAddress(v_event)
if contractAddr == v_myenv["CONTRACTADDR"] {
pushLink := fmt.Sprintf("%v?account=%s&random=%s", c_host, v_selfauth_account, authAddr)
_, err := auth.PushByEmail(c_email_push, "private authorization", "", fmt.Sprintf("[selfAuth] Please click the link and enter the dynamic google verification code, the contract chain operation can only be completed if the verification is successful: %s", pushLink), nil)
if err != nil {
rslog.Errorf(err.Error())
return err
}
rslog.Infof("push notify successed, url: %s", pushLink)
}
return nil
})
}
//// help functions
func fatalCheck(err error) {
if err != nil {
log.Fatalln(err.Error())
}
}
func readStringStdin() string {
reader := bufio.NewReader(os.Stdin)
inputVal, err := reader.ReadString('\n')
if err != nil {
log.Printf("invalid option: %v\n", err)
return ""
}
output := strings.TrimSuffix(inputVal, "\n") // Important!
return output
}
func renderQRCode(s string) error {
q, err := qrcode.New(s, qrcode.Medium)
if err != nil {
return err
}
fmt.Println(q.ToSmallString(false))
return nil
}
func httpResponse(w http.ResponseWriter, r *http.Request, data []byte, strType string) {
rslog.Infof("%s %s ===> Content-Type: %s, Content-Length: %d, Content: %s", r.Method, r.RequestURI, strType, len(data), string(data))
w.Header().Set("Content-Length", strconv.Itoa(len(data)))
w.Header().Set("Content-Type", strType)
w.WriteHeader(http.StatusOK)
w.Write(data)
}
//// Contract interaction functions
// loadEnv loads environment variables from location envLoc
// Call this at the top of every function that uses environment variables.
func loadEnv(envFile string) {
var err error
if v_myenv, err = godotenv.Read(envFile); err != nil {
log.Printf("could not load env from %s: %v", envFile, err)
}
}
// updateEnvFile saves the contract address to our .env file
func updateEnvFile(k, val, envFile string) {
v_myenv[k] = val
err := godotenv.Write(v_myenv, envFile)
if err != nil {
log.Printf("failed to update %s: %v\n", envFile, err)
}
}