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 public function __construct(ConfigBuilder
$grabyConfigBuilder, TokenStorage
$token, SiteCredentialRepository
$credentialRepository, LoggerInterface
$logger)
40 $this->grabyConfigBuilder
= $grabyConfigBuilder;
41 $this->credentialRepository
= $credentialRepository;
42 $this->logger
= $logger;
44 if ($token->getToken()) {
45 $this->currentUser
= $token->getToken()->getUser();
52 public function buildForHost($host)
54 // required by credentials below
55 $host = strtolower($host);
56 if ('www.' === substr($host, 0, 4)) {
57 $host = substr($host, 4);
60 if (!$this->currentUser
) {
61 $this->logger
->debug('Auth: no current user defined.');
67 // will try to see for a host without the first subdomain (fr.example.org & .example.org)
68 $split = explode('.', $host);
70 if (\
count($split) > 1) {
71 // remove first subdomain
73 $hosts[] = '.' . implode('.', $split);
76 $credentials = $this->credentialRepository
->findOneByHostsAndUser($hosts, $this->currentUser
->getId());
78 if (null === $credentials) {
79 $this->logger
->debug('Auth: no credentials available for host.', ['host' => $host]);
84 $config = $this->grabyConfigBuilder
->buildForHost($host);
87 'requiresLogin' => $config->requires_login
?: false,
88 'loginUri' => $config->login_uri
?: null,
89 'usernameField' => $config->login_username_field
?: null,
90 'passwordField' => $config->login_password_field
?: null,
91 'extraFields' => $this->processExtraFields($config->login_extra_fields
),
92 'notLoggedInXpath' => $config->not_logged_in_xpath
?: null,
93 'username' => $credentials['username'],
94 'password' => $credentials['password'],
97 $config = new SiteConfig($parameters);
99 // do not leak usernames and passwords in log
100 $parameters['username'] = '**masked**';
101 $parameters['password'] = '**masked**';
103 $this->logger
->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]);
109 * Processes login_extra_fields config, transforming an '=' separated array of strings
110 * into a key/value array.
112 * @param array|mixed $extraFieldsStrings
116 protected function processExtraFields($extraFieldsStrings)
118 if (!\
is_array($extraFieldsStrings)) {
123 foreach ($extraFieldsStrings as $extraField) {
124 if (false === strpos($extraField, '=')) {
128 list($fieldName, $fieldValue) = explode('=', $extraField, 2);
129 $extraFields[$fieldName] = $fieldValue;