Skip to content

Commit 7f4112c

Browse files
committed
feat: 增加登录错误限制
1 parent ccd4790 commit 7f4112c

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

biz/application/basicuser/basic_user.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package basicuser
22

33
import (
44
"context"
5+
"fmt"
6+
"strconv"
57

68
model "github.com/xh-polaris/synapse/biz/api/model/basicuser"
79
"github.com/xh-polaris/synapse/biz/application/base/token"
@@ -10,9 +12,11 @@ import (
1012
"github.com/xh-polaris/synapse/biz/conf"
1113
"github.com/xh-polaris/synapse/biz/domain/basicuser/entity"
1214
basicuser "github.com/xh-polaris/synapse/biz/domain/basicuser/service"
15+
"github.com/xh-polaris/synapse/biz/infra/contract/risk"
1316
"github.com/xh-polaris/synapse/biz/infra/contract/sms"
1417
ctxcache "github.com/xh-polaris/synapse/biz/pkg/ctxcache/ctx_cache"
1518
"github.com/xh-polaris/synapse/biz/pkg/errorx"
19+
"github.com/xh-polaris/synapse/biz/pkg/logs"
1620
"github.com/xh-polaris/synapse/biz/types/cst"
1721
"github.com/xh-polaris/synapse/biz/types/errno"
1822
)
@@ -69,11 +73,23 @@ func (s *BasicUserService) RegisterNewBasicUser(ctx context.Context, req *model.
6973
}
7074

7175
func (s *BasicUserService) validPhoneVerify(ctx context.Context, app, phone, code string) error {
76+
// 判断是否到上限
77+
key := fmt.Sprintf("risk:login:passport:%s", phone)
78+
limit, _, err := risk.CheckUpperLimit(ctx, key, conf.GetConfig().Token.MaxInPeriod)
79+
if err != nil {
80+
return err
81+
}
82+
if limit { // 达到上限, 不允许校验
83+
return errorx.New(errno.TooOftenLoginError, errorx.KV("period", strconv.Itoa(conf.GetConfig().SMS.Period/60)))
84+
}
7285
ok, err := s.sms.Check(ctx, app, "passport", phone, code)
7386
if err != nil {
7487
return err
7588
}
7689
if !ok {
90+
if err = risk.AddOnce(ctx, key, conf.GetConfig().Token.Period); err != nil {
91+
logs.Errorf("record send verify err:%s", err)
92+
}
7793
return errorx.New(errno.ErrVerifyCode)
7894
}
7995
return err

biz/conf/token.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import (
77
)
88

99
type Token struct {
10-
PublicKey string
11-
SecretKey string
12-
Expire int64
10+
PublicKey string
11+
SecretKey string
12+
Expire int64
13+
Period int // 重试周期
14+
MaxInPeriod int // 重试周期内最多登录失败次数
1315
}
1416

1517
func GetSecretKey(sk string) (*rsa.PrivateKey, error) {

biz/domain/basicuser/service/basic_user_impl.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@ package service
22

33
import (
44
"context"
5+
"fmt"
6+
"strconv"
57
"time"
68

79
"github.com/bytedance/sonic"
10+
"github.com/xh-polaris/synapse/biz/conf"
811
"github.com/xh-polaris/synapse/biz/domain/basicuser/dal/model"
912
"github.com/xh-polaris/synapse/biz/domain/basicuser/entity"
1013
"github.com/xh-polaris/synapse/biz/domain/basicuser/repo"
1114
"github.com/xh-polaris/synapse/biz/infra/contract/id"
15+
"github.com/xh-polaris/synapse/biz/infra/contract/risk"
1216
"github.com/xh-polaris/synapse/biz/pkg/errorx"
1317
"github.com/xh-polaris/synapse/biz/pkg/lang/crypt"
18+
"github.com/xh-polaris/synapse/biz/pkg/logs"
1419
"github.com/xh-polaris/synapse/biz/types/cst"
1520
"github.com/xh-polaris/synapse/biz/types/errno"
1621
)
@@ -38,10 +43,21 @@ func (i *userImpl) LoginByPhone(ctx context.Context, requirePassword bool, phone
3843
return nil, errorx.New(errno.PhoneNotExisted)
3944
}
4045
if requirePassword {
46+
key := fmt.Sprintf("risk:login:passport:%s", phone)
47+
limit, _, err := risk.CheckUpperLimit(ctx, key, conf.GetConfig().Token.MaxInPeriod)
48+
if err != nil {
49+
return nil, err
50+
}
51+
if limit { // 达到上限, 不允许校验
52+
return nil, errorx.New(errno.TooOftenLoginError, errorx.KV("period", strconv.Itoa(conf.GetConfig().SMS.Period/60)))
53+
}
4154
if u.Password == nil || *u.Password == "" {
4255
return nil, errorx.New(errno.NoPassword)
4356
}
4457
if !crypt.Check(verify, *u.Password) {
58+
if err = risk.AddOnce(ctx, key, conf.GetConfig().Token.Period); err != nil {
59+
logs.Errorf("record send verify err:%s", err)
60+
}
4561
return nil, errorx.New(errno.ErrPassword)
4662
}
4763
}

biz/infra/impl/sms/safe_sms.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func NewSafeSMSProvider(provider sms.Provider, cacheCli cache.Cmdable) (*SafeSMS
2929
// Send 发送验证码
3030
func (s *SafeSMSProvider) Send(ctx context.Context, app, cause, phone string, param *sms.SMSParam) error {
3131
// 判断是否到上限
32-
key := fmt.Sprintf("risk:sendVerifyCode:%s:%s:%S" + app + cause + phone)
32+
key := fmt.Sprintf("risk:sendVerifyCode:%s:%s:%s", app, cause, phone)
3333
limit, _, err := risk.CheckUpperLimit(ctx, key, conf.GetConfig().SMS.MaxInPeriod)
3434
if err != nil {
3535
return err

biz/types/errno/passport.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const (
1515
ErrResetPassword = 200_000_008
1616
UnSupportThirdParty = 200_000_009
1717
ErrThirdPartyLogin = 200_000_0010
18+
TooOftenLoginError = 200_000_0011
1819
)
1920

2021
func init() {
@@ -65,8 +66,14 @@ func init() {
6566
)
6667
code.Register(UnSupportThirdParty,
6768
"unsupported third party",
68-
code.WithAffectStability(false))
69+
code.WithAffectStability(false),
70+
)
6971
code.Register(ErrThirdPartyLogin,
7072
"third party login failed",
71-
code.WithAffectStability(false))
73+
code.WithAffectStability(false),
74+
)
75+
code.Register(TooOftenLoginError,
76+
"登录失败次数过多, 请 {period} 分钟后再试",
77+
code.WithAffectStability(false),
78+
)
7279
}

0 commit comments

Comments
 (0)