2019-04-24 21:52:08 +00:00
|
|
|
package regulation
|
|
|
|
|
|
|
|
import (
|
2021-11-23 09:45:38 +00:00
|
|
|
"context"
|
2019-04-24 21:52:08 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/v4/internal/models"
|
|
|
|
"github.com/authelia/authelia/v4/internal/storage"
|
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewRegulator create a regulator instance.
|
2021-11-23 09:45:38 +00:00
|
|
|
func NewRegulator(configuration *schema.RegulationConfiguration, provider storage.RegulatorProvider, 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
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
if configuration != nil {
|
2020-04-05 12:37:21 +00:00
|
|
|
findTime, err := utils.ParseDurationString(configuration.FindTime)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-04-05 12:37:21 +00:00
|
|
|
banTime, err := utils.ParseDurationString(configuration.BanTime)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if findTime > banTime {
|
2019-04-24 21:52:08 +00:00
|
|
|
panic(fmt.Errorf("find_time cannot be greater than ban_time"))
|
|
|
|
}
|
2020-04-05 12:37:21 +00:00
|
|
|
|
2020-01-26 21:42:05 +00:00
|
|
|
// Set regulator enabled only if MaxRetries is not 0.
|
|
|
|
regulator.enabled = configuration.MaxRetries > 0
|
2019-04-24 21:52:08 +00:00
|
|
|
regulator.maxRetries = configuration.MaxRetries
|
2020-04-05 12:37:21 +00:00
|
|
|
regulator.findTime = findTime
|
|
|
|
regulator.banTime = banTime
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
return regulator
|
|
|
|
}
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
// Mark an authentication attempt.
|
2020-05-02 05:06:39 +00:00
|
|
|
// We split Mark and Regulate in order to avoid timing attacks.
|
2021-11-23 09:45:38 +00:00
|
|
|
func (r *Regulator) Mark(ctx context.Context, username string, successful bool) error {
|
|
|
|
return r.storageProvider.AppendAuthenticationLog(ctx, models.AuthenticationAttempt{
|
2019-04-24 21:52:08 +00:00
|
|
|
Username: username,
|
|
|
|
Successful: successful,
|
2019-11-24 20:27:59 +00:00
|
|
|
Time: r.clock.Now(),
|
2019-04-24 21:52:08 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
// 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(ctx context.Context, username string) (time.Time, error) {
|
2019-04-24 21:52:08 +00:00
|
|
|
// If there is regulation configuration, no regulation applies.
|
|
|
|
if !r.enabled {
|
|
|
|
return time.Time{}, nil
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
attempts, err := r.storageProvider.LoadAuthenticationLogs(ctx, username, r.clock.Now().Add(-r.banTime), 10, 0)
|
2019-04-24 21:52:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
latestFailedAttempts := make([]models.AuthenticationAttempt, 0, r.maxRetries)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
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
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
return time.Time{}, nil
|
|
|
|
}
|