diff options
author | Jeremy Benoist <j0k3r@users.noreply.github.com> | 2016-11-22 19:12:53 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-22 19:12:53 +0100 |
commit | 176e0ea3caee9f4eccc1ddda5f84b14da2cca034 (patch) | |
tree | 543ff69ee6b56c6383f3f9758221fc64154dee5c /src | |
parent | 1d5dd2c2410d7866752bca5d65886afc6a7650ef (diff) | |
parent | d51093a7d964ca720793d0cfcf4af601f2de448a (diff) | |
download | wallabag-176e0ea3caee9f4eccc1ddda5f84b14da2cca034.tar.gz wallabag-176e0ea3caee9f4eccc1ddda5f84b14da2cca034.tar.zst wallabag-176e0ea3caee9f4eccc1ddda5f84b14da2cca034.zip |
Merge pull request #2317 from wallabag/restricted-access
Added authentication for restricted access articles
Diffstat (limited to 'src')
5 files changed, 165 insertions, 0 deletions
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 | |||
432 | 'value' => '0', | 432 | 'value' => '0', |
433 | 'section' => 'misc', | 433 | 'section' => 'misc', |
434 | ], | 434 | ], |
435 | [ | ||
436 | 'name' => 'restricted_access', | ||
437 | 'value' => '0', | ||
438 | 'section' => 'entry', | ||
439 | ], | ||
435 | ]; | 440 | ]; |
436 | 441 | ||
437 | foreach ($settings as $setting) { | 442 | 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 | |||
155 | 'value' => '0', | 155 | 'value' => '0', |
156 | 'section' => 'misc', | 156 | 'section' => 'misc', |
157 | ], | 157 | ], |
158 | [ | ||
159 | 'name' => 'restricted_access', | ||
160 | 'value' => '0', | ||
161 | 'section' => 'entry', | ||
162 | ], | ||
158 | ]; | 163 | ]; |
159 | 164 | ||
160 | foreach ($settings as $setting) { | 165 | foreach ($settings as $setting) { |
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 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\GuzzleSiteAuthenticator; | ||
4 | |||
5 | use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig; | ||
6 | use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfigBuilder; | ||
7 | use Graby\SiteConfig\ConfigBuilder; | ||
8 | use OutOfRangeException; | ||
9 | |||
10 | class GrabySiteConfigBuilder implements SiteConfigBuilder | ||
11 | { | ||
12 | /** | ||
13 | * @var \Graby\SiteConfig\ConfigBuilder | ||
14 | */ | ||
15 | private $grabyConfigBuilder; | ||
16 | /** | ||
17 | * @var array | ||
18 | */ | ||
19 | private $credentials; | ||
20 | |||
21 | /** | ||
22 | * GrabySiteConfigBuilder constructor. | ||
23 | * | ||
24 | * @param \Graby\SiteConfig\ConfigBuilder $grabyConfigBuilder | ||
25 | * @param array $credentials | ||
26 | */ | ||
27 | public function __construct(ConfigBuilder $grabyConfigBuilder, array $credentials = []) | ||
28 | { | ||
29 | $this->grabyConfigBuilder = $grabyConfigBuilder; | ||
30 | $this->credentials = $credentials; | ||
31 | } | ||
32 | |||
33 | /** | ||
34 | * Builds the SiteConfig for a host. | ||
35 | * | ||
36 | * @param string $host The "www." prefix is ignored | ||
37 | * | ||
38 | * @return SiteConfig | ||
39 | * | ||
40 | * @throws OutOfRangeException If there is no config for $host | ||
41 | */ | ||
42 | public function buildForHost($host) | ||
43 | { | ||
44 | // required by credentials below | ||
45 | $host = strtolower($host); | ||
46 | if (substr($host, 0, 4) == 'www.') { | ||
47 | $host = substr($host, 4); | ||
48 | } | ||
49 | |||
50 | $config = $this->grabyConfigBuilder->buildForHost($host); | ||
51 | $parameters = [ | ||
52 | 'host' => $host, | ||
53 | 'requiresLogin' => $config->requires_login ?: false, | ||
54 | 'loginUri' => $config->login_uri ?: null, | ||
55 | 'usernameField' => $config->login_username_field ?: null, | ||
56 | 'passwordField' => $config->login_password_field ?: null, | ||
57 | 'extraFields' => is_array($config->login_extra_fields) ? $config->login_extra_fields : [], | ||
58 | 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null, | ||
59 | ]; | ||
60 | |||
61 | if (isset($this->credentials[$host])) { | ||
62 | $parameters['username'] = $this->credentials[$host]['username']; | ||
63 | $parameters['password'] = $this->credentials[$host]['password']; | ||
64 | } | ||
65 | |||
66 | return new SiteConfig($parameters); | ||
67 | } | ||
68 | } | ||
diff --git a/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php new file mode 100644 index 00000000..8891887b --- /dev/null +++ b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php | |||
@@ -0,0 +1,54 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Helper; | ||
4 | |||
5 | use Graby\Ring\Client\SafeCurlHandler; | ||
6 | use GuzzleHttp\Client; | ||
7 | use GuzzleHttp\Cookie\CookieJar; | ||
8 | use GuzzleHttp\Event\SubscriberInterface; | ||
9 | |||
10 | /** | ||
11 | * Builds and configures the Guzzle HTTP client. | ||
12 | */ | ||
13 | class HttpClientFactory | ||
14 | { | ||
15 | /** @var \GuzzleHttp\Event\SubscriberInterface */ | ||
16 | private $authenticatorSubscriber; | ||
17 | |||
18 | /** @var \GuzzleHttp\Cookie\CookieJar */ | ||
19 | private $cookieJar; | ||
20 | |||
21 | private $restrictedAccess; | ||
22 | |||
23 | /** | ||
24 | * HttpClientFactory constructor. | ||
25 | * | ||
26 | * @param \GuzzleHttp\Event\SubscriberInterface $authenticatorSubscriber | ||
27 | * @param \GuzzleHttp\Cookie\CookieJar $cookieJar | ||
28 | * @param string $restrictedAccess this param is a kind of boolean. Values: 0 or 1 | ||
29 | */ | ||
30 | public function __construct(SubscriberInterface $authenticatorSubscriber, CookieJar $cookieJar, $restrictedAccess) | ||
31 | { | ||
32 | $this->authenticatorSubscriber = $authenticatorSubscriber; | ||
33 | $this->cookieJar = $cookieJar; | ||
34 | $this->restrictedAccess = $restrictedAccess; | ||
35 | } | ||
36 | |||
37 | /** | ||
38 | * @return \GuzzleHttp\Client|null | ||
39 | */ | ||
40 | public function buildHttpClient() | ||
41 | { | ||
42 | if (0 === (int) $this->restrictedAccess) { | ||
43 | return null; | ||
44 | } | ||
45 | |||
46 | // we clear the cookie to avoid websites who use cookies for analytics | ||
47 | $this->cookieJar->clear(); | ||
48 | // need to set the (shared) cookie jar | ||
49 | $client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar]]); | ||
50 | $client->getEmitter()->attach($this->authenticatorSubscriber); | ||
51 | |||
52 | return $client; | ||
53 | } | ||
54 | } | ||
diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml index 0280bc18..bcf0c9ca 100644 --- a/src/Wallabag/CoreBundle/Resources/config/services.yml +++ b/src/Wallabag/CoreBundle/Resources/config/services.yml | |||
@@ -41,11 +41,44 @@ services: | |||
41 | arguments: | 41 | arguments: |
42 | - | 42 | - |
43 | error_message: '%wallabag_core.fetching_error_message%' | 43 | error_message: '%wallabag_core.fetching_error_message%' |
44 | - "@wallabag_core.guzzle.http_client" | ||
45 | - "@wallabag_core.graby.config_builder" | ||
44 | calls: | 46 | calls: |
45 | - [ setLogger, [ "@logger" ] ] | 47 | - [ setLogger, [ "@logger" ] ] |
46 | tags: | 48 | tags: |
47 | - { name: monolog.logger, channel: graby } | 49 | - { name: monolog.logger, channel: graby } |
48 | 50 | ||
51 | wallabag_core.graby.config_builder: | ||
52 | class: Graby\SiteConfig\ConfigBuilder | ||
53 | arguments: | ||
54 | - {} | ||
55 | - "@logger" | ||
56 | |||
57 | wallabag_core.guzzle.http_client: | ||
58 | class: GuzzleHttp\ClientInterface | ||
59 | factory: ["@wallabag_core.guzzle.http_client_factory", buildHttpClient] | ||
60 | |||
61 | wallabag_core.guzzle_authenticator.config_builder: | ||
62 | class: Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder | ||
63 | arguments: | ||
64 | - "@wallabag_core.graby.config_builder" | ||
65 | - "%sites_credentials%" | ||
66 | |||
67 | # service alias override | ||
68 | bd_guzzle_site_authenticator.site_config_builder: | ||
69 | alias: wallabag_core.guzzle_authenticator.config_builder | ||
70 | |||
71 | wallabag_core.guzzle.http_client_factory: | ||
72 | class: Wallabag\CoreBundle\Helper\HttpClientFactory | ||
73 | arguments: | ||
74 | - "@bd_guzzle_site_authenticator.authenticator_subscriber" | ||
75 | - "@wallabag_core.guzzle.cookie_jar" | ||
76 | - '@=service(''craue_config'').get(''restricted_access'')' | ||
77 | |||
78 | wallabag_core.guzzle.cookie_jar: | ||
79 | class: GuzzleHttp\Cookie\FileCookieJar | ||
80 | arguments: ["%kernel.cache_dir%/cookiejar.json"] | ||
81 | |||
49 | wallabag_core.content_proxy: | 82 | wallabag_core.content_proxy: |
50 | class: Wallabag\CoreBundle\Helper\ContentProxy | 83 | class: Wallabag\CoreBundle\Helper\ContentProxy |
51 | arguments: | 84 | arguments: |