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
;
11 use Wallabag\UserBundle\Entity\User
;
13 class GrabySiteConfigBuilder
implements SiteConfigBuilder
18 private $grabyConfigBuilder;
21 * @var SiteCredentialRepository
23 private $credentialRepository;
26 * @var LoggerInterface
36 * GrabySiteConfigBuilder constructor.
38 * @param ConfigBuilder $grabyConfigBuilder
39 * @param TokenStorage $token
40 * @param SiteCredentialRepository $credentialRepository
41 * @param LoggerInterface $logger
43 public function __construct(ConfigBuilder
$grabyConfigBuilder, TokenStorage
$token, SiteCredentialRepository
$credentialRepository, LoggerInterface
$logger)
45 $this->grabyConfigBuilder
= $grabyConfigBuilder;
46 $this->credentialRepository
= $credentialRepository;
47 $this->logger
= $logger;
49 if ($token->getToken()) {
50 $this->currentUser
= $token->getToken()->getUser();
57 public function buildForHost($host)
59 // required by credentials below
60 $host = strtolower($host);
61 if (substr($host, 0, 4) === 'www.') {
62 $host = substr($host, 4);
66 if ($this->currentUser
) {
67 $credentials = $this->credentialRepository
->findOneByHostAndUser($host, $this->currentUser
->getId());
70 if (null === $credentials) {
71 $this->logger
->debug('Auth: no credentials available for host.', ['host' => $host]);
76 $config = $this->grabyConfigBuilder
->buildForHost($host);
79 'requiresLogin' => $config->requires_login
?: false,
80 'loginUri' => $config->login_uri
?: null,
81 'usernameField' => $config->login_username_field
?: null,
82 'passwordField' => $config->login_password_field
?: null,
83 'extraFields' => $this->processExtraFields($config->login_extra_fields
),
84 'notLoggedInXpath' => $config->not_logged_in_xpath
?: null,
85 'username' => $credentials['username'],
86 'password' => $credentials['password'],
89 $config = new SiteConfig($parameters);
91 // do not leak usernames and passwords in log
92 $parameters['username'] = '**masked**';
93 $parameters['password'] = '**masked**';
95 $this->logger
->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]);
101 * Processes login_extra_fields config, transforming an '=' separated array of strings
102 * into a key/value array.
104 * @param array|mixed $extraFieldsStrings
108 protected function processExtraFields($extraFieldsStrings)
110 if (!is_array($extraFieldsStrings)) {
115 foreach ($extraFieldsStrings as $extraField) {
116 if (strpos($extraField, '=') === false) {
120 list($fieldName, $fieldValue) = explode('=', $extraField, 2);
121 $extraFields[$fieldName] = $fieldValue;