第三方统一回调接口

This commit is contained in:
junleea 2025-04-28 14:32:51 +08:00
parent 46de2a899b
commit 84231a5fe8
4 changed files with 79 additions and 2 deletions

View File

@ -62,6 +62,7 @@ func SetUpToolGroup(router *gin.Engine) {
toolGroup.GET("/get_auth_url", GetThirdPartyAuthUrl) toolGroup.GET("/get_auth_url", GetThirdPartyAuthUrl)
toolGroup.GET("/github_callback", handleGithubCallback) toolGroup.GET("/github_callback", handleGithubCallback)
toolGroup.GET("/gitee_callback", handleGiteeCallback) toolGroup.GET("/gitee_callback", handleGiteeCallback)
toolGroup.GET("/third_party_callback", handleThirdPartyCallback) //统一处理第三方登录回调
toolGroup.POST("/loginRedirect", LoginRedirect) toolGroup.POST("/loginRedirect", LoginRedirect)
//发送邮件 //发送邮件
toolGroup.POST("/send_mail", SendMailTool) toolGroup.POST("/send_mail", SendMailTool)
@ -810,3 +811,27 @@ type GetThirdPartyAddAuthUrlReq struct {
HType string `json:"type" form:"type"` //操作类型add,login HType string `json:"type" form:"type"` //操作类型add,login
//Platform string `json:"platform" form:"platform"` //操作类型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)
}

View File

@ -9,7 +9,7 @@ import (
var Config ConfigStruct var Config ConfigStruct
var SigningKey = []byte{} 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 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} // 文件类型 var File_Type = map[string]int{"im": 1, "avatar": 2, "file": 3, "config": 4} // 文件类型
const ( const (

View File

@ -298,7 +298,36 @@ func DoGiteeCallBack(state *proto.ThirdPartyLoginState, code string) {
HandleThirdPartyLoginStatus(state, &thirdPartyLoginStatus, &userInfo) HandleThirdPartyLoginStatus(state, &thirdPartyLoginStatus, &userInfo)
//更新redis中的第三方登录状态 //更新redis中的第三方登录状态
thirdPartyLoginStatusStr, _ := json.Marshal(thirdPartyLoginStatus) 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) worker.SetRedisWithExpire(state.UUID, string(thirdPartyLoginStatusStr), time.Minute*10)
} }
@ -366,3 +395,18 @@ func HandleThirdPartyLoginStatus(state *proto.ThirdPartyLoginState, thirdPartyLo
thirdPartyLoginStatus.Status = proto.ParameterError //参数错误 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)
}
}

View File

@ -45,3 +45,11 @@ func GetGiteeUserInfo(accessToken string) (proto.GitHubUserInfo, error) {
return resp, nil return resp, nil
} }
func GetGogsAccessTokenByCode() {
}
func GetGogsUserInfo() {
}