]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
Skip auth when no credentials are found
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / GuzzleSiteAuthenticator / GrabySiteConfigBuilder.php
1 <?php
2
3 namespace Wallabag\CoreBundle\GuzzleSiteAuthenticator;
4
5 use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig;
6 use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfigBuilder;
7 use Graby\SiteConfig\ConfigBuilder;
8 use OutOfRangeException;
9 use Psr\Log\LoggerInterface;
10
11 class GrabySiteConfigBuilder implements SiteConfigBuilder
12 {
13 /**
14 * @var ConfigBuilder
15 */
16 private $grabyConfigBuilder;
17 /**
18 * @var array
19 */
20 private $credentials;
21 /**
22 * @var LoggerInterface
23 */
24 private $logger;
25
26 /**
27 * GrabySiteConfigBuilder constructor.
28 *
29 * @param ConfigBuilder $grabyConfigBuilder
30 * @param array $credentials
31 * @param LoggerInterface $logger
32 */
33 public function __construct(ConfigBuilder $grabyConfigBuilder, array $credentials, LoggerInterface $logger)
34 {
35 $this->grabyConfigBuilder = $grabyConfigBuilder;
36 $this->credentials = $credentials;
37 $this->logger = $logger;
38 }
39
40 /**
41 * Builds the SiteConfig for a host.
42 *
43 * @param string $host The "www." prefix is ignored
44 *
45 * @return SiteConfig
46 *
47 * @throws OutOfRangeException If there is no config for $host
48 */
49 public function buildForHost($host)
50 {
51 // required by credentials below
52 $host = strtolower($host);
53 if (substr($host, 0, 4) == 'www.') {
54 $host = substr($host, 4);
55 }
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
63 $config = $this->grabyConfigBuilder->buildForHost($host);
64 $parameters = [
65 'host' => $host,
66 'requiresLogin' => $config->requires_login ?: false,
67 'loginUri' => $config->login_uri ?: null,
68 'usernameField' => $config->login_username_field ?: null,
69 'passwordField' => $config->login_password_field ?: null,
70 'extraFields' => $this->processExtraFields($config->login_extra_fields),
71 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null,
72 'username' => $this->credentials[$host]['username'],
73 'password' => $this->credentials[$host]['password'],
74 ];
75
76 $config = new SiteConfig($parameters);
77
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;
108 }
109
110 return $extraFields;
111 }
112 }