From fd7cec8224d3f62ab815363b1d91c448fe4f9c15 Mon Sep 17 00:00:00 2001 From: Robin <7434053+warrior_of_light_robin@user.noreply.gitee.com> Date: Tue, 26 Nov 2024 14:14:36 +0800 Subject: [PATCH 1/8] 1 --- app/mix/service/logic/token.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/mix/service/logic/token.go b/app/mix/service/logic/token.go index 407fdc1f..50665272 100644 --- a/app/mix/service/logic/token.go +++ b/app/mix/service/logic/token.go @@ -2,6 +2,7 @@ package logic import ( "fmt" + "service/api/base" tokenproto "service/api/proto/token/proto" "service/app/mix/dao" "service/dbstruct" @@ -168,5 +169,11 @@ func (p *Token) OpVerifyValid(ctx *gin.Context, token *jwt.Token) (int64, error) return -1, fmt.Errorf("登录失效,请重新登录!") } + // 校验是否本人 + req := ctx.MustGet("client_req").(base.BaseRequestAccessible) + if list[1].Mid != req.GetBaseRequest().Mid { + return -1, fmt.Errorf("非法令牌!") + } + return tokenUuid, nil } From b06038458ecb61ca81cd2352196bc5a73fb0eb67 Mon Sep 17 00:00:00 2001 From: Robin <7434053+warrior_of_light_robin@user.noreply.gitee.com> Date: Tue, 26 Nov 2024 14:22:23 +0800 Subject: [PATCH 2/8] 1 --- app/mix/service/logic/token.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/mix/service/logic/token.go b/app/mix/service/logic/token.go index 50665272..024a82d2 100644 --- a/app/mix/service/logic/token.go +++ b/app/mix/service/logic/token.go @@ -171,7 +171,7 @@ func (p *Token) OpVerifyValid(ctx *gin.Context, token *jwt.Token) (int64, error) // 校验是否本人 req := ctx.MustGet("client_req").(base.BaseRequestAccessible) - if list[1].Mid != req.GetBaseRequest().Mid { + if list[0].Mid != req.GetBaseRequest().Mid { return -1, fmt.Errorf("非法令牌!") } From 340e6f400fda1a3331050c63b5529671ba31c18e Mon Sep 17 00:00:00 2001 From: Robin <7434053+warrior_of_light_robin@user.noreply.gitee.com> Date: Tue, 26 Nov 2024 15:02:20 +0800 Subject: [PATCH 3/8] by Robin at 20241126 --- api/consts/consts.go | 1 + app/mix/dao/mongo.go | 45 +++++++++++++++-- .../service/apiservice_business_validation.go | 10 +++- .../service/logic/veri_code_wrong_times.go | 49 +++++++++++++++++++ app/mix/service/utilservice.go | 43 ++++++++++++++++ dbstruct/veri_code_wrong_times.go | 6 +++ 6 files changed, 150 insertions(+), 4 deletions(-) create mode 100644 app/mix/service/logic/veri_code_wrong_times.go create mode 100644 dbstruct/veri_code_wrong_times.go diff --git a/api/consts/consts.go b/api/consts/consts.go index ee1836ea..959919ec 100644 --- a/api/consts/consts.go +++ b/api/consts/consts.go @@ -36,6 +36,7 @@ const ( // apollo_config const ( MaxPswdWrongTimesKey = "max_pswd_wrong_times" + MaxVeriCodeWrongTimesKey = "max_veri_code_wrong_times" MaxVeriCodeValidDurationKey = "max_veri_code_valid_duration" IosKey = "ios" AndroidKey = "android" diff --git a/app/mix/dao/mongo.go b/app/mix/dao/mongo.go index 8dccfe78..fdae8f3c 100644 --- a/app/mix/dao/mongo.go +++ b/app/mix/dao/mongo.go @@ -108,9 +108,10 @@ const ( COLImage = "image" COLVideo = "video" - DBLogin = "login" - COLLogin = "login" - COLLoginHis = "login_his" + DBLogin = "login" + COLLogin = "login" + COLLoginHis = "login_his" + COLVeriCodeWrongTimes = "veri_code_wrong_times" DBToken = "token" COLToken = "token" @@ -337,6 +338,11 @@ func (m *Mongo) getColLoginHis() *qmgo.Collection { return m.clientMix.Database(DBLogin).Collection(COLLoginHis) } +// 验证码错误频次表 +func (m *Mongo) getColVeriCodeWrongTimes() *qmgo.Collection { + return m.clientMix.Database(DBLogin).Collection(COLLoginHis) +} + // Token表 func (m *Mongo) getColToken() *qmgo.Collection { return m.clientMix.Database(DBToken).Collection(COLToken) @@ -6432,3 +6438,36 @@ func (m *Mongo) GetRavenIQTestVisitUV(ctx *gin.Context, req *Raven_IQ_test_visit return } + +// 验证码错误频次 +func (m *Mongo) GetAndUpdateVeriCodeWrongTimes(ctx *gin.Context, mid int64) (veriCodeWrongTimes *dbstruct.VeriCodeWrongTimes, err error) { + col := m.getColVeriCodeWrongTimes() + + change := qmgo.Change{ + Update: qmgo.M{"$inc": qmgo.M{"wrong_times": 1}}, + Upsert: true, + ReturnNew: false, + } + + veriCodeWrongTimesInstance := dbstruct.VeriCodeWrongTimes{} + if err = col.Find(ctx, qmgo.M{"_id": mid}).Apply(change, &veriCodeWrongTimesInstance); err != nil { + logger.Error("change error : %v", err) + return + } + + return &veriCodeWrongTimesInstance, err +} + +// 删除验证码错误频次表 +func (m *Mongo) DeleteVeriCodeWrongTimes(ctx *gin.Context, id int64) error { + col := m.getColVeriCodeWrongTimes() + err := col.RemoveId(ctx, id) + return err +} + +// 清空验证码错误频次表 +func (m *Mongo) ClearVeriCodeWrongTimes(ctx *gin.Context) error { + col := m.getColVeriCodeWrongTimes() + _, err := col.RemoveAll(ctx, qmgo.M{}) + return err +} diff --git a/app/mix/service/apiservice_business_validation.go b/app/mix/service/apiservice_business_validation.go index bf9963dc..9b558b84 100644 --- a/app/mix/service/apiservice_business_validation.go +++ b/app/mix/service/apiservice_business_validation.go @@ -111,7 +111,15 @@ func (s *Service) ApiLoginByVeriCodeBusinessValidate(ctx *gin.Context, req *logi Collect() ec, _ = resultList[0].(errcode.ErrCode) - // 2.如果错误码是登录信息不存在,则判断为首次登录,业务逻辑将创建用户信息 + // 2.验证码错误处理 + if ec == errcode.ErrCodeLoginWrongPswd { + login, _ := resultList[1].(*dbstruct.Login) + if err := _DefaultLogin.OpHandleWrongPswd(ctx, login); err != nil { + logger.Error("OpHandleWrongPswd failed, err : %v", err) + } + } + + // 3.如果错误码是登录信息不存在,则判断为首次登录,业务逻辑将创建用户信息 if ec == errcode.ErrCodeLoginNotExist { vericode, _ = resultList[3].(*dbstruct.VeriCode) return diff --git a/app/mix/service/logic/veri_code_wrong_times.go b/app/mix/service/logic/veri_code_wrong_times.go new file mode 100644 index 00000000..98b6f6d8 --- /dev/null +++ b/app/mix/service/logic/veri_code_wrong_times.go @@ -0,0 +1,49 @@ +package logic + +import ( + "service/app/mix/dao" + "service/dbstruct" + "service/library/logger" + + "github.com/gin-gonic/gin" +) + +type VeriCodeWrongTimes struct { + store *dao.Store +} + +func NewVeriCodeWrongTimes(store *dao.Store) (a *VeriCodeWrongTimes) { + a = &VeriCodeWrongTimes{ + store: store, + } + return +} + +func (p *VeriCodeWrongTimes) OpGetAndUpdate(ctx *gin.Context, mid int64) (*dbstruct.VeriCodeWrongTimes, error) { + + veriCodeWrongTimes, err := p.store.GetAndUpdateVeriCodeWrongTimes(ctx, mid) + if err != nil { + logger.Error("GetAndUpdateVeriCodeWrongTimes fail, err: %v", err) + return nil, err + } + + return veriCodeWrongTimes, nil +} + +func (p *VeriCodeWrongTimes) OpDelete(ctx *gin.Context, mid int64) error { + err := p.store.DeleteVeriCodeWrongTimes(ctx, mid) + if err != nil { + logger.Error("DeleteVeriCodeWrongTimes fail, err: %v", err) + return err + } + return nil +} + +func (p *VeriCodeWrongTimes) OpClear(ctx *gin.Context) error { + err := p.store.ClearVeriCodeWrongTimes(ctx) + if err != nil { + logger.Error("ClearVeriCodeWrongTimes fail, err: %v", err) + return err + } + return nil +} diff --git a/app/mix/service/utilservice.go b/app/mix/service/utilservice.go index 0c80440b..ce664c65 100644 --- a/app/mix/service/utilservice.go +++ b/app/mix/service/utilservice.go @@ -2208,3 +2208,46 @@ func (s *Service) UtilEncryptVideosForZoneMomentVOs(ctx *gin.Context, list []*zo } } } + +// 处理验证码错误 +func (p *Service) utilHandleWrongVeriCode(ctx *gin.Context, login *dbstruct.Login) error { + + //将验证码错误次数+1 + loginUpdate := &dbstruct.Login{ + Id: goproto.Int64(util.DerefInt64(login.Id)), + WrongPswdTimes: goproto.Int64(util.DerefInt64(login.WrongPswdTimes) + 1), + } + + //读取验证码最大错误次数 + maxVeriCodeWrongTimes, err := apollo.GetIntValue(consts.MaxVeriCodeWrongTimesKey, apollo.ApolloOpts().SetNamespace("application")) + if err != nil { + logger.Error("Apollo read failed : %v", err) + return err + } + + //若达到最大密码错误限制,锁定账户 + if util.DerefInt64(loginUpdate.WrongPswdTimes) == int64(maxVeriCodeWrongTimes) { + logger.Error("Verification cdoe wrong times have reached the limit, this account will be locked!") + loginUpdate.IsLocked = goproto.Int64(1) + } + + //更新账户 + if err := p.OpUpdate(ctx, &loginproto.OpUpdateReq{ + Login: loginUpdate, + }); err != nil { + logger.Error("Update wrong password times failed : %v", err) + return err + } + + return nil +} + +// 抹去密码错误次数 +func (p *Login) OpClearWrongPswdTimes(ctx *gin.Context) error { + err := p.store.ClearLoginWrongPswdTimes(ctx) + if err != nil { + logger.Error("Clear wrong password times failed : %v", err) + return err + } + return err +} diff --git a/dbstruct/veri_code_wrong_times.go b/dbstruct/veri_code_wrong_times.go new file mode 100644 index 00000000..2d3c84ad --- /dev/null +++ b/dbstruct/veri_code_wrong_times.go @@ -0,0 +1,6 @@ +package dbstruct + +type VeriCodeWrongTimes struct { + Id int64 `json:"id" bson:"_id"` //id,用户的mid + WrongTimes int64 `json:"wrong_times" bson:"wrong_times"` //错误次数 +} From 056df14b1c05e974d4797fcfd2c17a68df70d2d4 Mon Sep 17 00:00:00 2001 From: Robin <7434053+warrior_of_light_robin@user.noreply.gitee.com> Date: Tue, 26 Nov 2024 16:11:42 +0800 Subject: [PATCH 4/8] by Robin at 20241126 --- api/errcode/errcode.go | 10 +++++ app/mix/dao/mongo.go | 18 ++++++++ .../service/apiservice_business_validation.go | 35 +++++++-------- app/mix/service/business_validator/login.go | 40 ++++++++++++++++- .../service/logic/veri_code_wrong_times.go | 11 +++++ .../service/opservice_business_validation.go | 23 +++++----- app/mix/service/service.go | 1 + app/mix/service/utilservice.go | 43 ------------------- dbstruct/login.go | 7 +++ 9 files changed, 114 insertions(+), 74 deletions(-) diff --git a/api/errcode/errcode.go b/api/errcode/errcode.go index ee278698..4daf4307 100644 --- a/api/errcode/errcode.go +++ b/api/errcode/errcode.go @@ -251,6 +251,10 @@ var ErrCodeMsgMap = map[ErrCode]string{ ErrCodeEmailSrvFail: "电子邮件表服务错误", ErrCodeEmailNotExist: "电子邮件表不存在", + ErrCodeVeriCodeWrongTimesSrvFail: "验证码错误频次表服务错误", + ErrCodeVeriCodeWrongTimesNotExist: "验证码错误频次表不存在", + ErrCodeVeriCodeWrongTimesReachedDailyUpperbound: "账户已锁定,请明日再试", + ErrCodeRavenIQTestSrvFail: "瑞文智商测试表服务错误", ErrCodeRavenIQTestNotExist: "瑞文智商测试表不存在", ErrCodeRavenIQTestQuestionNotExist: "瑞文智商测试表题目不存在", @@ -599,6 +603,12 @@ const ( ErrCodeEmailSrvFail ErrCode = -45001 // 电子邮件表服务错误 ErrCodeEmailNotExist ErrCode = -45002 // 电子邮件表不存在 + // MomentCreateTimes: 49xxx + ErrCodeVeriCodeWrongTimesSrvOk ErrCode = ErrCodeOk + ErrCodeVeriCodeWrongTimesSrvFail ErrCode = -49001 // 验证码错误频次表服务错误 + ErrCodeVeriCodeWrongTimesNotExist ErrCode = -49002 // 验证码错误频次表不存在 + ErrCodeVeriCodeWrongTimesReachedDailyUpperbound ErrCode = -49003 // 验证码错误次数已达每日上限 + // Media: 60xxx ErrCodeMediaSrvOk ErrCode = ErrCodeOk ErrCodeMediaSrvFail ErrCode = -60001 // 媒体服务错误 diff --git a/app/mix/dao/mongo.go b/app/mix/dao/mongo.go index fdae8f3c..565c2476 100644 --- a/app/mix/dao/mongo.go +++ b/app/mix/dao/mongo.go @@ -6458,6 +6458,24 @@ func (m *Mongo) GetAndUpdateVeriCodeWrongTimes(ctx *gin.Context, mid int64) (ver return &veriCodeWrongTimesInstance, err } +// 验证码错误频次 +func (m *Mongo) GetVeriCodeWrongTimes(ctx *gin.Context, id int64) (*dbstruct.VeriCodeWrongTimes, error) { + veriCodeWrongTimes := &dbstruct.VeriCodeWrongTimes{} + + col := m.getColVeriCodeWrongTimes() + query := qmgo.M{ + "_id": id, + } + + err := col.Find(ctx, query).One(veriCodeWrongTimes) + if err == qmgo.ErrNoSuchDocuments { + err = nil + return nil, err + } + + return veriCodeWrongTimes, err +} + // 删除验证码错误频次表 func (m *Mongo) DeleteVeriCodeWrongTimes(ctx *gin.Context, id int64) error { col := m.getColVeriCodeWrongTimes() diff --git a/app/mix/service/apiservice_business_validation.go b/app/mix/service/apiservice_business_validation.go index 9b558b84..bd54af06 100644 --- a/app/mix/service/apiservice_business_validation.go +++ b/app/mix/service/apiservice_business_validation.go @@ -97,11 +97,12 @@ func (s *Service) ApiLoginByVeriCodeBusinessValidate(ctx *gin.Context, req *logi // 1.业务校验 req.CalcPhoneHash() //计算手机号哈希 resultList := businessvalidator.NewLoginBusinessValidator(ctx, req). + QueryLogin(_DefaultLogin.OpListByPhoneHash). + EnsureVeriCodeWrongTimesNotReachedDailyUpperbound(_DefaultVeriCodeWrongTimes.OpGet). QueryVeriCode(_DefaultVeriCode.OpListByPhoneHash). EnsureVeriCodeExist(). - EnsureVeriCodeIsCorrect(). + EnsureVeriCodeIsCorrect(_DefaultVeriCodeWrongTimes.OpGetAndUpdate). EnsureVeriCodeIsValid(). - QueryLogin(_DefaultLogin.OpListByPhoneHash). EnsureLoginExist(). //EnsureLoginAcctNotLocked(). //验证码登录不校验是否爆破登录 EnsureLoginAcctNotBanned(). @@ -111,15 +112,7 @@ func (s *Service) ApiLoginByVeriCodeBusinessValidate(ctx *gin.Context, req *logi Collect() ec, _ = resultList[0].(errcode.ErrCode) - // 2.验证码错误处理 - if ec == errcode.ErrCodeLoginWrongPswd { - login, _ := resultList[1].(*dbstruct.Login) - if err := _DefaultLogin.OpHandleWrongPswd(ctx, login); err != nil { - logger.Error("OpHandleWrongPswd failed, err : %v", err) - } - } - - // 3.如果错误码是登录信息不存在,则判断为首次登录,业务逻辑将创建用户信息 + // 2.如果错误码是登录信息不存在,则判断为首次登录,业务逻辑将创建用户信息 if ec == errcode.ErrCodeLoginNotExist { vericode, _ = resultList[3].(*dbstruct.VeriCode) return @@ -169,12 +162,13 @@ func (s *Service) ApiResetPasswordBusinessValidate(ctx *gin.Context, req *loginp // 1.业务校验 req.CalcPhoneHash() //计算手机号哈希 resultList := businessvalidator.NewLoginBusinessValidator(ctx, req). - QueryVeriCode(_DefaultVeriCode.OpListByPhoneHash). - EnsureVeriCodeExist(). - EnsureVeriCodeIsCorrect(). - EnsureVeriCodeIsValid(). QueryLogin(_DefaultLogin.OpListByPhoneHash). EnsureLoginExist(). + EnsureVeriCodeWrongTimesNotReachedDailyUpperbound(_DefaultVeriCodeWrongTimes.OpGet). + QueryVeriCode(_DefaultVeriCode.OpListByPhoneHash). + EnsureVeriCodeExist(). + EnsureVeriCodeIsCorrect(_DefaultVeriCodeWrongTimes.OpGetAndUpdate). + EnsureVeriCodeIsValid(). EnsureLoginAcctEnabled(). EnsureLoginAcctNotBanned(). EnsureNewPasswordIsChanged(). @@ -203,12 +197,13 @@ func (s *Service) ApiUpdatePasswordBusinessValidate(ctx *gin.Context, req *login // 1.业务校验 req.CalcPhoneHash() //计算手机号哈希 resultList := businessvalidator.NewLoginBusinessValidator(ctx, req). - QueryVeriCode(_DefaultVeriCode.OpListByPhoneHash). - EnsureVeriCodeExist(). - EnsureVeriCodeIsCorrect(). - EnsureVeriCodeIsValid(). QueryLogin(_DefaultLogin.OpListByPhoneHash). EnsureLoginExist(). + EnsureVeriCodeWrongTimesNotReachedDailyUpperbound(_DefaultVeriCodeWrongTimes.OpGet). + QueryVeriCode(_DefaultVeriCode.OpListByPhoneHash). + EnsureVeriCodeExist(). + EnsureVeriCodeIsCorrect(_DefaultVeriCodeWrongTimes.OpGetAndUpdate). + EnsureVeriCodeIsValid(). EnsureLoginAcctEnabled(). EnsureLoginAcctNotLocked(). EnsureLoginAcctNotBanned(). @@ -709,7 +704,7 @@ func (s *Service) ApiCreateZoneThirdPartnerBusinessValidate(ctx *gin.Context, re resultList := businessvalidator.NewLoginBusinessValidator(ctx, req). QueryVeriCode(_DefaultVeriCode.OpListByPhoneHash). EnsureVeriCodeExist(). - EnsureVeriCodeIsCorrect(). + EnsureVeriCodeIsCorrect(_DefaultVeriCodeWrongTimes.OpGetAndUpdate). EnsureVeriCodeIsValid(). Validate(). Collect() diff --git a/app/mix/service/business_validator/login.go b/app/mix/service/business_validator/login.go index 30fd75c1..af3c5533 100644 --- a/app/mix/service/business_validator/login.go +++ b/app/mix/service/business_validator/login.go @@ -107,6 +107,35 @@ func (l *LoginBusinessValidator) EnsureLoginAcctNotBanned() *LoginBusinessValida return l } +func (l *LoginBusinessValidator) EnsureVeriCodeWrongTimesNotReachedDailyUpperbound(fun func(*gin.Context, int64) (*dbstruct.VeriCodeWrongTimes, error)) *LoginBusinessValidator { + l.oplist = append(l.oplist, func() { + + if l.login != nil { + // 读取每日错误上限 + maxDailyWrongTimes, err := apollo.GetIntValue(consts.MaxVeriCodeWrongTimesKey, apollo.ApolloOpts().SetNamespace("application")) + if err != nil { + logger.Error("Apollo read failed : %v", err) + l.ec = errcode.ErrCodeApolloReadFail + return + } + + wrongTimes, err := fun(l.ctx, l.login.GetMid()) + if err != nil { + l.ec = errcode.ErrCodeVeriCodeWrongTimesSrvFail + return + } + + if wrongTimes.WrongTimes >= int64(maxDailyWrongTimes) { + logger.Error("wrongs times of verification code of this mid has reached its daily upperbound") + l.ec = errcode.ErrCodeVeriCodeWrongTimesReachedDailyUpperbound + return + } + } + + }) + return l +} + func (l *LoginBusinessValidator) EnsurePasswordIsCorrect() *LoginBusinessValidator { l.oplist = append(l.oplist, func() { if l.passwordAccessor.GetPassword() != util.DerefString(l.login.Password) { @@ -194,10 +223,19 @@ func (l *LoginBusinessValidator) EnsureVeriCodeExist() *LoginBusinessValidator { return l } -func (l *LoginBusinessValidator) EnsureVeriCodeIsCorrect() *LoginBusinessValidator { +func (l *LoginBusinessValidator) EnsureVeriCodeIsCorrect(fun func(*gin.Context, int64) (*dbstruct.VeriCodeWrongTimes, error)) *LoginBusinessValidator { l.oplist = append(l.oplist, func() { if l.vericode.VeriCode != l.vericodeAccessor.GetVeriCode() { logger.Error("Wrong verification code") + + // 如果不是首次登录,则错误次数+1 + if l.login != nil { + _, err := fun(l.ctx, l.login.GetMid()) + if err != nil { + logger.Error("GetAndUpdateVeriCodeWrongTimes failed, err :%v", err) + } + } + l.ec = errcode.ErrCodeLoginWrongVeriCode return } diff --git a/app/mix/service/logic/veri_code_wrong_times.go b/app/mix/service/logic/veri_code_wrong_times.go index 98b6f6d8..18c7d43c 100644 --- a/app/mix/service/logic/veri_code_wrong_times.go +++ b/app/mix/service/logic/veri_code_wrong_times.go @@ -19,6 +19,17 @@ func NewVeriCodeWrongTimes(store *dao.Store) (a *VeriCodeWrongTimes) { return } +func (p *VeriCodeWrongTimes) OpGet(ctx *gin.Context, mid int64) (*dbstruct.VeriCodeWrongTimes, error) { + + veriCodeWrongTimes, err := p.store.GetVeriCodeWrongTimes(ctx, mid) + if err != nil { + logger.Error("GetVeriCodeWrongTimes fail, err: %v", err) + return nil, err + } + + return veriCodeWrongTimes, nil +} + func (p *VeriCodeWrongTimes) OpGetAndUpdate(ctx *gin.Context, mid int64) (*dbstruct.VeriCodeWrongTimes, error) { veriCodeWrongTimes, err := p.store.GetAndUpdateVeriCodeWrongTimes(ctx, mid) diff --git a/app/mix/service/opservice_business_validation.go b/app/mix/service/opservice_business_validation.go index 86b69fa8..4f2948fe 100644 --- a/app/mix/service/opservice_business_validation.go +++ b/app/mix/service/opservice_business_validation.go @@ -106,11 +106,12 @@ func (s *Service) OpLoginByVeriCodeBusinessValidate(ctx *gin.Context, req *login // 1.业务校验 req.CalcPhoneHash() //计算手机号哈希 resultList := businessvalidator.NewLoginBusinessValidator(ctx, req). + QueryLogin(_DefaultLogin.OpListByPhoneHash). + EnsureVeriCodeWrongTimesNotReachedDailyUpperbound(_DefaultVeriCodeWrongTimes.OpGet). QueryVeriCode(_DefaultVeriCode.OpListByPhoneHash). EnsureVeriCodeExist(). - EnsureVeriCodeIsCorrect(). + EnsureVeriCodeIsCorrect(_DefaultVeriCodeWrongTimes.OpGetAndUpdate). EnsureVeriCodeIsValid(). - QueryLogin(_DefaultLogin.OpListByPhoneHash). EnsureLoginExist(). EnsureLoginAcctNotLocked(). EnsureLoginAcctNotBanned(). @@ -172,12 +173,13 @@ func (s *Service) OpResetPasswordBusinessValidate(ctx *gin.Context, req *loginpr // 1.业务校验 req.CalcPhoneHash() //计算手机号哈希 resultList := businessvalidator.NewLoginBusinessValidator(ctx, req). - QueryVeriCode(_DefaultVeriCode.OpListByPhoneHash). - EnsureVeriCodeExist(). - EnsureVeriCodeIsCorrect(). - EnsureVeriCodeIsValid(). QueryLogin(_DefaultLogin.OpListByPhoneHash). EnsureLoginExist(). + EnsureVeriCodeWrongTimesNotReachedDailyUpperbound(_DefaultVeriCodeWrongTimes.OpGet). + QueryVeriCode(_DefaultVeriCode.OpListByPhoneHash). + EnsureVeriCodeExist(). + EnsureVeriCodeIsCorrect(_DefaultVeriCodeWrongTimes.OpGetAndUpdate). + EnsureVeriCodeIsValid(). EnsureLoginAcctEnabled(). EnsureLoginAcctNotBanned(). EnsureNewPasswordIsChanged(). @@ -207,12 +209,13 @@ func (s *Service) OpUpdatePasswordBusinessValidate(ctx *gin.Context, req *loginp // 1.业务校验 req.CalcPhoneHash() //计算手机号哈希 resultList := businessvalidator.NewLoginBusinessValidator(ctx, req). - QueryVeriCode(_DefaultVeriCode.OpListByPhoneHash). - EnsureVeriCodeExist(). - EnsureVeriCodeIsCorrect(). - EnsureVeriCodeIsValid(). QueryLogin(_DefaultLogin.OpListByPhoneHash). EnsureLoginExist(). + EnsureVeriCodeWrongTimesNotReachedDailyUpperbound(_DefaultVeriCodeWrongTimes.OpGet). + QueryVeriCode(_DefaultVeriCode.OpListByPhoneHash). + EnsureVeriCodeExist(). + EnsureVeriCodeIsCorrect(_DefaultVeriCodeWrongTimes.OpGetAndUpdate). + EnsureVeriCodeIsValid(). EnsureLoginAcctEnabled(). EnsureLoginAcctNotLocked(). EnsureLoginAcctNotBanned(). diff --git a/app/mix/service/service.go b/app/mix/service/service.go index 60a5b403..4bd89dce 100644 --- a/app/mix/service/service.go +++ b/app/mix/service/service.go @@ -154,6 +154,7 @@ var ( _DefaultEmail *logic.Email _DefaultRavenIQTest *logic.RavenIQTest _DefaultRavenIQTestVisit *logic.RavenIQTestVisit + _DefaultVeriCodeWrongTimes *logic.VeriCodeWrongTimes _DefaultStreamerDecrtByEs *logic.StreamerDecrtByEs _DefaultZoneDecrtByEs *logic.ZoneDecrtByEs diff --git a/app/mix/service/utilservice.go b/app/mix/service/utilservice.go index ce664c65..0c80440b 100644 --- a/app/mix/service/utilservice.go +++ b/app/mix/service/utilservice.go @@ -2208,46 +2208,3 @@ func (s *Service) UtilEncryptVideosForZoneMomentVOs(ctx *gin.Context, list []*zo } } } - -// 处理验证码错误 -func (p *Service) utilHandleWrongVeriCode(ctx *gin.Context, login *dbstruct.Login) error { - - //将验证码错误次数+1 - loginUpdate := &dbstruct.Login{ - Id: goproto.Int64(util.DerefInt64(login.Id)), - WrongPswdTimes: goproto.Int64(util.DerefInt64(login.WrongPswdTimes) + 1), - } - - //读取验证码最大错误次数 - maxVeriCodeWrongTimes, err := apollo.GetIntValue(consts.MaxVeriCodeWrongTimesKey, apollo.ApolloOpts().SetNamespace("application")) - if err != nil { - logger.Error("Apollo read failed : %v", err) - return err - } - - //若达到最大密码错误限制,锁定账户 - if util.DerefInt64(loginUpdate.WrongPswdTimes) == int64(maxVeriCodeWrongTimes) { - logger.Error("Verification cdoe wrong times have reached the limit, this account will be locked!") - loginUpdate.IsLocked = goproto.Int64(1) - } - - //更新账户 - if err := p.OpUpdate(ctx, &loginproto.OpUpdateReq{ - Login: loginUpdate, - }); err != nil { - logger.Error("Update wrong password times failed : %v", err) - return err - } - - return nil -} - -// 抹去密码错误次数 -func (p *Login) OpClearWrongPswdTimes(ctx *gin.Context) error { - err := p.store.ClearLoginWrongPswdTimes(ctx) - if err != nil { - logger.Error("Clear wrong password times failed : %v", err) - return err - } - return err -} diff --git a/dbstruct/login.go b/dbstruct/login.go index f66915be..8ee84be4 100644 --- a/dbstruct/login.go +++ b/dbstruct/login.go @@ -16,3 +16,10 @@ type Login struct { Ut *int64 `json:"ut" bson:"ut"` // 更新时间 DelFlag *int64 `json:"del_flag" bson:"del_flag"` // 删除标记,0-否,1-是 } + +func (p *Login) GetMid() int64 { + if p == nil || p.Mid == nil { + return -1 + } + return *p.Mid +} From f843490b0f5300ee3f1f568d39ea7c9f91df4f83 Mon Sep 17 00:00:00 2001 From: Robin <7434053+warrior_of_light_robin@user.noreply.gitee.com> Date: Tue, 26 Nov 2024 16:14:20 +0800 Subject: [PATCH 5/8] by Robin at 20241126 --- app/mix/service/cronservice.go | 1 + app/mix/service/xxljob_tasks.go | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/app/mix/service/cronservice.go b/app/mix/service/cronservice.go index 1f8bbc4b..5119a304 100644 --- a/app/mix/service/cronservice.go +++ b/app/mix/service/cronservice.go @@ -86,6 +86,7 @@ func (s *CronService) Init(c any) (exec xxl.Executor, err error) { exec.RegTask("clear_expired_btcb", s.ClearExpiredBtcb) exec.RegTask("reload_blocked_from_being_searched_list", s.ReloadBlockedFromBeingSearchedList) exec.RegTask("clear_auto_response_create_times", s.ClearAutoResponseCreateTimes) + exec.RegTask("clear_veri_code_wrong_times", s.ClearVeriCodeWrongTimes) exec.LogHandler(customLogHandle) //注册任务handler diff --git a/app/mix/service/xxljob_tasks.go b/app/mix/service/xxljob_tasks.go index 3d2d4bd7..56b26e78 100644 --- a/app/mix/service/xxljob_tasks.go +++ b/app/mix/service/xxljob_tasks.go @@ -607,3 +607,14 @@ func (s *CronService) ClearAutoResponseCreateTimes(ctx context.Context, param *x logger.Info("auto_response_create_times collection has been cleared") return "auto_response_create_times collection has been cleared" } + +func (s *CronService) ClearVeriCodeWrongTimes(ctx context.Context, param *xxl.RunReq) (msg string) { + logger.Info("task %v param: %v log_id: %v", param.ExecutorHandler, param.ExecutorParams, xxl.Int64ToStr(param.LogID)) + logger.Info("Clearing vericode_wrong_times collection...") + if err := _DefaultVeriCodeWrongTimes.OpClear(&gin.Context{}); err != nil { + logger.Error("Clear vericode_wrong_times collection fail: %v", err) + return fmt.Sprintf("Clear vericode_wrong_times collection fail: %v", err) + } + logger.Info("vericode_wrong_times collection has been cleared") + return "vericode_wrong_times collection has been cleared" +} From a91904f18f7788e183b2d939c2e854e4abca988a Mon Sep 17 00:00:00 2001 From: Robin <7434053+warrior_of_light_robin@user.noreply.gitee.com> Date: Tue, 26 Nov 2024 16:28:43 +0800 Subject: [PATCH 6/8] by Robin at 20241126 --- app/mix/service/business_validator/login.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/mix/service/business_validator/login.go b/app/mix/service/business_validator/login.go index af3c5533..e5e6d26c 100644 --- a/app/mix/service/business_validator/login.go +++ b/app/mix/service/business_validator/login.go @@ -124,6 +124,9 @@ func (l *LoginBusinessValidator) EnsureVeriCodeWrongTimesNotReachedDailyUpperbou l.ec = errcode.ErrCodeVeriCodeWrongTimesSrvFail return } + if wrongTimes == nil { + return + } if wrongTimes.WrongTimes >= int64(maxDailyWrongTimes) { logger.Error("wrongs times of verification code of this mid has reached its daily upperbound") From 4a44dd5646e4951ee7a0755be3b9069f44d94973 Mon Sep 17 00:00:00 2001 From: Robin <7434053+warrior_of_light_robin@user.noreply.gitee.com> Date: Tue, 26 Nov 2024 16:28:46 +0800 Subject: [PATCH 7/8] 1 --- app/mix/service/business_validator/login.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/app/mix/service/business_validator/login.go b/app/mix/service/business_validator/login.go index e5e6d26c..7040e81e 100644 --- a/app/mix/service/business_validator/login.go +++ b/app/mix/service/business_validator/login.go @@ -111,13 +111,6 @@ func (l *LoginBusinessValidator) EnsureVeriCodeWrongTimesNotReachedDailyUpperbou l.oplist = append(l.oplist, func() { if l.login != nil { - // 读取每日错误上限 - maxDailyWrongTimes, err := apollo.GetIntValue(consts.MaxVeriCodeWrongTimesKey, apollo.ApolloOpts().SetNamespace("application")) - if err != nil { - logger.Error("Apollo read failed : %v", err) - l.ec = errcode.ErrCodeApolloReadFail - return - } wrongTimes, err := fun(l.ctx, l.login.GetMid()) if err != nil { @@ -128,6 +121,14 @@ func (l *LoginBusinessValidator) EnsureVeriCodeWrongTimesNotReachedDailyUpperbou return } + // 读取每日错误上限 + maxDailyWrongTimes, err := apollo.GetIntValue(consts.MaxVeriCodeWrongTimesKey, apollo.ApolloOpts().SetNamespace("application")) + if err != nil { + logger.Error("Apollo read failed : %v", err) + l.ec = errcode.ErrCodeApolloReadFail + return + } + if wrongTimes.WrongTimes >= int64(maxDailyWrongTimes) { logger.Error("wrongs times of verification code of this mid has reached its daily upperbound") l.ec = errcode.ErrCodeVeriCodeWrongTimesReachedDailyUpperbound From 1a83ded7d1bdea321526f19d967252095e565cc1 Mon Sep 17 00:00:00 2001 From: Robin <7434053+warrior_of_light_robin@user.noreply.gitee.com> Date: Tue, 26 Nov 2024 16:52:21 +0800 Subject: [PATCH 8/8] 1 --- app/mix/service/service.go | 1 + 1 file changed, 1 insertion(+) diff --git a/app/mix/service/service.go b/app/mix/service/service.go index 4bd89dce..c09aabd4 100644 --- a/app/mix/service/service.go +++ b/app/mix/service/service.go @@ -261,6 +261,7 @@ func (s *Service) Init(c any) (err error) { _DefaultEmail = logic.NewEmail(store) _DefaultRavenIQTest = logic.NewRavenIQTest(store) _DefaultRavenIQTestVisit = logic.NewRavenIQTestVisit(store) + _DefaultVeriCodeWrongTimes = logic.NewVeriCodeWrongTimes(store) _DefaultVas = logic.NewVas(store, _DefaultStreamer, _DefaultAccount, _DefaultZone, _DefaultZoneThirdPartner, _DefaultZoneCollaborator) _DefaultStreamerAcct = logic.NewStreamerAcct(store)