2018-01-26 10:41:41 +01:00
|
|
|
package login
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2020-03-04 12:57:20 +01:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2018-01-26 10:41:41 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-04-27 22:14:36 +02:00
|
|
|
maxInvalidLoginAttempts int64 = 5
|
|
|
|
loginAttemptsWindow = time.Minute * 5
|
2018-01-26 10:41:41 +01:00
|
|
|
)
|
|
|
|
|
2020-12-11 11:44:44 +01:00
|
|
|
var validateLoginAttempts = func(query *models.LoginUserQuery) error {
|
|
|
|
if query.Cfg.DisableBruteForceLoginProtection {
|
2018-01-26 10:41:41 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-04 12:57:20 +01:00
|
|
|
loginAttemptCountQuery := models.GetUserLoginAttemptCountQuery{
|
2020-12-11 11:44:44 +01:00
|
|
|
Username: query.Username,
|
2018-01-26 10:41:41 +01:00
|
|
|
Since: time.Now().Add(-loginAttemptsWindow),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&loginAttemptCountQuery); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if loginAttemptCountQuery.Result >= maxInvalidLoginAttempts {
|
|
|
|
return ErrTooManyLoginAttempts
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-04 12:57:20 +01:00
|
|
|
var saveInvalidLoginAttempt = func(query *models.LoginUserQuery) error {
|
2020-12-11 11:44:44 +01:00
|
|
|
if query.Cfg.DisableBruteForceLoginProtection {
|
2019-10-09 12:57:44 +02:00
|
|
|
return nil
|
2018-01-26 10:41:41 +01:00
|
|
|
}
|
|
|
|
|
2020-03-04 12:57:20 +01:00
|
|
|
loginAttemptCommand := models.CreateLoginAttemptCommand{
|
2018-01-26 10:41:41 +01:00
|
|
|
Username: query.Username,
|
|
|
|
IpAddress: query.IpAddress,
|
|
|
|
}
|
|
|
|
|
2019-10-09 12:57:44 +02:00
|
|
|
return bus.Dispatch(&loginAttemptCommand)
|
2018-01-26 10:41:41 +01:00
|
|
|
}
|