3 namespace Wallabag\CoreBundle\GuzzleSiteAuthenticator
;
5 use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig
;
6 use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfigBuilder
;
7 use Graby\SiteConfig\ConfigBuilder
;
8 use Psr\Log\LoggerInterface
;
10 class GrabySiteConfigBuilder
implements SiteConfigBuilder
15 private $grabyConfigBuilder;
21 * @var LoggerInterface
26 * GrabySiteConfigBuilder constructor.
28 * @param ConfigBuilder $grabyConfigBuilder
29 * @param array $credentials
30 * @param LoggerInterface $logger
32 public function __construct(ConfigBuilder
$grabyConfigBuilder, array $credentials, LoggerInterface
$logger)
34 $this->grabyConfigBuilder
= $grabyConfigBuilder;
35 $this->credentials
= $credentials;
36 $this->logger
= $logger;
42 public function buildForHost($host)
44 // required by credentials below
45 $host = strtolower($host);
46 if (substr($host, 0, 4) == 'www.') {
47 $host = substr($host, 4);
50 if (empty($this->credentials
[$host])) {
51 $this->logger
->debug('Auth: no credentials available for host.', ['host' => $host]);
56 $config = $this->grabyConfigBuilder
->buildForHost($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'],
69 $config = new SiteConfig($parameters);
71 // do not leak password in log
72 $parameters['password'] = '**masked**';
74 $this->logger
->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]);
80 * Processes login_extra_fields config, transforming an '=' separated array of strings
81 * into a key/value array.
83 * @param array|mixed $extraFieldsStrings
87 protected function processExtraFields($extraFieldsStrings)
89 if (!is_array($extraFieldsStrings)) {
94 foreach ($extraFieldsStrings as $extraField) {
95 if (strpos($extraField, '=') === false) {
99 list($fieldName, $fieldValue) = explode('=', $extraField, 2);
100 $extraFields[$fieldName] = $fieldValue;