]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
Merge remote-tracking branch 'origin/master' into 2.3
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / GuzzleSiteAuthenticator / GrabySiteConfigBuilder.php
CommitLineData
7aab0ecf
BD
1<?php
2
3namespace Wallabag\CoreBundle\GuzzleSiteAuthenticator;
4
5use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig;
6use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfigBuilder;
7use Graby\SiteConfig\ConfigBuilder;
8use OutOfRangeException;
94b232bb 9use Psr\Log\LoggerInterface;
7aab0ecf
BD
10
11class GrabySiteConfigBuilder implements SiteConfigBuilder
12{
13 /**
94b232bb 14 * @var ConfigBuilder
7aab0ecf
BD
15 */
16 private $grabyConfigBuilder;
17 /**
18 * @var array
19 */
20 private $credentials;
94b232bb
JB
21 /**
22 * @var LoggerInterface
23 */
24 private $logger;
7aab0ecf
BD
25
26 /**
27 * GrabySiteConfigBuilder constructor.
28 *
94b232bb
JB
29 * @param ConfigBuilder $grabyConfigBuilder
30 * @param array $credentials
31 * @param LoggerInterface $logger
7aab0ecf 32 */
94b232bb 33 public function __construct(ConfigBuilder $grabyConfigBuilder, array $credentials, LoggerInterface $logger)
7aab0ecf
BD
34 {
35 $this->grabyConfigBuilder = $grabyConfigBuilder;
36 $this->credentials = $credentials;
94b232bb 37 $this->logger = $logger;
7aab0ecf
BD
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
150d9ec1 57 if (empty($this->credentials[$host])) {
94b232bb
JB
58 $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]);
59
60 return false;
61 }
62
7aab0ecf
BD
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,
662db41b 70 'extraFields' => $this->processExtraFields($config->login_extra_fields),
7aab0ecf 71 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null,
94b232bb
JB
72 'username' => $this->credentials[$host]['username'],
73 'password' => $this->credentials[$host]['password'],
7aab0ecf
BD
74 ];
75
94b232bb
JB
76 $config = new SiteConfig($parameters);
77
78 // do not leak password in log
79 $parameters['password'] = '**masked**';
7aab0ecf 80
94b232bb
JB
81 $this->logger->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]);
82
83 return $config;
7aab0ecf 84 }
662db41b
BD
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 }
94b232bb 105
662db41b
BD
106 list($fieldName, $fieldValue) = explode('=', $extraField, 2);
107 $extraFields[$fieldName] = $fieldValue;
108 }
109
110 return $extraFields;
111 }
7aab0ecf 112}