]>
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; |
b8427f22 | 9 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; |
f808b016 | 10 | use Wallabag\CoreBundle\Repository\SiteCredentialRepository; |
52b84c11 | 11 | use Wallabag\UserBundle\Entity\User; |
7aab0ecf BD |
12 | |
13 | class GrabySiteConfigBuilder implements SiteConfigBuilder | |
14 | { | |
15 | /** | |
94b232bb | 16 | * @var ConfigBuilder |
7aab0ecf BD |
17 | */ |
18 | private $grabyConfigBuilder; | |
5a9bc007 | 19 | |
7aab0ecf | 20 | /** |
5a9bc007 | 21 | * @var SiteCredentialRepository |
7aab0ecf | 22 | */ |
5a9bc007 JB |
23 | private $credentialRepository; |
24 | ||
94b232bb JB |
25 | /** |
26 | * @var LoggerInterface | |
27 | */ | |
28 | private $logger; | |
7aab0ecf | 29 | |
5a9bc007 | 30 | /** |
52b84c11 | 31 | * @var User|null |
5a9bc007 JB |
32 | */ |
33 | private $currentUser; | |
34 | ||
7aab0ecf BD |
35 | /** |
36 | * GrabySiteConfigBuilder constructor. | |
37 | * | |
5a9bc007 | 38 | * @param ConfigBuilder $grabyConfigBuilder |
9de9f1e5 | 39 | * @param TokenStorage $token |
5a9bc007 | 40 | * @param SiteCredentialRepository $credentialRepository |
9de9f1e5 | 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); | |
3ef055ce | 61 | if ('www.' === substr($host, 0, 4)) { |
7aab0ecf BD |
62 | $host = substr($host, 4); |
63 | } | |
64 | ||
35359bd3 JB |
65 | if (!$this->currentUser) { |
66 | $this->logger->debug('Auth: no current user defined.'); | |
67 | ||
68 | return false; | |
69 | } | |
70 | ||
71 | $hosts = [$host]; | |
72 | // will try to see for a host without the first subdomain (fr.example.org & .example.org) | |
73 | $split = explode('.', $host); | |
74 | ||
75 | if (\count($split) > 1) { | |
76 | // remove first subdomain | |
77 | array_shift($split); | |
78 | $hosts[] = '.' . implode('.', $split); | |
b8427f22 | 79 | } |
5a9bc007 | 80 | |
35359bd3 JB |
81 | $credentials = $this->credentialRepository->findOneByHostsAndUser($hosts, $this->currentUser->getId()); |
82 | ||
5a9bc007 | 83 | if (null === $credentials) { |
94b232bb JB |
84 | $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]); |
85 | ||
86 | return false; | |
87 | } | |
88 | ||
7aab0ecf BD |
89 | $config = $this->grabyConfigBuilder->buildForHost($host); |
90 | $parameters = [ | |
91 | 'host' => $host, | |
92 | 'requiresLogin' => $config->requires_login ?: false, | |
93 | 'loginUri' => $config->login_uri ?: null, | |
94 | 'usernameField' => $config->login_username_field ?: null, | |
95 | 'passwordField' => $config->login_password_field ?: null, | |
662db41b | 96 | 'extraFields' => $this->processExtraFields($config->login_extra_fields), |
7aab0ecf | 97 | 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null, |
5a9bc007 JB |
98 | 'username' => $credentials['username'], |
99 | 'password' => $credentials['password'], | |
7aab0ecf BD |
100 | ]; |
101 | ||
94b232bb JB |
102 | $config = new SiteConfig($parameters); |
103 | ||
bead8b42 TC |
104 | // do not leak usernames and passwords in log |
105 | $parameters['username'] = '**masked**'; | |
94b232bb | 106 | $parameters['password'] = '**masked**'; |
7aab0ecf | 107 | |
94b232bb JB |
108 | $this->logger->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]); |
109 | ||
110 | return $config; | |
7aab0ecf | 111 | } |
662db41b BD |
112 | |
113 | /** | |
114 | * Processes login_extra_fields config, transforming an '=' separated array of strings | |
115 | * into a key/value array. | |
116 | * | |
117 | * @param array|mixed $extraFieldsStrings | |
118 | * | |
119 | * @return array | |
120 | */ | |
121 | protected function processExtraFields($extraFieldsStrings) | |
122 | { | |
2a1ceb67 | 123 | if (!\is_array($extraFieldsStrings)) { |
662db41b BD |
124 | return []; |
125 | } | |
126 | ||
127 | $extraFields = []; | |
128 | foreach ($extraFieldsStrings as $extraField) { | |
3ef055ce | 129 | if (false === strpos($extraField, '=')) { |
662db41b BD |
130 | continue; |
131 | } | |
94b232bb | 132 | |
662db41b BD |
133 | list($fieldName, $fieldValue) = explode('=', $extraField, 2); |
134 | $extraFields[$fieldName] = $fieldValue; | |
135 | } | |
136 | ||
137 | return $extraFields; | |
138 | } | |
7aab0ecf | 139 | } |