]>
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 JB |
9 | use Wallabag\CoreBundle\Repository\SiteCredentialRepository; |
10 | use Wallabag\UserBundle\Entity\User; | |
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 JB |
29 | /** |
30 | * @var User | |
31 | */ | |
32 | private $currentUser; | |
33 | ||
34 | ||
7aab0ecf BD |
35 | /** |
36 | * GrabySiteConfigBuilder constructor. | |
37 | * | |
5a9bc007 JB |
38 | * @param ConfigBuilder $grabyConfigBuilder |
39 | * @param User $currentUser | |
40 | * @param SiteCredentialRepository $credentialRepository | |
94b232bb | 41 | * @param LoggerInterface $logger |
7aab0ecf | 42 | */ |
5a9bc007 | 43 | public function __construct(ConfigBuilder $grabyConfigBuilder, User $currentUser, SiteCredentialRepository $credentialRepository, LoggerInterface $logger) |
7aab0ecf BD |
44 | { |
45 | $this->grabyConfigBuilder = $grabyConfigBuilder; | |
5a9bc007 JB |
46 | $this->credentialRepository = $credentialRepository; |
47 | $this->currentUser = $currentUser; | |
94b232bb | 48 | $this->logger = $logger; |
7aab0ecf BD |
49 | } |
50 | ||
51 | /** | |
5fe65bae | 52 | * {@inheritdoc} |
7aab0ecf BD |
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 | ||
5a9bc007 JB |
62 | $credentials = $this->credentialRepository->findOneByHostAndUser($host, $this->currentUser->getId()); |
63 | ||
64 | if (null === $credentials) { | |
94b232bb JB |
65 | $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]); |
66 | ||
67 | return false; | |
68 | } | |
69 | ||
7aab0ecf BD |
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, | |
662db41b | 77 | 'extraFields' => $this->processExtraFields($config->login_extra_fields), |
7aab0ecf | 78 | 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null, |
5a9bc007 JB |
79 | 'username' => $credentials['username'], |
80 | 'password' => $credentials['password'], | |
7aab0ecf BD |
81 | ]; |
82 | ||
94b232bb JB |
83 | $config = new SiteConfig($parameters); |
84 | ||
85 | // do not leak password in log | |
86 | $parameters['password'] = '**masked**'; | |
7aab0ecf | 87 | |
94b232bb JB |
88 | $this->logger->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]); |
89 | ||
90 | return $config; | |
7aab0ecf | 91 | } |
662db41b BD |
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 | } | |
94b232bb | 112 | |
662db41b BD |
113 | list($fieldName, $fieldValue) = explode('=', $extraField, 2); |
114 | $extraFields[$fieldName] = $fieldValue; | |
115 | } | |
116 | ||
117 | return $extraFields; | |
118 | } | |
7aab0ecf | 119 | } |