2016-09-29 08:14:43 +00:00
|
|
|
<?php
|
|
|
|
|
2024-02-19 00:30:12 +00:00
|
|
|
namespace Wallabag\SiteConfig;
|
2016-09-29 08:14:43 +00:00
|
|
|
|
|
|
|
use Graby\SiteConfig\ConfigBuilder;
|
2017-05-09 20:25:18 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2022-11-14 22:11:46 +00:00
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
2024-02-19 00:30:12 +00:00
|
|
|
use Wallabag\Repository\SiteCredentialRepository;
|
2016-09-29 08:14:43 +00:00
|
|
|
|
|
|
|
class GrabySiteConfigBuilder implements SiteConfigBuilder
|
|
|
|
{
|
|
|
|
/**
|
2017-05-09 20:25:18 +00:00
|
|
|
* @var ConfigBuilder
|
2016-09-29 08:14:43 +00:00
|
|
|
*/
|
|
|
|
private $grabyConfigBuilder;
|
2017-05-01 20:13:17 +00:00
|
|
|
|
2016-09-29 08:14:43 +00:00
|
|
|
/**
|
2017-05-01 20:13:17 +00:00
|
|
|
* @var SiteCredentialRepository
|
2016-09-29 08:14:43 +00:00
|
|
|
*/
|
2017-05-01 20:13:17 +00:00
|
|
|
private $credentialRepository;
|
|
|
|
|
2017-05-09 20:25:18 +00:00
|
|
|
/**
|
|
|
|
* @var LoggerInterface
|
|
|
|
*/
|
|
|
|
private $logger;
|
2016-09-29 08:14:43 +00:00
|
|
|
|
2017-05-01 20:13:17 +00:00
|
|
|
/**
|
2022-11-14 22:11:46 +00:00
|
|
|
* @var TokenStorageInterface
|
2017-05-01 20:13:17 +00:00
|
|
|
*/
|
2020-04-07 08:29:47 +00:00
|
|
|
private $token;
|
2017-05-01 20:13:17 +00:00
|
|
|
|
2016-09-29 08:14:43 +00:00
|
|
|
/**
|
|
|
|
* GrabySiteConfigBuilder constructor.
|
|
|
|
*/
|
2022-11-14 22:11:46 +00:00
|
|
|
public function __construct(ConfigBuilder $grabyConfigBuilder, TokenStorageInterface $token, SiteCredentialRepository $credentialRepository, LoggerInterface $logger)
|
2016-09-29 08:14:43 +00:00
|
|
|
{
|
|
|
|
$this->grabyConfigBuilder = $grabyConfigBuilder;
|
2017-05-01 20:13:17 +00:00
|
|
|
$this->credentialRepository = $credentialRepository;
|
2017-05-09 20:25:18 +00:00
|
|
|
$this->logger = $logger;
|
2020-04-07 08:29:47 +00:00
|
|
|
$this->token = $token;
|
2016-09-29 08:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function buildForHost($host)
|
|
|
|
{
|
2020-04-07 08:29:47 +00:00
|
|
|
$user = $this->getUser();
|
|
|
|
|
2016-09-29 08:14:43 +00:00
|
|
|
// required by credentials below
|
|
|
|
$host = strtolower($host);
|
2017-10-09 14:47:15 +00:00
|
|
|
if ('www.' === substr($host, 0, 4)) {
|
2016-09-29 08:14:43 +00:00
|
|
|
$host = substr($host, 4);
|
|
|
|
}
|
|
|
|
|
2020-04-07 08:29:47 +00:00
|
|
|
if (!$user) {
|
2019-04-24 13:28:15 +00:00
|
|
|
$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);
|
2017-05-01 20:13:35 +00:00
|
|
|
}
|
2017-05-01 20:13:17 +00:00
|
|
|
|
2020-04-07 08:29:47 +00:00
|
|
|
$credentials = $this->credentialRepository->findOneByHostsAndUser($hosts, $user->getId());
|
2019-04-24 13:28:15 +00:00
|
|
|
|
2017-05-01 20:13:17 +00:00
|
|
|
if (null === $credentials) {
|
2017-05-09 20:25:18 +00:00
|
|
|
$this->logger->debug('Auth: no credentials available for host.', ['host' => $host]);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-29 08:14:43 +00:00
|
|
|
$config = $this->grabyConfigBuilder->buildForHost($host);
|
|
|
|
$parameters = [
|
|
|
|
'host' => $host,
|
|
|
|
'requiresLogin' => $config->requires_login ?: false,
|
|
|
|
'loginUri' => $config->login_uri ?: null,
|
|
|
|
'usernameField' => $config->login_username_field ?: null,
|
|
|
|
'passwordField' => $config->login_password_field ?: null,
|
2017-01-21 23:42:05 +00:00
|
|
|
'extraFields' => $this->processExtraFields($config->login_extra_fields),
|
2016-09-29 08:14:43 +00:00
|
|
|
'notLoggedInXpath' => $config->not_logged_in_xpath ?: null,
|
2017-05-01 20:13:17 +00:00
|
|
|
'username' => $credentials['username'],
|
|
|
|
'password' => $credentials['password'],
|
2016-09-29 08:14:43 +00:00
|
|
|
];
|
|
|
|
|
2017-05-09 20:25:18 +00:00
|
|
|
$config = new SiteConfig($parameters);
|
|
|
|
|
2017-06-14 13:02:34 +00:00
|
|
|
// do not leak usernames and passwords in log
|
|
|
|
$parameters['username'] = '**masked**';
|
2017-05-09 20:25:18 +00:00
|
|
|
$parameters['password'] = '**masked**';
|
2016-09-29 08:14:43 +00:00
|
|
|
|
2017-05-09 20:25:18 +00:00
|
|
|
$this->logger->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]);
|
|
|
|
|
|
|
|
return $config;
|
2016-09-29 08:14:43 +00:00
|
|
|
}
|
2017-01-21 23:42:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes login_extra_fields config, transforming an '=' separated array of strings
|
|
|
|
* into a key/value array.
|
|
|
|
*
|
|
|
|
* @param array|mixed $extraFieldsStrings
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function processExtraFields($extraFieldsStrings)
|
|
|
|
{
|
2018-09-05 12:25:32 +00:00
|
|
|
if (!\is_array($extraFieldsStrings)) {
|
2017-01-21 23:42:05 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$extraFields = [];
|
|
|
|
foreach ($extraFieldsStrings as $extraField) {
|
2024-01-01 18:11:01 +00:00
|
|
|
if (!str_contains($extraField, '=')) {
|
2017-01-21 23:42:05 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-05-09 20:25:18 +00:00
|
|
|
|
2017-01-21 23:42:05 +00:00
|
|
|
list($fieldName, $fieldValue) = explode('=', $extraField, 2);
|
|
|
|
$extraFields[$fieldName] = $fieldValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $extraFields;
|
|
|
|
}
|
2020-04-07 08:29:47 +00:00
|
|
|
|
|
|
|
private function getUser()
|
|
|
|
{
|
|
|
|
if ($this->token->getToken() && null !== $this->token->getToken()->getUser()) {
|
|
|
|
return $this->token->getToken()->getUser();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2016-09-29 08:14:43 +00:00
|
|
|
}
|