]>
Commit | Line | Data |
---|---|---|
7aab0ecf BD |
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; | |
94b232bb | 9 | use Psr\Log\LoggerInterface; |
7aab0ecf BD |
10 | |
11 | class 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 | /** | |
5fe65bae | 41 | * {@inheritdoc} |
7aab0ecf BD |
42 | */ |
43 | public function buildForHost($host) | |
44 | { | |
45 | // required by credentials below | |
46 | $host = strtolower($host); | |
47 | if (substr($host, 0, 4) == 'www.') { | |
48 | $host = substr($host, 4); | |
49 | } | |
50 | ||
150d9ec1 | 51 | if (empty($this->credentials[$host])) { |
94b232bb JB |
52 | $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]); |
53 | ||
54 | return false; | |
55 | } | |
56 | ||
7aab0ecf BD |
57 | $config = $this->grabyConfigBuilder->buildForHost($host); |
58 | $parameters = [ | |
59 | 'host' => $host, | |
60 | 'requiresLogin' => $config->requires_login ?: false, | |
61 | 'loginUri' => $config->login_uri ?: null, | |
62 | 'usernameField' => $config->login_username_field ?: null, | |
63 | 'passwordField' => $config->login_password_field ?: null, | |
662db41b | 64 | 'extraFields' => $this->processExtraFields($config->login_extra_fields), |
7aab0ecf | 65 | 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null, |
94b232bb JB |
66 | 'username' => $this->credentials[$host]['username'], |
67 | 'password' => $this->credentials[$host]['password'], | |
7aab0ecf BD |
68 | ]; |
69 | ||
94b232bb JB |
70 | $config = new SiteConfig($parameters); |
71 | ||
72 | // do not leak password in log | |
73 | $parameters['password'] = '**masked**'; | |
7aab0ecf | 74 | |
94b232bb JB |
75 | $this->logger->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]); |
76 | ||
77 | return $config; | |
7aab0ecf | 78 | } |
662db41b BD |
79 | |
80 | /** | |
81 | * Processes login_extra_fields config, transforming an '=' separated array of strings | |
82 | * into a key/value array. | |
83 | * | |
84 | * @param array|mixed $extraFieldsStrings | |
85 | * | |
86 | * @return array | |
87 | */ | |
88 | protected function processExtraFields($extraFieldsStrings) | |
89 | { | |
90 | if (!is_array($extraFieldsStrings)) { | |
91 | return []; | |
92 | } | |
93 | ||
94 | $extraFields = []; | |
95 | foreach ($extraFieldsStrings as $extraField) { | |
96 | if (strpos($extraField, '=') === false) { | |
97 | continue; | |
98 | } | |
94b232bb | 99 | |
662db41b BD |
100 | list($fieldName, $fieldValue) = explode('=', $extraField, 2); |
101 | $extraFields[$fieldName] = $fieldValue; | |
102 | } | |
103 | ||
104 | return $extraFields; | |
105 | } | |
7aab0ecf | 106 | } |