]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
b0be21763c4db127bfd6e08ccfc26c832f5de365
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / GuzzleSiteAuthenticator / GrabySiteConfigBuilder.php
1 <?php
2
3 namespace Wallabag\CoreBundle\GuzzleSiteAuthenticator;
4
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;
12
13 class GrabySiteConfigBuilder implements SiteConfigBuilder
14 {
15 /**
16 * @var ConfigBuilder
17 */
18 private $grabyConfigBuilder;
19
20 /**
21 * @var SiteCredentialRepository
22 */
23 private $credentialRepository;
24
25 /**
26 * @var LoggerInterface
27 */
28 private $logger;
29
30 /**
31 * @var User|null
32 */
33 private $currentUser;
34
35 /**
36 * GrabySiteConfigBuilder constructor.
37 */
38 public function __construct(ConfigBuilder $grabyConfigBuilder, TokenStorage $token, SiteCredentialRepository $credentialRepository, LoggerInterface $logger)
39 {
40 $this->grabyConfigBuilder = $grabyConfigBuilder;
41 $this->credentialRepository = $credentialRepository;
42 $this->logger = $logger;
43
44 if ($token->getToken()) {
45 $this->currentUser = $token->getToken()->getUser();
46 }
47 }
48
49 /**
50 * {@inheritdoc}
51 */
52 public function buildForHost($host)
53 {
54 // required by credentials below
55 $host = strtolower($host);
56 if ('www.' === substr($host, 0, 4)) {
57 $host = substr($host, 4);
58 }
59
60 if (!$this->currentUser) {
61 $this->logger->debug('Auth: no current user defined.');
62
63 return false;
64 }
65
66 $hosts = [$host];
67 // will try to see for a host without the first subdomain (fr.example.org & .example.org)
68 $split = explode('.', $host);
69
70 if (\count($split) > 1) {
71 // remove first subdomain
72 array_shift($split);
73 $hosts[] = '.' . implode('.', $split);
74 }
75
76 $credentials = $this->credentialRepository->findOneByHostsAndUser($hosts, $this->currentUser->getId());
77
78 if (null === $credentials) {
79 $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]);
80
81 return false;
82 }
83
84 $config = $this->grabyConfigBuilder->buildForHost($host);
85 $parameters = [
86 'host' => $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'],
95 ];
96
97 $config = new SiteConfig($parameters);
98
99 // do not leak usernames and passwords in log
100 $parameters['username'] = '**masked**';
101 $parameters['password'] = '**masked**';
102
103 $this->logger->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]);
104
105 return $config;
106 }
107
108 /**
109 * Processes login_extra_fields config, transforming an '=' separated array of strings
110 * into a key/value array.
111 *
112 * @param array|mixed $extraFieldsStrings
113 *
114 * @return array
115 */
116 protected function processExtraFields($extraFieldsStrings)
117 {
118 if (!\is_array($extraFieldsStrings)) {
119 return [];
120 }
121
122 $extraFields = [];
123 foreach ($extraFieldsStrings as $extraField) {
124 if (false === strpos($extraField, '=')) {
125 continue;
126 }
127
128 list($fieldName, $fieldValue) = explode('=', $extraField, 2);
129 $extraFields[$fieldName] = $fieldValue;
130 }
131
132 return $extraFields;
133 }
134 }