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.
This commit is contained in:
Jeremy Benoist 2017-05-09 22:25:18 +02:00
parent 0eb8220204
commit 94b232bbb8
No known key found for this signature in database
GPG Key ID: BCA73962457ACC3C
5 changed files with 59 additions and 23 deletions

View File

@ -84,7 +84,7 @@
"javibravo/simpleue": "^1.0", "javibravo/simpleue": "^1.0",
"symfony/dom-crawler": "^3.1", "symfony/dom-crawler": "^3.1",
"friendsofsymfony/jsrouting-bundle": "^1.6", "friendsofsymfony/jsrouting-bundle": "^1.6",
"bdunogier/guzzle-site-authenticator": "dev-master" "bdunogier/guzzle-site-authenticator": "dev-callback_forms"
}, },
"require-dev": { "require-dev": {
"doctrine/doctrine-fixtures-bundle": "~2.2", "doctrine/doctrine-fixtures-bundle": "~2.2",

View File

@ -6,28 +6,35 @@ use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig;
use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfigBuilder; use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfigBuilder;
use Graby\SiteConfig\ConfigBuilder; use Graby\SiteConfig\ConfigBuilder;
use OutOfRangeException; use OutOfRangeException;
use Psr\Log\LoggerInterface;
class GrabySiteConfigBuilder implements SiteConfigBuilder class GrabySiteConfigBuilder implements SiteConfigBuilder
{ {
/** /**
* @var \Graby\SiteConfig\ConfigBuilder * @var ConfigBuilder
*/ */
private $grabyConfigBuilder; private $grabyConfigBuilder;
/** /**
* @var array * @var array
*/ */
private $credentials; private $credentials;
/**
* @var LoggerInterface
*/
private $logger;
/** /**
* GrabySiteConfigBuilder constructor. * GrabySiteConfigBuilder constructor.
* *
* @param \Graby\SiteConfig\ConfigBuilder $grabyConfigBuilder * @param ConfigBuilder $grabyConfigBuilder
* @param array $credentials * @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->grabyConfigBuilder = $grabyConfigBuilder;
$this->credentials = $credentials; $this->credentials = $credentials;
$this->logger = $logger;
} }
/** /**
@ -47,6 +54,12 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
$host = substr($host, 4); $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); $config = $this->grabyConfigBuilder->buildForHost($host);
$parameters = [ $parameters = [
'host' => $host, 'host' => $host,
@ -56,14 +69,18 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
'passwordField' => $config->login_password_field ?: null, 'passwordField' => $config->login_password_field ?: null,
'extraFields' => $this->processExtraFields($config->login_extra_fields), 'extraFields' => $this->processExtraFields($config->login_extra_fields),
'notLoggedInXpath' => $config->not_logged_in_xpath ?: null, 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null,
'username' => $this->credentials[$host]['username'],
'password' => $this->credentials[$host]['password'],
]; ];
if (isset($this->credentials[$host])) { $config = new SiteConfig($parameters);
$parameters['username'] = $this->credentials[$host]['username'];
$parameters['password'] = $this->credentials[$host]['password'];
}
return new SiteConfig($parameters); // do not leak password in log
$parameters['password'] = '**masked**';
$this->logger->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]);
return $config;
} }
/** /**
@ -85,6 +102,7 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
if (strpos($extraField, '=') === false) { if (strpos($extraField, '=') === false) {
continue; continue;
} }
list($fieldName, $fieldValue) = explode('=', $extraField, 2); list($fieldName, $fieldValue) = explode('=', $extraField, 2);
$extraFields[$fieldName] = $fieldValue; $extraFields[$fieldName] = $fieldValue;
} }

View File

@ -51,6 +51,7 @@ class HttpClientFactory
$this->cookieJar->clear(); $this->cookieJar->clear();
// need to set the (shared) cookie jar // need to set the (shared) cookie jar
$client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar]]); $client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar]]);
foreach ($this->subscribers as $subscriber) { foreach ($this->subscribers as $subscriber) {
$client->getEmitter()->attach($subscriber); $client->getEmitter()->attach($subscriber);
} }

View File

@ -63,6 +63,9 @@ services:
arguments: arguments:
- "@wallabag_core.graby.config_builder" - "@wallabag_core.graby.config_builder"
- "%sites_credentials%" - "%sites_credentials%"
- '@logger'
tags:
- { name: monolog.logger, channel: graby }
# service alias override # service alias override
bd_guzzle_site_authenticator.site_config_builder: bd_guzzle_site_authenticator.site_config_builder:

View File

@ -2,6 +2,8 @@
namespace Tests\Wallabag\CoreBundle\GuzzleSiteAuthenticator; namespace Tests\Wallabag\CoreBundle\GuzzleSiteAuthenticator;
use Monolog\Handler\TestHandler;
use Monolog\Logger;
use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig; use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig;
use Graby\SiteConfig\SiteConfig as GrabySiteConfig; use Graby\SiteConfig\SiteConfig as GrabySiteConfig;
use PHPUnit_Framework_TestCase; use PHPUnit_Framework_TestCase;
@ -32,14 +34,19 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase
->with('example.com') ->with('example.com')
->will($this->returnValue($grabySiteConfig)); ->will($this->returnValue($grabySiteConfig));
$logger = new Logger('foo');
$handler = new TestHandler();
$logger->pushHandler($handler);
$this->builder = new GrabySiteConfigBuilder( $this->builder = new GrabySiteConfigBuilder(
$grabyConfigBuilderMock, $grabyConfigBuilderMock,
['example.com' => ['username' => 'foo', 'password' => 'bar']] ['example.com' => ['username' => 'foo', 'password' => 'bar']],
$logger
); );
$config = $this->builder->buildForHost('example.com'); $config = $this->builder->buildForHost('example.com');
self::assertEquals( $this->assertEquals(
new SiteConfig([ new SiteConfig([
'host' => 'example.com', 'host' => 'example.com',
'requiresLogin' => true, 'requiresLogin' => true,
@ -53,6 +60,10 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase
]), ]),
$config $config
); );
$records = $handler->getRecords();
$this->assertCount(1, $records, 'One log was recorded');
} }
public function testBuildConfigDoesntExist() public function testBuildConfigDoesntExist()
@ -67,19 +78,22 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase
->with('unknown.com') ->with('unknown.com')
->will($this->returnValue(new GrabySiteConfig())); ->will($this->returnValue(new GrabySiteConfig()));
$this->builder = new GrabySiteConfigBuilder($grabyConfigBuilderMock, []); $logger = new Logger('foo');
$handler = new TestHandler();
$logger->pushHandler($handler);
$this->builder = new GrabySiteConfigBuilder(
$grabyConfigBuilderMock,
[],
$logger
);
$config = $this->builder->buildForHost('unknown.com'); $config = $this->builder->buildForHost('unknown.com');
self::assertEquals( $this->assertFalse($config);
new SiteConfig([
'host' => 'unknown.com', $records = $handler->getRecords();
'requiresLogin' => false,
'username' => null, $this->assertCount(1, $records, 'One log was recorded');
'password' => null,
'extraFields' => [],
]),
$config
);
} }
} }