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.php62
1 files changed, 53 insertions, 9 deletions
diff --git a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
index 6d4129e8..c712bb26 100644
--- a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
+++ b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
@@ -6,28 +6,35 @@ use 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 OutOfRangeException;
9use Psr\Log\LoggerInterface;
9 10
10class GrabySiteConfigBuilder implements SiteConfigBuilder 11class 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 (empty($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,
@@ -54,15 +67,46 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
54 'loginUri' => $config->login_uri ?: null, 67 'loginUri' => $config->login_uri ?: null,
55 'usernameField' => $config->login_username_field ?: null, 68 'usernameField' => $config->login_username_field ?: null,
56 'passwordField' => $config->login_password_field ?: null, 69 'passwordField' => $config->login_password_field ?: null,
57 'extraFields' => is_array($config->login_extra_fields) ? $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
79 $parameters['password'] = '**masked**';
80
81 $this->logger->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]);
82
83 return $config;
84 }
85
86 /**
87 * Processes login_extra_fields config, transforming an '=' separated array of strings
88 * into a key/value array.
89 *
90 * @param array|mixed $extraFieldsStrings
91 *
92 * @return array
93 */
94 protected function processExtraFields($extraFieldsStrings)
95 {
96 if (!is_array($extraFieldsStrings)) {
97 return [];
98 }
99
100 $extraFields = [];
101 foreach ($extraFieldsStrings as $extraField) {
102 if (strpos($extraField, '=') === false) {
103 continue;
104 }
105
106 list($fieldName, $fieldValue) = explode('=', $extraField, 2);
107 $extraFields[$fieldName] = $fieldValue;
64 } 108 }
65 109
66 return new SiteConfig($parameters); 110 return $extraFields;
67 } 111 }
68} 112}