-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
214 lines (176 loc) · 4.53 KB
/
context.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
package gcontext
import (
"context"
"fmt"
"net/http"
"github.com/yoas0bi/micro-toolkit/http/request"
"github.com/rs/xid"
"google.golang.org/grpc/metadata"
)
const (
// NamespaceHeader 空间
NamespaceHeader = "x-rpc-namespace"
// InternalCallTokenHeader todo
InternalCallTokenHeader = "internal-call-token"
// ClientIDHeader tood
ClientIDHeader = "client-id"
// ClientSecretHeader todo
ClientSecretHeader = "client-secret"
// OauthTokenHeader todo
OauthTokenHeader = "x-oauth-token"
// RealIPHeader todo
RealIPHeader = "x-real-ip"
// UserAgentHeader todo
UserAgentHeader = "user-agent"
// RequestID todo
RequestID = "x-request-id"
)
// NewGrpcInCtx todo
func NewGrpcInCtx() *GrpcInCtx {
return &GrpcInCtx{newGrpcCtx(metadata.Pairs())}
}
// GetGrpcInCtx todo
func GetGrpcInCtx(ctx context.Context) (*GrpcInCtx, error) {
// 获取认证信息
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, fmt.Errorf("ctx is not an grpc incoming context")
}
return &GrpcInCtx{newGrpcCtx(md)}, nil
}
// GrpcInCtx todo
type GrpcInCtx struct {
*grpcCtx
}
// Context todo
func (c *GrpcInCtx) Context() context.Context {
return metadata.NewIncomingContext(context.Background(), c.md)
}
// SetClientCredentials todo
func (c *GrpcInCtx) SetClientCredentials(clientID, clientSecret string) {
c.set(ClientIDHeader, clientID)
c.set(ClientSecretHeader, clientSecret)
}
// GetClientID todo
func (c *GrpcInCtx) GetClientID() string {
return c.get(ClientIDHeader)
}
// GetClientSecret todo
func (c *GrpcInCtx) GetClientSecret() string {
return c.get(ClientSecretHeader)
}
// GetNamespace todo
func (c *GrpcInCtx) GetNamespace() string {
return c.get(NamespaceHeader)
}
// SetAccessToKen 设置ID
func (c *GrpcInCtx) SetAccessToKen(token string) {
c.set(OauthTokenHeader, token)
}
// GetAccessToKen todo
func (c *GrpcInCtx) GetAccessToKen() string {
return c.get(OauthTokenHeader)
}
// GetRequestID 获取ID
func (c *GrpcInCtx) GetRequestID() string {
return c.get(RequestID)
}
// NewGrpcOutCtx todo
func NewGrpcOutCtx() *GrpcOutCtx {
return &GrpcOutCtx{newGrpcCtx(metadata.Pairs())}
}
// NewGrpcOutCtxFromIn todo
func NewGrpcOutCtxFromIn(in *GrpcInCtx) *GrpcOutCtx {
return &GrpcOutCtx{newGrpcCtx(in.md)}
}
// GrpcOutCtx todo
type GrpcOutCtx struct {
*grpcCtx
}
// Context todo
func (c *GrpcOutCtx) Context() context.Context {
return metadata.NewOutgoingContext(context.Background(), c.md)
}
// SetNamesapce todo
func (c *GrpcOutCtx) SetNamesapce(ns string) {
c.set(NamespaceHeader, ns)
}
func newGrpcCtx(md metadata.MD) *grpcCtx {
return &grpcCtx{md: md}
}
// GrpcCtx todo
type grpcCtx struct {
md metadata.MD
}
// Get todo
func (c *grpcCtx) get(key string) string {
return c.getWithIndex(key, 0)
}
// Get todo
func (c *grpcCtx) getWithIndex(key string, index int) string {
if val, ok := c.md[key]; ok {
if len(val) > index {
return val[index]
}
}
return ""
}
func (c *grpcCtx) set(key string, values ...string) {
c.md.Set(key, values...)
}
// SetAccessToken todo
func (c *grpcCtx) SetAccessToken(ak string) {
c.set(OauthTokenHeader, ak)
}
// SetRequestID 设置ID
func (c *grpcCtx) SetRequestID(requestID string) {
c.set(RequestID, requestID)
}
// SetRemoteIP todo
func (c *grpcCtx) SetRemoteIP(ip string) {
c.set(RealIPHeader, ip)
}
// SetUserAgent todo
func (c *grpcCtx) SetUserAgent(ua string) {
c.set(UserAgentHeader, ua)
}
// NewGrpcOutCtxFromHTTPRequest 从上下文中获取Token
func NewGrpcOutCtxFromHTTPRequest(r *http.Request) (*GrpcOutCtx, error) {
rc := NewGrpcOutCtx()
rc.SetAccessToken(GetTokenFromHeader(r))
rc.SetRemoteIP(request.GetRemoteIP(r))
rc.SetUserAgent(r.UserAgent())
rc.SetNamesapce(r.Header.Get(NamespaceHeader))
rc.SetRemoteIP(request.GetRemoteIP(r))
rid := r.Header.Get(RequestID)
if rid == "" {
rid = xid.New().String()
}
rc.SetRequestID(rid)
return rc, nil
}
// NewGrpcOutCtxFromHTTPRequest 从上下文中获取Token
func NewGrpcInCtxFromHTTPRequest(r *http.Request) (*GrpcInCtx, error) {
rc := NewGrpcInCtx()
rc.SetAccessToken(GetTokenFromHeader(r))
rc.SetRemoteIP(request.GetRemoteIP(r))
rc.SetUserAgent(r.UserAgent())
rid := r.Header.Get(RequestID)
if rid == "" {
rid = xid.New().String()
}
rc.SetRequestID(rid)
return rc, nil
}
func GetTokenFromHeader(r *http.Request) string {
// 优先从只定义header中读取
tk := r.Header.Get(OauthTokenHeader)
if tk != "" {
return tk
}
return r.Header.Get("Authorization")
}
func GetClientCredentialsFromHTTPRequest(r *http.Request) (cid, cs string) {
cid, cs = r.Header.Get(ClientIDHeader), r.Header.Get(ClientSecretHeader)
return
}