Skip to content

Commit

Permalink
修复中间件鉴权影响读取 body
Browse files Browse the repository at this point in the history
  • Loading branch information
xmdhs committed Oct 14, 2023
1 parent a2b04cf commit a720a3d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
12 changes: 0 additions & 12 deletions handle/yggdrasil/texture.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,6 @@ func (y *Yggdrasil) getTokenbyAuthorization(ctx context.Context, w http.Response
return al[1]
}

func (y *Yggdrasil) validTextureType(ctx context.Context, w http.ResponseWriter, textureType string) bool {
switch textureType {
case "skin":
case "cape":
default:
y.logger.DebugContext(ctx, "错误的材质类型")
handleYgError(ctx, w, yggdrasil.Error{ErrorMessage: "错误的材质类型"}, 400)
return false
}
return true
}

func getUUIDbyParams(ctx context.Context, l *slog.Logger, w http.ResponseWriter) (string, string, bool) {
uuid := chi.URLParamFromCtx(ctx, "uuid")
textureType := chi.URLParamFromCtx(ctx, "textureType")
Expand Down
20 changes: 19 additions & 1 deletion handle/yggdrasil/yggdrasil.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package yggdrasil

import (
"bytes"
"context"
"encoding/json"
"errors"
Expand Down Expand Up @@ -95,14 +96,18 @@ const tokenKey = tokenValue("token")
func (y *Yggdrasil) Auth(handle http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
a, err := utils.DeCodeBody[yggdrasil.ValidateToken](r.Body, y.validate)
bw := bytes.NewBuffer(nil)
tr := io.TeeReader(r.Body, bw)
a, err := utils.DeCodeBody[yggdrasil.ValidateToken](tr, y.validate)
if err != nil || a.AccessToken == "" {
token := y.getTokenbyAuthorization(ctx, w, r)
if token == "" {
return
}
a.AccessToken = token
}
r.Body = readerClose{r: io.MultiReader(bw, r.Body), close: r.Body}

t, err := y.yggdrasilService.Auth(ctx, a)
if err != nil {
if errors.Is(err, utilsS.ErrTokenInvalid) {
Expand All @@ -117,3 +122,16 @@ func (y *Yggdrasil) Auth(handle http.Handler) http.Handler {
handle.ServeHTTP(w, r)
})
}

type readerClose struct {
r io.Reader
close io.Closer
}

func (r readerClose) Read(p []byte) (n int, err error) {
return r.r.Read(p)
}

func (r readerClose) Close() error {
return r.close.Close()
}

0 comments on commit a720a3d

Please sign in to comment.