diff options
author | Jérémy Benoist <j0k3r@users.noreply.github.com> | 2017-10-23 11:09:17 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-23 11:09:17 +0200 |
commit | 1953a872932a63792293b4aec087880265ba89f7 (patch) | |
tree | fd16599e737fcdaf193c933ef3ec4a4ee248b117 /src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php | |
parent | d83d25dadec2c38460a32d96f5d2903426fec9d3 (diff) | |
parent | 702f2d67d60ca963492b90dad74cb5f8dcc84e51 (diff) | |
download | wallabag-1953a872932a63792293b4aec087880265ba89f7.tar.gz wallabag-1953a872932a63792293b4aec087880265ba89f7.tar.zst wallabag-1953a872932a63792293b4aec087880265ba89f7.zip |
Merge pull request #3011 from wallabag/2.3
wallabag 2.3.0
Diffstat (limited to 'src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php')
-rw-r--r-- | src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php | 100 |
1 files changed, 79 insertions, 21 deletions
diff --git a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php index 6d4129e8..2c85da62 100644 --- a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php +++ b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php | |||
@@ -5,48 +5,74 @@ namespace Wallabag\CoreBundle\GuzzleSiteAuthenticator; | |||
5 | use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig; | 5 | use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig; |
6 | use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfigBuilder; | 6 | use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfigBuilder; |
7 | use Graby\SiteConfig\ConfigBuilder; | 7 | use Graby\SiteConfig\ConfigBuilder; |
8 | use OutOfRangeException; | 8 | use Psr\Log\LoggerInterface; |
9 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; | ||
10 | use Wallabag\CoreBundle\Repository\SiteCredentialRepository; | ||
11 | use Wallabag\UserBundle\Entity\User; | ||
9 | 12 | ||
10 | class GrabySiteConfigBuilder implements SiteConfigBuilder | 13 | class GrabySiteConfigBuilder implements SiteConfigBuilder |
11 | { | 14 | { |
12 | /** | 15 | /** |
13 | * @var \Graby\SiteConfig\ConfigBuilder | 16 | * @var ConfigBuilder |
14 | */ | 17 | */ |
15 | private $grabyConfigBuilder; | 18 | private $grabyConfigBuilder; |
19 | |||
20 | /** | ||
21 | * @var SiteCredentialRepository | ||
22 | */ | ||
23 | private $credentialRepository; | ||
24 | |||
16 | /** | 25 | /** |
17 | * @var array | 26 | * @var LoggerInterface |
18 | */ | 27 | */ |
19 | private $credentials; | 28 | private $logger; |
29 | |||
30 | /** | ||
31 | * @var User|null | ||
32 | */ | ||
33 | private $currentUser; | ||
20 | 34 | ||
21 | /** | 35 | /** |
22 | * GrabySiteConfigBuilder constructor. | 36 | * GrabySiteConfigBuilder constructor. |
23 | * | 37 | * |
24 | * @param \Graby\SiteConfig\ConfigBuilder $grabyConfigBuilder | 38 | * @param ConfigBuilder $grabyConfigBuilder |
25 | * @param array $credentials | 39 | * @param TokenStorage $token |
40 | * @param SiteCredentialRepository $credentialRepository | ||
41 | * @param LoggerInterface $logger | ||
26 | */ | 42 | */ |
27 | public function __construct(ConfigBuilder $grabyConfigBuilder, array $credentials = []) | 43 | public function __construct(ConfigBuilder $grabyConfigBuilder, TokenStorage $token, SiteCredentialRepository $credentialRepository, LoggerInterface $logger) |
28 | { | 44 | { |
29 | $this->grabyConfigBuilder = $grabyConfigBuilder; | 45 | $this->grabyConfigBuilder = $grabyConfigBuilder; |
30 | $this->credentials = $credentials; | 46 | $this->credentialRepository = $credentialRepository; |
47 | $this->logger = $logger; | ||
48 | |||
49 | if ($token->getToken()) { | ||
50 | $this->currentUser = $token->getToken()->getUser(); | ||
51 | } | ||
31 | } | 52 | } |
32 | 53 | ||
33 | /** | 54 | /** |
34 | * Builds the SiteConfig for a host. | 55 | * {@inheritdoc} |
35 | * | ||
36 | * @param string $host The "www." prefix is ignored | ||
37 | * | ||
38 | * @return SiteConfig | ||
39 | * | ||
40 | * @throws OutOfRangeException If there is no config for $host | ||
41 | */ | 56 | */ |
42 | public function buildForHost($host) | 57 | public function buildForHost($host) |
43 | { | 58 | { |
44 | // required by credentials below | 59 | // required by credentials below |
45 | $host = strtolower($host); | 60 | $host = strtolower($host); |
46 | if (substr($host, 0, 4) == 'www.') { | 61 | if ('www.' === substr($host, 0, 4)) { |
47 | $host = substr($host, 4); | 62 | $host = substr($host, 4); |
48 | } | 63 | } |
49 | 64 | ||
65 | $credentials = null; | ||
66 | if ($this->currentUser) { | ||
67 | $credentials = $this->credentialRepository->findOneByHostAndUser($host, $this->currentUser->getId()); | ||
68 | } | ||
69 | |||
70 | if (null === $credentials) { | ||
71 | $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]); | ||
72 | |||
73 | return false; | ||
74 | } | ||
75 | |||
50 | $config = $this->grabyConfigBuilder->buildForHost($host); | 76 | $config = $this->grabyConfigBuilder->buildForHost($host); |
51 | $parameters = [ | 77 | $parameters = [ |
52 | 'host' => $host, | 78 | 'host' => $host, |
@@ -54,15 +80,47 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder | |||
54 | 'loginUri' => $config->login_uri ?: null, | 80 | 'loginUri' => $config->login_uri ?: null, |
55 | 'usernameField' => $config->login_username_field ?: null, | 81 | 'usernameField' => $config->login_username_field ?: null, |
56 | 'passwordField' => $config->login_password_field ?: null, | 82 | 'passwordField' => $config->login_password_field ?: null, |
57 | 'extraFields' => is_array($config->login_extra_fields) ? $config->login_extra_fields : [], | 83 | 'extraFields' => $this->processExtraFields($config->login_extra_fields), |
58 | 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null, | 84 | 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null, |
85 | 'username' => $credentials['username'], | ||
86 | 'password' => $credentials['password'], | ||
59 | ]; | 87 | ]; |
60 | 88 | ||
61 | if (isset($this->credentials[$host])) { | 89 | $config = new SiteConfig($parameters); |
62 | $parameters['username'] = $this->credentials[$host]['username']; | 90 | |
63 | $parameters['password'] = $this->credentials[$host]['password']; | 91 | // do not leak usernames and passwords in log |
92 | $parameters['username'] = '**masked**'; | ||
93 | $parameters['password'] = '**masked**'; | ||
94 | |||
95 | $this->logger->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]); | ||
96 | |||
97 | return $config; | ||
98 | } | ||
99 | |||
100 | /** | ||
101 | * Processes login_extra_fields config, transforming an '=' separated array of strings | ||
102 | * into a key/value array. | ||
103 | * | ||
104 | * @param array|mixed $extraFieldsStrings | ||
105 | * | ||
106 | * @return array | ||
107 | */ | ||
108 | protected function processExtraFields($extraFieldsStrings) | ||
109 | { | ||
110 | if (!is_array($extraFieldsStrings)) { | ||
111 | return []; | ||
112 | } | ||
113 | |||
114 | $extraFields = []; | ||
115 | foreach ($extraFieldsStrings as $extraField) { | ||
116 | if (false === strpos($extraField, '=')) { | ||
117 | continue; | ||
118 | } | ||
119 | |||
120 | list($fieldName, $fieldValue) = explode('=', $extraField, 2); | ||
121 | $extraFields[$fieldName] = $fieldValue; | ||
64 | } | 122 | } |
65 | 123 | ||
66 | return new SiteConfig($parameters); | 124 | return $extraFields; |
67 | } | 125 | } |
68 | } | 126 | } |