58 lines
1.6 KiB
Go
58 lines
1.6 KiB
Go
package logic
|
|
|
|
import (
|
|
"fusenapi/utils/auth"
|
|
"fusenapi/utils/basic"
|
|
"log"
|
|
|
|
"context"
|
|
|
|
"fusenapi/server/home-user-auth/internal/svc"
|
|
"fusenapi/server/home-user-auth/internal/types"
|
|
|
|
"github.com/474420502/requests"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"golang.org/x/oauth2"
|
|
"golang.org/x/oauth2/google"
|
|
)
|
|
|
|
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
|
|
|
|
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,
|
|
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)
|
|
if err != nil {
|
|
resp.SetStatus(basic.CodeApiErr)
|
|
}
|
|
|
|
r, err := requests.Get("https://www.googleapis.com/oauth2/v2/userinfo" + token.AccessToken).Execute()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
log.Println(r.Json())
|
|
|
|
return resp.SetStatus(basic.CodeOK)
|
|
}
|