limits

package
v0.0.0-...-4b98c26 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2021 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrRateLimitExceeded = errors.New("Rate limit exceeded")

ErrRateLimitExceeded is the error returned when the limit was already reached before the check.

View Source
var ErrRateLimitReached = errors.New("Rate limit reached")

ErrRateLimitReached is the error returned when we were under the limit before the check, and reach the limit.

Functions

func CheckRateLimit

func CheckRateLimit(p prefixer.Prefixer, ct CounterType) error

CheckRateLimit returns an error if the counter for the given type and instance has reached the limit.

func CheckRateLimitKey

func CheckRateLimitKey(customKey string, ct CounterType) error

CheckRateLimitKey allows to check the rate-limit for a key

func GetMaximumLimit

func GetMaximumLimit(ct CounterType) int64

GetMaximumLimit returns the limit of a CounterType

func IsLimitReachedOrExceeded

func IsLimitReachedOrExceeded(err error) bool

IsLimitReachedOrExceeded return true if the limit has been reached or exceeded, false otherwise.

func ResetCounter

func ResetCounter(p prefixer.Prefixer, ct CounterType)

ResetCounter sets again to zero the counter for the given type and instance.

func SetMaximumLimit

func SetMaximumLimit(ct CounterType, newLimit int64)

SetMaximumLimit sets a new limit for a CounterType

Types

type Counter

type Counter interface {
	Increment(key string, timeLimit time.Duration) (int64, error)
	Reset(key string) error
}

Counter is an interface for counting number of attempts that can be used to rate limit the number of logins and 2FA tries, and thus block bruteforce attacks.

func NewMemCounter

func NewMemCounter() Counter

NewMemCounter returns a in-memory counter.

func NewRedisCounter

func NewRedisCounter(client redis.UniversalClient) Counter

NewRedisCounter returns a counter that can be mutualized between several cozy-stack processes by using redis.

type CounterType

type CounterType int

CounterType os an enum for the type of counters used by rate-limiting.

const (
	// AuthType is used for counting the number of login attempts.
	AuthType CounterType = iota
	// TwoFactorGenerationType is used for counting the number of times a 2FA
	// is generated.
	TwoFactorGenerationType
	// TwoFactorType is used for counting the number of 2FA attempts.
	TwoFactorType
	// OAuthClientType is used for counting the number of OAuth clients.
	// creations/updates.
	OAuthClientType
	// SharingInviteType is used for counting the number of sharing invitations
	// sent to a given instance.
	SharingInviteType
	// SharingPublicLinkType is used for counting the number of public sharing
	// link consultations
	SharingPublicLinkType
	// JobThumbnailType is used for counting the number of thumbnail jobs
	// executed by an instance
	JobThumbnailType
	// JobShareTrackType is used for counting the number of updates of the
	// io.cozy.shared database
	JobShareTrackType
	// JobShareReplicateType is used for counting the number of replications
	JobShareReplicateType
	// JobShareUploadType is used for counting the file uploads
	JobShareUploadType
	// JobKonnectorType is used for counting the number of konnector executions
	JobKonnectorType
	// JobZipType is used for cozies exports
	JobZipType
	// JobSendMailType is used for mail sending
	JobSendMailType
	// JobServiceType is used for generic services
	// Ex: categorization or matching for banking
	JobServiceType
	// JobNotificationType is used for mobile notifications pushing
	JobNotificationType
	// SendHintByMail is used for sending the password hint by email
	SendHintByMail
	// JobNotesPersistType is used for saving notes to the VFS
	JobNotesPersistType
	// JobClientType is used for the jobs associated to a @client trigger
	JobClientType
	// ExportType is used for creating an export of the data
	ExportType
	// WebhookTriggerType is used for calling a webhook trigger
	WebhookTriggerType
	// JobCleanClientType is used for cleaning unused OAuth clients
	JobCleanClientType
)

Jump to

Keyboard shortcuts

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