修复中间件鉴权影响读取 body
This commit is contained in:
parent
a2b04cfcf5
commit
a720a3d1fb
@ -30,18 +30,6 @@ func (y *Yggdrasil) getTokenbyAuthorization(ctx context.Context, w http.Response
|
|||||||
return al[1]
|
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) {
|
func getUUIDbyParams(ctx context.Context, l *slog.Logger, w http.ResponseWriter) (string, string, bool) {
|
||||||
uuid := chi.URLParamFromCtx(ctx, "uuid")
|
uuid := chi.URLParamFromCtx(ctx, "uuid")
|
||||||
textureType := chi.URLParamFromCtx(ctx, "textureType")
|
textureType := chi.URLParamFromCtx(ctx, "textureType")
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package yggdrasil
|
package yggdrasil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
@ -95,7 +96,9 @@ const tokenKey = tokenValue("token")
|
|||||||
func (y *Yggdrasil) Auth(handle http.Handler) http.Handler {
|
func (y *Yggdrasil) Auth(handle http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := r.Context()
|
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 == "" {
|
if err != nil || a.AccessToken == "" {
|
||||||
token := y.getTokenbyAuthorization(ctx, w, r)
|
token := y.getTokenbyAuthorization(ctx, w, r)
|
||||||
if token == "" {
|
if token == "" {
|
||||||
@ -103,6 +106,8 @@ func (y *Yggdrasil) Auth(handle http.Handler) http.Handler {
|
|||||||
}
|
}
|
||||||
a.AccessToken = token
|
a.AccessToken = token
|
||||||
}
|
}
|
||||||
|
r.Body = readerClose{r: io.MultiReader(bw, r.Body), close: r.Body}
|
||||||
|
|
||||||
t, err := y.yggdrasilService.Auth(ctx, a)
|
t, err := y.yggdrasilService.Auth(ctx, a)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, utilsS.ErrTokenInvalid) {
|
if errors.Is(err, utilsS.ErrTokenInvalid) {
|
||||||
@ -117,3 +122,16 @@ func (y *Yggdrasil) Auth(handle http.Handler) http.Handler {
|
|||||||
handle.ServeHTTP(w, r)
|
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()
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user