From 07a6a55bdec1a8987185c850ac35dadb8a571ab0 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Sat, 17 May 2025 11:27:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=AC=AC=E4=B8=89=E6=96=B9?= =?UTF-8?q?=E7=99=BB=E5=BD=95state=E4=B8=8D=E5=B0=86=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E5=AD=98=E5=88=B0url,=E4=BD=BF=E7=94=A8redis=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/tool.go | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/handler/tool.go b/handler/tool.go index 2797bd1..1d9ff66 100644 --- a/handler/tool.go +++ b/handler/tool.go @@ -10,6 +10,7 @@ import ( "encoding/json" "fmt" "github.com/gin-gonic/gin" + "github.com/google/uuid" "io" "log" "net/http" @@ -740,17 +741,17 @@ func LoginRedirect(c *gin.Context) { func GetThirdPartyAuthUrl(c *gin.Context) { platform := c.Query("platform") - uuid := c.Query("uuid") + uuid_ := c.Query("uuid") hType := c.Query("type") //操作类型add,login var resp proto.GenerateResp - if platform == "" || uuid == "" || hType == "" { + if platform == "" || uuid_ == "" || hType == "" { resp.Code = proto.ParameterError resp.Message = "platform or uuid is empty" c.JSON(http.StatusOK, resp) return } var state proto.ThirdPartyLoginState - state.UUID = uuid + state.UUID = uuid_ state.Type = hType state.Platform = platform state.Project = "SAW" @@ -774,14 +775,18 @@ func GetThirdPartyAuthUrl(c *gin.Context) { return } //需要将uuid绑定在该用户上 - worker.SetRedisWithExpire("user_add_platform_"+uuid, strconv.Itoa(userID), time.Minute*9) + worker.SetRedisWithExpire("user_add_platform_"+uuid_, strconv.Itoa(userID), time.Minute*9) state.UserID = userID } stateStr, _ := json.Marshal(state) + stateID := uuid.NewString() + worker.SetRedisWithExpire("state_id_"+stateID, string(stateStr), time.Minute*9) + var respUrl string //base64编码 stateBase64Str := base64.StdEncoding.EncodeToString(stateStr) + stateBase64Str = stateID switch platform { case "qq": params := url.Values{} @@ -794,7 +799,7 @@ func GetThirdPartyAuthUrl(c *gin.Context) { case "github": params := url.Values{} params.Add("client_id", proto.Config.GITHUB_CLIENT_ID) - params.Add("login", uuid) + params.Add("login", uuid_) params.Add("state", stateBase64Str) baseUri := proto.GitHuAuthorizeBaseUrl respUrl = fmt.Sprintf("%s?%s", baseUri, params.Encode()) @@ -851,23 +856,26 @@ type GetThirdPartyAddAuthUrlReq struct { func handleThirdPartyCallback(c *gin.Context) { var resp proto.GenerateResp - code := c.Query("code") //code - stateBase64Str := c.Query("state") //state + code := c.Query("code") //code + stateID := c.Query("state") //state + //解析base64 - decodedBytes, err := base64.StdEncoding.DecodeString(stateBase64Str) - if err != nil { - fmt.Println("Decoding error:", err) + //decodedBytes, err := base64.StdEncoding.DecodeString(stateBase64Str) + // + stateStr := worker.GetRedis("state_id_" + stateID) + if stateStr == "" { + log.Println("state is empty,stateID=", stateID) } else { - decodedStr := string(decodedBytes) //json解析 var state proto.ThirdPartyLoginState - err = json.Unmarshal([]byte(decodedStr), &state) - log.Println("handle callback state:", decodedStr, "\tcode:", code) + err := json.Unmarshal([]byte(stateStr), &state) + log.Println("handle callback state:", stateStr, "\tcode:", code) if err != nil { log.Println("json unmarshal error:", err) } else { service.DoThirdPartyCallBack(&state, code) } + worker.DelRedis("state_id_" + stateID) //删除state } resp.Code = 0 resp.Message = "success"