aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php')
-rw-r--r--src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php71
1 files changed, 54 insertions, 17 deletions
diff --git a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
index 6d4129e8..1c56fa9f 100644
--- a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
+++ b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
@@ -5,39 +5,39 @@ namespace Wallabag\CoreBundle\GuzzleSiteAuthenticator;
5use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig; 5use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig;
6use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfigBuilder; 6use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfigBuilder;
7use Graby\SiteConfig\ConfigBuilder; 7use Graby\SiteConfig\ConfigBuilder;
8use OutOfRangeException; 8use Psr\Log\LoggerInterface;
9 9
10class GrabySiteConfigBuilder implements SiteConfigBuilder 10class GrabySiteConfigBuilder implements SiteConfigBuilder
11{ 11{
12 /** 12 /**
13 * @var \Graby\SiteConfig\ConfigBuilder 13 * @var ConfigBuilder
14 */ 14 */
15 private $grabyConfigBuilder; 15 private $grabyConfigBuilder;
16 /** 16 /**
17 * @var array 17 * @var array
18 */ 18 */
19 private $credentials; 19 private $credentials;
20 /**
21 * @var LoggerInterface
22 */
23 private $logger;
20 24
21 /** 25 /**
22 * GrabySiteConfigBuilder constructor. 26 * GrabySiteConfigBuilder constructor.
23 * 27 *
24 * @param \Graby\SiteConfig\ConfigBuilder $grabyConfigBuilder 28 * @param ConfigBuilder $grabyConfigBuilder
25 * @param array $credentials 29 * @param array $credentials
30 * @param LoggerInterface $logger
26 */ 31 */
27 public function __construct(ConfigBuilder $grabyConfigBuilder, array $credentials = []) 32 public function __construct(ConfigBuilder $grabyConfigBuilder, array $credentials, LoggerInterface $logger)
28 { 33 {
29 $this->grabyConfigBuilder = $grabyConfigBuilder; 34 $this->grabyConfigBuilder = $grabyConfigBuilder;
30 $this->credentials = $credentials; 35 $this->credentials = $credentials;
36 $this->logger = $logger;
31 } 37 }
32 38
33 /** 39 /**
34 * Builds the SiteConfig for a host. 40 * {@inheritdoc}
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 */ 41 */
42 public function buildForHost($host) 42 public function buildForHost($host)
43 { 43 {
@@ -47,6 +47,12 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
47 $host = substr($host, 4); 47 $host = substr($host, 4);
48 } 48 }
49 49
50 if (empty($this->credentials[$host])) {
51 $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]);
52
53 return false;
54 }
55
50 $config = $this->grabyConfigBuilder->buildForHost($host); 56 $config = $this->grabyConfigBuilder->buildForHost($host);
51 $parameters = [ 57 $parameters = [
52 'host' => $host, 58 'host' => $host,
@@ -54,15 +60,46 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
54 'loginUri' => $config->login_uri ?: null, 60 'loginUri' => $config->login_uri ?: null,
55 'usernameField' => $config->login_username_field ?: null, 61 'usernameField' => $config->login_username_field ?: null,
56 'passwordField' => $config->login_password_field ?: null, 62 'passwordField' => $config->login_password_field ?: null,
57 'extraFields' => is_array($config->login_extra_fields) ? $config->login_extra_fields : [], 63 'extraFields' => $this->processExtraFields($config->login_extra_fields),
58 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null, 64 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null,
65 'username' => $this->credentials[$host]['username'],
66 'password' => $this->credentials[$host]['password'],
59 ]; 67 ];
60 68
61 if (isset($this->credentials[$host])) { 69 $config = new SiteConfig($parameters);
62 $parameters['username'] = $this->credentials[$host]['username']; 70
63 $parameters['password'] = $this->credentials[$host]['password']; 71 // do not leak password in log
72 $parameters['password'] = '**masked**';
73
74 $this->logger->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]);
75
76 return $config;
77 }
78
79 /**
80 * Processes login_extra_fields config, transforming an '=' separated array of strings
81 * into a key/value array.
82 *
83 * @param array|mixed $extraFieldsStrings
84 *
85 * @return array
86 */
87 protected function processExtraFields($extraFieldsStrings)
88 {
89 if (!is_array($extraFieldsStrings)) {
90 return [];
91 }
92
93 $extraFields = [];
94 foreach ($extraFieldsStrings as $extraField) {
95 if (strpos($extraField, '=') === false) {
96 continue;
97 }
98
99 list($fieldName, $fieldValue) = explode('=', $extraField, 2);
100 $extraFields[$fieldName] = $fieldValue;
64 } 101 }
65 102
66 return new SiteConfig($parameters); 103 return $extraFields;
67 } 104 }
68} 105}