/**
  * Performs an authentication against DB.
  * and returns IIdentity on success or throws AuthenticationException
  *
  * @param array credentials
  *
  * @return IIdentity
  * @throws AuthenticationException
  */
 public function authenticate(array $credentials)
 {
     if (($userData = $this->fetchUserData($credentials)) !== FALSE) {
         // Password hashing
         $ok = $this->passwordHasher->checkPassword($credentials[self::PASSWORD], $userData->{$this->fieldName[self::PASSWORD]});
         if (!$ok) {
             throw new AuthenticationException("Invalid password for user " . var_export($credentials[self::USERNAME], TRUE) . ".", self::INVALID_CREDENTIAL);
         }
         return $this->identityFactory->createIdentity($userData, $this);
     } else {
         throw new AuthenticationException("User " . var_export($credentials[self::USERNAME], TRUE) . " not found.", self::IDENTITY_NOT_FOUND);
     }
 }
 /**
  * @inheritDoc
  */
 public function authenticate($clientId, $clientSecret = NULL)
 {
     $clientData = $this->dbConnection->query("SELECT * FROM %n", $this->tableName, "WHERE [clientId] = %s", $clientId)->fetch();
     if ($clientData === FALSE) {
         return FALSE;
     }
     // Check client secret
     if ($clientData->secret !== NULL || $clientSecret !== NULL) {
         if (!$this->hasher->checkPassword($clientSecret, $clientData->secret)) {
             return FALSE;
         }
     }
     return new Client($clientData->clientId);
 }