376 lines
11 KiB
Go
376 lines
11 KiB
Go
package worker
|
|
|
|
import (
|
|
"StuAcaWorksAI/proto"
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
func GetGiteeAccessTokenByCode(code string, redirectURI string, clientID string, clientSecret string) (proto.GiteeOAuthTokenResponse, error) {
|
|
req := proto.GiteeOAuthRequest{
|
|
ClientID: clientID,
|
|
ClientSecret: clientSecret,
|
|
Code: code,
|
|
RedirectURI: redirectURI,
|
|
GrantType: "authorization_code",
|
|
}
|
|
var resp proto.GiteeOAuthTokenResponse
|
|
reqBytes, err := json.Marshal(req)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
url := "https://gitee.com/oauth/token"
|
|
|
|
err2, respBytes := DoPostRequestJSON(url, reqBytes, nil)
|
|
if err2 != nil {
|
|
return resp, err2
|
|
}
|
|
err = json.Unmarshal(respBytes, &resp)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
func GetGiteeUserInfo(accessToken string) (proto.GitHubUserInfo, error) {
|
|
url := "https://gitee.com/api/v5/user?access_token=" + accessToken
|
|
var resp proto.GitHubUserInfo
|
|
err2, respBytes := DoGetRequest(url, nil)
|
|
if err2 != nil {
|
|
return resp, err2
|
|
}
|
|
err := json.Unmarshal(respBytes, &resp)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
func GetGogsAccessTokenByCode() {
|
|
|
|
}
|
|
|
|
func GetGogsUserInfo() {
|
|
|
|
}
|
|
|
|
// 获取access token
|
|
func ExchangeCodeForAccessToken(clientID, clientSecret, code, redirectURI string) (proto.GitHubOAuthResponse, error) {
|
|
request := proto.GitHubOAuthRequest{
|
|
ClientID: clientID,
|
|
ClientSecret: clientSecret,
|
|
Code: code,
|
|
RedirectURI: redirectURI,
|
|
}
|
|
|
|
payload, err := json.Marshal(request)
|
|
if err != nil {
|
|
return proto.GitHubOAuthResponse{}, err
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", "https://github.com/login/oauth/access_token", bytes.NewBuffer(payload))
|
|
if err != nil {
|
|
return proto.GitHubOAuthResponse{}, err
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return proto.GitHubOAuthResponse{}, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return proto.GitHubOAuthResponse{}, err
|
|
}
|
|
|
|
var response proto.GitHubOAuthResponse
|
|
err = json.Unmarshal(body, &response)
|
|
if err != nil {
|
|
return proto.GitHubOAuthResponse{}, err
|
|
}
|
|
|
|
return response, nil
|
|
}
|
|
|
|
func ExchangeCodeForAccessTokenGithub(clientID, clientSecret, code, redirectURI string) (proto.GitHubOAuthResponse, error) {
|
|
request := proto.GitHubOAuthRequest{
|
|
ClientID: clientID,
|
|
ClientSecret: clientSecret,
|
|
Code: code,
|
|
RedirectURI: redirectURI,
|
|
}
|
|
var githubOAuthResponse proto.GitHubOAuthResponse
|
|
|
|
var onlineReq proto.OnlineServerReq
|
|
onlineReq.Type = "post"
|
|
onlineReq.PostType = "json"
|
|
onlineReq.Url = "https://github.com/login/oauth/access_token"
|
|
onlineReq.Data = request
|
|
|
|
header := make([]proto.OutlineServerReqData, 0)
|
|
header = append(header, proto.OutlineServerReqData{
|
|
Key: "Content-Type",
|
|
Value: "application/json",
|
|
})
|
|
header = append(header, proto.OutlineServerReqData{
|
|
Key: "Accept",
|
|
Value: "application/json",
|
|
})
|
|
superTokens := GetRedisSetMembers("super_permission_tokens")
|
|
header = append(header, proto.OutlineServerReqData{
|
|
Key: "token",
|
|
Value: superTokens[0],
|
|
})
|
|
onlineReq.Header = header
|
|
onlineReqBytes, _ := json.Marshal(onlineReq)
|
|
headers := map[string]string{
|
|
"token": superTokens[0],
|
|
"super_id": "1",
|
|
}
|
|
log.Println("ExchangeCodeForAccessTokenGithub onlineReqBytes:", string(onlineReqBytes))
|
|
|
|
err, respBytes := DoPostRequestJSON("https://vis.ljsea.top/tool/online_server_request?super_id=1", onlineReqBytes, headers)
|
|
if err != nil {
|
|
return githubOAuthResponse, err
|
|
}
|
|
log.Println("ExchangeCodeForAccessTokenGithub respBytes:", string(respBytes))
|
|
var onlineResp proto.OutlineServerReqResp
|
|
err = json.Unmarshal(respBytes, &onlineResp)
|
|
if err != nil {
|
|
return githubOAuthResponse, err
|
|
}
|
|
err = json.Unmarshal([]byte(onlineResp.Data.Response.Response), &githubOAuthResponse)
|
|
if err != nil {
|
|
return githubOAuthResponse, err
|
|
}
|
|
return githubOAuthResponse, nil
|
|
}
|
|
|
|
// 获取用户信息
|
|
func GetGitHubUserInfo(accessToken string) (proto.GitHubUserInfo, error) {
|
|
|
|
url := "https://api.github.com/user"
|
|
headers := map[string]string{
|
|
"Authorization": "Bearer " + accessToken,
|
|
}
|
|
err, data := DoGetRequest(url, headers)
|
|
var resp proto.GitHubUserInfo
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
err = json.Unmarshal(data, &resp)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
if resp.UserID == 0 {
|
|
return resp, errors.New("获取用户信息失败,请检查access_token是否正确")
|
|
}
|
|
return resp, err
|
|
}
|
|
func GetGitHubUserInfoV2(accessToken string) (proto.GitHubUserInfo, error) {
|
|
url := "https://api.github.com/user"
|
|
var onlineReq proto.OnlineServerReq
|
|
onlineReq.Type = "get"
|
|
onlineReq.Url = url
|
|
|
|
header := make([]proto.OutlineServerReqData, 0)
|
|
header = append(header, proto.OutlineServerReqData{
|
|
Key: "Authorization",
|
|
Value: "Bearer " + accessToken,
|
|
})
|
|
onlineReq.Header = header
|
|
superTokens := GetRedisSetMembers("super_permission_tokens")
|
|
onlineReqBytes, _ := json.Marshal(onlineReq)
|
|
headers := map[string]string{
|
|
"token": superTokens[0],
|
|
"super_id": "1",
|
|
}
|
|
log.Println("GetGitHubUserInfoV2 onlineReqBytes:", string(onlineReqBytes))
|
|
err, respBytes := DoPostRequestJSON("https://vis.ljsea.top/tool/online_server_request?super_id=1", onlineReqBytes, headers)
|
|
log.Println("GetGitHubUserInfoV2 respBytes:", string(respBytes))
|
|
var onlineResp proto.OutlineServerReqResp
|
|
var resp proto.GitHubUserInfo
|
|
err = json.Unmarshal(respBytes, &onlineResp)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
err = json.Unmarshal([]byte(onlineResp.Data.Response.Response), &resp)
|
|
//err, data := DoGetRequest(url, headers)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
if resp.UserID == 0 {
|
|
return resp, errors.New("获取用户信息失败,请检查access_token是否正确")
|
|
}
|
|
return resp, err
|
|
}
|
|
|
|
// 谷歌登录授权
|
|
const (
|
|
GoogleClientID = "194888366727-2uvqs43mimk46mmilc04pptrjkqfjn97.apps.googleusercontent.com"
|
|
GoogleClientSecret = "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"
|
|
)
|
|
|
|
func GetGoogleAccessTokenByCode(code string, redirectURI string, clientID string, clientSecret string) (proto.GoogleOAuthResponse, error) {
|
|
var resp proto.GoogleOAuthResponse
|
|
|
|
url := "https://www.googleapis.com/oauth2/v4/token"
|
|
req := proto.GoogleOAuthRequest{
|
|
ClientID: clientID,
|
|
ClientSecret: clientSecret,
|
|
Code: code,
|
|
RedirectURI: redirectURI,
|
|
GrantType: "authorization_code",
|
|
}
|
|
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
|
|
}
|
|
err = json.Unmarshal([]byte(onlineResp.Data.Response.Response), &resp)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
func GetGoogleUserInfo(accessToken string) (proto.GoogleUserInfoResp, error) {
|
|
var resp proto.GoogleUserInfoResp
|
|
url := "https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + accessToken
|
|
var onlineReq proto.OnlineServerReq
|
|
onlineReq.Type = "get"
|
|
onlineReq.Url = url
|
|
superTokens := GetRedisSetMembers("super_permission_tokens")
|
|
onlineReqBytes, _ := json.Marshal(onlineReq)
|
|
headers := map[string]string{
|
|
"token": superTokens[0],
|
|
"super_id": "1",
|
|
}
|
|
log.Println("GetGoogleUserInfo onlineReqBytes:", string(onlineReqBytes))
|
|
err, respBytes := DoPostRequestJSON("https://vis.ljsea.top/tool/online_server_request?super_id=1", onlineReqBytes, headers)
|
|
log.Println("GetGoogleUserInfo respBytes:", string(respBytes))
|
|
var onlineResp proto.OutlineServerReqResp
|
|
err = json.Unmarshal(respBytes, &onlineResp)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
err = json.Unmarshal([]byte(onlineResp.Data.Response.Response), &resp)
|
|
//err, respBytes := DoGetRequest(url, nil)
|
|
//if err != nil {
|
|
// return resp, err
|
|
//}
|
|
err = json.Unmarshal(respBytes, &resp)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
// facebook
|
|
const (
|
|
FacebookClientID = "1171721801397908"
|
|
FacebookClientSecret = "e5eadef4d764e4458fc68bd1e4792dbf"
|
|
)
|
|
|
|
func GetFacebookAccessTokenByCode(code string, redirectURI string, clientID string, clientSecret string) (proto.FacebookOAuthResponse, error) {
|
|
var resp proto.FacebookOAuthResponse
|
|
|
|
url := "https://graph.facebook.com/v22.0/oauth/access_token" + "?client_id=" + clientID + "&client_secret=" + clientSecret + "&code=" + code + "&redirect_uri=" + redirectURI
|
|
//req := proto.FaceBookOAuthRequest{
|
|
// ClientID: clientID,
|
|
// ClientSecret: clientSecret,
|
|
// Code: code,
|
|
// RedirectURI: redirectURI,
|
|
//}
|
|
log.Println("GetFacebookAccessTokenByCode url:", url)
|
|
var onlineReq proto.OnlineServerReq
|
|
onlineReq.Type = "get"
|
|
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("GetFacebookAccessTokenByCode onlineReqBytes:", string(onlineReqBytes))
|
|
err, respBytes := DoPostRequestJSON("https://vis.ljsea.top/tool/online_server_request?super_id=1", onlineReqBytes, headers)
|
|
log.Println("GetFacebookAccessTokenByCode respBytes:", string(respBytes))
|
|
var onlineResp proto.OutlineServerReqResp
|
|
err = json.Unmarshal(respBytes, &onlineResp)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
err = json.Unmarshal([]byte(onlineResp.Data.Response.Response), &resp)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
func GetFaceBookUserInfo(accessToken string) (proto.FaceBookUserInfoResp, error) {
|
|
var resp proto.FaceBookUserInfoResp
|
|
url := "https://graph.facebook.com/v22.0/me?fields=id,name"
|
|
var onlineReq proto.OnlineServerReq
|
|
onlineReq.Type = "get"
|
|
onlineReq.Url = url
|
|
onlineReqHeader := make([]proto.OutlineServerReqData, 0)
|
|
onlineReqHeader = append(onlineReqHeader, proto.OutlineServerReqData{
|
|
Key: "Authorization",
|
|
Value: "Bearer " + accessToken,
|
|
})
|
|
onlineReq.Header = onlineReqHeader
|
|
superTokens := GetRedisSetMembers("super_permission_tokens")
|
|
onlineReqBytes, _ := json.Marshal(onlineReq)
|
|
headers := map[string]string{
|
|
"token": superTokens[0],
|
|
"super_id": "1",
|
|
}
|
|
log.Println("GetGoogleUserInfo onlineReqBytes:", string(onlineReqBytes))
|
|
err, respBytes := DoPostRequestJSON("https://vis.ljsea.top/tool/online_server_request?super_id=1", onlineReqBytes, headers)
|
|
log.Println("GetGoogleUserInfo respBytes:", string(respBytes))
|
|
var onlineResp proto.OutlineServerReqResp
|
|
err = json.Unmarshal(respBytes, &onlineResp)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
err = json.Unmarshal([]byte(onlineResp.Data.Response.Response), &resp)
|
|
//err, respBytes := DoGetRequest(url, nil)
|
|
//if err != nil {
|
|
// return resp, err
|
|
//}
|
|
err = json.Unmarshal(respBytes, &resp)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
return resp, nil
|
|
}
|