]>
Commit | Line | Data |
---|---|---|
7aab0ecf BD |
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; | |
94b232bb | 8 | use Psr\Log\LoggerInterface; |
5a9bc007 | 9 | use Wallabag\CoreBundle\Repository\SiteCredentialRepository; |
b8427f22 | 10 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; |
7aab0ecf BD |
11 | |
12 | class 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 | ||
34 | ||
7aab0ecf BD |
35 | /** |
36 | * GrabySiteConfigBuilder constructor. | |
37 | * | |
5a9bc007 | 38 | * @param ConfigBuilder $grabyConfigBuilder |
b8427f22 | 39 | * @param TokenStorage $token |
5a9bc007 | 40 | * @param SiteCredentialRepository $credentialRepository |
94b232bb | 41 | * @param LoggerInterface $logger |
7aab0ecf | 42 | */ |
b8427f22 | 43 | public function __construct(ConfigBuilder $grabyConfigBuilder, TokenStorage $token, SiteCredentialRepository $credentialRepository, LoggerInterface $logger) |
7aab0ecf BD |
44 | { |
45 | $this->grabyConfigBuilder = $grabyConfigBuilder; | |
5a9bc007 | 46 | $this->credentialRepository = $credentialRepository; |
94b232bb | 47 | $this->logger = $logger; |
b8427f22 JB |
48 | |
49 | if ($token->getToken()) { | |
50 | $this->currentUser = $token->getToken()->getUser(); | |
51 | } | |
7aab0ecf BD |
52 | } |
53 | ||
54 | /** | |
5fe65bae | 55 | * {@inheritdoc} |
7aab0ecf BD |
56 | */ |
57 | public function buildForHost($host) | |
58 | { | |
59 | // required by credentials below | |
60 | $host = strtolower($host); | |
61 | if (substr($host, 0, 4) == 'www.') { | |
62 | $host = substr($host, 4); | |
63 | } | |
64 | ||
b8427f22 JB |
65 | $credentials = null; |
66 | if ($this->currentUser) { | |
67 | $credentials = $this->credentialRepository->findOneByHostAndUser($host, $this->currentUser->getId()); | |
68 | } | |
5a9bc007 JB |
69 | |
70 | if (null === $credentials) { | |
94b232bb JB |
71 | $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]); |
72 | ||
73 | return false; | |
74 | } | |
75 | ||
7aab0ecf BD |
76 | $config = $this->grabyConfigBuilder->buildForHost($host); |
77 | $parameters = [ | |
78 | 'host' => $host, | |
79 | 'requiresLogin' => $config->requires_login ?: false, | |
80 | 'loginUri' => $config->login_uri ?: null, | |
81 | 'usernameField' => $config->login_username_field ?: null, | |
82 | 'passwordField' => $config->login_password_field ?: null, | |
662db41b | 83 | 'extraFields' => $this->processExtraFields($config->login_extra_fields), |
7aab0ecf | 84 | 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null, |
5a9bc007 JB |
85 | 'username' => $credentials['username'], |
86 | 'password' => $credentials['password'], | |
7aab0ecf BD |
87 | ]; |
88 | ||
94b232bb JB |
89 | $config = new SiteConfig($parameters); |
90 | ||
91 | // do not leak password in log | |
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 | } |