]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
1c56fa9f44279c0fa800abc5726bbd9ad876e043
[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 Psr\Log\LoggerInterface;
9
10 class GrabySiteConfigBuilder implements SiteConfigBuilder
11 {
12 /**
13 * @var ConfigBuilder
14 */
15 private $grabyConfigBuilder;
16 /**
17 * @var array
18 */
19 private $credentials;
20 /**
21 * @var LoggerInterface
22 */
23 private $logger;
24
25 /**
26 * GrabySiteConfigBuilder constructor.
27 *
28 * @param ConfigBuilder $grabyConfigBuilder
29 * @param array $credentials
30 * @param LoggerInterface $logger
31 */
32 public function __construct(ConfigBuilder $grabyConfigBuilder, array $credentials, LoggerInterface $logger)
33 {
34 $this->grabyConfigBuilder = $grabyConfigBuilder;
35 $this->credentials = $credentials;
36 $this->logger = $logger;
37 }
38
39 /**
40 * {@inheritdoc}
41 */
42 public function buildForHost($host)
43 {
44 // required by credentials below
45 $host = strtolower($host);
46 if (substr($host, 0, 4) == 'www.') {
47 $host = substr($host, 4);
48 }
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
56 $config = $this->grabyConfigBuilder->buildForHost($host);
57 $parameters = [
58 'host' => $host,
59 'requiresLogin' => $config->requires_login ?: false,
60 'loginUri' => $config->login_uri ?: null,
61 'usernameField' => $config->login_username_field ?: null,
62 'passwordField' => $config->login_password_field ?: null,
63 'extraFields' => $this->processExtraFields($config->login_extra_fields),
64 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null,
65 'username' => $this->credentials[$host]['username'],
66 'password' => $this->credentials[$host]['password'],
67 ];
68
69 $config = new SiteConfig($parameters);
70
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;
101 }
102
103 return $extraFields;
104 }
105 }