feat(auth): lab7 share login limits across replicas and bound Argon2 work

This commit is contained in:
Голиков Maximillian Антон
2026-11-29 12:32:16 +03:00
parent 4fe1d17e50
commit a907161087
8 changed files with 76 additions and 1 deletions
+10
View File
@@ -17,6 +17,7 @@ type SessionService struct {
users domain.UserRepository
sessions domain.SessionRepository
hasher PasswordHasher
loginDelay func(context.Context, string) (int, error)
ttl time.Duration
}
@@ -26,12 +27,14 @@ func NewSessionService(
sessions domain.SessionRepository,
hasher PasswordHasher,
ttl time.Duration,
loginDelay func(context.Context, string) (int, error),
) *SessionService {
return &SessionService{
users: users,
sessions: sessions,
hasher: hasher,
ttl: ttl,
loginDelay: loginDelay,
}
}
@@ -39,6 +42,13 @@ func NewSessionService(
// Возвращает сессию, открытый токен и пользователя.
// Открытый токен уходит клиенту; в БД сохраняется только его SHA-256.
func (s *SessionService) Login(ctx context.Context, username, password string) (*domain.Session, string, *domain.User, error) {
delay, err := s.loginDelay(ctx, username)
if err != nil {
return nil, "", nil, domain.ErrUnavailable
}
if delay > 0 {
return nil, "", nil, &domain.RateLimitedError{Seconds: delay}
}
user, err := s.users.GetByUsername(ctx, username)
if err != nil {
if errors.Is(err, domain.ErrNotFound) {