/
rate_limits.go
146 lines (125 loc) · 3.84 KB
/
rate_limits.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package service
import (
"github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/lib/ratelimit"
"github.com/authgear/authgear-server/pkg/util/httputil"
)
const (
VerifyPasswordPerIP ratelimit.BucketName = "VerifyPasswordPerIP"
VerifyPasswordPerUserPerIP ratelimit.BucketName = "VerifyPasswordPerUserPerIP"
VerifyTOTPPerIP ratelimit.BucketName = "VerifyTOTPPerIP"
VerifyTOTPPerUserPerIP ratelimit.BucketName = "VerifyTOTPPerUserPerIP"
VerifyPasskeyPerIP ratelimit.BucketName = "VerifyPasskeyPerIP"
)
type RateLimiter interface {
Reserve(spec ratelimit.BucketSpec) *ratelimit.Reservation
Cancel(r *ratelimit.Reservation)
}
type Reservation struct {
perUserPerIP *ratelimit.Reservation
perIP *ratelimit.Reservation
}
func (r *Reservation) Error() error {
if r == nil {
return nil
}
if err := r.perUserPerIP.Error(); err != nil {
return err
}
if err := r.perIP.Error(); err != nil {
return err
}
return nil
}
func (r *Reservation) Consume() {
if r == nil {
return
}
r.perUserPerIP.Consume()
r.perIP.Consume()
}
type RateLimits struct {
IP httputil.RemoteIP
Config *config.AuthenticationConfig
RateLimiter RateLimiter
}
func (l *RateLimits) specPerIP(authType model.AuthenticatorType) ratelimit.BucketSpec {
switch authType {
case model.AuthenticatorTypePassword:
config := l.Config.RateLimits.Password.PerIP
if config.Enabled == nil {
config = l.Config.RateLimits.General.PerIP
}
return ratelimit.NewBucketSpec(
config, VerifyPasswordPerIP,
string(l.IP),
)
case model.AuthenticatorTypeOOBEmail, model.AuthenticatorTypeOOBSMS:
// OOB rate limits are handled by OTP mechanism.
return ratelimit.BucketSpecDisabled
case model.AuthenticatorTypeTOTP:
config := l.Config.RateLimits.TOTP.PerIP
if config.Enabled == nil {
config = l.Config.RateLimits.General.PerIP
}
return ratelimit.NewBucketSpec(
config, VerifyTOTPPerIP,
string(l.IP),
)
case model.AuthenticatorTypePasskey:
config := l.Config.RateLimits.Passkey.PerIP
if config.Enabled == nil {
config = l.Config.RateLimits.General.PerIP
}
return ratelimit.NewBucketSpec(
config, VerifyPasskeyPerIP,
string(l.IP),
)
default:
panic("authenticator: unknown type: " + authType)
}
}
func (l *RateLimits) specPerUserPerIP(userID string, authType model.AuthenticatorType) ratelimit.BucketSpec {
switch authType {
case model.AuthenticatorTypePassword:
config := l.Config.RateLimits.Password.PerUserPerIP
if config.Enabled == nil {
config = l.Config.RateLimits.General.PerUserPerIP
}
return ratelimit.NewBucketSpec(
config, VerifyPasswordPerUserPerIP,
userID, string(l.IP),
)
case model.AuthenticatorTypeOOBEmail, model.AuthenticatorTypeOOBSMS:
// OOB rate limits are handled by OTP mechanism.
return ratelimit.BucketSpecDisabled
case model.AuthenticatorTypeTOTP:
config := l.Config.RateLimits.TOTP.PerUserPerIP
if config.Enabled == nil {
config = l.Config.RateLimits.General.PerUserPerIP
}
return ratelimit.NewBucketSpec(
config, VerifyTOTPPerUserPerIP,
userID, string(l.IP),
)
case model.AuthenticatorTypePasskey:
// Per-user rate limit for passkey is handled as account enumeration rate limit,
// since we lookup user by passkey credential ID.
return ratelimit.BucketSpecDisabled
default:
panic("authenticator: unknown type: " + authType)
}
}
func (l *RateLimits) Cancel(r *Reservation) {
l.RateLimiter.Cancel(r.perIP)
l.RateLimiter.Cancel(r.perUserPerIP)
}
func (l *RateLimits) Reserve(userID string, authType model.AuthenticatorType) *Reservation {
specPerUserPerIP := l.specPerUserPerIP(userID, authType)
specPerIP := l.specPerIP(authType)
return &Reservation{
perUserPerIP: l.RateLimiter.Reserve(specPerUserPerIP),
perIP: l.RateLimiter.Reserve(specPerIP),
}
}