Raw
 1package ldap
 2
 3import (
 4	"net/netip"
 5	"time"
 6
 7	"tug/internal/platform/ratelimit"
 8)
 9
10const (
11	ActionBind        = "ldap.bind"
12	bindWindowEntries = 10000
13	bindWindowEvery   = time.Hour
14)
15
16var bindWindowRate = ratelimit.Rate{Burst: 1, Every: bindWindowEvery}
17
18type bindKey struct {
19	userID string
20	peer   netip.Prefix
21}
22
23type bindWindow struct {
24	table *ratelimit.Table[bindKey]
25}
26
27func newBindWindow(now func() time.Time, max int) *bindWindow {
28	return &bindWindow{table: ratelimit.New[bindKey](max, now)}
29}
30
31func (w *bindWindow) first(userID string, addr netip.Addr) bool {
32	_, ok := w.table.Take(bindKey{userID: userID, peer: ratelimit.Prefix(addr)}, bindWindowRate)
33	return ok
34}
35
36func (w *bindWindow) len() int {
37	return w.table.Len()
38}