171 lines
3.9 KiB
Go
171 lines
3.9 KiB
Go
|
|
package worker
|
||
|
|
|
||
|
|
import (
|
||
|
|
"StuAcaWorksAI/proto"
|
||
|
|
"bytes"
|
||
|
|
"encoding/json"
|
||
|
|
"errors"
|
||
|
|
"io"
|
||
|
|
"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 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
|
||
|
|
}
|
||
|
|
|
||
|
|
// 谷歌登录授权
|
||
|
|
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://oauth2.googleapis.com/token"
|
||
|
|
req := proto.GoogleOAuthRequest{
|
||
|
|
ClientID: clientID,
|
||
|
|
ClientSecret: clientSecret,
|
||
|
|
Code: code,
|
||
|
|
}
|
||
|
|
reqBytes, err := json.Marshal(req)
|
||
|
|
if err != nil {
|
||
|
|
return resp, err
|
||
|
|
}
|
||
|
|
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 GetGoogleUserInfo(accessToken string) (proto.GitHubUserInfo, error) {
|
||
|
|
var resp proto.GitHubUserInfo
|
||
|
|
|
||
|
|
url := "https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + accessToken
|
||
|
|
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
|
||
|
|
}
|