]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
Add a live test for restricted article
[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 Wallabag\CoreBundle\Repository\SiteCredentialRepository;
10 use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
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 password in log
91 $parameters['password'] = '**masked**';
92
93 $this->logger->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]);
94
95 return $config;
96 }
97
98 /**
99 * Processes login_extra_fields config, transforming an '=' separated array of strings
100 * into a key/value array.
101 *
102 * @param array|mixed $extraFieldsStrings
103 *
104 * @return array
105 */
106 protected function processExtraFields($extraFieldsStrings)
107 {
108 if (!is_array($extraFieldsStrings)) {
109 return [];
110 }
111
112 $extraFields = [];
113 foreach ($extraFieldsStrings as $extraField) {
114 if (strpos($extraField, '=') === false) {
115 continue;
116 }
117
118 list($fieldName, $fieldValue) = explode('=', $extraField, 2);
119 $extraFields[$fieldName] = $fieldValue;
120 }
121
122 return $extraFields;
123 }
124 }