forked from wallabag/wallabag
Move site config from property to parameter in LoginFormAuthenticator
This commit is contained in:
@ -3,6 +3,7 @@
|
||||
namespace Wallabag\SiteConfig\Authenticator;
|
||||
|
||||
use GuzzleHttp\ClientInterface;
|
||||
use Wallabag\SiteConfig\SiteConfig;
|
||||
|
||||
interface Authenticator
|
||||
{
|
||||
@ -11,14 +12,14 @@ interface Authenticator
|
||||
*
|
||||
* @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).
|
||||
*
|
||||
* @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.
|
||||
@ -27,5 +28,5 @@ interface Authenticator
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
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
|
||||
{
|
||||
/** @var SiteConfig */
|
||||
private $siteConfig;
|
||||
|
||||
public function __construct(SiteConfig $siteConfig)
|
||||
{
|
||||
// @todo OptionResolver
|
||||
$this->siteConfig = $siteConfig;
|
||||
}
|
||||
|
||||
public function login(ClientInterface $guzzle)
|
||||
public function login(SiteConfig $siteConfig, ClientInterface $guzzle)
|
||||
{
|
||||
$postFields = [
|
||||
$this->siteConfig->getUsernameField() => $this->siteConfig->getUsername(),
|
||||
$this->siteConfig->getPasswordField() => $this->siteConfig->getPassword(),
|
||||
$siteConfig->getUsernameField() => $siteConfig->getUsername(),
|
||||
$siteConfig->getPasswordField() => $siteConfig->getPassword(),
|
||||
] + $this->getExtraFields($guzzle);
|
||||
|
||||
$guzzle->post(
|
||||
$this->siteConfig->getLoginUri(),
|
||||
$siteConfig->getLoginUri(),
|
||||
['body' => $postFields, 'allow_redirects' => true, 'verify' => false]
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isLoggedIn(ClientInterface $guzzle)
|
||||
public function isLoggedIn(SiteConfig $siteConfig, ClientInterface $guzzle)
|
||||
{
|
||||
if (($cookieJar = $guzzle->getDefaultOption('cookies')) instanceof CookieJar) {
|
||||
/** @var \GuzzleHttp\Cookie\SetCookie $cookie */
|
||||
foreach ($cookieJar as $cookie) {
|
||||
// check required cookies
|
||||
if ($cookie->getDomain() === $this->siteConfig->getHost()) {
|
||||
if ($cookie->getDomain() === $siteConfig->getHost()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -50,13 +41,13 @@ class LoginFormAuthenticator implements Authenticator
|
||||
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
|
||||
try {
|
||||
$crawler = new Crawler((string) $html);
|
||||
|
||||
$loggedIn = $crawler->evaluate((string) $this->siteConfig->getNotLoggedInXpath());
|
||||
$loggedIn = $crawler->evaluate((string) $siteConfig->getNotLoggedInXpath());
|
||||
} catch (\Throwable $e) {
|
||||
return false;
|
||||
}
|
||||
@ -70,17 +61,17 @@ class LoginFormAuthenticator implements Authenticator
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getExtraFields(ClientInterface $guzzle)
|
||||
private function getExtraFields(SiteConfig $siteConfig, ClientInterface $guzzle)
|
||||
{
|
||||
$extraFields = [];
|
||||
|
||||
foreach ($this->siteConfig->getExtraFields() as $fieldName => $fieldValue) {
|
||||
foreach ($siteConfig->getExtraFields() as $fieldName => $fieldValue) {
|
||||
if ('@=' === substr($fieldValue, 0, 2)) {
|
||||
$expressionLanguage = $this->getExpressionLanguage($guzzle);
|
||||
$fieldValue = $expressionLanguage->evaluate(
|
||||
substr($fieldValue, 2),
|
||||
[
|
||||
'config' => $this->siteConfig,
|
||||
'config' => $siteConfig,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user