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