X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FCoreBundle%2FGuzzleSiteAuthenticator%2FGrabySiteConfigBuilder.php;h=b0be21763c4db127bfd6e08ccfc26c832f5de365;hb=48b0163d247554d7e2f1ec63b717c8216ea9ec59;hp=c712bb26e2de7c66f487132da00e63e8c718d463;hpb=150d9ec1dcca0f71ba19a7f55e599eb716541dc3;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php index c712bb26..b0be2176 100644 --- a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php +++ b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php @@ -5,8 +5,10 @@ namespace Wallabag\CoreBundle\GuzzleSiteAuthenticator; use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig; use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfigBuilder; use Graby\SiteConfig\ConfigBuilder; -use OutOfRangeException; use Psr\Log\LoggerInterface; +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; +use Wallabag\CoreBundle\Repository\SiteCredentialRepository; +use Wallabag\UserBundle\Entity\User; class GrabySiteConfigBuilder implements SiteConfigBuilder { @@ -14,47 +16,66 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder * @var ConfigBuilder */ private $grabyConfigBuilder; + /** - * @var array + * @var SiteCredentialRepository */ - private $credentials; + private $credentialRepository; + /** * @var LoggerInterface */ private $logger; + /** + * @var User|null + */ + private $currentUser; + /** * GrabySiteConfigBuilder constructor. - * - * @param ConfigBuilder $grabyConfigBuilder - * @param array $credentials - * @param LoggerInterface $logger */ - public function __construct(ConfigBuilder $grabyConfigBuilder, array $credentials, LoggerInterface $logger) + public function __construct(ConfigBuilder $grabyConfigBuilder, TokenStorage $token, SiteCredentialRepository $credentialRepository, LoggerInterface $logger) { $this->grabyConfigBuilder = $grabyConfigBuilder; - $this->credentials = $credentials; + $this->credentialRepository = $credentialRepository; $this->logger = $logger; + + if ($token->getToken()) { + $this->currentUser = $token->getToken()->getUser(); + } } /** - * Builds the SiteConfig for a host. - * - * @param string $host The "www." prefix is ignored - * - * @return SiteConfig - * - * @throws OutOfRangeException If there is no config for $host + * {@inheritdoc} */ public function buildForHost($host) { // required by credentials below $host = strtolower($host); - if (substr($host, 0, 4) == 'www.') { + if ('www.' === substr($host, 0, 4)) { $host = substr($host, 4); } - if (empty($this->credentials[$host])) { + if (!$this->currentUser) { + $this->logger->debug('Auth: no current user defined.'); + + return false; + } + + $hosts = [$host]; + // will try to see for a host without the first subdomain (fr.example.org & .example.org) + $split = explode('.', $host); + + if (\count($split) > 1) { + // remove first subdomain + array_shift($split); + $hosts[] = '.' . implode('.', $split); + } + + $credentials = $this->credentialRepository->findOneByHostsAndUser($hosts, $this->currentUser->getId()); + + if (null === $credentials) { $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]); return false; @@ -69,13 +90,14 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder 'passwordField' => $config->login_password_field ?: null, 'extraFields' => $this->processExtraFields($config->login_extra_fields), 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null, - 'username' => $this->credentials[$host]['username'], - 'password' => $this->credentials[$host]['password'], + 'username' => $credentials['username'], + 'password' => $credentials['password'], ]; $config = new SiteConfig($parameters); - // do not leak password in log + // do not leak usernames and passwords in log + $parameters['username'] = '**masked**'; $parameters['password'] = '**masked**'; $this->logger->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]); @@ -93,13 +115,13 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder */ protected function processExtraFields($extraFieldsStrings) { - if (!is_array($extraFieldsStrings)) { + if (!\is_array($extraFieldsStrings)) { return []; } $extraFields = []; foreach ($extraFieldsStrings as $extraField) { - if (strpos($extraField, '=') === false) { + if (false === strpos($extraField, '=')) { continue; }