From bfb9a157d5473521669b4854a9e24e1ae9d1543d Mon Sep 17 00:00:00 2001 From: xmdhs Date: Mon, 4 Sep 2023 22:33:02 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E5=8A=A8=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handle/yggdrasil/{authenticate.go => user.go} | 25 +++++++++++++ handle/yggdrasil/validate.go | 35 ------------------- 2 files changed, 25 insertions(+), 35 deletions(-) rename handle/yggdrasil/{authenticate.go => user.go} (57%) delete mode 100644 handle/yggdrasil/validate.go diff --git a/handle/yggdrasil/authenticate.go b/handle/yggdrasil/user.go similarity index 57% rename from handle/yggdrasil/authenticate.go rename to handle/yggdrasil/user.go index c52622b..6a4535e 100644 --- a/handle/yggdrasil/authenticate.go +++ b/handle/yggdrasil/user.go @@ -7,6 +7,7 @@ import ( "github.com/julienschmidt/httprouter" "github.com/xmdhs/authlib-skin/model/yggdrasil" + sutils "github.com/xmdhs/authlib-skin/service/utils" yggdrasilS "github.com/xmdhs/authlib-skin/service/yggdrasil" "github.com/xmdhs/authlib-skin/utils" ) @@ -35,3 +36,27 @@ func (y *Yggdrasil) Authenticate() httprouter.Handle { w.Write(b) } } + +func (y *Yggdrasil) Validate() httprouter.Handle { + return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + cxt := r.Context() + a, err := utils.DeCodeBody[yggdrasil.ValidateToken](r.Body, y.validate) + if err != nil { + y.logger.DebugContext(cxt, err.Error()) + handleYgError(cxt, w, yggdrasil.Error{ErrorMessage: err.Error()}, 400) + return + } + err = y.yggdrasilService.ValidateToken(cxt, a) + if err != nil { + if errors.Is(err, sutils.ErrTokenInvalid) { + y.logger.DebugContext(cxt, err.Error()) + handleYgError(cxt, w, yggdrasil.Error{ErrorMessage: "Invalid token.", Error: "ForbiddenOperationException"}, 403) + return + } + y.logger.WarnContext(cxt, err.Error()) + handleYgError(cxt, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500) + return + } + w.WriteHeader(204) + } +} diff --git a/handle/yggdrasil/validate.go b/handle/yggdrasil/validate.go deleted file mode 100644 index 69b6d32..0000000 --- a/handle/yggdrasil/validate.go +++ /dev/null @@ -1,35 +0,0 @@ -package yggdrasil - -import ( - "errors" - "net/http" - - "github.com/julienschmidt/httprouter" - "github.com/xmdhs/authlib-skin/model/yggdrasil" - sutils "github.com/xmdhs/authlib-skin/service/utils" - "github.com/xmdhs/authlib-skin/utils" -) - -func (y *Yggdrasil) Validate() httprouter.Handle { - return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { - cxt := r.Context() - a, err := utils.DeCodeBody[yggdrasil.ValidateToken](r.Body, y.validate) - if err != nil { - y.logger.DebugContext(cxt, err.Error()) - handleYgError(cxt, w, yggdrasil.Error{ErrorMessage: err.Error()}, 400) - return - } - err = y.yggdrasilService.ValidateToken(cxt, a) - if err != nil { - if errors.Is(err, sutils.ErrTokenInvalid) { - y.logger.DebugContext(cxt, err.Error()) - handleYgError(cxt, w, yggdrasil.Error{ErrorMessage: "Invalid token.", Error: "ForbiddenOperationException"}, 403) - return - } - y.logger.WarnContext(cxt, err.Error()) - handleYgError(cxt, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500) - return - } - w.WriteHeader(204) - } -}