verification_code_rdb

package
v0.0.0-...-6a35cc3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 23, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UserIsValid                        = iota + 1
	InvalidTypeUnusedCodeTooMany       // 未核销的验证码过多(频繁请求验证码但不进行验证)
	InvalidTypeRequestTooFrequently    // 请求验证码过于频繁(短时间内连续多次请求验证码)
	InvalidTypeVerifyFailTooFrequently // 验证码核销失败过于频繁
)

Variables

This section is empty.

Functions

This section is empty.

Types

type InvalidType

type InvalidType int

InvalidType 违规类型

type VerificationCodeRdb

type VerificationCodeRdb struct {
	ModuleName string // 业务模块名称, 不同业务对应不同的名称,防止发生不同业务的数据碰撞(部分redis-key与该字段关联)

	VerificationCodeRdbInterface
	// contains filtered or unexported fields
}

VerificationCodeRdb 用于验证码相关服务的通用Rdb结构

func CreateVerificationCodeRdb

func CreateVerificationCodeRdb(rdb *redis.Client, moduleName string, strategy VerificationCodeServiceStrategy) (*VerificationCodeRdb, error)

CreateVerificationCodeRdb 创建用于验证码服务的Rdb

func (*VerificationCodeRdb) AddTemporarilyBanStrategy

func (r *VerificationCodeRdb) AddTemporarilyBanStrategy(threshold int, duration int64)

AddTemporarilyBanStrategy 添加临时封禁策略

func (VerificationCodeRdb) CheckIsRequestTooFrequently

func (r VerificationCodeRdb) CheckIsRequestTooFrequently(objName string) (bool, error)

CheckIsRequestTooFrequently 判断申请验证码是否过于频繁, threshold: 阈值(单位为秒) 若上一次请求的验证码尚未被核销,且当前时间距离上次请求的时间差小于等于阈值,则返回true. 一般在请求验证码前调用判断

func (VerificationCodeRdb) CheckIsUnusedCodeTooMany

func (r VerificationCodeRdb) CheckIsUnusedCodeTooMany(objName string) (bool, error)

CheckIsUnusedCodeTooMany 判断当日未使用的验证码是否过多(用于防止恶意刷接口) threshold: 阈值 一般在请求验证码和核销验证码前调用判断

func (VerificationCodeRdb) CheckIsVerifyFailTooFrequently

func (r VerificationCodeRdb) CheckIsVerifyFailTooFrequently(objName string) (bool, error)

CheckIsVerifyFailTooFrequently 判断用户是否验证错误过于频繁

func (*VerificationCodeRdb) DelTemporarilyBanStrategy

func (r *VerificationCodeRdb) DelTemporarilyBanStrategy(threshold int)

DelTemporarilyBanStrategy 删除临时封禁策略

func (*VerificationCodeRdb) ModifyDenyThresholdOfFailedCount

func (r *VerificationCodeRdb) ModifyDenyThresholdOfFailedCount(threshold int)

ModifyDenyThresholdOfFailedCount 修改因失败次数过多而拒绝请求的数量阈值

func (*VerificationCodeRdb) ModifyDenyThresholdOfUnusedCode

func (r *VerificationCodeRdb) ModifyDenyThresholdOfUnusedCode(threshold int)

ModifyDenyThresholdOfUnusedCode 修改因未核销的验证码数量过多而拒绝请求的数量阈值

func (*VerificationCodeRdb) ModifyRequestTimeIntervalThreshold

func (r *VerificationCodeRdb) ModifyRequestTimeIntervalThreshold(intervalThreshold int64)

ModifyRequestTimeIntervalThreshold 修改验证码请求间隔阈值

func (*VerificationCodeRdb) ModifyValidityDuration

func (r *VerificationCodeRdb) ModifyValidityDuration(duration int64) error

ModifyValidityDuration 修改临时封禁策略

func (VerificationCodeRdb) PreCheckBeforeSendVerificationCode

func (r VerificationCodeRdb) PreCheckBeforeSendVerificationCode(objName string) (it InvalidType, err error)

PreCheckBeforeSendVerificationCode 发送验证码前的校验(组合校验用户当前状态是否合法) 校验请求是否过于频繁、验证错误次数是否过多、未核销的验证码是否过多(是否频繁请求验证码但不进行验证)

func (VerificationCodeRdb) PreCheckBeforeVerifyAndUseVerificationCode

