]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
Replace continue; with break; to avoid PHP 7.3 warnings
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / GuzzleSiteAuthenticator / GrabySiteConfigBuilder.php
CommitLineData
7aab0ecf
BD
1<?php
2
3namespace Wallabag\CoreBundle\GuzzleSiteAuthenticator;
4
5use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig;
6use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfigBuilder;
7use Graby\SiteConfig\ConfigBuilder;
94b232bb 8use Psr\Log\LoggerInterface;
b8427f22 9use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
f808b016 10use Wallabag\CoreBundle\Repository\SiteCredentialRepository;
52b84c11 11use Wallabag\UserBundle\Entity\User;
7aab0ecf
BD
12
13class 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
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
bead8b42
TC
91 // do not leak usernames and passwords in log
92 $parameters['username'] = '**masked**';
94b232bb 93 $parameters['password'] = '**masked**';
7aab0ecf 94
94b232bb
JB
95 $this->logger->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]);
96
97 return $config;
7aab0ecf 98 }
662db41b
BD
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 {
2a1ceb67 110 if (!\is_array($extraFieldsStrings)) {
662db41b
BD
111 return [];
112 }
113
114 $extraFields = [];
115 foreach ($extraFieldsStrings as $extraField) {
3ef055ce 116 if (false === strpos($extraField, '=')) {
ceddccb7 117 break;
662db41b 118 }
94b232bb 119
662db41b
BD
120 list($fieldName, $fieldValue) = explode('=', $extraField, 2);
121 $extraFields[$fieldName] = $fieldValue;
122 }
123
124 return $extraFields;
125 }
7aab0ecf 126}