2019-04-24 21:52:08 +00:00
|
|
|
package regulation
|
|
|
|
|
|
|
|
import (
|
2021-11-23 09:45:38 +00:00
|
|
|
"context"
|
2022-06-14 07:20:13 +00:00
|
|
|
"strings"
|
2019-04-24 21:52:08 +00:00
|
|
|
"time"
|
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2022-03-06 05:47:40 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/model"
|
2021-08-11 01:04:35 +00:00
|
|
|
"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.
|
2023-05-24 12:33:05 +00:00
|
|
|
func NewRegulator(config schema.RegulationConfiguration, store storage.RegulatorProvider, clock utils.Clock) *Regulator {
|
2022-03-02 06:40:26 +00:00
|
|
|
return &Regulator{
|
2023-05-24 12:33:05 +00:00
|
|
|
enabled: config.MaxRetries > 0,
|
|
|
|
store: store,
|
|
|
|
clock: clock,
|
|
|
|
config: config,
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2022-06-14 07:20:13 +00:00
|
|
|
func (r *Regulator) Mark(ctx Context, successful, banned bool, username, requestURI, requestMethod, authType string) error {
|
2023-01-25 09:36:40 +00:00
|
|
|
ctx.RecordAuthn(successful, banned, strings.ToLower(authType))
|
2022-06-14 07:20:13 +00:00
|
|
|
|
2023-05-24 12:33:05 +00:00
|
|
|
return r.store.AppendAuthenticationLog(ctx, model.AuthenticationAttempt{
|
2021-11-29 03:09:14 +00:00
|
|
|
Time: r.clock.Now(),
|
|
|
|
Successful: successful,
|
|
|
|
Banned: banned,
|
|
|
|
Username: username,
|
|
|
|
Type: authType,
|
2022-06-14 07:20:13 +00:00
|
|
|
RemoteIP: model.NewNullIP(ctx.RemoteIP()),
|
2021-11-29 03:09:14 +00:00
|
|
|
RequestURI: requestURI,
|
|
|
|
RequestMethod: requestMethod,
|
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.
|
2023-06-23 20:07:23 +00:00
|
|
|
func (r *Regulator) Regulate(ctx context.Context, username string, remoteIp 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
|
|
|
|
2023-06-23 20:07:23 +00:00
|
|
|
attempts, err := r.store.LoadAuthenticationLogs(ctx, username, remoteIp, r.clock.Now().Add(-r.config.BanTime), 10, 0)
|
2019-04-24 21:52:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, nil
|
|
|
|
}
|
|
|
|
|
2022-03-06 05:47:40 +00:00
|
|
|
latestFailedAttempts := make([]model.AuthenticationAttempt, 0, r.config.MaxRetries)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
for _, attempt := range attempts {
|
2022-03-02 06:40:26 +00:00
|
|
|
if attempt.Successful || len(latestFailedAttempts) >= r.config.MaxRetries {
|
2019-04-24 21:52:08 +00:00
|
|
|
// 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.
|
2022-03-02 06:40:26 +00:00
|
|
|
if len(latestFailedAttempts) < r.config.MaxRetries {
|
2019-04-24 21:52:08 +00:00
|
|
|
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(
|
2022-03-02 06:40:26 +00:00
|
|
|
latestFailedAttempts[r.config.MaxRetries-1].Time)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2022-03-02 06:40:26 +00:00
|
|
|
if durationBetweenLatestAttempts < r.config.FindTime {
|
|
|
|
bannedUntil := latestFailedAttempts[0].Time.Add(r.config.BanTime)
|
2019-04-24 21:52:08 +00:00
|
|
|
return bannedUntil, ErrUserIsBanned
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
return time.Time{}, nil
|
|
|
|
}
|