-
Notifications
You must be signed in to change notification settings - Fork 15
/
jwt.go
48 lines (39 loc) · 1.13 KB
/
jwt.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
package middleware
import (
"context"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/ozline/tiktok/cmd/api/biz/pack"
"github.com/ozline/tiktok/pkg/constants"
"github.com/ozline/tiktok/pkg/errno"
"github.com/ozline/tiktok/pkg/utils"
)
func AuthToken() app.HandlerFunc {
return func(ctx context.Context, c *app.RequestContext) {
token, ok := c.GetQuery("token")
if !ok {
token, ok = c.GetPostForm("token")
}
if !ok {
hlog.CtxInfof(ctx, "token is not exist clientIP: %v\n", c.ClientIP())
pack.SendFailResponse(c, errno.AuthorizationFailedError)
c.Abort()
return
}
hlog.CtxInfof(ctx, "token: %v clientIP: %v\n", token, c.ClientIP())
claims, err := utils.CheckToken(token)
if err != nil {
hlog.CtxInfof(ctx, "token is invalid clientIP: %v\n", c.ClientIP())
pack.SendFailResponse(c, errno.AuthorizationFailedError)
c.Abort()
return
}
if claims.UserId < constants.StartID {
hlog.CtxInfof(ctx, "userid invaild")
pack.SendFailResponse(c, errno.AuthorizationFailedError)
c.Abort()
}
// c.Set("current_user_id", claims.UserId)
c.Next(ctx)
}
}