From a720a3d1fbafdffeab461e277679285724730597 Mon Sep 17 00:00:00 2001 From: xmdhs Date: Sat, 14 Oct 2023 18:19:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=AD=E9=97=B4=E4=BB=B6?= =?UTF-8?q?=E9=89=B4=E6=9D=83=E5=BD=B1=E5=93=8D=E8=AF=BB=E5=8F=96=20body?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handle/yggdrasil/texture.go | 12 ------------ handle/yggdrasil/yggdrasil.go | 20 +++++++++++++++++++- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/handle/yggdrasil/texture.go b/handle/yggdrasil/texture.go index 2ca4069..a805ce4 100644 --- a/handle/yggdrasil/texture.go +++ b/handle/yggdrasil/texture.go @@ -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") diff --git a/handle/yggdrasil/yggdrasil.go b/handle/yggdrasil/yggdrasil.go index cbe9dce..6fa5f42 100644 --- a/handle/yggdrasil/yggdrasil.go +++ b/handle/yggdrasil/yggdrasil.go @@ -1,6 +1,7 @@ package yggdrasil import ( + "bytes" "context" "encoding/json" "errors" @@ -95,7 +96,9 @@ 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 == "" { @@ -103,6 +106,8 @@ func (y *Yggdrasil) Auth(handle http.Handler) http.Handler { } 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) { @@ -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() +}