]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Skip auth when no credentials are found
authorJeremy Benoist <jeremy.benoist@gmail.com>
Tue, 9 May 2017 20:25:18 +0000 (22:25 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Tue, 9 May 2017 20:53:42 +0000 (22:53 +0200)
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.

composer.json
src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
src/Wallabag/CoreBundle/Helper/HttpClientFactory.php
src/Wallabag/CoreBundle/Resources/config/services.yml
tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php

index bfd42339b1e0bf6fbe2683578b1e5a7fbefe4ed2..9b69a12cac533588ec9c8f75c11402af516bfc5c 100644 (file)
@@ -84,7 +84,7 @@
         "javibravo/simpleue": "^1.0",
         "symfony/dom-crawler": "^3.1",
         "friendsofsymfony/jsrouting-bundle": "^1.6",
-        "bdunogier/guzzle-site-authenticator": "dev-master"
+        "bdunogier/guzzle-site-authenticator": "dev-callback_forms"
     },
     "require-dev": {
         "doctrine/doctrine-fixtures-bundle": "~2.2",
index 1c866f17e47eb6f228028caef5293b7dbee76039..a16ed49d9b478fb3042a35a4fad77a4fb418e35c 100644 (file)
@@ -6,28 +6,35 @@ use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig;
 use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfigBuilder;
 use Graby\SiteConfig\ConfigBuilder;
 use OutOfRangeException;
+use Psr\Log\LoggerInterface;
 
 class GrabySiteConfigBuilder implements SiteConfigBuilder
 {
     /**
-     * @var \Graby\SiteConfig\ConfigBuilder
+     * @var ConfigBuilder
      */
     private $grabyConfigBuilder;
     /**
      * @var array
      */
     private $credentials;
+    /**
+     * @var LoggerInterface
+     */
+    private $logger;
 
     /**
      * GrabySiteConfigBuilder constructor.
      *
-     * @param \Graby\SiteConfig\ConfigBuilder $grabyConfigBuilder
-     * @param array                           $credentials
+     * @param ConfigBuilder   $grabyConfigBuilder
+     * @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->credentials = $credentials;
+        $this->logger = $logger;
     }
 
     /**
@@ -47,6 +54,12 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
             $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);
         $parameters = [
             'host' => $host,
@@ -56,14 +69,18 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
             'passwordField' => $config->login_password_field ?: null,
             'extraFields' => $this->processExtraFields($config->login_extra_fields),
             'notLoggedInXpath' => $config->not_logged_in_xpath ?: null,
+            'username' => $this->credentials[$host]['username'],
+            'password' => $this->credentials[$host]['password'],
         ];
 
-        if (isset($this->credentials[$host])) {
-            $parameters['username'] = $this->credentials[$host]['username'];
-            $parameters['password'] = $this->credentials[$host]['password'];
-        }
+        $config = new SiteConfig($parameters);
+
+        // do not leak password in log
+        $parameters['password'] = '**masked**';
 
-        return new SiteConfig($parameters);
+        $this->logger->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]);
+
+        return $config;
     }
 
     /**
@@ -85,6 +102,7 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
             if (strpos($extraField, '=') === false) {
                 continue;
             }
+
             list($fieldName, $fieldValue) = explode('=', $extraField, 2);
             $extraFields[$fieldName] = $fieldValue;
         }
index 11ef26d83b78c7f74ed68a440a2fbdd2c6de3b7e..43f5b119767dc395115be1825499b4ba9da30124 100644 (file)
@@ -51,6 +51,7 @@ class HttpClientFactory
         $this->cookieJar->clear();
         // need to set the (shared) cookie jar
         $client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar]]);
+
         foreach ($this->subscribers as $subscriber) {
             $client->getEmitter()->attach($subscriber);
         }
index 68f900a1f58392289e8688f021aa9bcf2465f68c..6c9195ce5abd07e2450eff32d8f56f3a5aa42e4e 100644 (file)
@@ -63,6 +63,9 @@ services:
         arguments:
             - "@wallabag_core.graby.config_builder"
             - "%sites_credentials%"
+            - '@logger'
+        tags:
+            - { name: monolog.logger, channel: graby }
 
     # service alias override
     bd_guzzle_site_authenticator.site_config_builder:
index 8341b11f468aedafd429bd44c78c309c22b0081c..8b50bce9801f565c75238b32b98fbd2e03b87d5a 100644 (file)
@@ -2,6 +2,8 @@
 
 namespace Tests\Wallabag\CoreBundle\GuzzleSiteAuthenticator;
 
+use Monolog\Handler\TestHandler;
+use Monolog\Logger;
 use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig;
 use Graby\SiteConfig\SiteConfig as GrabySiteConfig;
 use PHPUnit_Framework_TestCase;
@@ -32,14 +34,19 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase
             ->with('example.com')
             ->will($this->returnValue($grabySiteConfig));
 
+        $logger = new Logger('foo');
+        $handler = new TestHandler();
+        $logger->pushHandler($handler);
+
         $this->builder = new GrabySiteConfigBuilder(
             $grabyConfigBuilderMock,
-            ['example.com' => ['username' => 'foo', 'password' => 'bar']]
+            ['example.com' => ['username' => 'foo', 'password' => 'bar']],
+            $logger
         );
 
         $config = $this->builder->buildForHost('example.com');
 
-        self::assertEquals(
+        $this->assertEquals(
             new SiteConfig([
                 'host' => 'example.com',
                 'requiresLogin' => true,
@@ -53,6 +60,10 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase
             ]),
             $config
         );
+
+        $records = $handler->getRecords();
+
+        $this->assertCount(1, $records, 'One log was recorded');
     }
 
     public function testBuildConfigDoesntExist()
@@ -67,19 +78,22 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase
             ->with('unknown.com')
             ->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');
 
-        self::assertEquals(
-            new SiteConfig([
-                'host' => 'unknown.com',
-                'requiresLogin' => false,
-                'username' => null,
-                'password' => null,
-                'extraFields' => [],
-            ]),
-            $config
-        );
+        $this->assertFalse($config);
+
+        $records = $handler->getRecords();
+
+        $this->assertCount(1, $records, 'One log was recorded');
     }
 }