1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-07-22 17:18:37 +00:00

Merge pull request #2317 from wallabag/restricted-access

Added authentication for restricted access articles
This commit is contained in:
Jeremy Benoist 2016-11-22 19:12:53 +01:00 committed by GitHub
commit 176e0ea3ca
28 changed files with 484 additions and 1 deletions

View file

@ -432,6 +432,11 @@ class InstallCommand extends ContainerAwareCommand
'value' => '0',
'section' => 'misc',
],
[
'name' => 'restricted_access',
'value' => '0',
'section' => 'entry',
],
];
foreach ($settings as $setting) {

View file

@ -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) {

View file

@ -0,0 +1,68 @@
<?php
namespace Wallabag\CoreBundle\GuzzleSiteAuthenticator;
use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig;
use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfigBuilder;
use Graby\SiteConfig\ConfigBuilder;
use OutOfRangeException;
class GrabySiteConfigBuilder implements SiteConfigBuilder
{
/**
* @var \Graby\SiteConfig\ConfigBuilder
*/
private $grabyConfigBuilder;
/**
* @var array
*/
private $credentials;
/**
* GrabySiteConfigBuilder constructor.
*
* @param \Graby\SiteConfig\ConfigBuilder $grabyConfigBuilder
* @param array $credentials
*/
public function __construct(ConfigBuilder $grabyConfigBuilder, array $credentials = [])
{
$this->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);
}
}

View file

@ -0,0 +1,54 @@
<?php
namespace Wallabag\CoreBundle\Helper;
use Graby\Ring\Client\SafeCurlHandler;
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Event\SubscriberInterface;
/**
* Builds and configures the Guzzle HTTP client.
*/
class HttpClientFactory
{
/** @var \GuzzleHttp\Event\SubscriberInterface */
private $authenticatorSubscriber;
/** @var \GuzzleHttp\Cookie\CookieJar */
private $cookieJar;
private $restrictedAccess;
/**
* HttpClientFactory constructor.
*
* @param \GuzzleHttp\Event\SubscriberInterface $authenticatorSubscriber
* @param \GuzzleHttp\Cookie\CookieJar $cookieJar
* @param string $restrictedAccess this param is a kind of boolean. Values: 0 or 1
*/
public function __construct(SubscriberInterface $authenticatorSubscriber, CookieJar $cookieJar, $restrictedAccess)
{
$this->authenticatorSubscriber = $authenticatorSubscriber;
$this->cookieJar = $cookieJar;
$this->restrictedAccess = $restrictedAccess;
}
/**
* @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
$client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar]]);
$client->getEmitter()->attach($this->authenticatorSubscriber);
return $client;
}
}

View file

@ -41,11 +41,44 @@ 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"
- '@=service(''craue_config'').get(''restricted_access'')'
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: