/**
  * Authenticates a user by its credentials
  *
  * @param AuthDTO $authDTO
  *
  * @return \Sententiaregum\User\Domain\Value\ApiKey
  *
  * @throws AuthenticationException If the username is invalid
  */
 public function createToken(AuthDTO $authDTO)
 {
     $user = $this->userRepository->findOneByName($authDTO->getUsername());
     if (!$user) {
         throw AuthenticationException::fromInvalidUsername();
     }
     try {
         $token = $user->authenticateToken($authDTO, $this->apiKeyGenerator);
     } catch (AuthenticationException $ex) {
         // modify authentication failure
         $this->userRepository->modify($user);
         throw $ex;
     }
     $this->userRepository->modify($user);
     return $token;
 }
Ejemplo n.º 2
0
 /**
  * Authenticates the user token
  *
  * @param AuthDTO $credentials
  * @param ApiKeyFactoryInterface $apiKeyGenerator
  *
  * @return Value\ApiKey
  *
  * @throws AuthenticationException If the credentials are invalid
  * @throws AuthenticationException If the user was locked
  * @throws AuthenticationException If the api key generation failed
  */
 public function authenticateToken(AuthDTO $credentials, ApiKeyFactoryInterface $apiKeyGenerator)
 {
     $this->ensureActivated();
     if (!$this->getCredentials()->compare($credentials)) {
         if (null === $this->authenticationFailureReport) {
             $this->authenticationFailureReport = new AuthenticationFailure($this);
         }
         $this->authenticationFailureReport->updateReport();
         throw AuthenticationException::fromCredentialFailure();
     }
     if ($this->getSimpleProfile()->isLocked()) {
         throw AuthenticationException::fromLockedUser();
     }
     if (null === ($token = $this->getToken())) {
         try {
             $token = $apiKeyGenerator->generateKeyCode();
         } catch (\OverflowException $ex) {
             throw AuthenticationException::fromInvalidApiKey();
         }
         $this->token = new Token($token);
     }
     return $this->getToken();
 }