-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.go
297 lines (261 loc) · 5.49 KB
/
sample.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package main
import (
"crypto/md5"
"database/sql"
"encoding/hex"
"fmt"
"strconv"
"time"
"github.com/cheetah-fun-gs/gouser"
_ "github.com/go-sql-driver/mysql"
redigo "github.com/gomodule/redigo/redis"
)
func defaultGenerateSign(accessKey string, data interface{}) string {
ts := data.(int64)
h := md5.New()
h.Write([]byte(accessKey))
h.Write([]byte(strconv.Itoa(int(ts))))
return hex.EncodeToString(h.Sum(nil))
}
const (
testAuthName = "testAuth"
)
type testAuth struct{}
func (auth *testAuth) GetName() string {
return testAuthName
}
func (auth *testAuth) Verify(v interface{}) (uid, extra string, err error) {
uid = v.(string) + "_testAuth"
return
}
func dial() (redigo.Conn, error) {
return redigo.DialTimeout("tcp", "127.0.0.1:6379", 2*time.Second, 2*time.Second, 2*time.Second)
}
func main() {
pool := &redigo.Pool{
Dial: dial,
}
defer pool.Close()
db, err := sql.Open("mysql", "admin:admin123@tcp(127.0.0.1:3306)/test?parseTime=true&charset=utf8mb4&loc=Asia%2FShanghai")
if err != nil {
panic(err)
}
defer db.Close()
name := "demo"
secret := "tZli3W^4Rb#V"
mgr := gouser.New(name, secret, pool, db, gouser.Config{IsEnableAccessKey: true})
// 设置认证
mgr.SetAuthMgr(&testAuth{})
if err := mgr.EnsureTables(); err != nil {
panic(err)
}
for _, tableName := range mgr.TableNames() {
if _, err = db.Exec(fmt.Sprintf("truncate table %s;", tableName)); err != nil {
panic(err)
}
}
redisConn := pool.Get()
redisConn.Do("flushdb")
redisConn.Close()
// 游客注册
user, err := mgr.RegisterTourist()
if err != nil {
panic(err)
}
token, _, err := user.Login()
if err != nil {
panic(err)
}
ok, err := mgr.VerifyToken(user.UID, token)
if err != nil {
panic(err)
}
if !ok {
panic("token Verify fail")
}
// 修改uid
testuid := "test_uid"
if err = user.UpdateUID(testuid); err != nil {
panic(err)
}
ok, user, err = mgr.FindUserByUID(testuid)
if err != nil {
panic(err)
}
if !ok {
panic("testuid not found")
}
// 修改email
time.Sleep(200 * time.Millisecond)
testemail := "test123@123.com"
emailcode, _, err := user.UpdateEmailApplyCode()
if err != nil {
panic(err)
}
if err = user.UpdateEmail(testemail, emailcode); err != nil {
panic(err)
}
ok, _, err = mgr.FindUserByEmail(testemail)
if err != nil {
panic(err)
}
if !ok {
panic("testemail not found")
}
// 修改mobile
time.Sleep(200 * time.Millisecond)
testmobile := "13000000000"
mobilecode, _, _, err := user.UpdateMobileApplyCode(testmobile)
if err != nil {
panic(err)
}
if err = user.UpdateMobile(testmobile, mobilecode); err != nil {
panic(err)
}
ok, _, err = mgr.FindUserByMobile(testmobile)
if err != nil {
panic(err)
}
if !ok {
panic("testemail not found")
}
// 游客登录
user, _, _, err = mgr.LoginTourist()
if err != nil {
panic(err)
}
// accesskey
accessKey, err := user.GenerateAccessKey("test")
if err != nil {
panic(err)
}
ts := time.Now().Unix()
sign := defaultGenerateSign(accessKey.AccessKey, ts)
ok, err = mgr.VerifySign(user.UID, accessKey.ID, ts, sign)
if err != nil {
panic(err)
}
if !ok {
panic("access key Verify error")
}
// 绑定第三方
if err = user.BindAuth(testAuthName, user.UID); err != nil {
panic(err)
}
// 第三方登录
authcode := "testabc"
if _, _, _, err = mgr.LoginAuth(testAuthName, authcode); err != nil {
panic(err)
}
ok, _, err = mgr.FindUserByAuth(testAuthName, authcode+"_testAuth")
if err != nil {
panic(err)
}
if !ok {
panic("auth user not found")
}
// lapd注册
lapdUID := "test_lapd"
lapdPass := "test_lapd"
user, err = mgr.RegisterLAPD(lapdUID, lapdPass)
if err != nil {
panic(err)
}
token, _, err = user.Login()
if err != nil {
panic(err)
}
ok, err = mgr.VerifyToken(user.UID, token)
if err != nil {
panic(err)
}
if !ok {
panic("token Verify fail")
}
ok, _, err = mgr.FindUserByAny(lapdUID)
if err != nil {
panic(err)
}
if !ok {
panic("lapdUID not found")
}
// 修改密码
time.Sleep(200 * time.Millisecond)
testpassword := "test_uid123"
passwardcode, _, err := user.UpdatePasswordApplyCode()
if err != nil {
panic(err)
}
if err = user.UpdatePasswordWithCode(testpassword, passwardcode); err != nil {
panic(err)
}
if _, _, _, err = mgr.LoginLAPD(testuid, testpassword); err != nil {
panic(err)
}
// 邮箱注册
email := "test_email@abc.com"
emailcode, _, err = mgr.RegisterEmailApplyCode(email)
if err != nil {
panic(err)
}
user, err = mgr.RegisterEmail(email, emailcode)
if err != nil {
panic(err)
}
token, _, err = user.Login()
if err != nil {
panic(err)
}
ok, err = mgr.VerifyToken(user.UID, token)
if err != nil {
panic(err)
}
if !ok {
panic("token Verify fail")
}
ok, _, err = mgr.FindUserByEmail(email)
if err != nil {
panic(err)
}
if !ok {
panic("email not found")
}
// 手机注册
mobile := "13000000001"
mobilecode, _, _, err = mgr.RegisterMobileApplyCode(mobile)
if err != nil {
panic(err)
}
user, err = mgr.RegisterMobile(mobile, mobilecode)
if err != nil {
panic(err)
}
token, _, err = user.Login()
if err != nil {
panic(err)
}
ok, err = mgr.VerifyToken(user.UID, token)
if err != nil {
panic(err)
}
if !ok {
panic("token Verify fail")
}
ok, _, err = mgr.FindUserByMobile(mobile)
if err != nil {
panic(err)
}
if !ok {
panic("mobile not found")
}
// 手机直接登录
mobile = "13000000002"
mobilecode, _, _, err = mgr.LoginMobileApplyCode(mobile)
if err != nil {
panic(err)
}
user, _, _, err = mgr.LoginMobile(mobile, mobilecode)
if err != nil {
panic(err)
}
}