feat(auth): lab7 share login limits across replicas and bound Argon2 work
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user