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;
43 public function buildForHost($host)
45 // required by credentials below
46 $host = strtolower($host);
47 if (substr($host, 0, 4) == 'www.') {
48 $host = substr($host, 4);
51 if (empty($this->credentials
[$host])) {
52 $this->logger
->debug('Auth: no credentials available for host.', ['host' => $host]);
57 $config = $this->grabyConfigBuilder
->buildForHost($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,
64 'extraFields' => $this->processExtraFields($config->login_extra_fields
),
65 'notLoggedInXpath' => $config->not_logged_in_xpath
?: null,
66 'username' => $this->credentials
[$host]['username'],
67 'password' => $this->credentials
[$host]['password'],
70 $config = new SiteConfig($parameters);
72 // do not leak password in log
73 $parameters['password'] = '**masked**';
75 $this->logger
->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]);
81 * Processes login_extra_fields config, transforming an '=' separated array of strings
82 * into a key/value array.
84 * @param array|mixed $extraFieldsStrings
88 protected function processExtraFields($extraFieldsStrings)
90 if (!is_array($extraFieldsStrings)) {
95 foreach ($extraFieldsStrings as $extraField) {
96 if (strpos($extraField, '=') === false) {
100 list($fieldName, $fieldValue) = explode('=', $extraField, 2);
101 $extraFields[$fieldName] = $fieldValue;