3 namespace Wallabag\CoreBundle\GuzzleSiteAuthenticator
;
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
;
11 class GrabySiteConfigBuilder
implements SiteConfigBuilder
16 private $grabyConfigBuilder;
22 * @var LoggerInterface
27 * GrabySiteConfigBuilder constructor.
29 * @param ConfigBuilder $grabyConfigBuilder
30 * @param array $credentials
31 * @param LoggerInterface $logger
33 public function __construct(ConfigBuilder
$grabyConfigBuilder, array $credentials, LoggerInterface
$logger)
35 $this->grabyConfigBuilder
= $grabyConfigBuilder;
36 $this->credentials
= $credentials;
37 $this->logger
= $logger;
41 * Builds the SiteConfig for a host.
43 * @param string $host The "www." prefix is ignored
47 * @throws OutOfRangeException If there is no config for $host
49 public function buildForHost($host)
51 // required by credentials below
52 $host = strtolower($host);
53 if (substr($host, 0, 4) == 'www.') {
54 $host = substr($host, 4);
57 if (empty($this->credentials
[$host])) {
58 $this->logger
->debug('Auth: no credentials available for host.', ['host' => $host]);
63 $config = $this->grabyConfigBuilder
->buildForHost($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'],
76 $config = new SiteConfig($parameters);
78 // do not leak password in log
79 $parameters['password'] = '**masked**';
81 $this->logger
->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]);
87 * Processes login_extra_fields config, transforming an '=' separated array of strings
88 * into a key/value array.
90 * @param array|mixed $extraFieldsStrings
94 protected function processExtraFields($extraFieldsStrings)
96 if (!is_array($extraFieldsStrings)) {
101 foreach ($extraFieldsStrings as $extraField) {
102 if (strpos($extraField, '=') === false) {
106 list($fieldName, $fieldValue) = explode('=', $extraField, 2);
107 $extraFields[$fieldName] = $fieldValue;