2019-04-24 21:52:08 +00:00
|
|
|
package regulation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2019-11-17 10:47:07 +00:00
|
|
|
"github.com/clems4ever/authelia/internal/configuration/schema"
|
|
|
|
"github.com/clems4ever/authelia/internal/models"
|
|
|
|
"github.com/clems4ever/authelia/internal/storage"
|
2019-11-24 20:27:59 +00:00
|
|
|
"github.com/clems4ever/authelia/internal/utils"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewRegulator create a regulator instance.
|
2019-11-24 20:27:59 +00:00
|
|
|
func NewRegulator(configuration *schema.RegulationConfiguration, provider storage.Provider, clock utils.Clock) *Regulator {
|
2019-04-24 21:52:08 +00:00
|
|
|
regulator := &Regulator{storageProvider: provider}
|
2019-11-24 20:27:59 +00:00
|
|
|
regulator.clock = clock
|
2019-04-24 21:52:08 +00:00
|
|
|
if configuration != nil {
|
|
|
|
if configuration.FindTime > configuration.BanTime {
|
|
|
|
panic(fmt.Errorf("find_time cannot be greater than ban_time"))
|
|
|
|
}
|
|
|
|
regulator.enabled = true
|
|
|
|
regulator.maxRetries = configuration.MaxRetries
|
|
|
|
regulator.findTime = time.Duration(configuration.FindTime) * time.Second
|
|
|
|
regulator.banTime = time.Duration(configuration.BanTime) * time.Second
|
|
|
|
}
|
|
|
|
return regulator
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark mark an authentication attempt.
|
|
|
|
// We split Mark and Regulate in order to avoid timing attacks since if
|
|
|
|
func (r *Regulator) Mark(username string, successful bool) error {
|
|
|
|
return r.storageProvider.AppendAuthenticationLog(models.AuthenticationAttempt{
|
|
|
|
Username: username,
|
|
|
|
Successful: successful,
|
2019-11-24 20:27:59 +00:00
|
|
|
Time: r.clock.Now(),
|
2019-04-24 21:52:08 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regulate regulate the authentication attempts for a given user.
|
|
|
|
// This method returns ErrUserIsBanned if the user is banned along with the time until when
|
|
|
|
// the user is banned.
|
|
|
|
func (r *Regulator) Regulate(username string) (time.Time, error) {
|
|
|
|
// If there is regulation configuration, no regulation applies.
|
|
|
|
if !r.enabled {
|
|
|
|
return time.Time{}, nil
|
|
|
|
}
|
2019-11-24 20:27:59 +00:00
|
|
|
now := r.clock.Now()
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
// TODO(c.michaud): make sure FindTime < BanTime.
|
|
|
|
attempts, err := r.storageProvider.LoadLatestAuthenticationLogs(username, now.Add(-r.banTime))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
latestFailedAttempts := make([]models.AuthenticationAttempt, 0, r.maxRetries)
|
|
|
|
for _, attempt := range attempts {
|
|
|
|
if attempt.Successful || len(latestFailedAttempts) >= r.maxRetries {
|
|
|
|
// We stop appending failed attempts once we find the first successful attempts or we reach
|
|
|
|
// the configured number of retries, meaning the user is already banned.
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
latestFailedAttempts = append(latestFailedAttempts, attempt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the number of failed attempts within the ban time is less than the max number of retries
|
|
|
|
// then the user is not banned.
|
|
|
|
if len(latestFailedAttempts) < r.maxRetries {
|
|
|
|
return time.Time{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we compute the time between the latest attempt and the MaxRetry-th one. If it's
|
|
|
|
// within the FindTime then it means that the user has been banned.
|
|
|
|
durationBetweenLatestAttempts := latestFailedAttempts[0].Time.Sub(
|
|
|
|
latestFailedAttempts[r.maxRetries-1].Time)
|
|
|
|
|
|
|
|
if durationBetweenLatestAttempts < r.findTime {
|
|
|
|
bannedUntil := latestFailedAttempts[0].Time.Add(r.banTime)
|
|
|
|
return bannedUntil, ErrUserIsBanned
|
|
|
|
}
|
|
|
|
return time.Time{}, nil
|
|
|
|
}
|