-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
46 lines (42 loc) · 1.05 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
package middleware
import (
"github.com/gin-gonic/gin"
"league/common/context"
"league/common/errs"
"league/log"
"league/service"
)
// Auth 登录态校验
func Auth() gin.HandlerFunc {
return func(ctx *gin.Context) {
var userId string
var expiresAt int64
c := context.CustomContext{Context: ctx}
tokenString := ctx.GetHeader("X-Token")
authService := service.NewAuthService(ctx)
if len(tokenString) > 0 {
if token, err := authService.VerifyJwtString(tokenString); err == nil {
userId = token.UserId
expiresAt = token.ExpiresAt
}
}
ctx.Set("UserId", userId)
ctx.Set("ExpiresAt", expiresAt)
// 校验权限
path := ctx.Request.URL.Path
method := ctx.Request.Method
if isAllow := authService.IsAllow(userId, path, method); isAllow {
log.WithField(ctx, "Path", path, "Method", method).Debugf("Check permission passed")
ctx.Next()
} else {
if len(userId) > 0 {
// 有登录 无权限
c.CJSON(errs.ErrAuthUnauthorized)
} else {
// 未登录
c.CJSON(errs.ErrAuthNoLogin)
}
ctx.Abort()
}
}
}