-
Notifications
You must be signed in to change notification settings - Fork 3
/
headers.go
60 lines (52 loc) · 2.17 KB
/
headers.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
package twirp_hook
import (
"context"
"encoding/json"
"github.com/learninto/goutil/ctxkit"
"github.com/learninto/goutil/jwt"
"github.com/learninto/goutil/log"
"github.com/learninto/goutil/twirp"
)
// NewHeaders
func NewHeaders() *twirp.ServerHooks {
type user struct {
// Comment: 企业id
CompanyID int64 `json:"company_id"`
// Comment: 唯一标识
ID int64 `json:"id"`
DepartmentID int64 `json:"department_id"`
// Comment: 角色id数组 英文逗号隔开
PartIds string `json:"part_ids"`
// Comment: 部门id数组 英文逗号隔开
DepartmentIds string `json:"department_ids"`
// Comment: 用户昵称
NickName string `json:"nick_name"`
// Comment: 用户登录账号
UserName string `json:"user_name"`
}
return &twirp.ServerHooks{
RequestRouted: func(ctx context.Context) (context.Context, error) {
req, _ := twirp.HttpRequest(ctx)
u := user{}
if c, err := jwt.NewJWT().ParseToken(req.Header.Get("Sign")); err == nil {
_ = json.Unmarshal(c.Data, &u)
} else {
log.Get(ctx).Error("jwt解密 Error", err)
}
ctx = ctxkit.WithUserID(ctx, u.ID) // 注入用户id
ctx = ctxkit.WithUserName(ctx, u.UserName) // 注入用户登录账号
ctx = ctxkit.WithNickName(ctx, u.NickName) // 注入用户昵称
ctx = ctxkit.WithCompanyID(ctx, u.CompanyID) // 注入公司id
ctx = ctxkit.WithDepartmentID(ctx, u.DepartmentID) // 注入管辖部门id
ctx = ctxkit.WithPartIds(ctx, u.PartIds) // 注入角色id
ctx = ctxkit.WithDepartmentIds(ctx, u.DepartmentIds) // 注入部门id
ctx = ctxkit.WithSignKey(ctx, req.Header.Get("Sign")) // 注入签名
ctx = ctxkit.WithDevice(ctx, req.Header.Get("Device")) // 注入 用户设备 iso、android、web
ctx = ctxkit.WithMobiApp(ctx, req.Header.Get("MobiApp")) // 注入 APP 标识
ctx = ctxkit.WithVersion(ctx, req.Header.Get("Version")) // 注入 版本 标识
ctx = ctxkit.WithPlatform(ctx, req.Header.Get("Platform")) // 注入 平台 标识
ctx = ctxkit.WithUserIP(ctx, req.RemoteAddr) // TODO 注入 客户端IP 标识 目前貌似不准确待测试
return ctx, nil
},
}
}