添加google第三方登录,修改中继请求post模式选择
This commit is contained in:
parent
0b55f868a8
commit
c8521e4931
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue