]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
Add a real configuration for CS-Fixer
[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 /**
b8427f22 30 * @var Wallabag\UserBundle\Entity\User|null
5a9bc007
JB
31 */
32 private $currentUser;
33
7aab0ecf
BD
34 /**
35 * GrabySiteConfigBuilder constructor.
36 *
5a9bc007 37 * @param ConfigBuilder $grabyConfigBuilder
9de9f1e5 38 * @param TokenStorage $token
5a9bc007 39 * @param SiteCredentialRepository $credentialRepository
9de9f1e5 40 * @param LoggerInterface $logger
7aab0ecf 41 */
b8427f22 42 public function __construct(ConfigBuilder $grabyConfigBuilder, TokenStorage $token, SiteCredentialRepository $credentialRepository, LoggerInterface $logger)
7aab0ecf
BD
43 {
44 $this->grabyConfigBuilder = $grabyConfigBuilder;
5a9bc007 45 $this->credentialRepository = $credentialRepository;
94b232bb 46 $this->logger = $logger;
b8427f22
JB
47
48 if ($token->getToken()) {
49 $this->currentUser = $token->getToken()->getUser();
50 }
7aab0ecf
BD
51 }
52
53 /**
5fe65bae 54 * {@inheritdoc}
7aab0ecf
BD
55 */
56 public function buildForHost($host)
57 {
58 // required by credentials below
59 $host = strtolower($host);
f808b016 60 if (substr($host, 0, 4) === 'www.') {
7aab0ecf
BD
61 $host = substr($host, 4);
62 }
63
b8427f22
JB
64 $credentials = null;
65 if ($this->currentUser) {
66 $credentials = $this->credentialRepository->findOneByHostAndUser($host, $this->currentUser->getId());
67 }
5a9bc007
JB
68
69 if (null === $credentials) {
94b232bb
JB
70 $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]);
71
72 return false;
73 }
74
7aab0ecf
BD
75 $config = $this->grabyConfigBuilder->buildForHost($host);
76 $parameters = [
77 'host' => $host,
78 'requiresLogin' => $config->requires_login ?: false,
79 'loginUri' => $config->login_uri ?: null,
80 'usernameField' => $config->login_username_field ?: null,
81 'passwordField' => $config->login_password_field ?: null,
662db41b 82 'extraFields' => $this->processExtraFields($config->login_extra_fields),
7aab0ecf 83 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null,
5a9bc007
JB
84 'username' => $credentials['username'],
85 'password' => $credentials['password'],
7aab0ecf
BD
86 ];
87
94b232bb
JB
88 $config = new SiteConfig($parameters);
89
bead8b42
TC
90 // do not leak usernames and passwords in log
91 $parameters['username'] = '**masked**';
94b232bb 92 $parameters['password'] = '**masked**';
7aab0ecf 93
94b232bb
JB
94 $this->logger->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]);
95
96 return $config;
7aab0ecf 97 }
662db41b
BD
98
99 /**
100 * Processes login_extra_fields config, transforming an '=' separated array of strings
101 * into a key/value array.
102 *
103 * @param array|mixed $extraFieldsStrings
104 *
105 * @return array
106 */
107 protected function processExtraFields($extraFieldsStrings)
108 {
109 if (!is_array($extraFieldsStrings)) {
110 return [];
111 }
112
113 $extraFields = [];
114 foreach ($extraFieldsStrings as $extraField) {
115 if (strpos($extraField, '=') === false) {
116 continue;
117 }
94b232bb 118
662db41b
BD
119 list($fieldName, $fieldValue) = explode('=', $extraField, 2);
120 $extraFields[$fieldName] = $fieldValue;
121 }
122
123 return $extraFields;
124 }
7aab0ecf 125}