2023-07-24 09:22:06 +00:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
2023-07-27 08:48:43 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
2023-07-24 09:22:06 +00:00
|
|
|
"fmt"
|
|
|
|
"fusenapi/utils/auth"
|
|
|
|
"fusenapi/utils/basic"
|
2023-07-27 08:48:43 +00:00
|
|
|
"io"
|
2023-07-24 09:22:06 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"fusenapi/server/auth/internal/svc"
|
|
|
|
"fusenapi/server/auth/internal/types"
|
|
|
|
|
|
|
|
"github.com/474420502/requests"
|
2023-08-25 07:37:35 +00:00
|
|
|
"github.com/google/uuid"
|
2023-07-24 09:22:06 +00:00
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
"github.com/zeromicro/go-zero/rest/httpx"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"golang.org/x/oauth2/google"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UserGoogleLoginLogic struct {
|
|
|
|
logx.Logger
|
|
|
|
ctx context.Context
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
|
|
|
|
token string // 登录 token
|
|
|
|
|
|
|
|
isRegistered bool // 是否注册
|
|
|
|
registerToken string // 注册邮箱的token
|
2023-07-27 02:18:49 +00:00
|
|
|
registerInfo *auth.RegisterToken
|
2023-07-24 09:22:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserGoogleLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserGoogleLoginLogic {
|
|
|
|
return &UserGoogleLoginLogic{
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
ctx: ctx,
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 处理进入前逻辑w,r
|
|
|
|
// func (l *UserGoogleLoginLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// }
|
|
|
|
|
|
|
|
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{
|
2023-08-10 08:25:41 +00:00
|
|
|
RedirectURL: fmt.Sprintf("http://%s/api/user/oauth2/login/google", l.svcCtx.Config.MainAddress),
|
2023-07-24 09:22:06 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2023-08-24 03:47:22 +00:00
|
|
|
token, err := googleOauthConfig.Exchange(l.ctx, req.Code)
|
2023-07-24 09:22:06 +00:00
|
|
|
if err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
resp.SetStatus(basic.CodeApiErr)
|
|
|
|
}
|
2023-08-10 08:25:41 +00:00
|
|
|
|
2023-07-24 09:22:06 +00:00
|
|
|
ses := requests.NewSession()
|
|
|
|
r, err := ses.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + token.AccessToken).Execute()
|
|
|
|
if err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatus(basic.CodeOAuthGoogleApiErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println(r.Json())
|
|
|
|
|
|
|
|
googleId := r.Json().Get("id").Int()
|
|
|
|
user, err := l.svcCtx.AllModels.FsUser.FindUserByGoogleId(context.TODO(), googleId)
|
|
|
|
if err != nil {
|
|
|
|
if err != gorm.ErrRecordNotFound {
|
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatus(basic.CodeDbSqlErr)
|
|
|
|
}
|
|
|
|
|
2023-07-27 08:48:43 +00:00
|
|
|
nonce := make([]byte, 16)
|
|
|
|
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatus(basic.CodeOK)
|
|
|
|
}
|
2023-07-24 09:22:06 +00:00
|
|
|
|
2023-07-27 08:48:43 +00:00
|
|
|
l.registerInfo = &auth.RegisterToken{
|
2023-08-29 06:19:47 +00:00
|
|
|
|
2023-08-25 07:37:35 +00:00
|
|
|
Password: base64.RawURLEncoding.EncodeToString(nonce),
|
2023-07-27 08:48:43 +00:00
|
|
|
Platform: "google",
|
|
|
|
OperateType: auth.OpTypeRegister,
|
2023-08-25 07:37:35 +00:00
|
|
|
TraceId: uuid.NewString(),
|
2023-07-27 08:48:43 +00:00
|
|
|
CreateAt: time.Now(),
|
2023-08-29 06:19:47 +00:00
|
|
|
Extend: map[string]interface{}{
|
|
|
|
"google_id": googleId,
|
|
|
|
},
|
2023-07-27 08:48:43 +00:00
|
|
|
}
|
2023-07-24 09:22:06 +00:00
|
|
|
|
2023-07-27 08:48:43 +00:00
|
|
|
l.isRegistered = false
|
2023-08-29 06:19:47 +00:00
|
|
|
token, err := l.svcCtx.OAuthTokenManger.Encrypt(l.registerInfo)
|
2023-07-27 08:48:43 +00:00
|
|
|
if err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatus(basic.CodeOAuthRegisterTokenErr)
|
|
|
|
}
|
|
|
|
l.registerToken = token
|
2023-07-24 11:43:56 +00:00
|
|
|
|
2023-07-24 09:22:06 +00:00
|
|
|
return resp.SetStatus(basic.CodeOK)
|
|
|
|
}
|
|
|
|
|
2023-07-27 08:48:43 +00:00
|
|
|
l.isRegistered = true
|
2023-07-24 09:22:06 +00:00
|
|
|
// 如果密码匹配,则生成 JWT Token。
|
2023-08-28 06:21:06 +00:00
|
|
|
jwtToken, err := auth.GenerateJwtTokenUint64(auth.StringToHash(*user.PasswordHash), l.svcCtx.Config.Auth.AccessExpire, time.Now().UTC().Unix(), user.Id, 0)
|
2023-07-24 09:22:06 +00:00
|
|
|
|
|
|
|
// 如果生成 JWT Token 失败,则抛出错误并返回未认证的状态码。
|
|
|
|
if err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatus(basic.CodeServiceErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
l.token = jwtToken
|
|
|
|
|
|
|
|
return resp.SetStatus(basic.CodeOK)
|
|
|
|
}
|
2023-07-26 16:03:38 +00:00
|
|
|
|
|
|
|
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
|
|
|
func (l *UserGoogleLoginLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
|
|
|
|
|
|
|
if resp.Code == 200 {
|
|
|
|
|
|
|
|
rurl := fmt.Sprintf(
|
|
|
|
l.svcCtx.Config.MainAddress+"/oauth?token=%s&is_registered=%t®ister_token=%s",
|
|
|
|
l.token,
|
|
|
|
l.isRegistered,
|
|
|
|
l.registerToken,
|
|
|
|
)
|
|
|
|
|
|
|
|
html := fmt.Sprintf(`
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Redirect</title>
|
|
|
|
<script type="text/javascript">
|
|
|
|
window.onload = function() {
|
|
|
|
window.location = "%s";
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`, rurl)
|
|
|
|
fmt.Fprintln(w, html)
|
|
|
|
} else {
|
|
|
|
httpx.OkJson(w, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|