正常的版本

This commit is contained in:
eson
2023-07-18 16:44:46 +08:00
parent e4e9339071
commit c13225d2fa
6 changed files with 46 additions and 23 deletions

View File

@@ -7,14 +7,15 @@ Auth:
AccessSecret: fusen2023
AccessExpire: 2592000
RefreshAfter: 1592000
OAuth:
- name: google
appid: 1064842923358-e94msq2glj6qr4lrva9ts3q8h.apps.googleusercontent.com
secret: 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
- name: facebook
appid: 1095953604597065
secret: b146872550a190d5275b1420c212002e
OAuth:
google:
appid: "1064842923358-e94msq2glj6qr4lrva9ts3q8h.apps.googleusercontent.com"
secret: "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"
facebook:
appid: "1095953604597065"
secret: "b146872550a190d5275b1420c212002e"
Stripe:
SK: sk_test_51IisojHygnIJZeghPVSBhkwySfcyDV4SoAduIxu3J7bvSJ9cZMD96LY1LO6SpdbYquLJX5oKvgEBB67KT9pecfCy00iEC4pp9y
SK: "sk_test_51IisojHygnIJZeghPVSBhkwySfcyDV4SoAduIxu3J7bvSJ9cZMD96LY1LO6SpdbYquLJX5oKvgEBB67KT9pecfCy00iEC4pp9y"

View File

@@ -21,6 +21,7 @@ func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
c.Timeout = int64(time.Second * 15)

View File

@@ -6,18 +6,22 @@ import (
"github.com/zeromicro/go-zero/rest"
)
type OAuth struct {
Name string `yaml:"name"`
Appid string `yaml:"appid"`
Secret string `yaml:"secret"`
}
type Config struct {
rest.RestConf
SourceMysql string
Auth types.Auth
OAuth []OAuth
OAuth struct {
Google struct {
Appid string
Secret string
}
Facebook struct {
Appid string
Secret string
}
}
Stripe struct {
SK string

View File

@@ -4,6 +4,7 @@ import (
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"log"
"net/http"
"context"
@@ -12,6 +13,7 @@ import (
"github.com/474420502/requests"
"github.com/zeromicro/go-zero/core/logx"
"golang.org/x/net/proxy"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)
@@ -34,20 +36,35 @@ func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, us
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
dialer, err := proxy.SOCKS5("tcp", "127.0.0.1:1080", nil, proxy.Direct)
if err != nil {
log.Fatal(err)
}
customClient := &http.Client{
Transport: &http.Transport{
Dial: dialer.Dial,
},
}
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, customClient)
var googleOauthConfig = &oauth2.Config{
RedirectURL: "http://localhost:9900/api/user/oauth2/login/google",
ClientID: l.svcCtx.Config.OAuth[0].Appid,
ClientSecret: l.svcCtx.Config.OAuth[0].Secret,
ClientID: l.svcCtx.Config.OAuth.Google.Appid,
ClientSecret: l.svcCtx.Config.OAuth.Google.Secret,
Scopes: []string{"https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile"},
Endpoint: google.Endpoint,
}
token, err := googleOauthConfig.Exchange(context.Background(), req.Code)
token, err := googleOauthConfig.Exchange(ctx, req.Code)
if err != nil {
resp.SetStatus(basic.CodeApiErr)
}
ses := requests.NewSession()
ses.Config().SetProxy("socks5://127.0.0.1:1080")
r, err := requests.Get("https://www.googleapis.com/oauth2/v2/userinfo" + token.AccessToken).Execute()
r, err := ses.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + token.AccessToken).Execute()
if err != nil {
panic(err)
}