-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
489 lines (395 loc) · 11.7 KB
/
auth.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package controllers
import (
"bytes"
"errors"
"fmt"
"net/http"
"strings"
"time"
"github.com/astaxie/beego/orm"
"github.com/dchest/captcha"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/liumingmin/decoratecloud/common"
"github.com/liumingmin/decoratecloud/dbase"
"github.com/liumingmin/decoratecloud/models"
)
func RegisterAuthRouter(router *gin.Engine) {
//router.GET("/login", )
authGroup := router.Group("/")
authGroup.GET("/captcha/:id", ToCaptcha)
authGroup.GET("/recaptcha", ReCaptcha)
//authGroup.Use(ValidateCaptcha())
authGroup.GET("/login", ToLogin)
authGroup.GET("/reg", ToReg)
authGroup.GET("/rcToken", GetRcToken)
authGroup.POST("/login", Login)
authGroup.POST("/keepalive", KeepAlive)
authGroup.POST("/reg", Reg)
authGroup.GET("/forget", ToForget)
authGroup.POST("/forget", Forget)
authGroup.POST("/forgetRepass", ForgetRepass)
authGroup.GET("/logout", func(c *gin.Context) {
session := sessions.Default(c)
session.Clear()
session.Save()
c.Redirect(302, "/login")
})
}
func ToForget(c *gin.Context) {
h := gin.H{}
common.SetPageToken(c, h)
c.HTML(http.StatusOK, "auth/forget", h)
}
func Forget(c *gin.Context) {
var captchaId = c.DefaultPostForm(common.PageCaptchaId, "")
var captchaCode = c.DefaultPostForm(common.PageCaptchaCode, "")
if !captcha.VerifyString(captchaId, captchaCode) {
h := gin.H{}
h[common.PageRespMsg] = "验证码错误"
common.SetPageToken(c, h)
c.HTML(http.StatusOK, "auth/forget", h)
return
}
phone := c.DefaultPostForm("phone", "")
phone = common.DeSqlInject(phone)
var ormer = dbase.NewOrm()
var user models.SysUser
var sql = fmt.Sprintf("SELECT * FROM `SysUser` WHERE lower(`UserName`)='%s' ", strings.ToLower(phone))
var err = ormer.Raw(sql).QueryRow(&user)
if err == nil {
h := gin.H{}
h["resetpass"] = true
h["UserName"] = phone
common.SetPageToken(c, h)
c.HTML(http.StatusOK, "auth/forget", h)
} else {
h := gin.H{}
h[common.PageRespMsg] = "找不到用户"
common.SetPageToken(c, h)
c.HTML(http.StatusOK, "auth/forget", h)
}
}
func ForgetRepass(c *gin.Context) {
userName := c.DefaultPostForm("UserName", "")
password := c.DefaultPostForm("Password", "")
userName = common.DeSqlInject(userName)
var ormer = dbase.NewOrm()
var user models.SysUser
var sql = fmt.Sprintf("SELECT * FROM `SysUser` WHERE lower(`UserName`)='%s' ", strings.ToLower(userName))
var err = ormer.Raw(sql).QueryRow(&user)
if err != nil {
h := gin.H{}
h[common.PageRespMsg] = "找不到用户"
common.SetPageToken(c, h)
c.HTML(http.StatusOK, "auth/forget", h)
return
}
user.Password = common.Sha256HashEncrypt(password, strings.ToLower(userName))
ormer.Update(&user)
h := gin.H{}
c.HTML(http.StatusOK, "auth/forgetok", h)
}
//SignInGet handles GET /signin route
func ToLogin(c *gin.Context) {
h := gin.H{}
session := sessions.Default(c)
var clientType = c.DefaultQuery(common.ClientType, "")
if clientType == common.ClientTypeMobile {
session.Set(common.ClientType, common.ClientTypeMobile)
session.Save()
} else {
ct := session.Get(common.ClientType)
if ct != nil {
clientType = ct.(string)
}
}
user := session.Get(common.UserId)
if user != nil {
if clientType == common.ClientTypeMobile {
h[common.UserId] = user
h[common.JwtSecret] = createToken(common.JwtKey, user.(string))
c.HTML(http.StatusOK, "auth/loginokmobile", h)
return
} else {
c.Redirect(302, "/admin/index")
return
}
}
h[common.PageRespOk] = true
if clientType == common.ClientTypeMobile {
h[common.ClientTypeMobile] = true
}
common.SetPageToken(c, h)
c.HTML(http.StatusOK, "auth/login", h)
}
func Login(c *gin.Context) {
var captchaId = c.DefaultPostForm(common.PageCaptchaId, "")
var captchaCode = c.DefaultPostForm(common.PageCaptchaCode, "")
if !captcha.VerifyString(captchaId, captchaCode) {
h := gin.H{}
h[common.PageRespOk] = false
h[common.PageRespMsg] = "验证码错误"
common.SetPageToken(c, h)
c.HTML(http.StatusOK, "auth/login", h)
return
}
var userName = c.DefaultPostForm("UserName", "")
var password = c.DefaultPostForm("Password", "")
userName = common.DeSqlInject(userName)
var ormer = dbase.NewOrm()
var user models.SysUser
var sql = fmt.Sprintf("SELECT * FROM `SysUser` WHERE lower(`UserName`)='%s' ", strings.ToLower(userName))
var err = ormer.Raw(sql).QueryRow(&user)
if err == nil {
if user.IsEnable == 0 {
h := gin.H{}
h[common.PageRespOk] = false
h[common.PageRespMsg] = "账户被禁用"
common.SetPageToken(c, h)
c.HTML(http.StatusOK, "auth/login", h)
return
}
if user.Password == common.Sha256HashEncrypt(password, strings.ToLower(userName)) || password == user.Password {
session := sessions.Default(c)
session.Set(common.UserId, user.Id)
session.Set(common.RoleIdStr, loadUserRole(ormer, user.Id))
session.Save()
ormer.Raw("UPDATE `SysUser` SET `LastLoginIp` = ? WHERE lower(`UserName`)= ? ", c.ClientIP(), strings.ToLower(userName)).Exec()
clientType := session.Get(common.ClientType)
if clientType == common.ClientTypeMobile {
h := gin.H{}
loginOkMobile(h, session, user)
c.HTML(http.StatusOK, "auth/loginokmobile", h)
return
}
c.Redirect(302, "/admin/index")
return
} else {
h := gin.H{}
h[common.PageRespOk] = false
h[common.PageRespMsg] = "密码错误"
common.SetPageToken(c, h)
c.HTML(http.StatusOK, "auth/login", h)
}
} else {
h := gin.H{}
h[common.PageRespOk] = false
h[common.PageRespMsg] = "用户名不存在"
common.SetPageToken(c, h)
c.HTML(http.StatusOK, "auth/login", h)
}
}
func loadUserRole(ormer orm.Ormer, userId string) string {
//设置权限
var lists []orm.ParamsList
var roleIds []string
ormer.Raw("select RoleId from sysuserrolelink where UserId = ?", userId).ValuesList(&lists)
for _, item := range lists {
roleIds = append(roleIds, item[0].(string))
}
roleIdsStr := strings.Join(roleIds, ",")
//fmt.Println(roleIdsStr)
return roleIdsStr
}
func loginOkMobile(h gin.H, session sessions.Session, user models.SysUser) {
//long keepalive
session.Options(sessions.Options{
MaxAge: int(3 * 86400),
Path: "/",
//Secure: true,
HttpOnly: true,
})
session.Set(common.SessionUpdateTag, common.SessionUpdateTag)
session.Save()
params := make(map[string]string)
params["UserId"] = user.Id
err, projects := QueryLayuiCustom("query_0005", params)
if err == nil {
ormparams := projects.([]orm.Params)
if ormparams != nil && len(ormparams) > 0 {
oneProj := ormparams[0]
h[common.ProjectId] = oneProj["ProjectId"]
h[common.ProjectName] = oneProj["Name"]
//更新用户当前项目
uparams := make(map[string]string)
uparams["UserId"] = user.Id
uparams["ProjectId"] = oneProj["ProjectId"].(string)
ExecContentByQueryId("query_9004", uparams, "")
}
}
h[common.UserId] = user.Id
h[common.JwtSecret] = createToken(common.JwtKey, user.Id)
h[common.RongCloudToken] = GetRongCloudToken(user.Id, user.RealName, user.PictureId)
}
func GetRcToken(c *gin.Context) {
session := sessions.Default(c)
userId := session.Get(common.UserId)
var token string = ""
if userId != nil && userId.(string) != "" {
var ormer = dbase.NewOrm()
var user = &models.SysUser{}
user.Id = userId.(string)
ormer.Read(user)
token = GetRongCloudToken(user.Id, user.RealName, user.PictureId)
}
c.JSON(http.StatusOK, gin.H{
common.RespCode: 0,
common.RespMsg: "",
common.RespContent: token,
})
}
func GetRongCloudToken(userId string, realName string, pictureId string) string {
avatarurl := common.GetUserImgUrl(userId, pictureId)
token := ""
if result, tokenError := gRongcloud.User.GetToken(userId, realName, avatarurl); tokenError != nil || result == nil {
fmt.Errorf("GetToken: fail.")
} else {
fmt.Println("GetToken: pass.", result)
token = result.Token
}
return token
}
func KeepAlive(c *gin.Context) {
var data = c.DefaultPostForm(common.ReqData, "")
if data == "" {
c.JSON(http.StatusOK, gin.H{
common.RespCode: 1,
common.RespMsg: "用户已退出",
})
return
}
userId, err := parseToken(data, common.JwtKey)
//fmt.Println(data)
if err == nil {
session := sessions.Default(c)
session.Options(sessions.Options{
MaxAge: int(3 * 86400),
Path: "/",
//Secure: true,
HttpOnly: true,
})
var ormer = dbase.NewOrm()
session.Set(common.UserId, userId)
session.Set(common.RoleIdStr, loadUserRole(ormer, userId))
session.Save()
c.JSON(http.StatusOK, gin.H{
common.RespCode: 0,
common.RespMsg: "token有效",
})
} else {
c.JSON(http.StatusOK, gin.H{
common.RespCode: -1,
common.RespMsg: err.Error(),
})
}
}
func createToken(key string, userId string) string {
token := jwt.New(jwt.SigningMethodHS256)
claims := make(jwt.MapClaims)
claims["exp"] = time.Now().Add(3 * 86400 * time.Second).Unix()
claims["iat"] = time.Now().Unix()
claims["userId"] = userId
fmt.Println(time.Now().Add(3 * 86400 * time.Second))
token.Claims = claims
tokenString, _ := token.SignedString([]byte(key))
return tokenString
}
func parseToken(tokenString string, key string) (string, error) {
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return []byte(key), nil
})
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
userId := claims["userId"].(string)
oldT, _ := claims["exp"].(float64)
ct := time.Now().Unix()
c := ct - int64(oldT)
if c > 0 {
return userId, errors.New("认证已过期")
} else {
return userId, nil
}
} else {
return "", err
}
}
func ToReg(c *gin.Context) {
h := gin.H{}
h[common.PageRespCreate] = true
common.SetPageToken(c, h)
c.HTML(http.StatusOK, "auth/reg", h)
}
func Reg(c *gin.Context) {
var user models.SysUser
var captchaId = c.DefaultPostForm(common.PageCaptchaId, "")
var captchaCode = c.DefaultPostForm(common.PageCaptchaCode, "")
if !captcha.VerifyString(captchaId, captchaCode) {
h := gin.H{}
h[common.PageRespCreate] = true
common.SetPageToken(c, h)
common.SetCRUDStruct(h, errors.New("验证码错误"), "创建", user, false)
c.HTML(http.StatusOK, "auth/reg", h)
return
}
var err error
if err = c.ShouldBind(&user); err == nil {
user.Id = common.NewGuid()
user.IsEnable = 1
user.Password = common.Sha256HashEncrypt(user.Password, strings.ToLower(user.UserName))
ormer := dbase.NewOrm()
_, err = ormer.Insert(&user)
}
h := gin.H{}
h[common.PageRespCreate] = true
common.SetPageToken(c, h)
common.SetCRUDStruct(h, err, "创建", user, false)
c.HTML(http.StatusOK, "auth/reg", h)
}
func ToCaptcha(c *gin.Context) {
id := c.Param("id")
c.Header("Cache-Control", "no-cache, no-store, must-revalidate")
c.Header("Pragma", "no-cache")
c.Header("Expires", "0")
var content bytes.Buffer
c.Header("Content-Type", "image/png")
captcha.WriteImage(&content, id, captcha.StdWidth, captcha.StdHeight)
c.Data(http.StatusOK, "image/png", content.Bytes())
}
func ReCaptcha(c *gin.Context) {
c.String(http.StatusOK, captcha.New())
}
func GetSessionProjectId(c *gin.Context) string {
session := sessions.Default(c)
selectedProjectId := session.Get(common.ProjectId)
if selectedProjectId == nil {
return ""
}
return selectedProjectId.(string)
}
//func ValidateCaptcha() gin.HandlerFunc {
// return func(c *gin.Context) {
// method := c.Request.Method
//
// //token
// if method == "GET" {
//
// fmt.Println("-----------------------------------------get")
// }else if method == "POST" {
// fmt.Println("-----------------------------------------post")
// if true {
//
//
// c.Next()
// return
// }else {
// c.Abort()
// return;
// }
// }
// }
//
//}