mirror of
https://github.com/wallabag/wallabag.git
synced 2025-02-18 03:35:17 +00:00
Move site config from property to parameter in LoginFormAuthenticator
This commit is contained in:
parent
aec8e26058
commit
81d269dec1
5 changed files with 35 additions and 43 deletions
|
@ -62,14 +62,14 @@ class AuthenticatorSubscriber implements SubscriberInterface, LoggerAwareInterfa
|
||||||
}
|
}
|
||||||
|
|
||||||
$client = $event->getClient();
|
$client = $event->getClient();
|
||||||
$authenticator = $this->authenticatorFactory->buildFromSiteConfig($config);
|
$authenticator = $this->authenticatorFactory->buildFromSiteConfig();
|
||||||
|
|
||||||
if (!$authenticator->isLoggedIn($client)) {
|
if (!$authenticator->isLoggedIn($config, $client)) {
|
||||||
$this->logger->debug('loginIfRequired> user is not logged in, attach authenticator');
|
$this->logger->debug('loginIfRequired> user is not logged in, attach authenticator');
|
||||||
|
|
||||||
$emitter = $client->getEmitter();
|
$emitter = $client->getEmitter();
|
||||||
$emitter->detach($this);
|
$emitter->detach($this);
|
||||||
$authenticator->login($client);
|
$authenticator->login($config, $client);
|
||||||
$emitter->attach($this);
|
$emitter->attach($this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,8 +94,8 @@ class AuthenticatorSubscriber implements SubscriberInterface, LoggerAwareInterfa
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$authenticator = $this->authenticatorFactory->buildFromSiteConfig($config);
|
$authenticator = $this->authenticatorFactory->buildFromSiteConfig();
|
||||||
$isLoginRequired = $authenticator->isLoginRequired($body);
|
$isLoginRequired = $authenticator->isLoginRequired($config, $body);
|
||||||
|
|
||||||
$this->logger->debug('loginIfRequested> retry #' . $this->retries . ' with login ' . ($isLoginRequired ? '' : 'not ') . 'required');
|
$this->logger->debug('loginIfRequested> retry #' . $this->retries . ' with login ' . ($isLoginRequired ? '' : 'not ') . 'required');
|
||||||
|
|
||||||
|
@ -104,7 +104,7 @@ class AuthenticatorSubscriber implements SubscriberInterface, LoggerAwareInterfa
|
||||||
|
|
||||||
$emitter = $client->getEmitter();
|
$emitter = $client->getEmitter();
|
||||||
$emitter->detach($this);
|
$emitter->detach($this);
|
||||||
$authenticator->login($client);
|
$authenticator->login($config, $client);
|
||||||
$emitter->attach($this);
|
$emitter->attach($this);
|
||||||
|
|
||||||
$event->retry();
|
$event->retry();
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
namespace Wallabag\SiteConfig\Authenticator;
|
namespace Wallabag\SiteConfig\Authenticator;
|
||||||
|
|
||||||
use GuzzleHttp\ClientInterface;
|
use GuzzleHttp\ClientInterface;
|
||||||
|
use Wallabag\SiteConfig\SiteConfig;
|
||||||
|
|
||||||
interface Authenticator
|
interface Authenticator
|
||||||
{
|
{
|
||||||
|
@ -11,14 +12,14 @@ interface Authenticator
|
||||||
*
|
*
|
||||||
* @return self
|
* @return self
|
||||||
*/
|
*/
|
||||||
public function login(ClientInterface $guzzle);
|
public function login(SiteConfig $siteConfig, ClientInterface $guzzle);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if we are logged into the site, but without calling the server (e.g. do we have a Cookie).
|
* Checks if we are logged into the site, but without calling the server (e.g. do we have a Cookie).
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isLoggedIn(ClientInterface $guzzle);
|
public function isLoggedIn(SiteConfig $siteConfig, ClientInterface $guzzle);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks from the HTML of a page if authentication is requested by a grabbed page.
|
* Checks from the HTML of a page if authentication is requested by a grabbed page.
|
||||||
|
@ -27,5 +28,5 @@ interface Authenticator
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isLoginRequired($html);
|
public function isLoginRequired(SiteConfig $siteConfig, $html);
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,8 +14,8 @@ class Factory
|
||||||
*
|
*
|
||||||
* @throw \OutOfRangeException if there are no credentials for this host
|
* @throw \OutOfRangeException if there are no credentials for this host
|
||||||
*/
|
*/
|
||||||
public function buildFromSiteConfig(SiteConfig $siteConfig)
|
public function buildFromSiteConfig()
|
||||||
{
|
{
|
||||||
return new LoginFormAuthenticator($siteConfig);
|
return new LoginFormAuthenticator();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,37 +11,28 @@ use Wallabag\SiteConfig\SiteConfig;
|
||||||
|
|
||||||
class LoginFormAuthenticator implements Authenticator
|
class LoginFormAuthenticator implements Authenticator
|
||||||
{
|
{
|
||||||
/** @var SiteConfig */
|
public function login(SiteConfig $siteConfig, ClientInterface $guzzle)
|
||||||
private $siteConfig;
|
|
||||||
|
|
||||||
public function __construct(SiteConfig $siteConfig)
|
|
||||||
{
|
|
||||||
// @todo OptionResolver
|
|
||||||
$this->siteConfig = $siteConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function login(ClientInterface $guzzle)
|
|
||||||
{
|
{
|
||||||
$postFields = [
|
$postFields = [
|
||||||
$this->siteConfig->getUsernameField() => $this->siteConfig->getUsername(),
|
$siteConfig->getUsernameField() => $siteConfig->getUsername(),
|
||||||
$this->siteConfig->getPasswordField() => $this->siteConfig->getPassword(),
|
$siteConfig->getPasswordField() => $siteConfig->getPassword(),
|
||||||
] + $this->getExtraFields($guzzle);
|
] + $this->getExtraFields($guzzle);
|
||||||
|
|
||||||
$guzzle->post(
|
$guzzle->post(
|
||||||
$this->siteConfig->getLoginUri(),
|
$siteConfig->getLoginUri(),
|
||||||
['body' => $postFields, 'allow_redirects' => true, 'verify' => false]
|
['body' => $postFields, 'allow_redirects' => true, 'verify' => false]
|
||||||
);
|
);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isLoggedIn(ClientInterface $guzzle)
|
public function isLoggedIn(SiteConfig $siteConfig, ClientInterface $guzzle)
|
||||||
{
|
{
|
||||||
if (($cookieJar = $guzzle->getDefaultOption('cookies')) instanceof CookieJar) {
|
if (($cookieJar = $guzzle->getDefaultOption('cookies')) instanceof CookieJar) {
|
||||||
/** @var \GuzzleHttp\Cookie\SetCookie $cookie */
|
/** @var \GuzzleHttp\Cookie\SetCookie $cookie */
|
||||||
foreach ($cookieJar as $cookie) {
|
foreach ($cookieJar as $cookie) {
|
||||||
// check required cookies
|
// check required cookies
|
||||||
if ($cookie->getDomain() === $this->siteConfig->getHost()) {
|
if ($cookie->getDomain() === $siteConfig->getHost()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,13 +41,13 @@ class LoginFormAuthenticator implements Authenticator
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isLoginRequired($html)
|
public function isLoginRequired(SiteConfig $siteConfig, $html)
|
||||||
{
|
{
|
||||||
// need to check for the login dom element ($options['not_logged_in_xpath']) in the HTML
|
// need to check for the login dom element ($options['not_logged_in_xpath']) in the HTML
|
||||||
try {
|
try {
|
||||||
$crawler = new Crawler((string) $html);
|
$crawler = new Crawler((string) $html);
|
||||||
|
|
||||||
$loggedIn = $crawler->evaluate((string) $this->siteConfig->getNotLoggedInXpath());
|
$loggedIn = $crawler->evaluate((string) $siteConfig->getNotLoggedInXpath());
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -70,17 +61,17 @@ class LoginFormAuthenticator implements Authenticator
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function getExtraFields(ClientInterface $guzzle)
|
private function getExtraFields(SiteConfig $siteConfig, ClientInterface $guzzle)
|
||||||
{
|
{
|
||||||
$extraFields = [];
|
$extraFields = [];
|
||||||
|
|
||||||
foreach ($this->siteConfig->getExtraFields() as $fieldName => $fieldValue) {
|
foreach ($siteConfig->getExtraFields() as $fieldName => $fieldValue) {
|
||||||
if ('@=' === substr($fieldValue, 0, 2)) {
|
if ('@=' === substr($fieldValue, 0, 2)) {
|
||||||
$expressionLanguage = $this->getExpressionLanguage($guzzle);
|
$expressionLanguage = $this->getExpressionLanguage($guzzle);
|
||||||
$fieldValue = $expressionLanguage->evaluate(
|
$fieldValue = $expressionLanguage->evaluate(
|
||||||
substr($fieldValue, 2),
|
substr($fieldValue, 2),
|
||||||
[
|
[
|
||||||
'config' => $this->siteConfig,
|
'config' => $siteConfig,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,8 +35,8 @@ class LoginFormAuthenticatorTest extends TestCase
|
||||||
'password' => 'unkn0wn',
|
'password' => 'unkn0wn',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$auth = new LoginFormAuthenticator($siteConfig);
|
$auth = new LoginFormAuthenticator();
|
||||||
$res = $auth->login($guzzle);
|
$res = $auth->login($siteConfig, $guzzle);
|
||||||
|
|
||||||
$this->assertInstanceOf(LoginFormAuthenticator::class, $res);
|
$this->assertInstanceOf(LoginFormAuthenticator::class, $res);
|
||||||
}
|
}
|
||||||
|
@ -65,8 +65,8 @@ class LoginFormAuthenticatorTest extends TestCase
|
||||||
'password' => 'unkn0wn',
|
'password' => 'unkn0wn',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$auth = new LoginFormAuthenticator($siteConfig);
|
$auth = new LoginFormAuthenticator();
|
||||||
$res = $auth->login($guzzle);
|
$res = $auth->login($siteConfig, $guzzle);
|
||||||
|
|
||||||
$this->assertInstanceOf(LoginFormAuthenticator::class, $res);
|
$this->assertInstanceOf(LoginFormAuthenticator::class, $res);
|
||||||
}
|
}
|
||||||
|
@ -128,8 +128,8 @@ class LoginFormAuthenticatorTest extends TestCase
|
||||||
'password' => 'unkn0wn',
|
'password' => 'unkn0wn',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$auth = new LoginFormAuthenticator($siteConfig);
|
$auth = new LoginFormAuthenticator();
|
||||||
$res = $auth->login($client);
|
$res = $auth->login($siteConfig, $client);
|
||||||
|
|
||||||
$this->assertInstanceOf(LoginFormAuthenticator::class, $res);
|
$this->assertInstanceOf(LoginFormAuthenticator::class, $res);
|
||||||
}
|
}
|
||||||
|
@ -194,8 +194,8 @@ class LoginFormAuthenticatorTest extends TestCase
|
||||||
'password' => 'unkn0wn',
|
'password' => 'unkn0wn',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$auth = new LoginFormAuthenticator($siteConfig);
|
$auth = new LoginFormAuthenticator();
|
||||||
$res = $auth->login($client);
|
$res = $auth->login($siteConfig, $client);
|
||||||
|
|
||||||
$this->assertInstanceOf(LoginFormAuthenticator::class, $res);
|
$this->assertInstanceOf(LoginFormAuthenticator::class, $res);
|
||||||
}
|
}
|
||||||
|
@ -210,8 +210,8 @@ class LoginFormAuthenticatorTest extends TestCase
|
||||||
'password' => 'unkn0wn',
|
'password' => 'unkn0wn',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$auth = new LoginFormAuthenticator($siteConfig);
|
$auth = new LoginFormAuthenticator();
|
||||||
$loginRequired = $auth->isLoginRequired(file_get_contents(__DIR__ . '/../../fixtures/nextinpact-login.html'));
|
$loginRequired = $auth->isLoginRequired($siteConfig, file_get_contents(__DIR__ . '/../../fixtures/nextinpact-login.html'));
|
||||||
|
|
||||||
$this->assertFalse($loginRequired);
|
$this->assertFalse($loginRequired);
|
||||||
}
|
}
|
||||||
|
@ -227,8 +227,8 @@ class LoginFormAuthenticatorTest extends TestCase
|
||||||
'notLoggedInXpath' => '//h2[@class="title_reserve_article"]',
|
'notLoggedInXpath' => '//h2[@class="title_reserve_article"]',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$auth = new LoginFormAuthenticator($siteConfig);
|
$auth = new LoginFormAuthenticator();
|
||||||
$loginRequired = $auth->isLoginRequired(file_get_contents(__DIR__ . '/../../fixtures/nextinpact-article.html'));
|
$loginRequired = $auth->isLoginRequired($siteConfig, file_get_contents(__DIR__ . '/../../fixtures/nextinpact-article.html'));
|
||||||
|
|
||||||
$this->assertTrue($loginRequired);
|
$this->assertTrue($loginRequired);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue