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 Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage
;
10 use Wallabag\CoreBundle\Repository\SiteCredentialRepository
;
12 class GrabySiteConfigBuilder
implements SiteConfigBuilder
17 private $grabyConfigBuilder;
20 * @var SiteCredentialRepository
22 private $credentialRepository;
25 * @var LoggerInterface
30 * @var Wallabag\UserBundle\Entity\User|null
35 * GrabySiteConfigBuilder constructor.
37 * @param ConfigBuilder $grabyConfigBuilder
38 * @param TokenStorage $token
39 * @param SiteCredentialRepository $credentialRepository
40 * @param LoggerInterface $logger
42 public function __construct(ConfigBuilder
$grabyConfigBuilder, TokenStorage
$token, SiteCredentialRepository
$credentialRepository, LoggerInterface
$logger)
44 $this->grabyConfigBuilder
= $grabyConfigBuilder;
45 $this->credentialRepository
= $credentialRepository;
46 $this->logger
= $logger;
48 if ($token->getToken()) {
49 $this->currentUser
= $token->getToken()->getUser();
56 public function buildForHost($host)
58 // required by credentials below
59 $host = strtolower($host);
60 if (substr($host, 0, 4) === 'www.') {
61 $host = substr($host, 4);
65 if ($this->currentUser
) {
66 $credentials = $this->credentialRepository
->findOneByHostAndUser($host, $this->currentUser
->getId());
69 if (null === $credentials) {
70 $this->logger
->debug('Auth: no credentials available for host.', ['host' => $host]);
75 $config = $this->grabyConfigBuilder
->buildForHost($host);
78 'requiresLogin' => $config->requires_login
?: false,
79 'loginUri' => $config->login_uri
?: null,
80 'usernameField' => $config->login_username_field
?: null,
81 'passwordField' => $config->login_password_field
?: null,
82 'extraFields' => $this->processExtraFields($config->login_extra_fields
),
83 'notLoggedInXpath' => $config->not_logged_in_xpath
?: null,
84 'username' => $credentials['username'],
85 'password' => $credentials['password'],
88 $config = new SiteConfig($parameters);
90 // do not leak usernames and passwords in log
91 $parameters['username'] = '**masked**';
92 $parameters['password'] = '**masked**';
94 $this->logger
->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]);
100 * Processes login_extra_fields config, transforming an '=' separated array of strings
101 * into a key/value array.
103 * @param array|mixed $extraFieldsStrings
107 protected function processExtraFields($extraFieldsStrings)
109 if (!is_array($extraFieldsStrings)) {
114 foreach ($extraFieldsStrings as $extraField) {
115 if (strpos($extraField, '=') === false) {
119 list($fieldName, $fieldValue) = explode('=', $extraField, 2);
120 $extraFields[$fieldName] = $fieldValue;