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