forked from Nixius/authelia
39 lines
722 B
Go
39 lines
722 B
Go
package handlers
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
const resendCooldown = time.Minute
|
|
|
|
type resendLimiter struct {
|
|
mu sync.RWMutex
|
|
last map[string]time.Time
|
|
}
|
|
|
|
func (rl *resendLimiter) allow(username string) (ok bool, retryAfter int) {
|
|
rl.mu.Lock()
|
|
defer rl.mu.Unlock()
|
|
if rl.last == nil {
|
|
rl.last = make(map[string]time.Time)
|
|
}
|
|
last := rl.last[username]
|
|
elapsed := time.Since(last)
|
|
if elapsed < resendCooldown {
|
|
return false, int((resendCooldown - elapsed).Seconds())
|
|
}
|
|
return true, 0
|
|
}
|
|
|
|
func (rl *resendLimiter) record(username string) {
|
|
rl.mu.Lock()
|
|
defer rl.mu.Unlock()
|
|
if rl.last == nil {
|
|
rl.last = make(map[string]time.Time)
|
|
}
|
|
rl.last[username] = time.Now()
|
|
}
|
|
|
|
var resendRateLimiter resendLimiter
|