Build post chat and attachment workflows
All checks were successful
CI / backend (push) Successful in 13m8s
CI / frontend (push) Successful in 10m46s
CI / docker (push) Successful in 2m59s

This commit is contained in:
Cauê Faleiros
2026-06-08 16:44:33 -03:00
parent 8c7e5fbbe4
commit 0b920da187
30 changed files with 3021 additions and 204 deletions

View File

@@ -0,0 +1,68 @@
package auth
import (
"strings"
"sync"
"time"
)
type LoginRateLimiter struct {
mu sync.Mutex
attempts map[string]loginAttempt
limit int
window time.Duration
}
type loginAttempt struct {
Count int
ExpiresAt time.Time
}
func NewLoginRateLimiter(limit int, window time.Duration) *LoginRateLimiter {
return &LoginRateLimiter{
attempts: map[string]loginAttempt{},
limit: limit,
window: window,
}
}
func (l *LoginRateLimiter) IsBlocked(key string) bool {
l.mu.Lock()
defer l.mu.Unlock()
attempt, ok := l.attempts[key]
if !ok || time.Now().After(attempt.ExpiresAt) {
delete(l.attempts, key)
return false
}
return attempt.Count >= l.limit
}
func (l *LoginRateLimiter) RecordFailure(key string) {
l.mu.Lock()
defer l.mu.Unlock()
now := time.Now()
attempt, ok := l.attempts[key]
if !ok || now.After(attempt.ExpiresAt) {
l.attempts[key] = loginAttempt{
Count: 1,
ExpiresAt: now.Add(l.window),
}
return
}
attempt.Count++
l.attempts[key] = attempt
}
func (l *LoginRateLimiter) Clear(key string) {
l.mu.Lock()
defer l.mu.Unlock()
delete(l.attempts, key)
}
func LoginRateLimitKey(ip string, email string) string {
return strings.TrimSpace(ip) + ":" + strings.ToLower(strings.TrimSpace(email))
}