Documentation
¶
Overview ¶
Package attempts limits the password attempts of an Auth. It runs in memory, per instance, before any store access and before the password hash, over a durable record the store keeps by the submitted name. The memory holds three kinds of key: the name, the client's address and the login's XSRF cookie token. A name is held by its store record: an attempt takes its place in the record before its password is compared (Take), in the store's one transaction, which says whether it was counted, so of attempts made at once no more than the limit are ever compared; one that was not counted is refused, and a right password gives the attempt back and resets the name (Succeeded). The record the store answered is cached here for a few seconds (readTTL), so an attempt during a lock is refused the moment it arrives with nothing hashed and the store asked at most once in that time, and a lock an administrator cleared in the store is gone from every instance within it. The client's keys are counted here alone: an attempt takes its place under them in the step that finds them free, every wrong and every refused attempt counts against them, and a client that keeps trying during a wait extends its own lock while the name's stays as it was. The memory is bounded; the store is the truth for the name.
Index ¶
- Constants
- func Normalize(name string) string
- type Attempt
- type Client
- type Limiter
- func (l *Limiter) Clear(ctx context.Context, name string) error
- func (l *Limiter) Failed(ctx context.Context, name string, client Client) (lock time.Duration, err error)
- func (l *Limiter) Limit() dbtype.SignInAttemptLimit
- func (l *Limiter) SetClock(now func() time.Time)
- func (l *Limiter) Succeeded(ctx context.Context, taken *Attempt) error
- func (l *Limiter) Take(ctx context.Context, name string, client Client) (taken *Attempt, wait time.Duration, err error)
- type Store
Constants ¶
const ( // ClientMultiple is how many times the name limit's failures a client's address or // cookie may fail, across names, within the window before the client is locked for // the lock period. ClientMultiple = 10 )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Attempt ¶ added in v0.5.0
type Attempt struct {
// Lock is how long the name is held now that the attempt was counted: zero unless
// the attempt is the one that reached the limit.
Lock time.Duration
// contains filtered or unexported fields
}
Attempt is an attempt whose place under the limit was taken before its password was compared (Limiter.Take): what the limiter gives back when the password proves right.
type Client ¶
Client identifies the client of an attempt: its address and its login's XSRF cookie token. Either is empty when unknown, as both are for an attempt through the API.
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter is the in-memory layer over a Store.
func New ¶
func New(limit dbtype.SignInAttemptLimit, store Store) *Limiter
New returns a limiter under limit over store.
func (*Limiter) Clear ¶
Clear resets name whatever memory holds: an MFA completion or a password change.
func (*Limiter) Failed ¶
func (l *Limiter) Failed(ctx context.Context, name string, client Client) (lock time.Duration, err error)
Failed records a failed attempt on name by client that took no place beforehand: a failure learned after the attempt's own was given back. It is counted in the store under the limit and against the client's keys in memory. The record the store answers is kept in memory, and lock is how long it now holds the name, zero while it does not.
func (*Limiter) Limit ¶
func (l *Limiter) Limit() dbtype.SignInAttemptLimit
Limit returns the limit the limiter counts under.
func (*Limiter) Succeeded ¶
Succeeded gives taken back after its password proved right: the name's record is deleted in the store, the attempt's own count and every other with it, the failures before it and the places of attempts still being compared, the name leaves memory, and the client's keys are given the attempt's count back. The clear does not end with the request (giveBackTimeout): a person whose request was canceled once their right password was compared keeps no count for it. The store is cleared before memory is, so a lock another attempt cached while the clear was on its way goes with it; one an attempt caches after that, from an answer the store gave before the clear, is trusted for readTTL at most, as a lock cleared by another instance is.
func (*Limiter) Take ¶ added in v0.5.0
func (l *Limiter) Take(ctx context.Context, name string, client Client) (taken *Attempt, wait time.Duration, err error)
Take takes an attempt on name by client before its password is compared. The attempt answered is nil when it was refused, and wait is then how long the caller is to wait; otherwise the password may be compared, and a right one hands the attempt to Succeeded.
The name's lock answers from memory while a record that holds it is cached (readTTL), the client's locks from memory alone, and an attempt refused there asks the store nothing. An attempt that goes on is counted first: against the client's keys in the step that found them free, and against the name in the store's one transaction, which says whether it was counted. Of attempts made at once, as many as the limit are so counted and go on, and every other is refused, uncompared; the one that reaches the limit locks the name and goes on itself. A refused attempt counts against the client's keys and never against the name. err is a store failure, which leaves nothing counted against the client.
type Store ¶
type Store interface {
// RecordSignInFailure records a failure on name under limit, in one transaction, and
// returns the record, which says whether the failure was counted (Counted).
RecordSignInFailure(ctx context.Context, name string, limit dbtype.SignInAttemptLimit) (*dbtype.SignInAttempts, error)
// ClearSignInAttempts deletes name's record.
ClearSignInAttempts(ctx context.Context, name string) error
}
Store keeps the durable record of a name's attempts.