func (r VerificationCodeRdb) PreCheckBeforeVerifyAndUseVerificationCode(objName string) (it InvalidType, err error)

PreCheckBeforeVerifyAndUseVerificationCode 核销验证码前的校验(组合校验用户当前状态是否合法) 校验验证错误次数是否过多、未核销的验证码是否过多(是否频繁请求验证码但不进行验证)

func (VerificationCodeRdb) QueryCountOfUnusedVerificationCode

func (r VerificationCodeRdb) QueryCountOfUnusedVerificationCode(objName string) (int, error)

QueryCountOfUnusedVerificationCode 查询用户当日未核销的验证码数量

func (VerificationCodeRdb) QueryDenyThresholdOfFailedCount

func (r VerificationCodeRdb) QueryDenyThresholdOfFailedCount() int

QueryDenyThresholdOfFailedCount 查询因失败次数过多而拒绝请求的数量阈值

func (VerificationCodeRdb) QueryDenyThresholdOfUnusedCode

func (r VerificationCodeRdb) QueryDenyThresholdOfUnusedCode() int

QueryDenyThresholdOfUnusedCode 查询因未核销的验证码数量过多而拒绝请求的数量阈值

func (VerificationCodeRdb) QueryErrorsCountToday

func (r VerificationCodeRdb) QueryErrorsCountToday(objName string) (int, error)

QueryErrorsCountToday 查询用户当日失败的次数

func (VerificationCodeRdb) QueryLastErrorTime

func (r VerificationCodeRdb) QueryLastErrorTime(objName string) (exist bool, lastTime time.Time, err error)

QueryLastErrorTime 查询用户最后一次验证失败的时间

func (VerificationCodeRdb) QueryRequestTimeIntervalThreshold

func (r VerificationCodeRdb) QueryRequestTimeIntervalThreshold() int64

QueryRequestTimeIntervalThreshold 查询验证码请求间隔阈值

func (*VerificationCodeRdb) QueryTemporarilyBanStrategy

func (r *VerificationCodeRdb) QueryTemporarilyBanStrategy() *map[int]int64

QueryTemporarilyBanStrategy 查询临时封禁策略

func (VerificationCodeRdb) QueryValidityDuration

func (r VerificationCodeRdb) QueryValidityDuration() int64

QueryValidityDuration 查询验证码的默认有效期

func (VerificationCodeRdb) QueryVerificationCodeRegisteredPeriod

func (r VerificationCodeRdb) QueryVerificationCodeRegisteredPeriod(objName string) (invalid bool, period int64, err error)

QueryVerificationCodeRegisteredPeriod 获取验证码已等待核销的时长 invalid: 验证码是否已失效. 若invalid为true,则说明验证码已失效, period的大小无意义 period: 验证码已等待核销的时长, 单位为秒

func (VerificationCodeRdb) QueryVerificationCodeTTL

func (r VerificationCodeRdb) QueryVerificationCodeTTL(objName string) (int64, error)

QueryVerificationCodeTTL 查询验证码剩余的有效时长(单位为秒)

func (VerificationCodeRdb) SetAndRegisterVerificationCode

func (r VerificationCodeRdb) SetAndRegisterVerificationCode(objName string, verCode string) error

SetAndRegisterVerificationCode 添加并记录验证码(添加该用户的验证码缓存,并且向该用户未核销的验证码集合中添加该验证码)

func (VerificationCodeRdb) VerifyAndUseVerificationCode

func (r VerificationCodeRdb) VerifyAndUseVerificationCode(objName string, verCode string) (exist bool, success bool, err error)

VerifyAndUseVerificationCode 核销验证码

func (VerificationCodeRdb) VerifyConnection

func (r VerificationCodeRdb) VerifyConnection() (bool, error)

VerifyConnection 判断redis是否成功连接并可用(在执行关键步骤前应先调用本函数验证redis是否可用,避免无谓的资源消耗,包括但不限于验证码发送费用、服务端资源等)

type VerificationCodeRdbInterface

