diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2017-05-09 22:25:18 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2017-05-09 22:53:42 +0200 |
commit | 94b232bbb8de4699911a6446a1a96f75370cab50 (patch) | |
tree | 9f6b43ca6613e800e1cf1262bb1a79139cda8d67 | |
parent | 0eb8220204953b874ebd2dbd0362973f3f45074c (diff) | |
download | wallabag-94b232bbb8de4699911a6446a1a96f75370cab50.tar.gz wallabag-94b232bbb8de4699911a6446a1a96f75370cab50.tar.zst wallabag-94b232bbb8de4699911a6446a1a96f75370cab50.zip |
Skip auth when no credentials are found
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.
5 files changed, 59 insertions, 23 deletions
diff --git a/composer.json b/composer.json index bfd42339..9b69a12c 100644 --- a/composer.json +++ b/composer.json | |||
@@ -84,7 +84,7 @@ | |||
84 | "javibravo/simpleue": "^1.0", | 84 | "javibravo/simpleue": "^1.0", |
85 | "symfony/dom-crawler": "^3.1", | 85 | "symfony/dom-crawler": "^3.1", |
86 | "friendsofsymfony/jsrouting-bundle": "^1.6", | 86 | "friendsofsymfony/jsrouting-bundle": "^1.6", |
87 | "bdunogier/guzzle-site-authenticator": "dev-master" | 87 | "bdunogier/guzzle-site-authenticator": "dev-callback_forms" |
88 | }, | 88 | }, |
89 | "require-dev": { | 89 | "require-dev": { |
90 | "doctrine/doctrine-fixtures-bundle": "~2.2", | 90 | "doctrine/doctrine-fixtures-bundle": "~2.2", |
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; | |||
6 | use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfigBuilder; | 6 | use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfigBuilder; |
7 | use Graby\SiteConfig\ConfigBuilder; | 7 | use Graby\SiteConfig\ConfigBuilder; |
8 | use OutOfRangeException; | 8 | use OutOfRangeException; |
9 | use Psr\Log\LoggerInterface; | ||
9 | 10 | ||
10 | class GrabySiteConfigBuilder implements SiteConfigBuilder | 11 | class GrabySiteConfigBuilder implements SiteConfigBuilder |
11 | { | 12 | { |
12 | /** | 13 | /** |
13 | * @var \Graby\SiteConfig\ConfigBuilder | 14 | * @var ConfigBuilder |
14 | */ | 15 | */ |
15 | private $grabyConfigBuilder; | 16 | private $grabyConfigBuilder; |
16 | /** | 17 | /** |
17 | * @var array | 18 | * @var array |
18 | */ | 19 | */ |
19 | private $credentials; | 20 | private $credentials; |
21 | /** | ||
22 | * @var LoggerInterface | ||
23 | */ | ||
24 | private $logger; | ||
20 | 25 | ||
21 | /** | 26 | /** |
22 | * GrabySiteConfigBuilder constructor. | 27 | * GrabySiteConfigBuilder constructor. |
23 | * | 28 | * |
24 | * @param \Graby\SiteConfig\ConfigBuilder $grabyConfigBuilder | 29 | * @param ConfigBuilder $grabyConfigBuilder |
25 | * @param array $credentials | 30 | * @param array $credentials |
31 | * @param LoggerInterface $logger | ||
26 | */ | 32 | */ |
27 | public function __construct(ConfigBuilder $grabyConfigBuilder, array $credentials = []) | 33 | public function __construct(ConfigBuilder $grabyConfigBuilder, array $credentials, LoggerInterface $logger) |
28 | { | 34 | { |
29 | $this->grabyConfigBuilder = $grabyConfigBuilder; | 35 | $this->grabyConfigBuilder = $grabyConfigBuilder; |
30 | $this->credentials = $credentials; | 36 | $this->credentials = $credentials; |
37 | $this->logger = $logger; | ||
31 | } | 38 | } |
32 | 39 | ||
33 | /** | 40 | /** |
@@ -47,6 +54,12 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder | |||
47 | $host = substr($host, 4); | 54 | $host = substr($host, 4); |
48 | } | 55 | } |
49 | 56 | ||
57 | if (!isset($this->credentials[$host])) { | ||
58 | $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]); | ||
59 | |||
60 | return false; | ||
61 | } | ||
62 | |||
50 | $config = $this->grabyConfigBuilder->buildForHost($host); | 63 | $config = $this->grabyConfigBuilder->buildForHost($host); |
51 | $parameters = [ | 64 | $parameters = [ |
52 | 'host' => $host, | 65 | 'host' => $host, |
@@ -56,14 +69,18 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder | |||
56 | 'passwordField' => $config->login_password_field ?: null, | 69 | 'passwordField' => $config->login_password_field ?: null, |
57 | 'extraFields' => $this->processExtraFields($config->login_extra_fields), | 70 | 'extraFields' => $this->processExtraFields($config->login_extra_fields), |
58 | 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null, | 71 | 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null, |
72 | 'username' => $this->credentials[$host]['username'], | ||
73 | 'password' => $this->credentials[$host]['password'], | ||
59 | ]; | 74 | ]; |
60 | 75 | ||
61 | if (isset($this->credentials[$host])) { | 76 | $config = new SiteConfig($parameters); |
62 | $parameters['username'] = $this->credentials[$host]['username']; | 77 | |
63 | $parameters['password'] = $this->credentials[$host]['password']; | 78 | // do not leak password in log |
64 | } | 79 | $parameters['password'] = '**masked**'; |
65 | 80 | ||
66 | return new SiteConfig($parameters); | 81 | $this->logger->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]); |
82 | |||
83 | return $config; | ||
67 | } | 84 | } |
68 | 85 | ||
69 | /** | 86 | /** |
@@ -85,6 +102,7 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder | |||
85 | if (strpos($extraField, '=') === false) { | 102 | if (strpos($extraField, '=') === false) { |
86 | continue; | 103 | continue; |
87 | } | 104 | } |
105 | |||
88 | list($fieldName, $fieldValue) = explode('=', $extraField, 2); | 106 | list($fieldName, $fieldValue) = explode('=', $extraField, 2); |
89 | $extraFields[$fieldName] = $fieldValue; | 107 | $extraFields[$fieldName] = $fieldValue; |
90 | } | 108 | } |
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 | |||
51 | $this->cookieJar->clear(); | 51 | $this->cookieJar->clear(); |
52 | // need to set the (shared) cookie jar | 52 | // need to set the (shared) cookie jar |
53 | $client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar]]); | 53 | $client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar]]); |
54 | |||
54 | foreach ($this->subscribers as $subscriber) { | 55 | foreach ($this->subscribers as $subscriber) { |
55 | $client->getEmitter()->attach($subscriber); | 56 | $client->getEmitter()->attach($subscriber); |
56 | } | 57 | } |
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: | |||
63 | arguments: | 63 | arguments: |
64 | - "@wallabag_core.graby.config_builder" | 64 | - "@wallabag_core.graby.config_builder" |
65 | - "%sites_credentials%" | 65 | - "%sites_credentials%" |
66 | - '@logger' | ||
67 | tags: | ||
68 | - { name: monolog.logger, channel: graby } | ||
66 | 69 | ||
67 | # service alias override | 70 | # service alias override |
68 | bd_guzzle_site_authenticator.site_config_builder: | 71 | bd_guzzle_site_authenticator.site_config_builder: |
diff --git a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php index 8341b11f..8b50bce9 100644 --- a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php +++ b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php | |||
@@ -2,6 +2,8 @@ | |||
2 | 2 | ||
3 | namespace Tests\Wallabag\CoreBundle\GuzzleSiteAuthenticator; | 3 | namespace Tests\Wallabag\CoreBundle\GuzzleSiteAuthenticator; |
4 | 4 | ||
5 | use Monolog\Handler\TestHandler; | ||
6 | use Monolog\Logger; | ||
5 | use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig; | 7 | use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig; |
6 | use Graby\SiteConfig\SiteConfig as GrabySiteConfig; | 8 | use Graby\SiteConfig\SiteConfig as GrabySiteConfig; |
7 | use PHPUnit_Framework_TestCase; | 9 | use PHPUnit_Framework_TestCase; |
@@ -32,14 +34,19 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase | |||
32 | ->with('example.com') | 34 | ->with('example.com') |
33 | ->will($this->returnValue($grabySiteConfig)); | 35 | ->will($this->returnValue($grabySiteConfig)); |
34 | 36 | ||
37 | $logger = new Logger('foo'); | ||
38 | $handler = new TestHandler(); | ||
39 | $logger->pushHandler($handler); | ||
40 | |||
35 | $this->builder = new GrabySiteConfigBuilder( | 41 | $this->builder = new GrabySiteConfigBuilder( |
36 | $grabyConfigBuilderMock, | 42 | $grabyConfigBuilderMock, |
37 | ['example.com' => ['username' => 'foo', 'password' => 'bar']] | 43 | ['example.com' => ['username' => 'foo', 'password' => 'bar']], |
44 | $logger | ||
38 | ); | 45 | ); |
39 | 46 | ||
40 | $config = $this->builder->buildForHost('example.com'); | 47 | $config = $this->builder->buildForHost('example.com'); |
41 | 48 | ||
42 | self::assertEquals( | 49 | $this->assertEquals( |
43 | new SiteConfig([ | 50 | new SiteConfig([ |
44 | 'host' => 'example.com', | 51 | 'host' => 'example.com', |
45 | 'requiresLogin' => true, | 52 | 'requiresLogin' => true, |
@@ -53,6 +60,10 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase | |||
53 | ]), | 60 | ]), |
54 | $config | 61 | $config |
55 | ); | 62 | ); |
63 | |||
64 | $records = $handler->getRecords(); | ||
65 | |||
66 | $this->assertCount(1, $records, 'One log was recorded'); | ||
56 | } | 67 | } |
57 | 68 | ||
58 | public function testBuildConfigDoesntExist() | 69 | public function testBuildConfigDoesntExist() |
@@ -67,19 +78,22 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase | |||
67 | ->with('unknown.com') | 78 | ->with('unknown.com') |
68 | ->will($this->returnValue(new GrabySiteConfig())); | 79 | ->will($this->returnValue(new GrabySiteConfig())); |
69 | 80 | ||
70 | $this->builder = new GrabySiteConfigBuilder($grabyConfigBuilderMock, []); | 81 | $logger = new Logger('foo'); |
82 | $handler = new TestHandler(); | ||
83 | $logger->pushHandler($handler); | ||
84 | |||
85 | $this->builder = new GrabySiteConfigBuilder( | ||
86 | $grabyConfigBuilderMock, | ||
87 | [], | ||
88 | $logger | ||
89 | ); | ||
71 | 90 | ||
72 | $config = $this->builder->buildForHost('unknown.com'); | 91 | $config = $this->builder->buildForHost('unknown.com'); |
73 | 92 | ||
74 | self::assertEquals( | 93 | $this->assertFalse($config); |
75 | new SiteConfig([ | 94 | |
76 | 'host' => 'unknown.com', | 95 | $records = $handler->getRecords(); |
77 | 'requiresLogin' => false, | 96 | |
78 | 'username' => null, | 97 | $this->assertCount(1, $records, 'One log was recorded'); |
79 | 'password' => null, | ||
80 | 'extraFields' => [], | ||
81 | ]), | ||
82 | $config | ||
83 | ); | ||
84 | } | 98 | } |
85 | } | 99 | } |