25 lines
674 B
Go
25 lines
674 B
Go
package messenger
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
// Shared PG counters, not per-process memory or untrusted forwarding headers.
|
|
// Unknown and known usernames use the same hash/bucket and admission rules.
|
|
func (s *Server) admitAuth(w http.ResponseWriter, r *http.Request, username string) bool {
|
|
key := fmt.Sprintf("%x", sha256.Sum256([]byte(username)))
|
|
var allowed bool
|
|
if e := s.db.QueryRow(r.Context(), "SELECT auth_admit($1)", key).Scan(&allowed); e != nil {
|
|
unavailable(w)
|
|
return false
|
|
}
|
|
if !allowed {
|
|
w.Header().Set("Retry-After", "60")
|
|
fail(w, 429, "rate_limited", "Authentication rate limit exceeded")
|
|
return false
|
|
}
|
|
return true
|
|
}
|