-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
256 lines (239 loc) · 6.91 KB
/
handler.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package openid
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/go-chassis/foundation/httputil"
"github.com/go-chassis/go-chassis/v2/core"
"github.com/go-chassis/go-chassis/v2/core/common"
"github.com/go-chassis/go-chassis/v2/core/handler"
"github.com/go-chassis/go-chassis/v2/core/invocation"
"github.com/go-chassis/go-chassis/v2/core/status"
"github.com/go-chassis/go-chassis/v2/server/restful"
"github.com/go-chassis/openlog"
"github.com/ktcatv/openid/iam"
"github.com/ktcatv/openid/restfulext"
)
//errors
var (
ErrNoHeader = errors.New("no authorization in header")
ErrInvalidAuth = errors.New("invalid authentication")
)
//Handler is is a openid interceptor
type Handler struct {
}
//Handle intercept unauthorized request
// TODO: 似乎应重定向到登录界面
func (h *Handler) Handle(chain *handler.Chain, i *invocation.Invocation, cb invocation.ResponseCallBack) {
var err error
if auth == nil {
//openid is not initialized, then skip authentication, do not report error
chain.Next(i, cb)
return
}
//=====================================================================
req, route, err := restfulext.GetRoute(i)
if err != nil {
openlog.Error(fmt.Sprintf("take route of invocation failed, %s", err.Error()))
chain.Next(i, cb)
return
}
if route == nil {
// 可能出现这样情况 resfulext 版本发生重大变化, 不再匹配
openlog.Error("restful version of go-chassis has major changes")
handler.WriteBackErr(err, status.Status(i.Protocol, status.ServiceUnavailable), cb)
return
}
if !mustAuth(req) {
openlog.Error("skip auth", openlog.WithTags(openlog.Tags{"request": req}))
chain.Next(i, cb)
return
}
var ai = restfulext.GetAuthItem(route)
if ai == nil {
ai = &iam.AuthItem{
SRO: strings.Join([]string{i.MicroServiceName, i.SchemaID, i.OperationID}, ":"),
Description: route.Doc,
}
}
// i.SetHeader("X-SRO", ai.SRO)
if ignoreAuth, ok := route.Metadata["ignoreAuth"]; ok {
if ignoreAuth.(bool) {
chain.Next(i, cb)
return
}
}
//=====================================================================
var authorization, tokenType string
authorization = common.GetXCSEContext(restful.HeaderAuth, req)
tokenType = common.GetXCSEContext(ContextHeaderAuthorizationType, req)
if authorization == "" {
// 处理无授权直接请求(不通过 Edge-Service 代理)
handler.WriteBackErr(ErrNoHeader, status.Status(i.Protocol, status.Unauthorized), cb)
return
}
var tokenValue string
if tokenValue, err = handleBearer(authorization); err != nil {
handler.WriteBackErr(ErrInvalidAuth, status.Status(i.Protocol, status.Unauthorized), cb)
return
}
var idToken *JWTToken
if idToken, err = verifyToken(tokenType, tokenValue); err != nil {
handler.WriteBackErr(err, status.Status(i.Protocol, status.Unauthorized), cb)
return
}
// FIXME: Subject为登录用户标识,在账户-密码登录模式下与用户名相同
// TODO: 验证用户是否存在
if !isAuthorized(i, idToken) {
// return
// handler.WriteBackErr(ErrInvalidAuth, status.Status(i.Protocol, status.Unauthorized), cb)
}
i.SetHeader("x-kt-user-id", idToken.Claims.Subject)
chain.Next(i, cb)
}
func verifySubjectExist(subject string) bool {
// var err error
var req *http.Request = &http.Request{
Method: http.MethodPost,
URL: &url.URL{
Path: "/v1/token/query",
},
Header: http.Header{},
}
req.Header.Add("accept", "*/*")
req.Header.Add("accept-encoding", "gzip, deflate, br")
req.Header.Add("connection", "keep-alive")
// req.Header.Add("content-length", "0")
req.Proto = "HTTP/1.1"
req.ProtoMajor = 1
req.ProtoMinor = 1
buf, err := json.Marshal(map[string]string{
"subject": subject,
})
if err != nil {
return false
}
req.Body = ioutil.NopCloser(bytes.NewBuffer(buf))
reply := &http.Response{}
err = core.NewRPCInvoker().Invoke(
context.Background(),
"authentication-server",
"TokenEndpoint",
"VERIFY-SUBJECT-EXISTS-FUNCTION",
req,
reply,
core.WithProtocol("rest"),
)
if err != nil {
openlog.Error("do request failed.")
return false
}
defer reply.Body.Close()
replyByte := httputil.ReadBody(reply)
if replyByte == nil {
return false
}
// TODO:
return true
}
func isAuthorized(i *invocation.Invocation, idToken *JWTToken) bool {
// verifySubjectExist(idToken.Claims.Subject)
return verifySubjectExist(idToken.Claims.Subject)
}
func verifyToken(tokenType, tokenValue string) (*JWTToken, error) {
var err error
if tokenType == AuthorizationTypeAccessToken {
// 处理使用 ACCESS_TOKEN 直接请求或代理(含透传)访问, 查询 ID_TOKEN
var data []byte
if data, err = queryTokenByAccessToken(tokenValue); err != nil {
return nil, ErrInvalidAuth
}
var response *TokenResponse = &TokenResponse{}
if err = response.Decode(data); err != nil {
return nil, ErrInvalidAuth
}
return response.IDToken, nil
}
if tokenType == AuthorizationTypeIDToken {
// 处理使用 ID_TOKEN 直接请求或代理(含透传)访问, Token 存在签名, 需要验证
idToken := &JWTToken{}
idToken.Value = tokenValue
var claims map[string]interface{}
if claims, err = openidTokenManager.Verify(tokenValue, auth.SecretFunc); err != nil {
return nil, ErrInvalidAuth
}
buf, _ := json.Marshal(claims)
if err = json.Unmarshal(buf, &idToken.Claims); err != nil {
return nil, ErrInvalidAuth
}
return idToken, nil
}
// 未知授权类型
return nil, ErrInvalidAuth
}
func handleBearer(tokenValue string) (string, error) {
if tokens := strings.Split(tokenValue, " "); len(tokens) == 2 {
if tokens[0] == TokenTypeBearer {
return tokens[1], nil
} else {
return "", ErrInvalidAuth
}
} else {
// 被代理移除 Bearer
return tokenValue, nil
}
}
func queryTokenByAccessToken(accessToken string) ([]byte, error) {
var err error
var req *http.Request = &http.Request{
Method: http.MethodPost,
URL: &url.URL{
Path: "/v1/token/query",
RawQuery: "access_token=" + accessToken,
},
Header: http.Header{},
}
req.Header.Add("accept", "*/*")
req.Header.Add("accept-encoding", "gzip, deflate, br")
req.Header.Add("connection", "keep-alive")
req.Header.Add("content-length", "0")
req.Proto = "HTTP/1.1"
req.ProtoMajor = 1
req.ProtoMinor = 1
var reply *http.Response = &http.Response{}
err = core.NewRPCInvoker().Invoke(context.Background(),
"authentication-server", "TokenEndpoint", "queryToken",
req, reply, core.WithProtocol("rest"))
if err != nil {
openlog.Error("do request failed.")
return nil, err
}
defer reply.Body.Close()
message := httputil.ReadBody(reply)
return message, nil
}
// Name returns the router string
func (h *Handler) Name() string {
return "openid"
}
func newHandler() handler.Handler {
return &Handler{}
}
func mustAuth(req *http.Request) bool {
if auth.MustAuth == nil {
return true
}
return auth.MustAuth(req)
}
func init() {
err := handler.RegisterHandler("openid", newHandler)
if err != nil {
openlog.Error(err.Error())
}
}