limit_verified

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2024 License: MIT Imports: 5 Imported by: 0

README

limit verified

验证码限制器

redis 存储格式:

global key:
keyPrefix:{target} ----> { sendCnt -- sendCnt, codeCnt -- codeCnt }
code key:
keyPrefix:{target}:_entry_:{kind} -----> { code -- code, quota -- quota, err -- err, lasted -- lasted }

sendCnt: 发送次数
codeCnt: code 发送次数
code: code 验证码
quota: code 错误次数限制
lasted: code 发送时间

Documentation

Index

Constants

View Source
const DefaultKind = "default"

Variables

View Source
var (
	// ErrUnknownCode is an error that represents unknown status code.
	ErrUnknownCode           = errors.New("limit: unknown status code")
	ErrMaxSendPerDay         = errors.New("limit: reach the maximum send times")
	ErrResendTooFrequently   = errors.New("limit: resend too frequently")
	ErrCodeRequiredOrExpired = errors.New("limit: code is required or expired")
	ErrCodeMaxErrorQuota     = errors.New("limit: over the maximum error quota")
	ErrCodeVerification      = errors.New("limit: code verified failed")
)

error defined for verified

Functions

This section is empty.

Types

type CodeParam

type CodeParam struct {
	Kind   string // optional, 默认为: DefaultKind
	Target string // required
	Code   string // required
	// contains filtered or unexported fields
}

type CodeParamOption

type CodeParamOption func(*CodeParam)

CodeParamOption LimitVerified code 选项

func WithAvailWindowSecond

func WithAvailWindowSecond(sec int) CodeParamOption

WithAvailWindowSecond 验证码有效窗口时间, 覆盖默认值, 单位: 秒

func WithMaxErrorQuota

func WithMaxErrorQuota(cnt int) CodeParamOption

WithMaxErrorQuota 验证码最大验证失败次数, 覆盖默认值

func WithResendIntervalSecond

func WithResendIntervalSecond(sec int) CodeParamOption

WithResendIntervalSecond 重发验证码间隔时间, 覆盖默认值, 单位: 秒

type DecrArgs

type DecrArgs struct {
	KeyPrefix string // 存验证码key的前缀
	Target    string // CodeParam.Target
}

type IncrArgs

type IncrArgs struct {
	KeyPrefix     string        // 存验证码key的前缀
	Target        string        // CodeParam.Target
	KeyExpires    time.Duration // 存验证码key的过期时间
	MaxSendPerDay int           // 限制一天最大发送次数(全局)
}

type LimitVerified

type LimitVerified[P LimitVerifiedProvider, S Storage] struct {
	// contains filtered or unexported fields
}

LimitVerified limit verified code

func NewLimitVerified

func NewLimitVerified[P LimitVerifiedProvider, S Storage](p P, store S, opts ...Option) *LimitVerified[P, S]

NewLimitVerified new a limit verified

func (*LimitVerified[P, S]) Decr

func (v *LimitVerified[P, S]) Decr(ctx context.Context, target string) error

Decr send cnt.

func (*LimitVerified[P, S]) Incr

func (v *LimitVerified[P, S]) Incr(ctx context.Context, target string) error

Incr send cnt.

func (*LimitVerified[P, S]) Name

func (v *LimitVerified[P, S]) Name() string

Name the provider name

func (*LimitVerified[P, S]) SendCode

func (v *LimitVerified[P, S]) SendCode(ctx context.Context, c CodeParam, opts ...CodeParamOption) error

SendCode send code and store.

func (*LimitVerified[P, S]) VerifyCode

func (v *LimitVerified[P, S]) VerifyCode(ctx context.Context, c CodeParam) error

VerifyCode verify code from cache.

type LimitVerifiedProvider

type LimitVerifiedProvider interface {
	Name() string
	SendCode(CodeParam) error
}

LimitVerifiedProvider the provider

type Option

type Option func(OptionSetter)

Option LimitVerified 选项

func WithCodeAvailWindowSecond

func WithCodeAvailWindowSecond(sec int) Option

WithCodeAvailWindowSecond 验证码有效窗口时间, 默认180, 单位: 秒

func WithCodeMaxErrorQuota

func WithCodeMaxErrorQuota(cnt int) Option

WithCodeMaxErrorQuota 验证码最大验证失败次数, 默认: 3

func WithCodeMaxSendPerDay

func WithCodeMaxSendPerDay(cnt int) Option

WithMaxSendPerDay 验证码限制一天最大发送次数(验证码全局), 默认: 10

func WithCodeResendIntervalSecond

func WithCodeResendIntervalSecond(sec int) Option

WithCodeResendIntervalSecond 验证码重发间隔时间, 默认60, 单位: 秒

func WithKeyExpires

func WithKeyExpires(expires time.Duration) Option

WithKeyExpires redis存验证码key的过期时间, 默认 24小时

func WithKeyPrefix

func WithKeyPrefix(k string) Option

WithKeyPrefix redis存验证码key的前缀, 默认 limit:verified:

func WithMaxSendPerDay

func WithMaxSendPerDay(cnt int) Option

WithMaxSendPerDay 限制一天最大发送次数(全局), 默认: 10

type OptionSetter added in v0.1.0

type OptionSetter interface {
	// contains filtered or unexported methods
}

OptionSetter option setter

type RollbackArgs

type RollbackArgs struct {
	KeyPrefix string // 存验证码key的前缀
	Kind      string // CodeParam.Kind
	Target    string // CodeParam.Target
	Code      string // CodeParam.Code
	NowSecond string // 当前时间, 秒
}

type Storage

type Storage interface {
	// Store code
	Store(context.Context, *StoreArgs) error
	// Rollback when store success but send code failed.
	Rollback(context.Context, *RollbackArgs) error
	// Verify code
	Verify(context.Context, *VerifyArgs) error
	// Incr send cnt.
	Incr(context.Context, *IncrArgs) error
	// Decr send cnt.
	Decr(context.Context, *DecrArgs) error
}

type StoreArgs

type StoreArgs struct {
	KeyPrefix                string        // 存验证码key的前缀
	Kind                     string        // CodeParam.Kind
	Target                   string        // CodeParam.Target
	KeyExpires               time.Duration // 存验证码key的过期时间
	MaxSendPerDay            int           // 限制一天最大发送次数(全局)
	Code                     string        // CodeParam.Code
	CodeMaxSendPerDay        int           // 验证码限制一天最大发送次数(验证码全局)
	CodeMaxErrorQuota        int           // 验证码最大验证失败次数
	CodeAvailWindowSecond    int           // 验证码有效窗口时间
	CodeResendIntervalSecond int           // 验证码重发间隔时间,
	NowSecond                string        // 当前时间, 秒
}

StoreArgs store arguments

type VerifyArgs

type VerifyArgs struct {
	KeyPrefix string // 存验证码key的前缀
	Kind      string // CodeParam.Kind
	Target    string // CodeParam.Target
	Code      string // CodeParam.Code
	NowSecond string // 当前时间, 秒
}

Directories

Path Synopsis
v8
v9

Jump to

Keyboard shortcuts

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