2018-01-26 10:41:41 +01:00
|
|
|
package login
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
maxInvalidLoginAttempts int64 = 5
|
|
|
|
|
loginAttemptsWindow time.Duration = time.Minute * 5
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var validateLoginAttempts = func(username string) error {
|
|
|
|
|
if setting.DisableBruteForceLoginProtection {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loginAttemptCountQuery := m.GetUserLoginAttemptCountQuery{
|
|
|
|
|
Username: username,
|
|
|
|
|
Since: time.Now().Add(-loginAttemptsWindow),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&loginAttemptCountQuery); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if loginAttemptCountQuery.Result >= maxInvalidLoginAttempts {
|
|
|
|
|
return ErrTooManyLoginAttempts
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-08 17:13:58 -05:00
|
|
|
var saveInvalidLoginAttempt = func(query *m.LoginUserQuery) {
|
2018-01-26 10:41:41 +01:00
|
|
|
if setting.DisableBruteForceLoginProtection {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loginAttemptCommand := m.CreateLoginAttemptCommand{
|
|
|
|
|
Username: query.Username,
|
|
|
|
|
IpAddress: query.IpAddress,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bus.Dispatch(&loginAttemptCommand)
|
|
|
|
|
}
|