48 lines
1.4 KiB
Go
48 lines
1.4 KiB
Go
package middleware
|
|
|
|
import (
|
|
"time"
|
|
|
|
sharedlimit "git.sendico.io/tech/sxchnge/bff/edge/shared/interface/ratelimit"
|
|
)
|
|
|
|
const (
|
|
defaultLoginAccountLimit = 5
|
|
defaultLoginAccountWindow = 15 * time.Minute
|
|
defaultLoginIPLimit = 50
|
|
defaultLoginIPWindow = 15 * time.Minute
|
|
)
|
|
|
|
type LoginRateLimitWindowConfig = sharedlimit.WindowConfig
|
|
|
|
type LoginRateLimitsConfig struct {
|
|
PerAccount LoginRateLimitWindowConfig `yaml:"per_account"`
|
|
PerIP LoginRateLimitWindowConfig `yaml:"per_ip"`
|
|
}
|
|
|
|
type LoginRateLimitWindow = sharedlimit.Window
|
|
|
|
type LoginRateLimits struct {
|
|
PerAccount LoginRateLimitWindow
|
|
PerIP LoginRateLimitWindow
|
|
}
|
|
|
|
func (c *Config) ResolveLoginRateLimits() (LoginRateLimits, error) {
|
|
if c == nil || c.LoginRateLimits == nil {
|
|
return LoginRateLimits{
|
|
PerAccount: LoginRateLimitWindow{Limit: defaultLoginAccountLimit, Window: defaultLoginAccountWindow},
|
|
PerIP: LoginRateLimitWindow{Limit: defaultLoginIPLimit, Window: defaultLoginIPWindow},
|
|
}, nil
|
|
}
|
|
|
|
account, err := sharedlimit.ResolveWindow(c.LoginRateLimits.PerAccount, "middleware.login_rate_limits.per_account")
|
|
if err != nil {
|
|
return LoginRateLimits{}, err
|
|
}
|
|
ip, err := sharedlimit.ResolveWindow(c.LoginRateLimits.PerIP, "middleware.login_rate_limits.per_ip")
|
|
if err != nil {
|
|
return LoginRateLimits{}, err
|
|
}
|
|
return LoginRateLimits{PerAccount: account, PerIP: ip}, nil
|
|
}
|