2023-07-26 16:03:38 +00:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
2023-08-25 07:37:35 +00:00
|
|
|
"fmt"
|
2023-08-27 15:54:28 +00:00
|
|
|
"fusenapi/model/gmodel"
|
2023-07-26 16:03:38 +00:00
|
|
|
"fusenapi/utils/auth"
|
|
|
|
"fusenapi/utils/basic"
|
2023-08-25 07:37:35 +00:00
|
|
|
"fusenapi/utils/wevent"
|
2023-08-29 08:24:20 +00:00
|
|
|
"net/http"
|
2023-07-27 08:48:43 +00:00
|
|
|
"time"
|
2023-07-26 16:03:38 +00:00
|
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"fusenapi/server/auth/internal/svc"
|
|
|
|
"fusenapi/server/auth/internal/types"
|
|
|
|
|
2023-08-25 07:37:35 +00:00
|
|
|
"github.com/474420502/requests"
|
2023-07-26 16:03:38 +00:00
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
2023-08-29 08:24:20 +00:00
|
|
|
"github.com/zeromicro/go-zero/rest/httpx"
|
2023-08-29 07:34:38 +00:00
|
|
|
"gorm.io/gorm"
|
2023-07-26 16:03:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type UserEmailConfirmationLogic struct {
|
|
|
|
logx.Logger
|
|
|
|
ctx context.Context
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserEmailConfirmationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserEmailConfirmationLogic {
|
|
|
|
return &UserEmailConfirmationLogic{
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
ctx: ctx,
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 处理进入前逻辑w,r
|
|
|
|
// func (l *UserEmailConfirmationLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// }
|
|
|
|
|
2023-08-27 15:54:28 +00:00
|
|
|
func FinishRegister(svcCtx *svc.ServiceContext, user *gmodel.FsUser, token *auth.RegisterToken) error {
|
|
|
|
// 创建签证
|
|
|
|
jwtToken, err := auth.GenerateJwtTokenUint64(
|
|
|
|
auth.StringToHash(*user.PasswordHash),
|
|
|
|
svcCtx.Config.Auth.AccessExpire,
|
|
|
|
time.Now().UTC().Unix(),
|
|
|
|
user.Id,
|
2023-09-04 10:43:31 +00:00
|
|
|
0,
|
2023-08-27 15:54:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
event := wevent.NewWebsocketEventSuccess(wevent.UserEmailRegister, token.TraceId)
|
|
|
|
event.Data = wevent.DataEmailRegister{
|
|
|
|
JwtToken: jwtToken,
|
|
|
|
}
|
2023-09-04 09:45:58 +00:00
|
|
|
err = CommonNotify(svcCtx.Config.WebsocketAddr, token.Wid, event)
|
2023-08-27 15:54:28 +00:00
|
|
|
if err != nil {
|
|
|
|
// logx.Error(err, token.TraceId)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-04 09:45:58 +00:00
|
|
|
func CommonNotify(WebsocketAddr, wid string, event *wevent.WebsocketEvent) error {
|
2023-09-05 09:35:33 +00:00
|
|
|
|
2023-09-05 09:24:46 +00:00
|
|
|
reqWebsocketAddr := fmt.Sprintf("%s/api/websocket/common_notify", WebsocketAddr)
|
|
|
|
tp := requests.Post(reqWebsocketAddr)
|
2023-08-27 15:54:28 +00:00
|
|
|
tp.SetBodyJson(requests.M{
|
|
|
|
"wid": wid,
|
|
|
|
"data": event,
|
|
|
|
})
|
|
|
|
|
|
|
|
wresp, err := tp.Execute()
|
|
|
|
if err != nil {
|
|
|
|
// logx.Error(err, token.TraceId)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := wresp.Json()
|
2023-09-05 09:24:46 +00:00
|
|
|
|
|
|
|
if !result.Get("code").Exists() {
|
|
|
|
return fmt.Errorf("send %s is error", reqWebsocketAddr)
|
|
|
|
}
|
|
|
|
|
2023-08-27 15:54:28 +00:00
|
|
|
if result.Get("code").Int() != 200 {
|
2023-09-05 09:07:42 +00:00
|
|
|
return fmt.Errorf("%s", result.String())
|
2023-08-27 15:54:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-26 16:03:38 +00:00
|
|
|
func (l *UserEmailConfirmationLogic) UserEmailConfirmation(req *types.RequestEmailConfirmation, userinfo *auth.UserInfo) (resp *basic.Response) {
|
|
|
|
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
|
|
|
// userinfo 传入值时, 一定不为null
|
|
|
|
|
2023-09-03 17:40:28 +00:00
|
|
|
switch auth.OperateType(req.OpType) {
|
2023-07-27 08:48:43 +00:00
|
|
|
case auth.OpTypeRegister:
|
2023-09-03 17:40:28 +00:00
|
|
|
|
|
|
|
token, err := l.svcCtx.OAuthTokenManger.Decrypt(req.Token)
|
|
|
|
if err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatus(basic.CodeOAuthRegisterTokenErr)
|
|
|
|
}
|
|
|
|
|
2023-08-30 10:44:41 +00:00
|
|
|
if time.Since(token.CreateAt) > 30*time.Minute {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeOAuthConfirmationTimeoutErr, "Verification links expire after 30 minute.")
|
2023-07-27 08:48:43 +00:00
|
|
|
}
|
2023-08-30 10:44:41 +00:00
|
|
|
|
2023-08-29 06:27:00 +00:00
|
|
|
logx.Info(token.Platform)
|
2023-07-27 08:48:43 +00:00
|
|
|
switch token.Platform {
|
2023-08-29 10:06:39 +00:00
|
|
|
case string(auth.PLATFORM_GOOGLE):
|
2023-07-27 11:47:52 +00:00
|
|
|
// 谷歌平台的注册流程
|
|
|
|
user, err := l.svcCtx.AllModels.FsUser.RegisterByGoogleOAuth(l.ctx, token)
|
2023-07-27 08:48:43 +00:00
|
|
|
if err != nil {
|
2023-08-25 07:37:35 +00:00
|
|
|
logx.Error(err, token.TraceId)
|
2023-08-30 10:44:41 +00:00
|
|
|
return resp.SetStatus(basic.CodeDbSqlErr, err.Error())
|
2023-07-27 08:48:43 +00:00
|
|
|
}
|
|
|
|
|
2023-08-29 08:15:34 +00:00
|
|
|
err = FinishRegister(l.svcCtx, user, token)
|
|
|
|
if err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
}
|
2023-08-27 15:54:28 +00:00
|
|
|
logx.Info("success", token.TraceId)
|
2023-07-27 11:47:52 +00:00
|
|
|
|
2023-08-29 10:06:39 +00:00
|
|
|
case string(auth.PLATFORM_FACEBOOK):
|
2023-08-30 08:28:55 +00:00
|
|
|
|
2023-08-29 10:06:39 +00:00
|
|
|
case string(auth.PLATFORM_FUSEN):
|
2023-08-29 02:56:35 +00:00
|
|
|
// log.Println("aaaa", token)
|
2023-08-24 10:28:01 +00:00
|
|
|
user, err := l.svcCtx.AllModels.FsUser.RegisterByFusen(l.ctx, token)
|
2023-08-29 07:34:38 +00:00
|
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
2023-08-30 08:28:55 +00:00
|
|
|
logx.Error(err)
|
2023-08-30 06:24:51 +00:00
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, err.Error())
|
2023-08-24 10:28:01 +00:00
|
|
|
}
|
2023-08-29 08:02:54 +00:00
|
|
|
err = FinishRegister(l.svcCtx, user, token)
|
|
|
|
if err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
}
|
|
|
|
logx.Info("success:", token.TraceId)
|
2023-07-27 08:48:43 +00:00
|
|
|
}
|
2023-09-03 17:40:28 +00:00
|
|
|
case auth.OpTypeResetToken:
|
|
|
|
|
|
|
|
rt, err := l.svcCtx.ResetTokenManger.Decrypt(req.Token) // ResetToken
|
|
|
|
if err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatus(basic.CodeOAuthResetTokenDecryptErr, err.Error())
|
|
|
|
}
|
|
|
|
|
2023-09-05 07:00:45 +00:00
|
|
|
if time.Since(rt.CreateAt) > 30*time.Minute {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeOAuthConfirmationTimeoutErr, "Verification links expire after 30 minute.")
|
|
|
|
}
|
|
|
|
|
2023-09-03 17:40:28 +00:00
|
|
|
// TODO: 存储
|
|
|
|
if rt.OperateType != auth.OpTypeResetToken {
|
|
|
|
return resp.SetStatus(basic.CodeOAuthTypeErr, "error OperateType: rt.OperateType != auth.OpTypeResetToken")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = l.svcCtx.AllModels.FsUser.Transaction(l.ctx, func(tx *gorm.DB) error {
|
|
|
|
user := &gmodel.FsUser{Id: int64(rt.UserId)}
|
|
|
|
err := tx.Take(user).Error
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if *user.PasswordHash != rt.OldPassword {
|
2023-09-04 08:59:43 +00:00
|
|
|
return fmt.Errorf("password had been reset")
|
2023-09-03 17:40:28 +00:00
|
|
|
}
|
|
|
|
return tx.Update("PasswordHash", rt.NewPassword).Error
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return resp.SetStatus(basic.CodeDbSqlErr, err.Error())
|
|
|
|
}
|
|
|
|
|
2023-09-04 09:45:58 +00:00
|
|
|
event := wevent.NewWebsocketEventSuccess(wevent.UserResetToken, rt.TraceId)
|
|
|
|
err = CommonNotify(l.svcCtx.Config.MainAddress, rt.Wid, event)
|
|
|
|
if err != nil {
|
|
|
|
logx.Error(err, rt.TraceId)
|
|
|
|
return resp.SetStatus(basic.CodeResetPasswordErr, err.Error())
|
|
|
|
}
|
|
|
|
|
2023-09-03 17:40:28 +00:00
|
|
|
return resp.SetStatus(basic.CodeOK)
|
2023-07-27 08:48:43 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return resp.SetStatus(basic.CodeOAuthRegisterTokenErr)
|
|
|
|
}
|
|
|
|
|
2023-07-26 16:03:38 +00:00
|
|
|
return resp.SetStatus(basic.CodeOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
2023-08-29 08:24:20 +00:00
|
|
|
func (l *UserEmailConfirmationLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
2023-08-29 08:26:06 +00:00
|
|
|
if resp.Code == 200 {
|
|
|
|
successHtml := `<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
2023-08-30 10:44:41 +00:00
|
|
|
<title>Registration Successful</title>
|
|
|
|
|
2023-08-29 08:37:12 +00:00
|
|
|
<style>
|
2023-08-30 10:44:41 +00:00
|
|
|
body {
|
|
|
|
font-family: sans-serif;
|
|
|
|
font-size: 16px;
|
|
|
|
}
|
|
|
|
|
|
|
|
h1 {
|
|
|
|
font-size: 24px;
|
|
|
|
color: red;
|
|
|
|
}
|
|
|
|
|
|
|
|
@media screen and (max-width: 480px) {
|
2023-08-29 08:37:12 +00:00
|
|
|
body {
|
2023-08-30 10:44:41 +00:00
|
|
|
font-size: 14px;
|
2023-08-29 08:37:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
h1 {
|
2023-08-30 10:44:41 +00:00
|
|
|
font-size: 18px;
|
2023-08-29 08:37:12 +00:00
|
|
|
}
|
2023-08-30 10:44:41 +00:00
|
|
|
}
|
2023-08-29 08:37:12 +00:00
|
|
|
</style>
|
2023-08-30 10:44:41 +00:00
|
|
|
|
2023-08-29 08:26:06 +00:00
|
|
|
</head>
|
2023-08-30 10:44:41 +00:00
|
|
|
|
2023-08-29 08:26:06 +00:00
|
|
|
<body>
|
2023-08-29 08:37:12 +00:00
|
|
|
|
2023-08-30 10:44:41 +00:00
|
|
|
<h1>Congratulations! Your registration is successful.</h1>
|
|
|
|
|
|
|
|
<p>Thank you for registering on our website. Your account has been activated.</p>
|
|
|
|
|
|
|
|
<p>You can now login using your email address and password to enjoy full services and features on our website.</p>
|
2023-08-29 08:37:12 +00:00
|
|
|
|
2023-08-30 10:44:41 +00:00
|
|
|
<p>Thanks again for your trust and support. If you have any questions, please feel free to contact us.</p>
|
2023-08-29 08:26:06 +00:00
|
|
|
|
2023-08-30 10:44:41 +00:00
|
|
|
<p>We wish you a pleasant experience!</p>
|
2023-08-29 08:37:12 +00:00
|
|
|
|
2023-08-29 08:26:06 +00:00
|
|
|
</body>
|
|
|
|
</html>`
|
2023-08-29 08:35:02 +00:00
|
|
|
w.Write([]byte(successHtml))
|
|
|
|
httpx.Ok(w)
|
2023-08-29 08:26:06 +00:00
|
|
|
} else {
|
2023-08-29 09:14:20 +00:00
|
|
|
|
|
|
|
errorHtml := `
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
2023-08-30 10:44:41 +00:00
|
|
|
<title>Failed to register</title>
|
2023-08-29 09:14:20 +00:00
|
|
|
<style>
|
|
|
|
body {
|
|
|
|
font-family: sans-serif;
|
|
|
|
font-size: 16px;
|
|
|
|
}
|
|
|
|
|
|
|
|
h1 {
|
|
|
|
font-size: 24px;
|
|
|
|
color: blue;
|
|
|
|
}
|
|
|
|
|
|
|
|
@media screen and (max-width: 480px) {
|
|
|
|
body {
|
|
|
|
font-size: 14px;
|
|
|
|
}
|
|
|
|
|
|
|
|
h1 {
|
|
|
|
font-size: 18px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>%s</h1>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`
|
|
|
|
|
|
|
|
errorHtml = fmt.Sprintf(errorHtml, resp.Message)
|
|
|
|
w.Write([]byte(errorHtml))
|
|
|
|
httpx.Ok(w)
|
2023-08-29 08:26:06 +00:00
|
|
|
}
|
2023-08-29 08:24:20 +00:00
|
|
|
}
|