feat(auth): add argon2id passwords, replay-safe totp and redis rate limits
Refs #3 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
96c1555889
commit
caff396264
6 files changed
+243
-3
No files matched your search
@@ -0,0 +1,32 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
type Limiter struct {
|
||||
rdb redis.UniversalClient
|
||||
}
|
||||
|
||||
func NewLimiter(rdb redis.UniversalClient) *Limiter { return &Limiter{rdb: rdb} }
|
||||
|
||||
var incrWithTTL = redis.NewScript(`
|
||||
local n = redis.call("INCR", KEYS[1])
|
||||
if n == 1 then redis.call("PEXPIRE", KEYS[1], ARGV[1]) end
|
||||
return n`)
|
||||
|
||||
// Allow counts one attempt in a fixed window shared by all replicas.
|
||||
func (l *Limiter) Allow(ctx context.Context, key string, limit int, window time.Duration) (bool, error) {
|
||||
n, err := incrWithTTL.Run(ctx, l.rdb, []string{"rl:" + key}, window.Milliseconds()).Int()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return n <= limit, nil
|
||||
}
|
||||
|
||||
func (l *Limiter) Reset(ctx context.Context, key string) error {
|
||||
return l.rdb.Del(ctx, "rl:"+key).Err()
|
||||
}
|
||||
Reference in new issue
Block a user