修复中间件鉴权影响读取 body

This commit is contained in:
xmdhs 2023-10-14 18:19:39 +08:00
parent a2b04cfcf5
commit a720a3d1fb
No known key found for this signature in database
GPG Key ID: E809D6D43DEFCC95
2 changed files with 19 additions and 13 deletions

View File

@ -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")

View File

@ -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()
}