添加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("client_id", worker.GoogleClientID)
|
||||||
params.Add("response_type", "token") //直接返回token
|
params.Add("response_type", "token") //直接返回token
|
||||||
params.Add("redirect_uri", "https://pm.ljsea.top/tool/third_party_callback")
|
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)
|
params.Add("state", stateBase64Str)
|
||||||
baseUri := proto.GoogleAuthorizeBaseUrl
|
baseUri := proto.GoogleAuthorizeBaseUrl
|
||||||
respUrl = fmt.Sprintf("%s?%s", baseUri, params.Encode())
|
respUrl = fmt.Sprintf("%s?%s", baseUri, params.Encode())
|
||||||
|
|
@ -842,11 +842,11 @@ func handleThirdPartyCallback(c *gin.Context) {
|
||||||
//json解析
|
//json解析
|
||||||
var state proto.ThirdPartyLoginState
|
var state proto.ThirdPartyLoginState
|
||||||
err = json.Unmarshal([]byte(decodedStr), &state)
|
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 {
|
if err != nil {
|
||||||
log.Println("json unmarshal error:", err)
|
log.Println("json unmarshal error:", err)
|
||||||
} else {
|
} else {
|
||||||
service.DoThirdPartyCallBack(c, &state, code)
|
service.DoThirdPartyCallBack(&state, code)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
resp.Code = 0
|
resp.Code = 0
|
||||||
|
|
|
||||||
|
|
@ -102,12 +102,19 @@ type GiteeOAuthTokenResponse struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type GoogleOAuthResponse 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 {
|
type GoogleOAuthRequest struct {
|
||||||
ClientID string `json:"client_id"`
|
ClientID string `json:"client_id"`
|
||||||
ClientSecret string `json:"client_secret"`
|
ClientSecret string `json:"client_secret"`
|
||||||
Code string `json:"code"`
|
Code string `json:"code"`
|
||||||
|
RedirectURI string `json:"redirect_uri"`
|
||||||
|
GrantType string `json:"grant_type"` // authorization_code
|
||||||
}
|
}
|
||||||
|
|
||||||
type OAuthGetTokenRequest struct {
|
type OAuthGetTokenRequest struct {
|
||||||
|
|
@ -162,10 +169,11 @@ type GoogleUserInfoResp struct {
|
||||||
|
|
||||||
// 国外服务器负责请求的请求
|
// 国外服务器负责请求的请求
|
||||||
type OnlineServerReq struct {
|
type OnlineServerReq struct {
|
||||||
Type string `json:"type" form:"type"` // 请求类型,get,post
|
Type string `json:"type" form:"type"` // 请求类型,get,post
|
||||||
Url string `json:"url" form:"url"` // 请求地址
|
PostType string `json:"post_type" form:"post_type"` // post请求类型,form,json
|
||||||
Data any `json:"data" form:"data"` // 请求数据
|
Url string `json:"url" form:"url"` // 请求地址
|
||||||
Header []OutlineServerReqData `json:"header" form:"header"` // 请求头
|
Data any `json:"data" form:"data"` // 请求数据
|
||||||
|
Header []OutlineServerReqData `json:"header" form:"header"` // 请求头
|
||||||
}
|
}
|
||||||
|
|
||||||
type OutlineServerReqData struct {
|
type OutlineServerReqData struct {
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/golang-jwt/jwt"
|
"github.com/golang-jwt/jwt"
|
||||||
"log"
|
"log"
|
||||||
"regexp"
|
"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 {
|
switch state.Platform {
|
||||||
case "github":
|
case "github":
|
||||||
DoGithubCallBack(state, code)
|
DoGithubCallBack(state, code)
|
||||||
|
|
@ -485,22 +484,23 @@ func DoThirdPartyCallBack(c *gin.Context, state *proto.ThirdPartyLoginState, cod
|
||||||
// TODO
|
// TODO
|
||||||
log.Println("DoThirdPartyCallBack gogs error:", state.Platform)
|
log.Println("DoThirdPartyCallBack gogs error:", state.Platform)
|
||||||
case "google":
|
case "google":
|
||||||
accessToken := c.Query("access_token")
|
DoGoogleCallBack(state, code)
|
||||||
DoGoogleCallBack(state, accessToken)
|
|
||||||
default:
|
default:
|
||||||
log.Println("DoThirdPartyCallBack platform error:", state.Platform)
|
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")
|
log.Println("get google access token is empty")
|
||||||
return
|
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 {
|
if err != nil {
|
||||||
log.Println("get google user info error:", err)
|
log.Println("get google user info error:", err)
|
||||||
return
|
return
|
||||||
|
|
@ -542,7 +542,16 @@ func DoRequestToForeignServer(req *proto.OnlineServerReq) (proto.OutlineServerRe
|
||||||
log.Println("DoRequestToForeignServer post error:", err)
|
log.Println("DoRequestToForeignServer post error:", err)
|
||||||
break
|
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 {
|
if err2 != nil {
|
||||||
log.Println("DoRequestToForeignServer get error:", err2)
|
log.Println("DoRequestToForeignServer get error:", err2)
|
||||||
return resp, err2
|
return resp, err2
|
||||||
|
|
|
||||||
|
|
@ -113,6 +113,7 @@ func ExchangeCodeForAccessTokenGithub(clientID, clientSecret, code, redirectURI
|
||||||
|
|
||||||
var onlineReq proto.OnlineServerReq
|
var onlineReq proto.OnlineServerReq
|
||||||
onlineReq.Type = "post"
|
onlineReq.Type = "post"
|
||||||
|
onlineReq.PostType = "json"
|
||||||
onlineReq.Url = "https://github.com/login/oauth/access_token"
|
onlineReq.Url = "https://github.com/login/oauth/access_token"
|
||||||
onlineReq.Data = request
|
onlineReq.Data = request
|
||||||
|
|
||||||
|
|
@ -223,21 +224,35 @@ const (
|
||||||
func GetGoogleAccessTokenByCode(code string, redirectURI string, clientID string, clientSecret string) (proto.GoogleOAuthResponse, error) {
|
func GetGoogleAccessTokenByCode(code string, redirectURI string, clientID string, clientSecret string) (proto.GoogleOAuthResponse, error) {
|
||||||
var resp proto.GoogleOAuthResponse
|
var resp proto.GoogleOAuthResponse
|
||||||
|
|
||||||
url := "https://oauth2.googleapis.com/token"
|
url := "https://www.googleapis.com/oauth2/v4/token"
|
||||||
req := proto.GoogleOAuthRequest{
|
req := proto.GoogleOAuthRequest{
|
||||||
ClientID: clientID,
|
ClientID: clientID,
|
||||||
ClientSecret: clientSecret,
|
ClientSecret: clientSecret,
|
||||||
Code: code,
|
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 {
|
if err != nil {
|
||||||
return resp, err
|
return resp, err
|
||||||
}
|
}
|
||||||
err2, respBytes := DoPostRequestJSON(url, reqBytes, nil)
|
err = json.Unmarshal([]byte(onlineResp.Data.Response.Response), &resp)
|
||||||
if err2 != nil {
|
|
||||||
return resp, err2
|
|
||||||
}
|
|
||||||
err = json.Unmarshal(respBytes, &resp)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return resp, err
|
return resp, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue