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 ('www.' === substr($host, 0, 4)) {
62 $host = substr($host, 4);
65 if (!$this->currentUser
) {
66 $this->logger
->debug('Auth: no current user defined.');
72 // will try to see for a host without the first subdomain (fr.example.org & .example.org)
73 $split = explode('.', $host);
75 if (\
count($split) > 1) {
76 // remove first subdomain
78 $hosts[] = '.' . implode('.', $split);
81 $credentials = $this->credentialRepository
->findOneByHostsAndUser($hosts, $this->currentUser
->getId());
83 if (null === $credentials) {
84 $this->logger
->debug('Auth: no credentials available for host.', ['host' => $host]);
89 $config = $this->grabyConfigBuilder
->buildForHost($host);
92 'requiresLogin' => $config->requires_login
?: false,
93 'loginUri' => $config->login_uri
?: null,
94 'usernameField' => $config->login_username_field
?: null,
95 'passwordField' => $config->login_password_field
?: null,
96 'extraFields' => $this->processExtraFields($config->login_extra_fields
),
97 'notLoggedInXpath' => $config->not_logged_in_xpath
?: null,
98 'username' => $credentials['username'],
99 'password' => $credentials['password'],
102 $config = new SiteConfig($parameters);
104 // do not leak usernames and passwords in log
105 $parameters['username'] = '**masked**';
106 $parameters['password'] = '**masked**';
108 $this->logger
->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]);
114 * Processes login_extra_fields config, transforming an '=' separated array of strings
115 * into a key/value array.
117 * @param array|mixed $extraFieldsStrings
121 protected function processExtraFields($extraFieldsStrings)
123 if (!\
is_array($extraFieldsStrings)) {
128 foreach ($extraFieldsStrings as $extraField) {
129 if (false === strpos($extraField, '=')) {
133 list($fieldName, $fieldValue) = explode('=', $extraField, 2);
134 $extraFields[$fieldName] = $fieldValue;