From 1a45340882c8226d4792b89b94679e7fdd35e4c8 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Mon, 28 Aug 2023 11:28:09 +0800 Subject: [PATCH] fix --- utils/basic/request_parse.go | 34 +++++++++++++++++++------------ utils/basic/request_parse_test.go | 15 ++++++++++++++ 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/utils/basic/request_parse.go b/utils/basic/request_parse.go index 8b99eab7..a547a3eb 100644 --- a/utils/basic/request_parse.go +++ b/utils/basic/request_parse.go @@ -2,6 +2,7 @@ package basic import ( "errors" + "fmt" "log" "strconv" @@ -53,12 +54,12 @@ func NormalAfterLogic(w http.ResponseWriter, r *http.Request, resp *Response) { } } -func RequestParse(w http.ResponseWriter, r *http.Request, svcCtx any, LogicRequest any) (*auth.UserInfo, error) { +func ParseJwtToken(r *http.Request, svcCtx any) (*auth.UserInfo, error) { var userinfo *auth.UserInfo var err error // log.Println(io.ReadAll(r.Body)) token := r.Header.Get("Authorization") - userId, err := strconv.ParseInt(token, 10, 63) + userId, err := strconv.ParseInt(token, 10, 64) if err != nil { userinfo = &auth.UserInfo{ UserId: userId, @@ -97,11 +98,9 @@ func RequestParse(w http.ResponseWriter, r *http.Request, svcCtx any, LogicReque // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 if err != nil { log.Println(token) - httpx.OkJsonCtx(r.Context(), w, &Response{ - Code: 401, // 返回401状态码,表示未授权 - Message: "unauthorized", // 返回未授权信息 - }) - logx.Info("unauthorized:", err.Error()) // 记录错误日志 + if err != nil { + return nil, fmt.Errorf("unauthorized") + } return nil, err } @@ -110,12 +109,7 @@ func RequestParse(w http.ResponseWriter, r *http.Request, svcCtx any, LogicReque userinfo, err = auth.GetUserInfoFormMapClaims(claims) // 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息 if err != nil { - httpx.OkJsonCtx(r.Context(), w, &Response{ - Code: 401, - Message: "unauthorized", - }) - logx.Info("unauthorized:", err.Error()) - return nil, err + return nil, fmt.Errorf("unauthorized") } } } else { @@ -124,6 +118,20 @@ func RequestParse(w http.ResponseWriter, r *http.Request, svcCtx any, LogicReque } } + return userinfo, nil +} + +func RequestParse(w http.ResponseWriter, r *http.Request, svcCtx any, LogicRequest any) (*auth.UserInfo, error) { + + // 新的解析jwtToken + userinfo, err := ParseJwtToken(r, svcCtx) + if err != nil { + httpx.OkJsonCtx(r.Context(), w, &Response{ + Code: 510, + Message: err.Error(), + }) + } + // 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据 if err = httpx.Parse(r, LogicRequest); err != nil { httpx.OkJsonCtx(r.Context(), w, &Response{ diff --git a/utils/basic/request_parse_test.go b/utils/basic/request_parse_test.go index 85e7325f..6a0a6b48 100644 --- a/utils/basic/request_parse_test.go +++ b/utils/basic/request_parse_test.go @@ -1,6 +1,8 @@ package basic import ( + "crypto/sha256" + "encoding/base64" "fusenapi/utils/auth" "log" "testing" @@ -10,3 +12,16 @@ func TestRequestParse(t *testing.T) { a, us, err := auth.TParseJwtTokenHeader[auth.UserInfo]("saTGjruwq7SG4vnQVEo3vsZvbfhzx8zZ3zWA+8nWVdid5tssnYQNECiP+pYCK6YhZ+LRH8m7f7JXrgyqtpYQMOhVOcNWTYAClk0Jnft6+QIPegzY9+v4k7eVMiWf5c/x") log.Println(a, us, err) } + +func TestHash(t *testing.T) { + a := "fs12345678" + h := sha256.New() + h.Write([]byte(a)) + s := base64.RawURLEncoding.EncodeToString(h.Sum(nil)) + key := auth.StringToHash(s) + // log.Println(auth.GenerateJwtTokenUint64(auth.StringToHash(s), 400000000, time.Now().UTC().Unix(), 39, 0)) + authkey := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjIwOTMxOTIyMjYsImd1ZXN0X2lkIjowLCJpYXQiOjE2OTMxOTIyMjYsInVzZXJfaWQiOjM5fQ.DrvtD7gKB0gz1rAOAQHSnyBK3exTFqoLlacpZiadpB4" + log.Println(authkey) + log.Println(auth.TParseJwtTokenHeader[auth.UserInfo](authkey)) + log.Println(auth.ParseJwtTokenUint64Secret(authkey, key)) +}