]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
Add a real configuration for CS-Fixer
[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 Wallabag\UserBundle\Entity\User|null
31 */
32 private $currentUser;
33
34 /**
35 * GrabySiteConfigBuilder constructor.
36 *
37 * @param ConfigBuilder $grabyConfigBuilder
38 * @param TokenStorage $token
39 * @param SiteCredentialRepository $credentialRepository
40 * @param LoggerInterface $logger
41 */
42 public function __construct(ConfigBuilder $grabyConfigBuilder, TokenStorage $token, SiteCredentialRepository $credentialRepository, LoggerInterface $logger)
43 {
44 $this->grabyConfigBuilder = $grabyConfigBuilder;
45 $this->credentialRepository = $credentialRepository;
46 $this->logger = $logger;
47
48 if ($token->getToken()) {
49 $this->currentUser = $token->getToken()->getUser();
50 }
51 }
52
53 /**
54 * {@inheritdoc}
55 */
56 public function buildForHost($host)
57 {
58 // required by credentials below
59 $host = strtolower($host);
60 if (substr($host, 0, 4) === 'www.') {
61 $host = substr($host, 4);
62 }
63
64 $credentials = null;
65 if ($this->currentUser) {
66 $credentials = $this->credentialRepository->findOneByHostAndUser($host, $this->currentUser->getId());
67 }
68
69 if (null === $credentials) {
70 $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]);
71
72 return false;
73 }
74
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,
82 'extraFields' => $this->processExtraFields($config->login_extra_fields),
83 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null,
84 'username' => $credentials['username'],
85 'password' => $credentials['password'],
86 ];
87
88 $config = new SiteConfig($parameters);
89
90 // do not leak usernames and passwords in log
91 $parameters['username'] = '**masked**';
92 $parameters['password'] = '**masked**';
93
94 $this->logger->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]);
95
96 return $config;
97 }
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 }
118
119 list($fieldName, $fieldValue) = explode('=', $extraField, 2);
120 $extraFields[$fieldName] = $fieldValue;
121 }
122
123 return $extraFields;
124 }
125 }