From c13225d2fa56c02dfb42c151a56b13b44ee7d4c4 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 18 Jul 2023 16:44:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=AD=A3=E5=B8=B8=E7=9A=84=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- go.sum | 4 +-- server/home-user-auth/etc/home-user-auth.yaml | 19 +++++++------- server/home-user-auth/home-user-auth.go | 1 + .../home-user-auth/internal/config/config.go | 18 +++++++------ .../internal/logic/usergoogleloginlogic.go | 25 ++++++++++++++++--- 6 files changed, 46 insertions(+), 23 deletions(-) diff --git a/go.mod b/go.mod index 6f578ca0..862c6ebd 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,7 @@ require ( ) require ( - github.com/474420502/requests v1.39.0 + github.com/474420502/requests v1.40.0 github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/tidwall/gjson v1.12.0 diff --git a/go.sum b/go.sum index 202f71cb..ee869494 100644 --- a/go.sum +++ b/go.sum @@ -36,8 +36,8 @@ cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RX cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/474420502/random v0.4.1 h1:HUUyLXRWMijVb7CJoEC16f0aFQOW25Lkr80Mut6PoKU= -github.com/474420502/requests v1.39.0 h1:ICrBfgkw3Jtatv/tKvDsKhIIFg0HslOE7BSIgfh2qO0= -github.com/474420502/requests v1.39.0/go.mod h1:r1Uwbbq/t3Cn95VOfgJjZmD7sfwp+gjldq/zDncNYRc= +github.com/474420502/requests v1.40.0 h1:VDuLxSG/3IGBvMfjPV8+o7s1l5mOwLAgfo5Og6vMAJw= +github.com/474420502/requests v1.40.0/go.mod h1:2SCVzim0ONFYG09g/GrM7RTeJIC6qTyZfnohsjnG5C8= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60= diff --git a/server/home-user-auth/etc/home-user-auth.yaml b/server/home-user-auth/etc/home-user-auth.yaml index 462805ce..beefa010 100644 --- a/server/home-user-auth/etc/home-user-auth.yaml +++ b/server/home-user-auth/etc/home-user-auth.yaml @@ -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" diff --git a/server/home-user-auth/home-user-auth.go b/server/home-user-auth/home-user-auth.go index 14b4587f..28598d4f 100644 --- a/server/home-user-auth/home-user-auth.go +++ b/server/home-user-auth/home-user-auth.go @@ -21,6 +21,7 @@ func main() { flag.Parse() var c config.Config + conf.MustLoad(*configFile, &c) c.Timeout = int64(time.Second * 15) diff --git a/server/home-user-auth/internal/config/config.go b/server/home-user-auth/internal/config/config.go index c79a4367..63e5a39a 100644 --- a/server/home-user-auth/internal/config/config.go +++ b/server/home-user-auth/internal/config/config.go @@ -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 diff --git a/server/home-user-auth/internal/logic/usergoogleloginlogic.go b/server/home-user-auth/internal/logic/usergoogleloginlogic.go index 3bbb83c5..51d203dc 100644 --- a/server/home-user-auth/internal/logic/usergoogleloginlogic.go +++ b/server/home-user-auth/internal/logic/usergoogleloginlogic.go @@ -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) }