]> git.immae.eu Git - github/wallabag/wallabag.git/blame - 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
CommitLineData
7aab0ecf
BD
1<?php
2
3namespace Wallabag\CoreBundle\GuzzleSiteAuthenticator;
4
5use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig;
6use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfigBuilder;
7use Graby\SiteConfig\ConfigBuilder;
94b232bb 8use Psr\Log\LoggerInterface;
b8427f22 9use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
f808b016 10use Wallabag\CoreBundle\Repository\SiteCredentialRepository;
7aab0ecf
BD
11
12class GrabySiteConfigBuilder implements SiteConfigBuilder
13{
14 /**
94b232bb 15 * @var ConfigBuilder
7aab0ecf
BD
16 */
17 private $grabyConfigBuilder;
5a9bc007 18
7aab0ecf 19 /**
5a9bc007 20 * @var SiteCredentialRepository
7aab0ecf 21 */
5a9bc007
JB
22 private $credentialRepository;
23
94b232bb
JB
24 /**
25 * @var LoggerInterface
26 */
27 private $logger;
7aab0ecf 28
5a9bc007 29 /**
de9b5b5f 30 * @var TokenStorage
5a9bc007 31 */
de9b5b5f 32 private $token;
5a9bc007 33
7aab0ecf
BD
34 /**
35 * GrabySiteConfigBuilder constructor.
7aab0ecf 36 */
b8427f22 37 public function __construct(ConfigBuilder $grabyConfigBuilder, TokenStorage $token, SiteCredentialRepository $credentialRepository, LoggerInterface $logger)
7aab0ecf
BD
38 {
39 $this->grabyConfigBuilder = $grabyConfigBuilder;
5a9bc007 40 $this->credentialRepository = $credentialRepository;
94b232bb 41 $this->logger = $logger;
de9b5b5f 42 $this->token = $token;
7aab0ecf
BD
43 }
44
45 /**
5fe65bae 46 * {@inheritdoc}
7aab0ecf
BD
47 */
48 public function buildForHost($host)
49 {
de9b5b5f
NL
50 $user = $this->getUser();
51
7aab0ecf
BD
52 // required by credentials below
53 $host = strtolower($host);
3ef055ce 54 if ('www.' === substr($host, 0, 4)) {
7aab0ecf
BD
55 $host = substr($host, 4);
56 }
57
de9b5b5f 58 if (!$user) {
35359bd3
JB
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);
b8427f22 72 }
5a9bc007 73
de9b5b5f 74 $credentials = $this->credentialRepository->findOneByHostsAndUser($hosts, $user->getId());
35359bd3 75
5a9bc007 76 if (null === $credentials) {
94b232bb
JB
77 $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]);
78
79 return false;
80 }
81
7aab0ecf
BD
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,
662db41b 89 'extraFields' => $this->processExtraFields($config->login_extra_fields),
7aab0ecf 90 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null,
5a9bc007
JB
91 'username' => $credentials['username'],
92 'password' => $credentials['password'],
7aab0ecf
BD
93 ];
94
94b232bb
JB
95 $config = new SiteConfig($parameters);
96
bead8b42
TC
97 // do not leak usernames and passwords in log
98 $parameters['username'] = '**masked**';
94b232bb 99 $parameters['password'] = '**masked**';
7aab0ecf 100
94b232bb
JB
101 $this->logger->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]);
102
103 return $config;
7aab0ecf 104 }
662db41b
BD
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 {
2a1ceb67 116 if (!\is_array($extraFieldsStrings)) {
662db41b
BD
117 return [];
118 }
119
120 $extraFields = [];
121 foreach ($extraFieldsStrings as $extraField) {
3ef055ce 122 if (false === strpos($extraField, '=')) {
662db41b
BD
123 continue;
124 }
94b232bb 125
662db41b
BD
126 list($fieldName, $fieldValue) = explode('=', $extraField, 2);
127 $extraFields[$fieldName] = $fieldValue;
128 }
129
130 return $extraFields;
131 }
de9b5b5f
NL
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 }
7aab0ecf 141}