From 7aab0ecf2f78ce58f28b53c1fa19bfd824cc3cd7 Mon Sep 17 00:00:00 2001 From: Bertrand Dunogier Date: Thu, 29 Sep 2016 10:14:43 +0200 Subject: Added authentication for restricted access articles Fix #438. Thank you so much @bdunogier --- .../GrabySiteConfigBuilder.php | 68 ++++++++++++++++++++++ .../CoreBundle/Helper/HttpClientFactory.php | 44 ++++++++++++++ .../CoreBundle/Resources/config/services.yml | 32 ++++++++++ 3 files changed, 144 insertions(+) create mode 100644 src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php create mode 100644 src/Wallabag/CoreBundle/Helper/HttpClientFactory.php (limited to 'src/Wallabag') diff --git a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php new file mode 100644 index 00000000..6d4129e8 --- /dev/null +++ b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php @@ -0,0 +1,68 @@ +grabyConfigBuilder = $grabyConfigBuilder; + $this->credentials = $credentials; + } + + /** + * Builds the SiteConfig for a host. + * + * @param string $host The "www." prefix is ignored + * + * @return SiteConfig + * + * @throws OutOfRangeException If there is no config for $host + */ + public function buildForHost($host) + { + // required by credentials below + $host = strtolower($host); + if (substr($host, 0, 4) == 'www.') { + $host = substr($host, 4); + } + + $config = $this->grabyConfigBuilder->buildForHost($host); + $parameters = [ + 'host' => $host, + 'requiresLogin' => $config->requires_login ?: false, + 'loginUri' => $config->login_uri ?: null, + 'usernameField' => $config->login_username_field ?: null, + 'passwordField' => $config->login_password_field ?: null, + 'extraFields' => is_array($config->login_extra_fields) ? $config->login_extra_fields : [], + 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null, + ]; + + if (isset($this->credentials[$host])) { + $parameters['username'] = $this->credentials[$host]['username']; + $parameters['password'] = $this->credentials[$host]['password']; + } + + return new SiteConfig($parameters); + } +} diff --git a/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php new file mode 100644 index 00000000..3e1d1ed5 --- /dev/null +++ b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php @@ -0,0 +1,44 @@ +authenticatorSubscriber = $authenticatorSubscriber; + $this->cookieJar = $cookieJar; + } + + /** + * @return \GuzzleHttp\Client + */ + public function buildHttpClient() + { + // need to set the (shared) cookie jar + $client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar]]); + $client->getEmitter()->attach($this->authenticatorSubscriber); + + return $client; + } +} diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml index 0280bc18..1b7f39fc 100644 --- a/src/Wallabag/CoreBundle/Resources/config/services.yml +++ b/src/Wallabag/CoreBundle/Resources/config/services.yml @@ -41,11 +41,43 @@ services: arguments: - error_message: '%wallabag_core.fetching_error_message%' + - "@wallabag_core.guzzle.http_client" + - "@wallabag_core.graby.config_builder" calls: - [ setLogger, [ "@logger" ] ] tags: - { name: monolog.logger, channel: graby } + wallabag_core.graby.config_builder: + class: Graby\SiteConfig\ConfigBuilder + arguments: + - {} + - "@logger" + + wallabag_core.guzzle.http_client: + class: GuzzleHttp\ClientInterface + factory: ["@wallabag_core.guzzle.http_client_factory", buildHttpClient] + + wallabag_core.guzzle_authenticator.config_builder: + class: Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder + arguments: + - "@wallabag_core.graby.config_builder" + - "%sites_credentials%" + + # service alias override + bd_guzzle_site_authenticator.site_config_builder: + alias: wallabag_core.guzzle_authenticator.config_builder + + wallabag_core.guzzle.http_client_factory: + class: Wallabag\CoreBundle\Helper\HttpClientFactory + arguments: + - "@bd_guzzle_site_authenticator.authenticator_subscriber" + - "@wallabag_core.guzzle.cookie_jar" + + wallabag_core.guzzle.cookie_jar: + class: GuzzleHttp\Cookie\FileCookieJar + arguments: ["%kernel.cache_dir%/cookiejar.json"] + wallabag_core.content_proxy: class: Wallabag\CoreBundle\Helper\ContentProxy arguments: -- cgit v1.2.3 From 40f3ea57fbc86247495ef3c335f40f966a69aa50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Tue, 22 Nov 2016 14:25:51 +0100 Subject: Cleared CookieJar to avoid websites who use cookies for analytics --- src/Wallabag/CoreBundle/Helper/HttpClientFactory.php | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/Wallabag') diff --git a/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php index 3e1d1ed5..161a5e9f 100644 --- a/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php +++ b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php @@ -35,6 +35,8 @@ class HttpClientFactory */ public function buildHttpClient() { + // we clear the cookie to avoid websites who use cookies for analytics + $this->cookieJar->clear(); // need to set the (shared) cookie jar $client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar]]); $client->getEmitter()->attach($this->authenticatorSubscriber); -- cgit v1.2.3 From d64bf7953b0e4e793d7f75ec50bf6e42652560b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Tue, 22 Nov 2016 14:56:53 +0100 Subject: Added internal setting to enable/disable articles with paywall --- src/Wallabag/CoreBundle/Command/InstallCommand.php | 5 +++++ src/Wallabag/CoreBundle/DataFixtures/ORM/LoadSettingData.php | 5 +++++ src/Wallabag/CoreBundle/Helper/HttpClientFactory.php | 12 ++++++++++-- src/Wallabag/CoreBundle/Resources/config/services.yml | 1 + 4 files changed, 21 insertions(+), 2 deletions(-) (limited to 'src/Wallabag') diff --git a/src/Wallabag/CoreBundle/Command/InstallCommand.php b/src/Wallabag/CoreBundle/Command/InstallCommand.php index e95c3a7b..f0738b91 100644 --- a/src/Wallabag/CoreBundle/Command/InstallCommand.php +++ b/src/Wallabag/CoreBundle/Command/InstallCommand.php @@ -432,6 +432,11 @@ class InstallCommand extends ContainerAwareCommand 'value' => '0', 'section' => 'misc', ], + [ + 'name' => 'restricted_access', + 'value' => '0', + 'section' => 'entry', + ], ]; foreach ($settings as $setting) { diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadSettingData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadSettingData.php index 1f74891a..a723656e 100644 --- a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadSettingData.php +++ b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadSettingData.php @@ -155,6 +155,11 @@ class LoadSettingData extends AbstractFixture implements OrderedFixtureInterface 'value' => '0', 'section' => 'misc', ], + [ + 'name' => 'restricted_access', + 'value' => '0', + 'section' => 'entry', + ], ]; foreach ($settings as $setting) { diff --git a/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php index 161a5e9f..ca84c8da 100644 --- a/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php +++ b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php @@ -18,23 +18,31 @@ class HttpClientFactory /** @var \GuzzleHttp\Cookie\CookieJar */ private $cookieJar; + private $restrictedAccess; + /** * HttpClientFactory constructor. * * @param \GuzzleHttp\Event\SubscriberInterface $authenticatorSubscriber * @param \GuzzleHttp\Cookie\CookieJar $cookieJar + * @param string $restrictedAccess */ - public function __construct(SubscriberInterface $authenticatorSubscriber, CookieJar $cookieJar) + public function __construct(SubscriberInterface $authenticatorSubscriber, CookieJar $cookieJar, $restrictedAccess) { $this->authenticatorSubscriber = $authenticatorSubscriber; $this->cookieJar = $cookieJar; + $this->restrictedAccess = $restrictedAccess; } /** - * @return \GuzzleHttp\Client + * @return \GuzzleHttp\Client|null */ public function buildHttpClient() { + if (0 === (int) $this->restrictedAccess) { + return null; + } + // we clear the cookie to avoid websites who use cookies for analytics $this->cookieJar->clear(); // need to set the (shared) cookie jar diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml index 1b7f39fc..bcf0c9ca 100644 --- a/src/Wallabag/CoreBundle/Resources/config/services.yml +++ b/src/Wallabag/CoreBundle/Resources/config/services.yml @@ -73,6 +73,7 @@ services: arguments: - "@bd_guzzle_site_authenticator.authenticator_subscriber" - "@wallabag_core.guzzle.cookie_jar" + - '@=service(''craue_config'').get(''restricted_access'')' wallabag_core.guzzle.cookie_jar: class: GuzzleHttp\Cookie\FileCookieJar -- cgit v1.2.3 From d51093a7d964ca720793d0cfcf4af601f2de448a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Tue, 22 Nov 2016 15:58:00 +0100 Subject: Added documentation and missing translations --- src/Wallabag/CoreBundle/Helper/HttpClientFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Wallabag') diff --git a/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php index ca84c8da..8891887b 100644 --- a/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php +++ b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php @@ -25,7 +25,7 @@ class HttpClientFactory * * @param \GuzzleHttp\Event\SubscriberInterface $authenticatorSubscriber * @param \GuzzleHttp\Cookie\CookieJar $cookieJar - * @param string $restrictedAccess + * @param string $restrictedAccess this param is a kind of boolean. Values: 0 or 1 */ public function __construct(SubscriberInterface $authenticatorSubscriber, CookieJar $cookieJar, $restrictedAccess) { -- cgit v1.2.3