-
Notifications
You must be signed in to change notification settings - Fork 2
/
QRAuthCode.go
168 lines (158 loc) · 3.83 KB
/
QRAuthCode.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
package main
import (
"crypto/rand"
"encoding/json"
"net/http"
"time"
"github.com/rs/zerolog/log"
)
type fetchQRRes struct {
Status string `json:"status"`
Code string `json:"code"`
}
// 由蜃境服务器拉取小程序码或者授权状态
func (tool *MirageTool) fetchQR(
w http.ResponseWriter,
r *http.Request,
) {
reqData := make(map[string]string)
json.NewDecoder(r.Body).Decode(&reqData)
if user, ok := tool.QRCache.Get(reqData["state"]); ok {
if user != nil { // 该state码已被扫码授权过
authCode := tool.GenAuthCode()
tool.AuthCodeCache.Set(authCode, user, 2*time.Minute)
res := fetchQRRes{
Status: "OK",
Code: authCode,
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(&res)
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}
return
}
// 该state码已拉取过二维码但未被扫码授权过
res := fetchQRRes{
Status: "Wait",
Code: "",
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(&res)
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}
return
}
// 该state码还未获取过二维码
miniQR := tool.genMiniProgramQR(reqData["state"])
if miniQR != "" {
// 需注意,cache里以state为索引记录的是user信息,而不进行二维码缓存
tool.QRCache.Set(reqData["state"], nil, 2*time.Minute)
res := fetchQRRes{
Status: "New",
Code: miniQR,
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(&res)
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}
return
}
res := fetchQRRes{
Status: "Error",
Code: "",
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(&res)
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}
return
}
type authQRRes struct {
Status string
}
// 小程序上用户完成授权确认
func (tool *MirageTool) authQR(
w http.ResponseWriter,
r *http.Request,
) {
reqData := make(map[string]string)
json.NewDecoder(r.Body).Decode(&reqData)
openID := tool.exchangeCodeToIDs(reqData["logincode"])
user := tool.GetUserByID(openID)
if user == nil {
res := authQRRes{
Status: "NoUser",
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(&res)
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}
return
}
stateCode := reqData["state"]
userInterface, expiration, ok := tool.QRCache.GetWithExpiration(stateCode)
if !ok || userInterface != nil {
res := authQRRes{
Status: "StateExpire",
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(&res)
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}
return
}
tool.QRCache.Set(stateCode, *user, expiration.Sub(time.Now()))
res := authQRRes{
Status: "OK",
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(&res)
if err != nil {
log.Error().
Caller().
Err(err).
Msg("Failed to write response")
}
return
}
func (t *MirageTool) GenAuthCode() string {
const letterBytes = "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
b := make([]byte, 64)
index := make([]byte, 64)
rand.Read(index)
for i := 0; i < 64; i++ {
b[i] = letterBytes[index[i]&63]
}
return string(b)
}