diff --git a/handler/tool.go b/handler/tool.go index b310dd1..f4945ab 100644 --- a/handler/tool.go +++ b/handler/tool.go @@ -62,6 +62,7 @@ func SetUpToolGroup(router *gin.Engine) { toolGroup.GET("/get_auth_url", GetThirdPartyAuthUrl) toolGroup.GET("/github_callback", handleGithubCallback) toolGroup.GET("/gitee_callback", handleGiteeCallback) + toolGroup.GET("/third_party_callback", handleThirdPartyCallback) //统一处理第三方登录回调 toolGroup.POST("/loginRedirect", LoginRedirect) //发送邮件 toolGroup.POST("/send_mail", SendMailTool) @@ -810,3 +811,27 @@ type GetThirdPartyAddAuthUrlReq struct { HType string `json:"type" form:"type"` //操作类型add,login //Platform string `json:"platform" form:"platform"` //操作类型add,login } + +func handleThirdPartyCallback(c *gin.Context) { + var resp proto.GenerateResp + code := c.Query("code") //code + stateBase64Str := c.Query("state") //state + //解析base64 + decodedBytes, err := base64.StdEncoding.DecodeString(stateBase64Str) + if err != nil { + fmt.Println("Decoding error:", err) + } else { + decodedStr := string(decodedBytes) + //json解析 + var state proto.ThirdPartyLoginState + err = json.Unmarshal([]byte(decodedStr), &state) + log.Println("handle github callback state:", decodedStr, "\tcode:", code) + if err != nil { + log.Println("json unmarshal error:", err) + } + service.DoThirdPartyCallBack(&state, code) + } + resp.Code = 0 + resp.Message = "success" + c.JSON(http.StatusOK, resp) +} diff --git a/proto/conf.go b/proto/conf.go index 46fd9cf..1b8f594 100644 --- a/proto/conf.go +++ b/proto/conf.go @@ -9,7 +9,7 @@ import ( var Config ConfigStruct var SigningKey = []byte{} -var Url_map = map[string]bool{"/login": true, "/register": true, "/uuid": true, "/gqr": true, "/cid/callback": true, "/tool/monitor": true, "/user/sync": true, "/tool/file/": true, "/user/reset": true, "/tool/qq_auth": true, "/tool/qq_callback": true, "/tool/github_auth": true, "/tool/github_callback": true, "/user/oAuth": true, "/user/oAuth_uuid": true, "/tool/loginRedirect": true, "/tool/get_auth_url": true, "/tool/gitee_callback": true} // 不需要token验证的url +var Url_map = map[string]bool{"/login": true, "/register": true, "/uuid": true, "/gqr": true, "/cid/callback": true, "/tool/monitor": true, "/user/sync": true, "/tool/file/": true, "/user/reset": true, "/tool/qq_auth": true, "/tool/qq_callback": true, "/tool/github_auth": true, "/tool/github_callback": true, "/user/oAuth": true, "/user/oAuth_uuid": true, "/tool/loginRedirect": true, "/tool/get_auth_url": true, "/tool/gitee_callback": true, "/tool//third_party_callback": true} // 不需要token验证的url var Per_menu_map = map[string]int{"/video/": 1, "/device/": 2, "/cid/": 3} var File_Type = map[string]int{"im": 1, "avatar": 2, "file": 3, "config": 4} // 文件类型 const ( diff --git a/service/toolService.go b/service/toolService.go index 8c623f4..420fb09 100644 --- a/service/toolService.go +++ b/service/toolService.go @@ -298,7 +298,36 @@ func DoGiteeCallBack(state *proto.ThirdPartyLoginState, code string) { HandleThirdPartyLoginStatus(state, &thirdPartyLoginStatus, &userInfo) //更新redis中的第三方登录状态 thirdPartyLoginStatusStr, _ := json.Marshal(thirdPartyLoginStatus) - log.Println("do handle github callback success, third party login status:", string(thirdPartyLoginStatusStr)) + log.Println("do handle gitee callback success, third party login status:", string(thirdPartyLoginStatusStr)) + worker.SetRedisWithExpire(state.UUID, string(thirdPartyLoginStatusStr), time.Minute*10) +} + +func DoGogsCallBack(state *proto.ThirdPartyLoginState, code string) { + //获取Access Token + resp, err := worker.GetGiteeAccessTokenByCode(code, "https://pm.ljsea.top/tool/gitee_callback", proto.Config.GITEE_CLIENT_ID, proto.Config.GITEE_CLIENT_SECRET) + if err != nil { + log.Println("get gitee access token error:", err) + return + } + if resp.AccessToken == "" { + log.Println("get gitee access token is empty") + log.Println("get gitee access token error:", resp) + return + } + log.Println("get gitee access token:", resp.AccessToken) + //获取用户信息 + userInfo, err := worker.GetGiteeUserInfo(resp.AccessToken) + if err != nil { + log.Println("get gitee user info error:", err) + return + } + log.Println("get gitee user info:", userInfo) + var thirdPartyLoginStatus proto.ThirdPartyLoginStatus + thirdPartyLoginStatus.Type = state.Platform + HandleThirdPartyLoginStatus(state, &thirdPartyLoginStatus, &userInfo) + //更新redis中的第三方登录状态 + thirdPartyLoginStatusStr, _ := json.Marshal(thirdPartyLoginStatus) + log.Println("do handle gitee callback success, third party login status:", string(thirdPartyLoginStatusStr)) worker.SetRedisWithExpire(state.UUID, string(thirdPartyLoginStatusStr), time.Minute*10) } @@ -366,3 +395,18 @@ func HandleThirdPartyLoginStatus(state *proto.ThirdPartyLoginState, thirdPartyLo thirdPartyLoginStatus.Status = proto.ParameterError //参数错误 } } + +func DoThirdPartyCallBack(state *proto.ThirdPartyLoginState, code string) { + switch state.Platform { + case "github": + DoGithubCallBack(state, code) + case "gitee": + DoGiteeCallBack(state, code) + case "qq": + // TODO + case "gogs": + // TODO + default: + log.Println("DoThirdPartyCallBack platform error:", state.Platform) + } +} diff --git a/worker/gitee.go b/worker/gitee.go index b4f81c8..e2c6750 100644 --- a/worker/gitee.go +++ b/worker/gitee.go @@ -45,3 +45,11 @@ func GetGiteeUserInfo(accessToken string) (proto.GitHubUserInfo, error) { return resp, nil } + +func GetGogsAccessTokenByCode() { + +} + +func GetGogsUserInfo() { + +}