From c8521e4931e76cbd5528e69f680bd8851b76a1b5 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Wed, 30 Apr 2025 16:10:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0google=E7=AC=AC=E4=B8=89?= =?UTF-8?q?=E6=96=B9=E7=99=BB=E5=BD=95,=E4=BF=AE=E6=94=B9=E4=B8=AD?= =?UTF-8?q?=E7=BB=A7=E8=AF=B7=E6=B1=82post=E6=A8=A1=E5=BC=8F=E9=80=89?= =?UTF-8?q?=E6=8B=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/tool.go | 6 +++--- proto/tool.go | 16 ++++++++++++---- service/toolService.go | 27 ++++++++++++++++++--------- worker/thirdParty.go | 29 ++++++++++++++++++++++------- 4 files changed, 55 insertions(+), 23 deletions(-) diff --git a/handler/tool.go b/handler/tool.go index 7d7b6e5..a4a4c59 100644 --- a/handler/tool.go +++ b/handler/tool.go @@ -811,7 +811,7 @@ func GetThirdPartyAuthUrl(c *gin.Context) { params.Add("client_id", worker.GoogleClientID) params.Add("response_type", "token") //直接返回token params.Add("redirect_uri", "https://pm.ljsea.top/tool/third_party_callback") - params.Add("scope", "https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/userinfo.profile+openid") + params.Add("scope", "https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/userinfo.profile") params.Add("state", stateBase64Str) baseUri := proto.GoogleAuthorizeBaseUrl respUrl = fmt.Sprintf("%s?%s", baseUri, params.Encode()) @@ -842,11 +842,11 @@ func handleThirdPartyCallback(c *gin.Context) { //json解析 var state proto.ThirdPartyLoginState err = json.Unmarshal([]byte(decodedStr), &state) - log.Println("handle github callback state:", decodedStr, "\tcode:", code) + log.Println("handle callback state:", decodedStr, "\tcode:", code) if err != nil { log.Println("json unmarshal error:", err) } else { - service.DoThirdPartyCallBack(c, &state, code) + service.DoThirdPartyCallBack(&state, code) } } resp.Code = 0 diff --git a/proto/tool.go b/proto/tool.go index b6c6b1b..4788962 100644 --- a/proto/tool.go +++ b/proto/tool.go @@ -102,12 +102,19 @@ type GiteeOAuthTokenResponse struct { } type GoogleOAuthResponse struct { + AccessToken string `json:"access_token"` + ExpiresIn int `json:"expires_in"` + Scope string `json:"scope"` + TokenType string `json:"token_type"` + IDToken string `json:"id_token"` // id_token } type GoogleOAuthRequest struct { ClientID string `json:"client_id"` ClientSecret string `json:"client_secret"` Code string `json:"code"` + RedirectURI string `json:"redirect_uri"` + GrantType string `json:"grant_type"` // authorization_code } type OAuthGetTokenRequest struct { @@ -162,10 +169,11 @@ type GoogleUserInfoResp struct { // 国外服务器负责请求的请求 type OnlineServerReq struct { - Type string `json:"type" form:"type"` // 请求类型,get,post - Url string `json:"url" form:"url"` // 请求地址 - Data any `json:"data" form:"data"` // 请求数据 - Header []OutlineServerReqData `json:"header" form:"header"` // 请求头 + Type string `json:"type" form:"type"` // 请求类型,get,post + PostType string `json:"post_type" form:"post_type"` // post请求类型,form,json + Url string `json:"url" form:"url"` // 请求地址 + Data any `json:"data" form:"data"` // 请求数据 + Header []OutlineServerReqData `json:"header" form:"header"` // 请求头 } type OutlineServerReqData struct { diff --git a/service/toolService.go b/service/toolService.go index b75dd24..dd247aa 100644 --- a/service/toolService.go +++ b/service/toolService.go @@ -7,7 +7,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/gin-gonic/gin" "github.com/golang-jwt/jwt" "log" "regexp" @@ -473,7 +472,7 @@ func HandleThirdPartyLoginStatusV2(state *proto.ThirdPartyLoginState, thirdParty } } -func DoThirdPartyCallBack(c *gin.Context, state *proto.ThirdPartyLoginState, code string) { +func DoThirdPartyCallBack(state *proto.ThirdPartyLoginState, code string) { switch state.Platform { case "github": DoGithubCallBack(state, code) @@ -485,22 +484,23 @@ func DoThirdPartyCallBack(c *gin.Context, state *proto.ThirdPartyLoginState, cod // TODO log.Println("DoThirdPartyCallBack gogs error:", state.Platform) case "google": - accessToken := c.Query("access_token") - DoGoogleCallBack(state, accessToken) + DoGoogleCallBack(state, code) default: log.Println("DoThirdPartyCallBack platform error:", state.Platform) } } -func DoGoogleCallBack(state *proto.ThirdPartyLoginState, accessToken string) { +func DoGoogleCallBack(state *proto.ThirdPartyLoginState, code string) { + //根据code获取Access Token + tokenResp, err := worker.GetGoogleAccessTokenByCode(code, "https://pm.ljsea.top/tool/google_callback", worker.GoogleClientID, worker.GoogleClientSecret) - if accessToken == "" { + if tokenResp.AccessToken == "" { log.Println("get google access token is empty") return } - log.Println("get google access token:", accessToken) + log.Println("get google access token:", tokenResp) //获取用户信息 - userInfo, err := worker.GetGoogleUserInfo(accessToken) + userInfo, err := worker.GetGoogleUserInfo(tokenResp.AccessToken) if err != nil { log.Println("get google user info error:", err) return @@ -542,7 +542,16 @@ func DoRequestToForeignServer(req *proto.OnlineServerReq) (proto.OutlineServerRe log.Println("DoRequestToForeignServer post error:", err) break } - err2, respBytes := worker.DoPostRequestJSON(req.Url, dataBytes, headers) + var err2 error + var respBytes []byte + if req.PostType == "json" { + err2, respBytes = worker.DoPostRequestJSON(req.Url, dataBytes, headers) + } else if req.PostType == "form" { + err2, respBytes = worker.DoPostRequestForm(req.Url, dataBytes, headers) + } else { + log.Println("DoRequestToForeignServer post type error:", req.PostType) + return resp, errors.New("request post type error") + } if err2 != nil { log.Println("DoRequestToForeignServer get error:", err2) return resp, err2 diff --git a/worker/thirdParty.go b/worker/thirdParty.go index 6283b79..6840216 100644 --- a/worker/thirdParty.go +++ b/worker/thirdParty.go @@ -113,6 +113,7 @@ func ExchangeCodeForAccessTokenGithub(clientID, clientSecret, code, redirectURI var onlineReq proto.OnlineServerReq onlineReq.Type = "post" + onlineReq.PostType = "json" onlineReq.Url = "https://github.com/login/oauth/access_token" onlineReq.Data = request @@ -223,21 +224,35 @@ const ( func GetGoogleAccessTokenByCode(code string, redirectURI string, clientID string, clientSecret string) (proto.GoogleOAuthResponse, error) { var resp proto.GoogleOAuthResponse - url := "https://oauth2.googleapis.com/token" + url := "https://www.googleapis.com/oauth2/v4/token" req := proto.GoogleOAuthRequest{ ClientID: clientID, ClientSecret: clientSecret, Code: code, + RedirectURI: redirectURI, + GrantType: "authorization_code", } - reqBytes, err := json.Marshal(req) + var onlineReq proto.OnlineServerReq + onlineReq.Type = "post" + onlineReq.PostType = "form" + onlineReq.Url = url + superTokens := GetRedisSetMembers("super_permission_tokens") + onlineReq.Data = req + onlineReqBytes, _ := json.Marshal(onlineReq) + headers := map[string]string{ + "token": superTokens[0], + "super_id": "1", + } + + log.Println("GetGoogleAccessTokenByCode onlineReqBytes:", string(onlineReqBytes)) + err, respBytes := DoPostRequestJSON("https://vis.ljsea.top/tool/online_server_request?super_id=1", onlineReqBytes, headers) + log.Println("GetGoogleAccessTokenByCode respBytes:", string(respBytes)) + var onlineResp proto.OutlineServerReqResp + err = json.Unmarshal(respBytes, &onlineResp) if err != nil { return resp, err } - err2, respBytes := DoPostRequestJSON(url, reqBytes, nil) - if err2 != nil { - return resp, err2 - } - err = json.Unmarshal(respBytes, &resp) + err = json.Unmarshal([]byte(onlineResp.Data.Response.Response), &resp) if err != nil { return resp, err }