From 94b232bbb8de4699911a6446a1a96f75370cab50 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Tue, 9 May 2017 22:25:18 +0200 Subject: Skip auth when no credentials are found MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If we can’t find a credential for the current host, even if it required login, we won’t add them and website will be fetched without any login. --- .../GrabySiteConfigBuilder.php | 36 ++++++++++++++++------ .../CoreBundle/Helper/HttpClientFactory.php | 1 + .../CoreBundle/Resources/config/services.yml | 3 ++ 3 files changed, 31 insertions(+), 9 deletions(-) (limited to 'src/Wallabag') diff --git a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php index 1c866f17..a16ed49d 100644 --- a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php +++ b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php @@ -6,28 +6,35 @@ use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig; use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfigBuilder; use Graby\SiteConfig\ConfigBuilder; use OutOfRangeException; +use Psr\Log\LoggerInterface; class GrabySiteConfigBuilder implements SiteConfigBuilder { /** - * @var \Graby\SiteConfig\ConfigBuilder + * @var ConfigBuilder */ private $grabyConfigBuilder; /** * @var array */ private $credentials; + /** + * @var LoggerInterface + */ + private $logger; /** * GrabySiteConfigBuilder constructor. * - * @param \Graby\SiteConfig\ConfigBuilder $grabyConfigBuilder - * @param array $credentials + * @param ConfigBuilder $grabyConfigBuilder + * @param array $credentials + * @param LoggerInterface $logger */ - public function __construct(ConfigBuilder $grabyConfigBuilder, array $credentials = []) + public function __construct(ConfigBuilder $grabyConfigBuilder, array $credentials, LoggerInterface $logger) { $this->grabyConfigBuilder = $grabyConfigBuilder; $this->credentials = $credentials; + $this->logger = $logger; } /** @@ -47,6 +54,12 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder $host = substr($host, 4); } + if (!isset($this->credentials[$host])) { + $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]); + + return false; + } + $config = $this->grabyConfigBuilder->buildForHost($host); $parameters = [ 'host' => $host, @@ -56,14 +69,18 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder 'passwordField' => $config->login_password_field ?: null, 'extraFields' => $this->processExtraFields($config->login_extra_fields), 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null, + 'username' => $this->credentials[$host]['username'], + 'password' => $this->credentials[$host]['password'], ]; - if (isset($this->credentials[$host])) { - $parameters['username'] = $this->credentials[$host]['username']; - $parameters['password'] = $this->credentials[$host]['password']; - } + $config = new SiteConfig($parameters); + + // do not leak password in log + $parameters['password'] = '**masked**'; - return new SiteConfig($parameters); + $this->logger->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]); + + return $config; } /** @@ -85,6 +102,7 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder if (strpos($extraField, '=') === false) { continue; } + list($fieldName, $fieldValue) = explode('=', $extraField, 2); $extraFields[$fieldName] = $fieldValue; } diff --git a/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php index 11ef26d8..43f5b119 100644 --- a/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php +++ b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php @@ -51,6 +51,7 @@ class HttpClientFactory $this->cookieJar->clear(); // need to set the (shared) cookie jar $client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar]]); + foreach ($this->subscribers as $subscriber) { $client->getEmitter()->attach($subscriber); } diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml index 68f900a1..6c9195ce 100644 --- a/src/Wallabag/CoreBundle/Resources/config/services.yml +++ b/src/Wallabag/CoreBundle/Resources/config/services.yml @@ -63,6 +63,9 @@ services: arguments: - "@wallabag_core.graby.config_builder" - "%sites_credentials%" + - '@logger' + tags: + - { name: monolog.logger, channel: graby } # service alias override bd_guzzle_site_authenticator.site_config_builder: -- cgit v1.2.3 From 150d9ec1dcca0f71ba19a7f55e599eb716541dc3 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Wed, 10 May 2017 10:37:53 +0200 Subject: Handle empty configuration --- .../CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Wallabag') diff --git a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php index a16ed49d..c712bb26 100644 --- a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php +++ b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php @@ -54,7 +54,7 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder $host = substr($host, 4); } - if (!isset($this->credentials[$host])) { + if (empty($this->credentials[$host])) { $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]); return false; -- cgit v1.2.3