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
;
9 use Wallabag\CoreBundle\Repository\SiteCredentialRepository
;
10 use Wallabag\UserBundle\Entity\User
;
12 class GrabySiteConfigBuilder
implements SiteConfigBuilder
17 private $grabyConfigBuilder;
20 * @var SiteCredentialRepository
22 private $credentialRepository;
25 * @var LoggerInterface
36 * GrabySiteConfigBuilder constructor.
38 * @param ConfigBuilder $grabyConfigBuilder
39 * @param User $currentUser
40 * @param SiteCredentialRepository $credentialRepository
41 * @param LoggerInterface $logger
43 public function __construct(ConfigBuilder
$grabyConfigBuilder, User
$currentUser, SiteCredentialRepository
$credentialRepository, LoggerInterface
$logger)
45 $this->grabyConfigBuilder
= $grabyConfigBuilder;
46 $this->credentialRepository
= $credentialRepository;
47 $this->currentUser
= $currentUser;
48 $this->logger
= $logger;
54 public function buildForHost($host)
56 // required by credentials below
57 $host = strtolower($host);
58 if (substr($host, 0, 4) == 'www.') {
59 $host = substr($host, 4);
62 $credentials = $this->credentialRepository
->findOneByHostAndUser($host, $this->currentUser
->getId());
64 if (null === $credentials) {
65 $this->logger
->debug('Auth: no credentials available for host.', ['host' => $host]);
70 $config = $this->grabyConfigBuilder
->buildForHost($host);
73 'requiresLogin' => $config->requires_login
?: false,
74 'loginUri' => $config->login_uri
?: null,
75 'usernameField' => $config->login_username_field
?: null,
76 'passwordField' => $config->login_password_field
?: null,
77 'extraFields' => $this->processExtraFields($config->login_extra_fields
),
78 'notLoggedInXpath' => $config->not_logged_in_xpath
?: null,
79 'username' => $credentials['username'],
80 'password' => $credentials['password'],
83 $config = new SiteConfig($parameters);
85 // do not leak password in log
86 $parameters['password'] = '**masked**';
88 $this->logger
->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]);
94 * Processes login_extra_fields config, transforming an '=' separated array of strings
95 * into a key/value array.
97 * @param array|mixed $extraFieldsStrings
101 protected function processExtraFields($extraFieldsStrings)
103 if (!is_array($extraFieldsStrings)) {
108 foreach ($extraFieldsStrings as $extraField) {
109 if (strpos($extraField, '=') === false) {
113 list($fieldName, $fieldValue) = explode('=', $extraField, 2);
114 $extraFields[$fieldName] = $fieldValue;