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
35 * GrabySiteConfigBuilder constructor.
37 public function __construct(ConfigBuilder
$grabyConfigBuilder, TokenStorage
$token, SiteCredentialRepository
$credentialRepository, LoggerInterface
$logger)
39 $this->grabyConfigBuilder
= $grabyConfigBuilder;
40 $this->credentialRepository
= $credentialRepository;
41 $this->logger
= $logger;
42 $this->token
= $token;
48 public function buildForHost($host)
50 $user = $this->getUser();
52 // required by credentials below
53 $host = strtolower($host);
54 if ('www.' === substr($host, 0, 4)) {
55 $host = substr($host, 4);
59 $this->logger
->debug('Auth: no current user defined.');
65 // will try to see for a host without the first subdomain (fr.example.org & .example.org)
66 $split = explode('.', $host);
68 if (\
count($split) > 1) {
69 // remove first subdomain
71 $hosts[] = '.' . implode('.', $split);
74 $credentials = $this->credentialRepository
->findOneByHostsAndUser($hosts, $user->getId());
76 if (null === $credentials) {
77 $this->logger
->debug('Auth: no credentials available for host.', ['host' => $host]);
82 $config = $this->grabyConfigBuilder
->buildForHost($host);
85 'requiresLogin' => $config->requires_login
?: false,
86 'loginUri' => $config->login_uri
?: null,
87 'usernameField' => $config->login_username_field
?: null,
88 'passwordField' => $config->login_password_field
?: null,
89 'extraFields' => $this->processExtraFields($config->login_extra_fields
),
90 'notLoggedInXpath' => $config->not_logged_in_xpath
?: null,
91 'username' => $credentials['username'],
92 'password' => $credentials['password'],
95 $config = new SiteConfig($parameters);
97 // do not leak usernames and passwords in log
98 $parameters['username'] = '**masked**';
99 $parameters['password'] = '**masked**';
101 $this->logger
->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]);
107 * Processes login_extra_fields config, transforming an '=' separated array of strings
108 * into a key/value array.
110 * @param array|mixed $extraFieldsStrings
114 protected function processExtraFields($extraFieldsStrings)
116 if (!\
is_array($extraFieldsStrings)) {
121 foreach ($extraFieldsStrings as $extraField) {
122 if (false === strpos($extraField, '=')) {
126 list($fieldName, $fieldValue) = explode('=', $extraField, 2);
127 $extraFields[$fieldName] = $fieldValue;
133 private function getUser()
135 if ($this->token
->getToken() && null !== $this->token
->getToken()->getUser()) {
136 return $this->token
->getToken()->getUser();