Skip to content

Commit

Permalink
Merge remote-tracking branch 'gitlab/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Dot-Liu committed Aug 3, 2022
2 parents eaa49d9 + cdd6fef commit aaa826f
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 19 deletions.
5 changes: 5 additions & 0 deletions drivers/auth/aksk/aksk.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ func (a *aksk) Auth(context http_service.IHttpContext) error {
if a.hideCredential {
context.Proxy().Header().DelHeader(auth.Authorization)
}

//将label set进context
for k, v := range user.Labels {
context.SetLabel(k, v)
}
return nil
}
}
Expand Down
5 changes: 5 additions & 0 deletions drivers/auth/apikey/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func (a *apikey) Auth(ctx http_service.IHttpContext) error {
for _, user := range a.users.users {
if authorization == user.Apikey {
if user.Expire == 0 || time.Now().Unix() < user.Expire {
//将label set进context
for k, v := range user.Labels {
ctx.SetLabel(k, v)
}

return nil
}
return auth.ErrorExpireUser
Expand Down
9 changes: 7 additions & 2 deletions drivers/auth/basic/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ type basicUsers struct {
users []User
}

func (b *basicUsers) check(username string, password string) error {
func (b *basicUsers) check(ctx http_service.IHttpContext, username string, password string) error {
for _, u := range b.users {
if u.Username == username && u.Password == password {
if u.Expire == 0 || time.Now().Unix() < u.Expire {
//将label set进context
for k, v := range u.Labels {
ctx.SetLabel(k, v)
}

return nil
}
return auth.ErrorExpireUser
Expand Down Expand Up @@ -91,7 +96,7 @@ func (b *basic) Auth(ctx http_service.IHttpContext) error {
if err != nil {
return err
}
return b.users.check(username, password)
return b.users.check(ctx, username, password)
}

//retrieveCredentials 获取basicAuth认证信息
Expand Down
9 changes: 7 additions & 2 deletions drivers/auth/jwt/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ func (j *jwt) doJWTAuthentication(context http_service.IHttpContext) error {

// 从配置中获取jwt凭证配置

jwtSecret, err := loadCredential(j.credentials, key, token.Header["alg"].(string))
jwtSecret, err := loadCredential(context, j.credentials, key, token.Header["alg"].(string))
if err != nil {
return errors.New("[jwt_auth] No credentials found for given " + keyClaimName)
}
Expand Down Expand Up @@ -597,11 +597,16 @@ func (j *jwt) doJWTAuthentication(context http_service.IHttpContext) error {
}

// 从配置中获取jwt凭证配置
func loadCredential(conf *jwtUsers, key, alg string) (JwtCredential, error) {
func loadCredential(context http_service.IHttpContext, conf *jwtUsers, key, alg string) (JwtCredential, error) {

for _, credential := range conf.credentials {
if credential.Iss == key {
if credential.Algorithm == alg {
//将label set进context
for k, v := range credential.Labels {
context.SetLabel(k, v)
}

return credential, nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/plugins/rate-limiting/rate-limiting.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (r *RateLimiting) DoFilter(ctx http_service.IHttpContext, next http_service
r.addRateHeader(ctx, rateSecondType)
r.addRateHeader(ctx, rateMinuteType)
r.addRateHeader(ctx, rateHourType)
r.addRateHeader(ctx, rateHourType)
r.addRateHeader(ctx, rateDayType)
}
return err
}
Expand Down
14 changes: 9 additions & 5 deletions http-entry/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ func (f Fields) Read(name string, ctx http_service.IHttpContext) (string, bool)
return r.Read("", ctx)
}
fs := strings.SplitN(name, "_", 2)
if len(fs) != 2 {
return "", false
if len(fs) == 2 {
r, has = f[fs[0]]
if has {
return r.Read(fs[1], ctx)
}
}
r, has = f[fs[0]]
if has {
return r.Read(fs[1], ctx)

if label := ctx.GetLabel(name); label != "" {
return label, true
}

return "", false
}

Expand Down
17 changes: 15 additions & 2 deletions node/http-context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,21 @@ type Context struct {
proxyRequests []http_service.IRequest
requestID string
response *Response
requestReader *RequestReader
ctx context.Context
labels map[string]string
}

func (ctx *Context) SetLabel(name string, value string) {
ctx.labels[name] = value
}

func (ctx *Context) GetLabel(name string) string {
return ctx.labels[name]
}

requestReader *RequestReader
ctx context.Context
func (ctx *Context) Labels() map[string]string {
return ctx.labels
}

func (ctx *Context) Proxies() []http_service.IRequest {
Expand Down Expand Up @@ -87,6 +99,7 @@ func NewContext(ctx *fasthttp.RequestCtx) *Context {
proxyRequest: NewProxyRequest(&ctx.Request, ctx.RemoteAddr().String()),
proxyRequests: make([]http_service.IRequest, 0, 5),
response: NewResponse(ctx),
labels: make(map[string]string),
}
//记录请求时间
newCtx.WithValue("request_time", ctx.Time())
Expand Down
17 changes: 10 additions & 7 deletions node/http-context/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,23 @@ func (r *Response) BodyLen() int {
func (r *Response) GetBody() []byte {
if strings.Contains(r.GetHeader("Content-Encoding"), "gzip") {
body, _ := r.BodyGunzip()
r.Headers().Del("Content-Encoding")
r.DelHeader("Content-Encoding")
r.SetHeader("Content-Length", strconv.Itoa(len(body)))
r.Response.SetBody(body)
}

return r.Response.Body()
}

func (r *Response) SetBody(bytes []byte) {
if strings.Contains(r.GetHeader("Content-Encoding"), "gzip") {
r.DelHeader("Content-Encoding")
}
r.Response.SetBody(bytes)
r.SetHeader("Content-Length", strconv.Itoa(len(bytes)))
r.responseError = nil
}

func (r *Response) StatusCode() int {
if r.responseError != nil {
return 504
Expand Down Expand Up @@ -82,9 +91,3 @@ func (r *Response) ProxyStatus() string {
func (r *Response) SetProxyStatus(code int, status string) {
r.proxyStatusCode = code
}

func (r *Response) SetBody(bytes []byte) {
r.Response.SetBody(bytes)
r.SetHeader("Content-Length", strconv.Itoa(len(bytes)))
r.responseError = nil
}

0 comments on commit aaa826f

Please sign in to comment.