middle.go
3.9 KB
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
package middleware
import (
"fmt"
"oppmg/common/log"
"oppmg/protocol"
serveauth "oppmg/services/auth"
"oppmg/storage/redisdata"
"github.com/astaxie/beego/plugins/cors"
"github.com/astaxie/beego/context"
)
//LogRequestData Before Router
var LogRequestData = func(ctx *context.Context) {
log.Info("====>Recv Request:%s", ctx.Input.URI())
if ctx.Input.RequestBody != nil {
log.Info("====>Recv data from client:\nHeadData: %s \nBodyData: %s", ctx.Request.Header, string(ctx.Input.RequestBody))
} else {
log.Info("====>Recv data from client:\nHeadData: %s ", ctx.Request.Header)
}
}
//CheckSign Before Router
// var CheckSign = func(ctx *context.Context) {
// var (
// headTimeStamp string
// headUuid string
// headAccessToken string
// headSign string
// signHex string
// )
// headTimeStamp = ctx.Input.Header(protocol.HeaderTimestamp)
// headUuid = ctx.Input.Header(protocol.HeaderUUID)
// headSign = ctx.Input.Header(protocol.HeaderSign)
// setsign := fmt.Sprintf("v!(MmM%v%v%vMmM)i^", headTimeStamp, headUuid, headAccessToken)
// sha256 := sha256.New()
// sha256.Write([]byte(setsign))
// signHex = hex.EncodeToString(sha256.Sum(nil))
// if strings.Compare(signHex, headSign) != 0 {
// msg := protocol.BadRequestParam("113")
// ctx.Output.JSON(msg, false, false)
// return
// }
// return
// }
//AuthToken Before Router
var AuthToken = func(ctx *context.Context) {
log.Debug("执行中间件AuthToken")
var (
storetoken protocol.LoginAuthToken
msg *protocol.ResponseMessage
err error
mtoken *serveauth.MyToken
)
accesstoken := ctx.Input.Header(protocol.HeaderAccessToken)
refreshToken := ctx.Input.Header(protocol.HeaderRefreshToken)
mtoken, err = serveauth.ValidJWTToken(accesstoken)
if err == nil {
storetoken, err = redisdata.GetLoginToken(mtoken.UID)
if err != nil {
log.Error("err:%s", err)
msg = protocol.NewMesage("10024")
ctx.Output.JSON(msg, false, false)
return
}
if storetoken.AccessToken != accesstoken {
msg = protocol.NewMesage("10025")
ctx.Output.JSON(msg, false, false)
return
}
ctx.Input.SetData(protocol.HeaderCompanyid, mtoken.CompanyID)
ctx.Input.SetData(protocol.HeaderUserid, mtoken.UID)
return
}
if ok := serveauth.IsJwtErrorExpired(err); ok {
//token过期
mtoken, err = serveauth.ValidJWTToken(refreshToken)
if err != nil {
msg = protocol.NewMesage("10024")
ctx.Output.JSON(msg, false, false)
return
}
storetoken, err = redisdata.GetLoginToken(mtoken.UID)
if err != nil {
log.Error("err:%s", err)
msg = protocol.NewMesage("10024")
ctx.Output.JSON(msg, false, false)
return
}
if storetoken.AccessToken != accesstoken {
msg = protocol.NewMesage("10025")
ctx.Output.JSON(msg, false, false)
return
}
logintoken, _ := serveauth.GenerateAuthToken(mtoken.UID, mtoken.CompanyID)
serveauth.ResetLoginTokenRedis(logintoken)
msg = protocol.NewReturnResponse(logintoken, nil)
ctx.Output.JSON(msg, false, false)
return
}
msg = protocol.NewMesage("10024")
ctx.Output.JSON(msg, false, false)
return
}
//AllowOption 允许跨域请求
var AllowOption = func(ctx *context.Context) {
if ctx.Request.Method != "OPTIONS" {
return
}
f := cors.Allow(&cors.Options{
AllowOrigins: []string{"*"}, //允许的请求来源
AllowMethods: []string{"POST", "GET", "OPTIONS", "PUT", "DELETE"}, //允许的请求类型
AllowHeaders: []string{"*"}, //允许的头部信息
ExposeHeaders: []string{"Content-Length"}, //允许暴露的头信息
AllowCredentials: false, //不允许共享AuthTuffic证书
})
f(ctx)
ctx.Output.Body([]byte("{}"))
return
}
var LogRouter = func(ctx *context.Context) {
// RouterPattern
p := ctx.Input.GetData("RouterPattern")
fmt.Println("====>r:", p)
return
}