2023-07-17 11:43:43 +00:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fusenapi/utils/auth"
|
|
|
|
"fusenapi/utils/basic"
|
2023-07-18 05:04:29 +00:00
|
|
|
"log"
|
2023-07-18 08:44:46 +00:00
|
|
|
"net/http"
|
2023-07-17 11:43:43 +00:00
|
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"fusenapi/server/home-user-auth/internal/svc"
|
|
|
|
"fusenapi/server/home-user-auth/internal/types"
|
|
|
|
|
2023-07-18 05:04:29 +00:00
|
|
|
"github.com/474420502/requests"
|
2023-07-17 11:43:43 +00:00
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
2023-07-18 08:44:46 +00:00
|
|
|
"golang.org/x/net/proxy"
|
2023-07-18 05:04:29 +00:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"golang.org/x/oauth2/google"
|
2023-07-17 11:43:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type UserGoogleLoginLogic struct {
|
|
|
|
logx.Logger
|
|
|
|
ctx context.Context
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserGoogleLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserGoogleLoginLogic {
|
|
|
|
return &UserGoogleLoginLogic{
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
ctx: ctx,
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, userinfo *auth.UserInfo) (resp *basic.Response) {
|
|
|
|
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
|
|
|
// userinfo 传入值时, 一定不为null
|
|
|
|
|
2023-07-18 08:44:46 +00:00
|
|
|
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)
|
|
|
|
|
2023-07-18 05:04:29 +00:00
|
|
|
var googleOauthConfig = &oauth2.Config{
|
|
|
|
RedirectURL: "http://localhost:9900/api/user/oauth2/login/google",
|
2023-07-18 08:44:46 +00:00
|
|
|
ClientID: l.svcCtx.Config.OAuth.Google.Appid,
|
|
|
|
ClientSecret: l.svcCtx.Config.OAuth.Google.Secret,
|
2023-07-18 05:04:29 +00:00
|
|
|
Scopes: []string{"https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile"},
|
|
|
|
Endpoint: google.Endpoint,
|
|
|
|
}
|
|
|
|
|
2023-07-18 08:44:46 +00:00
|
|
|
token, err := googleOauthConfig.Exchange(ctx, req.Code)
|
2023-07-18 05:04:29 +00:00
|
|
|
if err != nil {
|
|
|
|
resp.SetStatus(basic.CodeApiErr)
|
|
|
|
}
|
2023-07-18 08:44:46 +00:00
|
|
|
ses := requests.NewSession()
|
|
|
|
ses.Config().SetProxy("socks5://127.0.0.1:1080")
|
2023-07-18 05:04:29 +00:00
|
|
|
|
2023-07-18 08:44:46 +00:00
|
|
|
r, err := ses.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + token.AccessToken).Execute()
|
2023-07-18 05:04:29 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
log.Println(r.Json())
|
|
|
|
|
2023-07-17 11:43:43 +00:00
|
|
|
return resp.SetStatus(basic.CodeOK)
|
|
|
|
}
|