public function testIncreaseCount()
 {
     $attempt = new AuthenticationAttempt();
     $this->assertSame(0, $attempt->getAttemptCount());
     $attempt->increaseAttemptCount()->increaseAttemptCount();
     $this->assertSame(2, $attempt->getAttemptCount());
 }
 public function testSerialize()
 {
     $model = new AuthenticationAttempt();
     $model->setIp('127.0.0.1');
     $model->increaseAttemptCount();
     list($id, $latest, $range) = [$model->getId(), $model->getLatestFailedAttemptTime(), $model->getLastFailedAttemptTimesInRange()];
     $serialized = serialize($model);
     $new = unserialize($serialized);
     $this->assertSame($id, $new->getId());
     $this->assertSame('127.0.0.1', $new->getIp());
     $this->assertEquals($latest, $new->getLatestFailedAttemptTime());
 }
Example #3
0
 /**
  * Adds one ip of a failed authentication unless its authentication succeeded previously (users may mistype some times).
  *
  * @param string $ip
  *
  * @return $this
  */
 public function addFailedAuthenticationWithIp($ip)
 {
     if (!$this->isKnownIp($ip)) {
         if ($attempt = $this->getAuthAttemptModelByIp($ip, self::FAILED_AUTH_POOL)) {
             $attempt->increaseAttemptCount();
         } else {
             $attempt = new AuthenticationAttempt();
             $attempt->setIp($ip);
             $attempt->increaseAttemptCount();
             $this->failedAuthentications->add($attempt);
         }
     }
     return $this;
 }
Example #4
0
 /**
  * Checks if enough failed authentications are done in the past to rise an auth warning.
  *
  * @param AuthenticationAttempt                              $attempt
  * @param \AppBundle\Model\User\Util\Date\DateTimeComparison $comparison
  *
  * @return bool
  */
 private function needsAuthWarning(AuthenticationAttempt $attempt, DateTimeComparison $comparison) : bool
 {
     $count = $attempt->getAttemptCount();
     if (self::MAX_FAILED_ATTEMPTS_FROM_IP <= $count) {
         if ($count - 3 === 0) {
             return true;
         }
         return !$this->isPreviouslyLoginFailed('-6 hours', $comparison, true);
     }
     return false;
 }