fix
This commit is contained in:
parent
397dda8ebf
commit
03bd7cd5eb
|
@ -156,7 +156,7 @@ func (l *UserEmailConfirmationLogic) UserEmailConfirmation(req *types.RequestEma
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if *user.PasswordHash != rt.OldPassword {
|
if *user.PasswordHash != rt.OldPassword {
|
||||||
return fmt.Errorf("password had beed updated")
|
return fmt.Errorf("password had been reset")
|
||||||
}
|
}
|
||||||
return tx.Update("PasswordHash", rt.NewPassword).Error
|
return tx.Update("PasswordHash", rt.NewPassword).Error
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,18 +1,17 @@
|
||||||
package logic
|
package logic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"fusenapi/model/gmodel"
|
|
||||||
"fusenapi/utils/auth"
|
"fusenapi/utils/auth"
|
||||||
"fusenapi/utils/basic"
|
"fusenapi/utils/basic"
|
||||||
|
"time"
|
||||||
|
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"fusenapi/server/auth/internal/svc"
|
"fusenapi/server/auth/internal/svc"
|
||||||
"fusenapi/server/auth/internal/types"
|
"fusenapi/server/auth/internal/types"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserResetPasswordLogic struct {
|
type UserResetPasswordLogic struct {
|
||||||
|
@ -37,38 +36,78 @@ func (l *UserResetPasswordLogic) UserResetPassword(req *types.RequestUserResetPa
|
||||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||||
// userinfo 传入值时, 一定不为null
|
// userinfo 传入值时, 一定不为null
|
||||||
|
|
||||||
if !userinfo.IsUser() {
|
user, err := l.svcCtx.AllModels.FsUser.FindUserByEmail(context.TODO(), req.Email)
|
||||||
return resp.SetStatus(basic.CodeUnAuth)
|
|
||||||
}
|
|
||||||
|
|
||||||
rt, err := l.svcCtx.ResetTokenManger.Decrypt(req.ResetToken)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logx.Error(err)
|
logx.Error(err)
|
||||||
return resp.SetStatus(basic.CodeOAuthResetTokenDecryptErr, err.Error())
|
return resp.SetStatus(basic.CodeRequestParamsErr, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: 存储
|
token := &auth.ResetToken{
|
||||||
if rt.OperateType != auth.OpTypeResetToken {
|
// 操作的类型, 验证的token 必须要继承这个
|
||||||
return resp.SetStatus(basic.CodeOAuthTypeErr, "error OperateType: rt.OperateType != auth.OpTypeResetToken")
|
OperateType: auth.OpTypeResetToken,
|
||||||
|
UserId: userinfo.UserId,
|
||||||
|
Wid: req.Wid,
|
||||||
|
Email: req.Email,
|
||||||
|
OldPassword: *user.PasswordHash,
|
||||||
|
TraceId: uuid.NewString(),
|
||||||
|
CreateAt: time.Now().UTC(),
|
||||||
}
|
}
|
||||||
|
|
||||||
err = l.svcCtx.AllModels.FsUser.Transaction(l.ctx, func(tx *gorm.DB) error {
|
clurl, err := l.svcCtx.ResetTokenManger.Encrypt(token)
|
||||||
user := &gmodel.FsUser{Id: int64(rt.UserId)}
|
|
||||||
err := tx.Take(user).Error
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if *user.PasswordHash != rt.OldPassword {
|
|
||||||
return fmt.Errorf("password had beed updated")
|
|
||||||
}
|
|
||||||
return tx.Update("PasswordHash", req.Password).Error
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return resp.SetStatus(basic.CodeDbSqlErr, err.Error())
|
logx.Error(err)
|
||||||
|
return resp.SetStatus(basic.CodeOAuthResetTokenEncryptErr, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
userName := *user.FirstName + " " + *user.LastName
|
||||||
|
// 进入发送邮箱的系统
|
||||||
|
EmailManager.EmailTasks <- &EmailFormat{
|
||||||
|
TemplateName: "reset_password.tpl",
|
||||||
|
UniqueKey: "register-" + req.Email,
|
||||||
|
TargetEmail: req.Email,
|
||||||
|
CompanyName: "fusen",
|
||||||
|
ConfirmationLink: clurl,
|
||||||
|
SenderName: "support@fusenpack.com",
|
||||||
|
SenderTitle: "register-valid",
|
||||||
|
Extend: map[string]string{
|
||||||
|
"UserName": userName,
|
||||||
|
},
|
||||||
|
} // email进入队
|
||||||
|
|
||||||
return resp.SetStatus(basic.CodeOK)
|
return resp.SetStatus(basic.CodeOK)
|
||||||
|
|
||||||
|
// if !userinfo.IsUser() {
|
||||||
|
// return resp.SetStatus(basic.CodeUnAuth)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// rt, err := l.svcCtx.ResetTokenManger.Decrypt(req.ResetToken)
|
||||||
|
// if err != nil {
|
||||||
|
// logx.Error(err)
|
||||||
|
// return resp.SetStatus(basic.CodeOAuthResetTokenDecryptErr, err.Error())
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // 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 {
|
||||||
|
// return fmt.Errorf("password had beed updated")
|
||||||
|
// }
|
||||||
|
// return tx.Update("PasswordHash", req.Password).Error
|
||||||
|
// })
|
||||||
|
|
||||||
|
// if err != nil {
|
||||||
|
// return resp.SetStatus(basic.CodeDbSqlErr, err.Error())
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return resp.SetStatus(basic.CodeOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||||
|
|
|
@ -35,11 +35,8 @@ func NewUserResetTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Us
|
||||||
func (l *UserResetTokenLogic) UserResetToken(req *types.RequestUserResetToken, userinfo *auth.UserInfo) (resp *basic.Response) {
|
func (l *UserResetTokenLogic) UserResetToken(req *types.RequestUserResetToken, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||||
// userinfo 传入值时, 一定不为null
|
// userinfo 传入值时, 一定不为null
|
||||||
if !userinfo.IsUser() {
|
|
||||||
return resp.SetStatus(basic.CodeUnAuth)
|
|
||||||
}
|
|
||||||
|
|
||||||
user, err := l.svcCtx.AllModels.FsUser.FindUserById(context.TODO(), userinfo.UserId)
|
user, err := l.svcCtx.AllModels.FsUser.FindUserByEmail(context.TODO(), req.Email)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logx.Error(err)
|
logx.Error(err)
|
||||||
return resp.SetStatus(basic.CodeRequestParamsErr, err.Error())
|
return resp.SetStatus(basic.CodeRequestParamsErr, err.Error())
|
||||||
|
@ -50,10 +47,10 @@ func (l *UserResetTokenLogic) UserResetToken(req *types.RequestUserResetToken, u
|
||||||
OperateType: auth.OpTypeResetToken,
|
OperateType: auth.OpTypeResetToken,
|
||||||
UserId: userinfo.UserId,
|
UserId: userinfo.UserId,
|
||||||
Wid: req.Wid,
|
Wid: req.Wid,
|
||||||
Email: *user.Email,
|
Email: req.Email,
|
||||||
OldPassword: *user.PasswordHash,
|
OldPassword: *user.PasswordHash,
|
||||||
TraceId: uuid.NewString(),
|
TraceId: uuid.NewString(),
|
||||||
CreateAt: time.Now(),
|
CreateAt: time.Now().UTC(),
|
||||||
}
|
}
|
||||||
|
|
||||||
rtoken, err := l.svcCtx.ResetTokenManger.Encrypt(token)
|
rtoken, err := l.svcCtx.ResetTokenManger.Encrypt(token)
|
||||||
|
|
|
@ -37,8 +37,8 @@ type DataResetToken struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type RequestUserResetPassword struct {
|
type RequestUserResetPassword struct {
|
||||||
ResetToken string `json:"reset_token"` // 附带重置token, 确保流程唯一
|
Wid string `json:"wid"`
|
||||||
Password string `json:"password"` // 附带的hash密码
|
Email string `json:"email"` // email
|
||||||
}
|
}
|
||||||
|
|
||||||
type RequestGoogleLogin struct {
|
type RequestGoogleLogin struct {
|
||||||
|
|
|
@ -138,7 +138,6 @@ func (l *InfoLogic) Info(req *types.UserInfoRequest, userinfo *auth.UserInfo) (r
|
||||||
raw := l.svcCtx.MysqlConn.Raw(sqlstr)
|
raw := l.svcCtx.MysqlConn.Raw(sqlstr)
|
||||||
|
|
||||||
if raw.Error != nil {
|
if raw.Error != nil {
|
||||||
|
|
||||||
if raw.Error == gorm.ErrRecordNotFound {
|
if raw.Error == gorm.ErrRecordNotFound {
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -90,8 +90,8 @@ type (
|
||||||
|
|
||||||
// RequestUserResetPassword 重置密码
|
// RequestUserResetPassword 重置密码
|
||||||
RequestUserResetPassword {
|
RequestUserResetPassword {
|
||||||
ResetToken string `json:"reset_token"` // 附带重置token, 确保流程唯一
|
Wid string `json:"wid"`
|
||||||
Password string `json:"password"` // 附带的hash密码
|
Email string `json:"email"` // email
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@ var FsPasser *passer.Passer[sm.Result] = func() *passer.Passer[sm.Result] {
|
||||||
userState := &UserState{
|
userState := &UserState{
|
||||||
UserId: cmd.UserId,
|
UserId: cmd.UserId,
|
||||||
PwdHash: auth.StringToHash(*user.PasswordHash),
|
PwdHash: auth.StringToHash(*user.PasswordHash),
|
||||||
UpdateAt: time.Now(),
|
UpdateAt: time.Now().UTC(),
|
||||||
}
|
}
|
||||||
s.store[cmd.UserId] = userState
|
s.store[cmd.UserId] = userState
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user