Browse Source

endless loop prevention added to login method of Client class

pull/4/head
Paul Rock 7 years ago
parent
commit
df4e0a1dd6
  1. 33
      src/Client.php

33
src/Client.php

@ -358,12 +358,13 @@ class Client implements Interfaces\ClientInterface
/** /**
* Authorization logic * Authorization logic
* *
* @param bool $legacyRetry Retry login if we detect legacy version of RouterOS
* @return bool * @return bool
* @throws \RouterOS\Exceptions\ClientException * @throws \RouterOS\Exceptions\ClientException
* @throws \RouterOS\Exceptions\ConfigException * @throws \RouterOS\Exceptions\ConfigException
* @throws \RouterOS\Exceptions\QueryException * @throws \RouterOS\Exceptions\QueryException
*/ */
private function login(): bool
private function login(bool $legacyRetry = false): bool
{ {
// If legacy login scheme is enabled // If legacy login scheme is enabled
if ($this->config('legacy')) { if ($this->config('legacy')) {
@ -380,23 +381,39 @@ class Client implements Interfaces\ClientInterface
$query = (new Query('/login')) $query = (new Query('/login'))
->add('=name=' . $this->config('user')) ->add('=name=' . $this->config('user'))
->add('=password=' . $this->config('pass')); ->add('=password=' . $this->config('pass'));
// If we set modern auth scheme but router with legacy firmware then need to retry query,
// but need to prevent endless loop
$legacyRetry = true;
} }
// Execute query and get response // Execute query and get response
$response = $this->write($query)->read(false); $response = $this->write($query)->read(false);
// if :
// if:
// - we have more than one response // - we have more than one response
// - response is '!done' // - response is '!done'
// => problem with legacy version, swap it and retry // => problem with legacy version, swap it and retry
// Only tested with ROS pre 6.43, will test with post 6.43 => this could make legacy parameter obsolete ?
if (count($response)>1 && $response[0] === '!done' && !$this->config('legacy'))
{
$this->_config->set('legacy', true);
return $this->login();
// Only tested with ROS pre 6.43, will test with post 6.43 => this could make legacy parameter obsolete?
if ($legacyRetry && $this->isLegacy($response)) {
$this->_config->set('legacy', true);
return $this->login(true);
} }
// Return true if we have only one line from server and this line is !done // Return true if we have only one line from server and this line is !done
return (1 === count($response)) && isset($response[0]) && ($response[0] === '!done');
return 1 === \count($response) || (isset($response[0]) && $response[0] === '!done');
}
/**
* Detect by login request if firmware is legacy
*
* @param array $response
* @return bool
* @throws ConfigException
*/
private function isLegacy(array &$response): bool
{
return \count($response) > 1 && $response[0] === '!done' && !$this->config('legacy');
} }
/** /**

Loading…
Cancel
Save