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