type VerificationCodeRdbInterface interface {
	base.RdbBaseInterface
	PreCheckBeforeSendVerificationCode(objName string) (it InvalidType, err error)
	SetAndRegisterVerificationCode(objName string, verCode string) error
	PreCheckBeforeVerifyAndUseVerificationCode(objName string) (it InvalidType, err error)
	VerifyAndUseVerificationCode(objName string, verCode string) (exist bool, success bool, err error)
	CheckIsUnusedCodeTooMany(objName string) (bool, error)
	CheckIsRequestTooFrequently(objName string) (bool, error)
	CheckIsVerifyFailTooFrequently(objName string) (bool, error)
	QueryErrorsCountToday(objName string) (int, error)
	QueryLastErrorTime(objName string) (exist bool, lastTime time.Time, err error)
	QueryCountOfUnusedVerificationCode(objName string) (int, error)
	QueryVerificationCodeTTL(objName string) (int64, error)
	QueryVerificationCodeRegisteredPeriod(objName string) (invalid bool, period int64, err error)
	// contains filtered or unexported methods
}

type VerificationCodeServiceStrategy

type VerificationCodeServiceStrategy struct {
	ValidityDuration             int64     // 验证码有效期时长(秒), 必须大于0
	RequestTimeIntervalThreshold int64     // 验证码请求间隔时长限制(秒),即两次获取验证码的时间差值下限. 不需要该项限制则填0
	DenyThresholdOfUnusedCode    int       // 单日未核销的验证码数量阈值,超过该值后禁止手机号使用短信验证码业务. 不需要该项限制则填0
	DenyThresholdOfFailedCount   int       // 单日验证错误次数阈值,高于该值后禁止手机号使用短信验证码业务. 不需要该项限制则填0
	TemporarilyBanStrategy       *sync.Map // 短暂禁止手机号使用短信验证码业务的策略,key:失败次数的阈值;value:禁止时长(秒), 详见CheckIsVerifyFailTooFrequently
	// contains filtered or unexported fields
}

VerificationCodeServiceStrategy 验证码服务策略

func CreateVerificationCodeServiceStrategy

func CreateVerificationCodeServiceStrategy(duration int64, intervalThreshold int64, unusedThreshold int,
	failThreshold int, tempBanStrategy *map[int]int64) (*VerificationCodeServiceStrategy, error)

func (*VerificationCodeServiceStrategy) AddTemporarilyBanStrategy

func (s *VerificationCodeServiceStrategy) AddTemporarilyBanStrategy(threshold int, duration int64)

AddTemporarilyBanStrategy 添加短期禁止策略

func (*VerificationCodeServiceStrategy) DelTemporarilyBanStrategy

func (s *VerificationCodeServiceStrategy) DelTemporarilyBanStrategy(threshold int)

DelTemporarilyBanStrategy 删除短期禁止策略

func (*VerificationCodeServiceStrategy) ModifyDenyThresholdOfFailedCount

func (s *VerificationCodeServiceStrategy) ModifyDenyThresholdOfFailedCount(threshold int)

ModifyDenyThresholdOfFailedCount 修改单日验证错误次数阈值

func (*VerificationCodeServiceStrategy) ModifyDenyThresholdOfUnusedCode

func (s *VerificationCodeServiceStrategy) ModifyDenyThresholdOfUnusedCode(threshold int)

ModifyDenyThresholdOfUnusedCode 修改单日未核销的验证码数量阈值

func (*VerificationCodeServiceStrategy) ModifyRequestTimeIntervalThreshold

func (s *VerificationCodeServiceStrategy) ModifyRequestTimeIntervalThreshold(intervalThreshold int64)

ModifyRequestTimeIntervalThreshold 修改验证码请求间隔时长限制

func (*VerificationCodeServiceStrategy) ModifyValidityDuration

func (s *VerificationCodeServiceStrategy) ModifyValidityDuration(duration int64) error

ModifyValidityDuration 修改验证码有效期时长

func (VerificationCodeServiceStrategy) QueryDenyThresholdOfFailedCount

func (s VerificationCodeServiceStrategy) QueryDenyThresholdOfFailedCount() int

func (VerificationCodeServiceStrategy) QueryDenyThresholdOfUnusedCode

func (s VerificationCodeServiceStrategy) QueryDenyThresholdOfUnusedCode() int

func (VerificationCodeServiceStrategy) QueryRequestTimeIntervalThreshold

func (s VerificationCodeServiceStrategy) QueryRequestTimeIntervalThreshold() int64

func (VerificationCodeServiceStrategy) QueryTemporarilyBanStrategy

func (s VerificationCodeServiceStrategy) QueryTemporarilyBanStrategy() *map[int]int64

func (VerificationCodeServiceStrategy) QueryValidityDuration

func (s VerificationCodeServiceStrategy) QueryValidityDuration() int64

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL