@@ -31,45 +31,45 @@ |
||
31 | 31 | |
32 | 32 | class SyncFederationAddressBooks extends Command { |
33 | 33 | |
34 | - /** @var \OCA\Federation\SyncFederationAddressBooks */ |
|
35 | - private $syncService; |
|
34 | + /** @var \OCA\Federation\SyncFederationAddressBooks */ |
|
35 | + private $syncService; |
|
36 | 36 | |
37 | - /** |
|
38 | - * @param \OCA\Federation\SyncFederationAddressBooks $syncService |
|
39 | - */ |
|
40 | - function __construct(\OCA\Federation\SyncFederationAddressBooks $syncService) { |
|
41 | - parent::__construct(); |
|
37 | + /** |
|
38 | + * @param \OCA\Federation\SyncFederationAddressBooks $syncService |
|
39 | + */ |
|
40 | + function __construct(\OCA\Federation\SyncFederationAddressBooks $syncService) { |
|
41 | + parent::__construct(); |
|
42 | 42 | |
43 | - $this->syncService = $syncService; |
|
44 | - } |
|
43 | + $this->syncService = $syncService; |
|
44 | + } |
|
45 | 45 | |
46 | - protected function configure() { |
|
47 | - $this |
|
48 | - ->setName('federation:sync-addressbooks') |
|
49 | - ->setDescription('Synchronizes addressbooks of all federated clouds'); |
|
50 | - } |
|
46 | + protected function configure() { |
|
47 | + $this |
|
48 | + ->setName('federation:sync-addressbooks') |
|
49 | + ->setDescription('Synchronizes addressbooks of all federated clouds'); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * @param InputInterface $input |
|
54 | - * @param OutputInterface $output |
|
55 | - * @return int |
|
56 | - */ |
|
57 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
52 | + /** |
|
53 | + * @param InputInterface $input |
|
54 | + * @param OutputInterface $output |
|
55 | + * @return int |
|
56 | + */ |
|
57 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
58 | 58 | |
59 | - $progress = new ProgressBar($output); |
|
60 | - $progress->start(); |
|
61 | - $this->syncService->syncThemAll(function($url, $ex) use ($progress, $output) { |
|
62 | - if ($ex instanceof \Exception) { |
|
63 | - $output->writeln("Error while syncing $url : " . $ex->getMessage()); |
|
59 | + $progress = new ProgressBar($output); |
|
60 | + $progress->start(); |
|
61 | + $this->syncService->syncThemAll(function($url, $ex) use ($progress, $output) { |
|
62 | + if ($ex instanceof \Exception) { |
|
63 | + $output->writeln("Error while syncing $url : " . $ex->getMessage()); |
|
64 | 64 | |
65 | - } else { |
|
66 | - $progress->advance(); |
|
67 | - } |
|
68 | - }); |
|
65 | + } else { |
|
66 | + $progress->advance(); |
|
67 | + } |
|
68 | + }); |
|
69 | 69 | |
70 | - $progress->finish(); |
|
71 | - $output->writeln(''); |
|
70 | + $progress->finish(); |
|
71 | + $output->writeln(''); |
|
72 | 72 | |
73 | - return 0; |
|
74 | - } |
|
73 | + return 0; |
|
74 | + } |
|
75 | 75 | } |
@@ -32,52 +32,52 @@ |
||
32 | 32 | |
33 | 33 | class SyncFederationAddressBooks { |
34 | 34 | |
35 | - /** @var DbHandler */ |
|
36 | - protected $dbHandler; |
|
35 | + /** @var DbHandler */ |
|
36 | + protected $dbHandler; |
|
37 | 37 | |
38 | - /** @var SyncService */ |
|
39 | - private $syncService; |
|
38 | + /** @var SyncService */ |
|
39 | + private $syncService; |
|
40 | 40 | |
41 | - /** |
|
42 | - * @param DbHandler $dbHandler |
|
43 | - * @param SyncService $syncService |
|
44 | - */ |
|
45 | - function __construct(DbHandler $dbHandler, SyncService $syncService) { |
|
46 | - $this->syncService = $syncService; |
|
47 | - $this->dbHandler = $dbHandler; |
|
48 | - } |
|
41 | + /** |
|
42 | + * @param DbHandler $dbHandler |
|
43 | + * @param SyncService $syncService |
|
44 | + */ |
|
45 | + function __construct(DbHandler $dbHandler, SyncService $syncService) { |
|
46 | + $this->syncService = $syncService; |
|
47 | + $this->dbHandler = $dbHandler; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * @param \Closure $callback |
|
52 | - */ |
|
53 | - public function syncThemAll(\Closure $callback) { |
|
50 | + /** |
|
51 | + * @param \Closure $callback |
|
52 | + */ |
|
53 | + public function syncThemAll(\Closure $callback) { |
|
54 | 54 | |
55 | - $trustedServers = $this->dbHandler->getAllServer(); |
|
56 | - foreach ($trustedServers as $trustedServer) { |
|
57 | - $url = $trustedServer['url']; |
|
58 | - $callback($url, null); |
|
59 | - $sharedSecret = $trustedServer['shared_secret']; |
|
60 | - $syncToken = $trustedServer['sync_token']; |
|
55 | + $trustedServers = $this->dbHandler->getAllServer(); |
|
56 | + foreach ($trustedServers as $trustedServer) { |
|
57 | + $url = $trustedServer['url']; |
|
58 | + $callback($url, null); |
|
59 | + $sharedSecret = $trustedServer['shared_secret']; |
|
60 | + $syncToken = $trustedServer['sync_token']; |
|
61 | 61 | |
62 | - if (is_null($sharedSecret)) { |
|
63 | - continue; |
|
64 | - } |
|
65 | - $targetBookId = $trustedServer['url_hash']; |
|
66 | - $targetPrincipal = "principals/system/system"; |
|
67 | - $targetBookProperties = [ |
|
68 | - '{DAV:}displayname' => $url |
|
69 | - ]; |
|
70 | - try { |
|
71 | - $newToken = $this->syncService->syncRemoteAddressBook($url, 'system', $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetBookProperties); |
|
72 | - if ($newToken !== $syncToken) { |
|
73 | - $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_OK, $newToken); |
|
74 | - } |
|
75 | - } catch (\Exception $ex) { |
|
76 | - if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) { |
|
77 | - $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_ACCESS_REVOKED); |
|
78 | - } |
|
79 | - $callback($url, $ex); |
|
80 | - } |
|
81 | - } |
|
82 | - } |
|
62 | + if (is_null($sharedSecret)) { |
|
63 | + continue; |
|
64 | + } |
|
65 | + $targetBookId = $trustedServer['url_hash']; |
|
66 | + $targetPrincipal = "principals/system/system"; |
|
67 | + $targetBookProperties = [ |
|
68 | + '{DAV:}displayname' => $url |
|
69 | + ]; |
|
70 | + try { |
|
71 | + $newToken = $this->syncService->syncRemoteAddressBook($url, 'system', $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetBookProperties); |
|
72 | + if ($newToken !== $syncToken) { |
|
73 | + $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_OK, $newToken); |
|
74 | + } |
|
75 | + } catch (\Exception $ex) { |
|
76 | + if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) { |
|
77 | + $this->dbHandler->setServerStatus($url, TrustedServers::STATUS_ACCESS_REVOKED); |
|
78 | + } |
|
79 | + $callback($url, $ex); |
|
80 | + } |
|
81 | + } |
|
82 | + } |
|
83 | 83 | } |
@@ -38,241 +38,241 @@ |
||
38 | 38 | |
39 | 39 | class TrustedServers { |
40 | 40 | |
41 | - /** after a user list was exchanged at least once successfully */ |
|
42 | - const STATUS_OK = 1; |
|
43 | - /** waiting for shared secret or initial user list exchange */ |
|
44 | - const STATUS_PENDING = 2; |
|
45 | - /** something went wrong, misconfigured server, software bug,... user interaction needed */ |
|
46 | - const STATUS_FAILURE = 3; |
|
47 | - /** remote server revoked access */ |
|
48 | - const STATUS_ACCESS_REVOKED = 4; |
|
49 | - |
|
50 | - /** @var dbHandler */ |
|
51 | - private $dbHandler; |
|
52 | - |
|
53 | - /** @var IClientService */ |
|
54 | - private $httpClientService; |
|
55 | - |
|
56 | - /** @var ILogger */ |
|
57 | - private $logger; |
|
58 | - |
|
59 | - /** @var IJobList */ |
|
60 | - private $jobList; |
|
61 | - |
|
62 | - /** @var ISecureRandom */ |
|
63 | - private $secureRandom; |
|
64 | - |
|
65 | - /** @var IConfig */ |
|
66 | - private $config; |
|
67 | - |
|
68 | - /** @var EventDispatcherInterface */ |
|
69 | - private $dispatcher; |
|
70 | - |
|
71 | - /** |
|
72 | - * @param DbHandler $dbHandler |
|
73 | - * @param IClientService $httpClientService |
|
74 | - * @param ILogger $logger |
|
75 | - * @param IJobList $jobList |
|
76 | - * @param ISecureRandom $secureRandom |
|
77 | - * @param IConfig $config |
|
78 | - * @param EventDispatcherInterface $dispatcher |
|
79 | - */ |
|
80 | - public function __construct( |
|
81 | - DbHandler $dbHandler, |
|
82 | - IClientService $httpClientService, |
|
83 | - ILogger $logger, |
|
84 | - IJobList $jobList, |
|
85 | - ISecureRandom $secureRandom, |
|
86 | - IConfig $config, |
|
87 | - EventDispatcherInterface $dispatcher |
|
88 | - ) { |
|
89 | - $this->dbHandler = $dbHandler; |
|
90 | - $this->httpClientService = $httpClientService; |
|
91 | - $this->logger = $logger; |
|
92 | - $this->jobList = $jobList; |
|
93 | - $this->secureRandom = $secureRandom; |
|
94 | - $this->config = $config; |
|
95 | - $this->dispatcher = $dispatcher; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * add server to the list of trusted servers |
|
100 | - * |
|
101 | - * @param $url |
|
102 | - * @return int server id |
|
103 | - */ |
|
104 | - public function addServer($url) { |
|
105 | - $url = $this->updateProtocol($url); |
|
106 | - $result = $this->dbHandler->addServer($url); |
|
107 | - if ($result) { |
|
108 | - $token = $this->secureRandom->generate(16); |
|
109 | - $this->dbHandler->addToken($url, $token); |
|
110 | - $this->jobList->add( |
|
111 | - 'OCA\Federation\BackgroundJob\RequestSharedSecret', |
|
112 | - [ |
|
113 | - 'url' => $url, |
|
114 | - 'token' => $token |
|
115 | - ] |
|
116 | - ); |
|
117 | - } |
|
118 | - |
|
119 | - return $result; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * enable/disable to automatically add servers to the list of trusted servers |
|
124 | - * once a federated share was created and accepted successfully |
|
125 | - * |
|
126 | - * @param bool $status |
|
127 | - */ |
|
128 | - public function setAutoAddServers($status) { |
|
129 | - $value = $status ? '1' : '0'; |
|
130 | - $this->config->setAppValue('federation', 'autoAddServers', $value); |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * return if we automatically add servers to the list of trusted servers |
|
135 | - * once a federated share was created and accepted successfully |
|
136 | - * |
|
137 | - * @return bool |
|
138 | - */ |
|
139 | - public function getAutoAddServers() { |
|
140 | - $value = $this->config->getAppValue('federation', 'autoAddServers', '0'); |
|
141 | - return $value === '1'; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * get shared secret for the given server |
|
146 | - * |
|
147 | - * @param string $url |
|
148 | - * @return string |
|
149 | - */ |
|
150 | - public function getSharedSecret($url) { |
|
151 | - return $this->dbHandler->getSharedSecret($url); |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * add shared secret for the given server |
|
156 | - * |
|
157 | - * @param string $url |
|
158 | - * @param $sharedSecret |
|
159 | - */ |
|
160 | - public function addSharedSecret($url, $sharedSecret) { |
|
161 | - $this->dbHandler->addSharedSecret($url, $sharedSecret); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * remove server from the list of trusted servers |
|
166 | - * |
|
167 | - * @param int $id |
|
168 | - */ |
|
169 | - public function removeServer($id) { |
|
170 | - $server = $this->dbHandler->getServerById($id); |
|
171 | - $this->dbHandler->removeServer($id); |
|
172 | - $event = new GenericEvent($server['url_hash']); |
|
173 | - $this->dispatcher->dispatch('OCP\Federation\TrustedServerEvent::remove', $event); |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * get all trusted servers |
|
178 | - * |
|
179 | - * @return array |
|
180 | - */ |
|
181 | - public function getServers() { |
|
182 | - return $this->dbHandler->getAllServer(); |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * check if given server is a trusted ownCloud server |
|
187 | - * |
|
188 | - * @param string $url |
|
189 | - * @return bool |
|
190 | - */ |
|
191 | - public function isTrustedServer($url) { |
|
192 | - return $this->dbHandler->serverExists($url); |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * set server status |
|
197 | - * |
|
198 | - * @param string $url |
|
199 | - * @param int $status |
|
200 | - */ |
|
201 | - public function setServerStatus($url, $status) { |
|
202 | - $this->dbHandler->setServerStatus($url, $status); |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * @param string $url |
|
207 | - * @return int |
|
208 | - */ |
|
209 | - public function getServerStatus($url) { |
|
210 | - return $this->dbHandler->getServerStatus($url); |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * check if URL point to a ownCloud/Nextcloud server |
|
215 | - * |
|
216 | - * @param string $url |
|
217 | - * @return bool |
|
218 | - */ |
|
219 | - public function isOwnCloudServer($url) { |
|
220 | - $isValidOwnCloud = false; |
|
221 | - $client = $this->httpClientService->newClient(); |
|
222 | - try { |
|
223 | - $result = $client->get( |
|
224 | - $url . '/status.php', |
|
225 | - [ |
|
226 | - 'timeout' => 3, |
|
227 | - 'connect_timeout' => 3, |
|
228 | - ] |
|
229 | - ); |
|
230 | - if ($result->getStatusCode() === Http::STATUS_OK) { |
|
231 | - $isValidOwnCloud = $this->checkOwnCloudVersion($result->getBody()); |
|
232 | - |
|
233 | - } |
|
234 | - } catch (\Exception $e) { |
|
235 | - $this->logger->debug('No Nextcloud server: ' . $e->getMessage()); |
|
236 | - return false; |
|
237 | - } |
|
238 | - |
|
239 | - return $isValidOwnCloud; |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * check if ownCloud version is >= 9.0 |
|
244 | - * |
|
245 | - * @param $status |
|
246 | - * @return bool |
|
247 | - * @throws HintException |
|
248 | - */ |
|
249 | - protected function checkOwnCloudVersion($status) { |
|
250 | - $decoded = json_decode($status, true); |
|
251 | - if (!empty($decoded) && isset($decoded['version'])) { |
|
252 | - if (!version_compare($decoded['version'], '9.0.0', '>=')) { |
|
253 | - throw new HintException('Remote server version is too low. 9.0 is required.'); |
|
254 | - } |
|
255 | - return true; |
|
256 | - } |
|
257 | - return false; |
|
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * check if the URL contain a protocol, if not add https |
|
262 | - * |
|
263 | - * @param string $url |
|
264 | - * @return string |
|
265 | - */ |
|
266 | - protected function updateProtocol($url) { |
|
267 | - if ( |
|
268 | - strpos($url, 'https://') === 0 |
|
269 | - || strpos($url, 'http://') === 0 |
|
270 | - ) { |
|
271 | - |
|
272 | - return $url; |
|
273 | - |
|
274 | - } |
|
275 | - |
|
276 | - return 'https://' . $url; |
|
277 | - } |
|
41 | + /** after a user list was exchanged at least once successfully */ |
|
42 | + const STATUS_OK = 1; |
|
43 | + /** waiting for shared secret or initial user list exchange */ |
|
44 | + const STATUS_PENDING = 2; |
|
45 | + /** something went wrong, misconfigured server, software bug,... user interaction needed */ |
|
46 | + const STATUS_FAILURE = 3; |
|
47 | + /** remote server revoked access */ |
|
48 | + const STATUS_ACCESS_REVOKED = 4; |
|
49 | + |
|
50 | + /** @var dbHandler */ |
|
51 | + private $dbHandler; |
|
52 | + |
|
53 | + /** @var IClientService */ |
|
54 | + private $httpClientService; |
|
55 | + |
|
56 | + /** @var ILogger */ |
|
57 | + private $logger; |
|
58 | + |
|
59 | + /** @var IJobList */ |
|
60 | + private $jobList; |
|
61 | + |
|
62 | + /** @var ISecureRandom */ |
|
63 | + private $secureRandom; |
|
64 | + |
|
65 | + /** @var IConfig */ |
|
66 | + private $config; |
|
67 | + |
|
68 | + /** @var EventDispatcherInterface */ |
|
69 | + private $dispatcher; |
|
70 | + |
|
71 | + /** |
|
72 | + * @param DbHandler $dbHandler |
|
73 | + * @param IClientService $httpClientService |
|
74 | + * @param ILogger $logger |
|
75 | + * @param IJobList $jobList |
|
76 | + * @param ISecureRandom $secureRandom |
|
77 | + * @param IConfig $config |
|
78 | + * @param EventDispatcherInterface $dispatcher |
|
79 | + */ |
|
80 | + public function __construct( |
|
81 | + DbHandler $dbHandler, |
|
82 | + IClientService $httpClientService, |
|
83 | + ILogger $logger, |
|
84 | + IJobList $jobList, |
|
85 | + ISecureRandom $secureRandom, |
|
86 | + IConfig $config, |
|
87 | + EventDispatcherInterface $dispatcher |
|
88 | + ) { |
|
89 | + $this->dbHandler = $dbHandler; |
|
90 | + $this->httpClientService = $httpClientService; |
|
91 | + $this->logger = $logger; |
|
92 | + $this->jobList = $jobList; |
|
93 | + $this->secureRandom = $secureRandom; |
|
94 | + $this->config = $config; |
|
95 | + $this->dispatcher = $dispatcher; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * add server to the list of trusted servers |
|
100 | + * |
|
101 | + * @param $url |
|
102 | + * @return int server id |
|
103 | + */ |
|
104 | + public function addServer($url) { |
|
105 | + $url = $this->updateProtocol($url); |
|
106 | + $result = $this->dbHandler->addServer($url); |
|
107 | + if ($result) { |
|
108 | + $token = $this->secureRandom->generate(16); |
|
109 | + $this->dbHandler->addToken($url, $token); |
|
110 | + $this->jobList->add( |
|
111 | + 'OCA\Federation\BackgroundJob\RequestSharedSecret', |
|
112 | + [ |
|
113 | + 'url' => $url, |
|
114 | + 'token' => $token |
|
115 | + ] |
|
116 | + ); |
|
117 | + } |
|
118 | + |
|
119 | + return $result; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * enable/disable to automatically add servers to the list of trusted servers |
|
124 | + * once a federated share was created and accepted successfully |
|
125 | + * |
|
126 | + * @param bool $status |
|
127 | + */ |
|
128 | + public function setAutoAddServers($status) { |
|
129 | + $value = $status ? '1' : '0'; |
|
130 | + $this->config->setAppValue('federation', 'autoAddServers', $value); |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * return if we automatically add servers to the list of trusted servers |
|
135 | + * once a federated share was created and accepted successfully |
|
136 | + * |
|
137 | + * @return bool |
|
138 | + */ |
|
139 | + public function getAutoAddServers() { |
|
140 | + $value = $this->config->getAppValue('federation', 'autoAddServers', '0'); |
|
141 | + return $value === '1'; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * get shared secret for the given server |
|
146 | + * |
|
147 | + * @param string $url |
|
148 | + * @return string |
|
149 | + */ |
|
150 | + public function getSharedSecret($url) { |
|
151 | + return $this->dbHandler->getSharedSecret($url); |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * add shared secret for the given server |
|
156 | + * |
|
157 | + * @param string $url |
|
158 | + * @param $sharedSecret |
|
159 | + */ |
|
160 | + public function addSharedSecret($url, $sharedSecret) { |
|
161 | + $this->dbHandler->addSharedSecret($url, $sharedSecret); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * remove server from the list of trusted servers |
|
166 | + * |
|
167 | + * @param int $id |
|
168 | + */ |
|
169 | + public function removeServer($id) { |
|
170 | + $server = $this->dbHandler->getServerById($id); |
|
171 | + $this->dbHandler->removeServer($id); |
|
172 | + $event = new GenericEvent($server['url_hash']); |
|
173 | + $this->dispatcher->dispatch('OCP\Federation\TrustedServerEvent::remove', $event); |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * get all trusted servers |
|
178 | + * |
|
179 | + * @return array |
|
180 | + */ |
|
181 | + public function getServers() { |
|
182 | + return $this->dbHandler->getAllServer(); |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * check if given server is a trusted ownCloud server |
|
187 | + * |
|
188 | + * @param string $url |
|
189 | + * @return bool |
|
190 | + */ |
|
191 | + public function isTrustedServer($url) { |
|
192 | + return $this->dbHandler->serverExists($url); |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * set server status |
|
197 | + * |
|
198 | + * @param string $url |
|
199 | + * @param int $status |
|
200 | + */ |
|
201 | + public function setServerStatus($url, $status) { |
|
202 | + $this->dbHandler->setServerStatus($url, $status); |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * @param string $url |
|
207 | + * @return int |
|
208 | + */ |
|
209 | + public function getServerStatus($url) { |
|
210 | + return $this->dbHandler->getServerStatus($url); |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * check if URL point to a ownCloud/Nextcloud server |
|
215 | + * |
|
216 | + * @param string $url |
|
217 | + * @return bool |
|
218 | + */ |
|
219 | + public function isOwnCloudServer($url) { |
|
220 | + $isValidOwnCloud = false; |
|
221 | + $client = $this->httpClientService->newClient(); |
|
222 | + try { |
|
223 | + $result = $client->get( |
|
224 | + $url . '/status.php', |
|
225 | + [ |
|
226 | + 'timeout' => 3, |
|
227 | + 'connect_timeout' => 3, |
|
228 | + ] |
|
229 | + ); |
|
230 | + if ($result->getStatusCode() === Http::STATUS_OK) { |
|
231 | + $isValidOwnCloud = $this->checkOwnCloudVersion($result->getBody()); |
|
232 | + |
|
233 | + } |
|
234 | + } catch (\Exception $e) { |
|
235 | + $this->logger->debug('No Nextcloud server: ' . $e->getMessage()); |
|
236 | + return false; |
|
237 | + } |
|
238 | + |
|
239 | + return $isValidOwnCloud; |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * check if ownCloud version is >= 9.0 |
|
244 | + * |
|
245 | + * @param $status |
|
246 | + * @return bool |
|
247 | + * @throws HintException |
|
248 | + */ |
|
249 | + protected function checkOwnCloudVersion($status) { |
|
250 | + $decoded = json_decode($status, true); |
|
251 | + if (!empty($decoded) && isset($decoded['version'])) { |
|
252 | + if (!version_compare($decoded['version'], '9.0.0', '>=')) { |
|
253 | + throw new HintException('Remote server version is too low. 9.0 is required.'); |
|
254 | + } |
|
255 | + return true; |
|
256 | + } |
|
257 | + return false; |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * check if the URL contain a protocol, if not add https |
|
262 | + * |
|
263 | + * @param string $url |
|
264 | + * @return string |
|
265 | + */ |
|
266 | + protected function updateProtocol($url) { |
|
267 | + if ( |
|
268 | + strpos($url, 'https://') === 0 |
|
269 | + || strpos($url, 'http://') === 0 |
|
270 | + ) { |
|
271 | + |
|
272 | + return $url; |
|
273 | + |
|
274 | + } |
|
275 | + |
|
276 | + return 'https://' . $url; |
|
277 | + } |
|
278 | 278 | } |
@@ -47,157 +47,157 @@ |
||
47 | 47 | */ |
48 | 48 | class GetSharedSecret extends Job{ |
49 | 49 | |
50 | - /** @var IClient */ |
|
51 | - private $httpClient; |
|
52 | - |
|
53 | - /** @var IJobList */ |
|
54 | - private $jobList; |
|
55 | - |
|
56 | - /** @var IURLGenerator */ |
|
57 | - private $urlGenerator; |
|
58 | - |
|
59 | - /** @var TrustedServers */ |
|
60 | - private $trustedServers; |
|
61 | - |
|
62 | - /** @var DbHandler */ |
|
63 | - private $dbHandler; |
|
64 | - |
|
65 | - /** @var ILogger */ |
|
66 | - private $logger; |
|
67 | - |
|
68 | - /** @var bool */ |
|
69 | - protected $retainJob = false; |
|
70 | - |
|
71 | - private $endPoint = '/ocs/v2.php/apps/federation/api/v1/shared-secret?format=json'; |
|
72 | - |
|
73 | - /** |
|
74 | - * RequestSharedSecret constructor. |
|
75 | - * |
|
76 | - * @param IClient $httpClient |
|
77 | - * @param IURLGenerator $urlGenerator |
|
78 | - * @param IJobList $jobList |
|
79 | - * @param TrustedServers $trustedServers |
|
80 | - * @param ILogger $logger |
|
81 | - * @param DbHandler $dbHandler |
|
82 | - */ |
|
83 | - public function __construct( |
|
84 | - IClient $httpClient = null, |
|
85 | - IURLGenerator $urlGenerator = null, |
|
86 | - IJobList $jobList = null, |
|
87 | - TrustedServers $trustedServers = null, |
|
88 | - ILogger $logger = null, |
|
89 | - DbHandler $dbHandler = null |
|
90 | - ) { |
|
91 | - $this->logger = $logger ? $logger : \OC::$server->getLogger(); |
|
92 | - $this->httpClient = $httpClient ? $httpClient : \OC::$server->getHTTPClientService()->newClient(); |
|
93 | - $this->jobList = $jobList ? $jobList : \OC::$server->getJobList(); |
|
94 | - $this->urlGenerator = $urlGenerator ? $urlGenerator : \OC::$server->getURLGenerator(); |
|
95 | - $this->dbHandler = $dbHandler ? $dbHandler : new DbHandler(\OC::$server->getDatabaseConnection(), \OC::$server->getL10N('federation')); |
|
96 | - if ($trustedServers) { |
|
97 | - $this->trustedServers = $trustedServers; |
|
98 | - } else { |
|
99 | - $this->trustedServers = new TrustedServers( |
|
100 | - $this->dbHandler, |
|
101 | - \OC::$server->getHTTPClientService(), |
|
102 | - $this->logger, |
|
103 | - $this->jobList, |
|
104 | - \OC::$server->getSecureRandom(), |
|
105 | - \OC::$server->getConfig(), |
|
106 | - \OC::$server->getEventDispatcher() |
|
107 | - ); |
|
108 | - } |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * run the job, then remove it from the joblist |
|
113 | - * |
|
114 | - * @param JobList $jobList |
|
115 | - * @param ILogger $logger |
|
116 | - */ |
|
117 | - public function execute($jobList, ILogger $logger = null) { |
|
118 | - $target = $this->argument['url']; |
|
119 | - // only execute if target is still in the list of trusted domains |
|
120 | - if ($this->trustedServers->isTrustedServer($target)) { |
|
121 | - $this->parentExecute($jobList, $logger); |
|
122 | - } |
|
123 | - |
|
124 | - if (!$this->retainJob) { |
|
125 | - $jobList->remove($this, $this->argument); |
|
126 | - } |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * call execute() method of parent |
|
131 | - * |
|
132 | - * @param JobList $jobList |
|
133 | - * @param ILogger $logger |
|
134 | - */ |
|
135 | - protected function parentExecute($jobList, $logger = null) { |
|
136 | - parent::execute($jobList, $logger); |
|
137 | - } |
|
138 | - |
|
139 | - protected function run($argument) { |
|
140 | - $target = $argument['url']; |
|
141 | - $source = $this->urlGenerator->getAbsoluteURL('/'); |
|
142 | - $source = rtrim($source, '/'); |
|
143 | - $token = $argument['token']; |
|
144 | - |
|
145 | - $result = null; |
|
146 | - try { |
|
147 | - $result = $this->httpClient->get( |
|
148 | - $target . $this->endPoint, |
|
149 | - [ |
|
150 | - 'query' => |
|
151 | - [ |
|
152 | - 'url' => $source, |
|
153 | - 'token' => $token |
|
154 | - ], |
|
155 | - 'timeout' => 3, |
|
156 | - 'connect_timeout' => 3, |
|
157 | - ] |
|
158 | - ); |
|
159 | - |
|
160 | - $status = $result->getStatusCode(); |
|
161 | - |
|
162 | - } catch (ClientException $e) { |
|
163 | - $status = $e->getCode(); |
|
164 | - if ($status === Http::STATUS_FORBIDDEN) { |
|
165 | - $this->logger->info($target . ' refused to exchange a shared secret with you.', ['app' => 'federation']); |
|
166 | - } else { |
|
167 | - $this->logger->logException($e, ['app' => 'federation']); |
|
168 | - } |
|
169 | - } catch (\Exception $e) { |
|
170 | - $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
|
171 | - $this->logger->logException($e, ['app' => 'federation']); |
|
172 | - } |
|
173 | - |
|
174 | - // if we received a unexpected response we try again later |
|
175 | - if ( |
|
176 | - $status !== Http::STATUS_OK |
|
177 | - && $status !== Http::STATUS_FORBIDDEN |
|
178 | - ) { |
|
179 | - $this->retainJob = true; |
|
180 | - } else { |
|
181 | - // reset token if we received a valid response |
|
182 | - $this->dbHandler->addToken($target, ''); |
|
183 | - } |
|
184 | - |
|
185 | - if ($status === Http::STATUS_OK && $result instanceof IResponse) { |
|
186 | - $body = $result->getBody(); |
|
187 | - $result = json_decode($body, true); |
|
188 | - if (isset($result['ocs']['data']['sharedSecret'])) { |
|
189 | - $this->trustedServers->addSharedSecret( |
|
190 | - $target, |
|
191 | - $result['ocs']['data']['sharedSecret'] |
|
192 | - ); |
|
193 | - } else { |
|
194 | - $this->logger->error( |
|
195 | - 'remote server "' . $target . '"" does not return a valid shared secret', |
|
196 | - ['app' => 'federation'] |
|
197 | - ); |
|
198 | - $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
|
199 | - } |
|
200 | - } |
|
201 | - |
|
202 | - } |
|
50 | + /** @var IClient */ |
|
51 | + private $httpClient; |
|
52 | + |
|
53 | + /** @var IJobList */ |
|
54 | + private $jobList; |
|
55 | + |
|
56 | + /** @var IURLGenerator */ |
|
57 | + private $urlGenerator; |
|
58 | + |
|
59 | + /** @var TrustedServers */ |
|
60 | + private $trustedServers; |
|
61 | + |
|
62 | + /** @var DbHandler */ |
|
63 | + private $dbHandler; |
|
64 | + |
|
65 | + /** @var ILogger */ |
|
66 | + private $logger; |
|
67 | + |
|
68 | + /** @var bool */ |
|
69 | + protected $retainJob = false; |
|
70 | + |
|
71 | + private $endPoint = '/ocs/v2.php/apps/federation/api/v1/shared-secret?format=json'; |
|
72 | + |
|
73 | + /** |
|
74 | + * RequestSharedSecret constructor. |
|
75 | + * |
|
76 | + * @param IClient $httpClient |
|
77 | + * @param IURLGenerator $urlGenerator |
|
78 | + * @param IJobList $jobList |
|
79 | + * @param TrustedServers $trustedServers |
|
80 | + * @param ILogger $logger |
|
81 | + * @param DbHandler $dbHandler |
|
82 | + */ |
|
83 | + public function __construct( |
|
84 | + IClient $httpClient = null, |
|
85 | + IURLGenerator $urlGenerator = null, |
|
86 | + IJobList $jobList = null, |
|
87 | + TrustedServers $trustedServers = null, |
|
88 | + ILogger $logger = null, |
|
89 | + DbHandler $dbHandler = null |
|
90 | + ) { |
|
91 | + $this->logger = $logger ? $logger : \OC::$server->getLogger(); |
|
92 | + $this->httpClient = $httpClient ? $httpClient : \OC::$server->getHTTPClientService()->newClient(); |
|
93 | + $this->jobList = $jobList ? $jobList : \OC::$server->getJobList(); |
|
94 | + $this->urlGenerator = $urlGenerator ? $urlGenerator : \OC::$server->getURLGenerator(); |
|
95 | + $this->dbHandler = $dbHandler ? $dbHandler : new DbHandler(\OC::$server->getDatabaseConnection(), \OC::$server->getL10N('federation')); |
|
96 | + if ($trustedServers) { |
|
97 | + $this->trustedServers = $trustedServers; |
|
98 | + } else { |
|
99 | + $this->trustedServers = new TrustedServers( |
|
100 | + $this->dbHandler, |
|
101 | + \OC::$server->getHTTPClientService(), |
|
102 | + $this->logger, |
|
103 | + $this->jobList, |
|
104 | + \OC::$server->getSecureRandom(), |
|
105 | + \OC::$server->getConfig(), |
|
106 | + \OC::$server->getEventDispatcher() |
|
107 | + ); |
|
108 | + } |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * run the job, then remove it from the joblist |
|
113 | + * |
|
114 | + * @param JobList $jobList |
|
115 | + * @param ILogger $logger |
|
116 | + */ |
|
117 | + public function execute($jobList, ILogger $logger = null) { |
|
118 | + $target = $this->argument['url']; |
|
119 | + // only execute if target is still in the list of trusted domains |
|
120 | + if ($this->trustedServers->isTrustedServer($target)) { |
|
121 | + $this->parentExecute($jobList, $logger); |
|
122 | + } |
|
123 | + |
|
124 | + if (!$this->retainJob) { |
|
125 | + $jobList->remove($this, $this->argument); |
|
126 | + } |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * call execute() method of parent |
|
131 | + * |
|
132 | + * @param JobList $jobList |
|
133 | + * @param ILogger $logger |
|
134 | + */ |
|
135 | + protected function parentExecute($jobList, $logger = null) { |
|
136 | + parent::execute($jobList, $logger); |
|
137 | + } |
|
138 | + |
|
139 | + protected function run($argument) { |
|
140 | + $target = $argument['url']; |
|
141 | + $source = $this->urlGenerator->getAbsoluteURL('/'); |
|
142 | + $source = rtrim($source, '/'); |
|
143 | + $token = $argument['token']; |
|
144 | + |
|
145 | + $result = null; |
|
146 | + try { |
|
147 | + $result = $this->httpClient->get( |
|
148 | + $target . $this->endPoint, |
|
149 | + [ |
|
150 | + 'query' => |
|
151 | + [ |
|
152 | + 'url' => $source, |
|
153 | + 'token' => $token |
|
154 | + ], |
|
155 | + 'timeout' => 3, |
|
156 | + 'connect_timeout' => 3, |
|
157 | + ] |
|
158 | + ); |
|
159 | + |
|
160 | + $status = $result->getStatusCode(); |
|
161 | + |
|
162 | + } catch (ClientException $e) { |
|
163 | + $status = $e->getCode(); |
|
164 | + if ($status === Http::STATUS_FORBIDDEN) { |
|
165 | + $this->logger->info($target . ' refused to exchange a shared secret with you.', ['app' => 'federation']); |
|
166 | + } else { |
|
167 | + $this->logger->logException($e, ['app' => 'federation']); |
|
168 | + } |
|
169 | + } catch (\Exception $e) { |
|
170 | + $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
|
171 | + $this->logger->logException($e, ['app' => 'federation']); |
|
172 | + } |
|
173 | + |
|
174 | + // if we received a unexpected response we try again later |
|
175 | + if ( |
|
176 | + $status !== Http::STATUS_OK |
|
177 | + && $status !== Http::STATUS_FORBIDDEN |
|
178 | + ) { |
|
179 | + $this->retainJob = true; |
|
180 | + } else { |
|
181 | + // reset token if we received a valid response |
|
182 | + $this->dbHandler->addToken($target, ''); |
|
183 | + } |
|
184 | + |
|
185 | + if ($status === Http::STATUS_OK && $result instanceof IResponse) { |
|
186 | + $body = $result->getBody(); |
|
187 | + $result = json_decode($body, true); |
|
188 | + if (isset($result['ocs']['data']['sharedSecret'])) { |
|
189 | + $this->trustedServers->addSharedSecret( |
|
190 | + $target, |
|
191 | + $result['ocs']['data']['sharedSecret'] |
|
192 | + ); |
|
193 | + } else { |
|
194 | + $this->logger->error( |
|
195 | + 'remote server "' . $target . '"" does not return a valid shared secret', |
|
196 | + ['app' => 'federation'] |
|
197 | + ); |
|
198 | + $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
|
199 | + } |
|
200 | + } |
|
201 | + |
|
202 | + } |
|
203 | 203 | } |
@@ -47,140 +47,140 @@ |
||
47 | 47 | */ |
48 | 48 | class RequestSharedSecret extends Job { |
49 | 49 | |
50 | - /** @var IClient */ |
|
51 | - private $httpClient; |
|
52 | - |
|
53 | - /** @var IJobList */ |
|
54 | - private $jobList; |
|
55 | - |
|
56 | - /** @var IURLGenerator */ |
|
57 | - private $urlGenerator; |
|
58 | - |
|
59 | - /** @var DbHandler */ |
|
60 | - private $dbHandler; |
|
61 | - |
|
62 | - /** @var TrustedServers */ |
|
63 | - private $trustedServers; |
|
64 | - |
|
65 | - private $endPoint = '/ocs/v2.php/apps/federation/api/v1/request-shared-secret?format=json'; |
|
66 | - |
|
67 | - /** @var ILogger */ |
|
68 | - private $logger; |
|
69 | - |
|
70 | - /** @var bool */ |
|
71 | - protected $retainJob = false; |
|
72 | - |
|
73 | - /** |
|
74 | - * RequestSharedSecret constructor. |
|
75 | - * |
|
76 | - * @param IClient $httpClient |
|
77 | - * @param IURLGenerator $urlGenerator |
|
78 | - * @param IJobList $jobList |
|
79 | - * @param TrustedServers $trustedServers |
|
80 | - * @param DbHandler $dbHandler |
|
81 | - */ |
|
82 | - public function __construct( |
|
83 | - IClient $httpClient = null, |
|
84 | - IURLGenerator $urlGenerator = null, |
|
85 | - IJobList $jobList = null, |
|
86 | - TrustedServers $trustedServers = null, |
|
87 | - DbHandler $dbHandler = null |
|
88 | - ) { |
|
89 | - $this->httpClient = $httpClient ? $httpClient : \OC::$server->getHTTPClientService()->newClient(); |
|
90 | - $this->jobList = $jobList ? $jobList : \OC::$server->getJobList(); |
|
91 | - $this->urlGenerator = $urlGenerator ? $urlGenerator : \OC::$server->getURLGenerator(); |
|
92 | - $this->dbHandler = $dbHandler ? $dbHandler : new DbHandler(\OC::$server->getDatabaseConnection(), \OC::$server->getL10N('federation')); |
|
93 | - $this->logger = \OC::$server->getLogger(); |
|
94 | - if ($trustedServers) { |
|
95 | - $this->trustedServers = $trustedServers; |
|
96 | - } else { |
|
97 | - $this->trustedServers = new TrustedServers( |
|
98 | - $this->dbHandler, |
|
99 | - \OC::$server->getHTTPClientService(), |
|
100 | - $this->logger, |
|
101 | - $this->jobList, |
|
102 | - \OC::$server->getSecureRandom(), |
|
103 | - \OC::$server->getConfig(), |
|
104 | - \OC::$server->getEventDispatcher() |
|
105 | - ); |
|
106 | - } |
|
107 | - } |
|
108 | - |
|
109 | - |
|
110 | - /** |
|
111 | - * run the job, then remove it from the joblist |
|
112 | - * |
|
113 | - * @param JobList $jobList |
|
114 | - * @param ILogger $logger |
|
115 | - */ |
|
116 | - public function execute($jobList, ILogger $logger = null) { |
|
117 | - $target = $this->argument['url']; |
|
118 | - // only execute if target is still in the list of trusted domains |
|
119 | - if ($this->trustedServers->isTrustedServer($target)) { |
|
120 | - $this->parentExecute($jobList, $logger); |
|
121 | - } |
|
122 | - |
|
123 | - if (!$this->retainJob) { |
|
124 | - $jobList->remove($this, $this->argument); |
|
125 | - } |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * call execute() method of parent |
|
130 | - * |
|
131 | - * @param JobList $jobList |
|
132 | - * @param ILogger $logger |
|
133 | - */ |
|
134 | - protected function parentExecute($jobList, $logger) { |
|
135 | - parent::execute($jobList, $logger); |
|
136 | - } |
|
137 | - |
|
138 | - protected function run($argument) { |
|
139 | - |
|
140 | - $target = $argument['url']; |
|
141 | - $source = $this->urlGenerator->getAbsoluteURL('/'); |
|
142 | - $source = rtrim($source, '/'); |
|
143 | - $token = $argument['token']; |
|
144 | - |
|
145 | - try { |
|
146 | - $result = $this->httpClient->post( |
|
147 | - $target . $this->endPoint, |
|
148 | - [ |
|
149 | - 'body' => [ |
|
150 | - 'url' => $source, |
|
151 | - 'token' => $token, |
|
152 | - ], |
|
153 | - 'timeout' => 3, |
|
154 | - 'connect_timeout' => 3, |
|
155 | - ] |
|
156 | - ); |
|
157 | - |
|
158 | - $status = $result->getStatusCode(); |
|
159 | - |
|
160 | - } catch (ClientException $e) { |
|
161 | - $status = $e->getCode(); |
|
162 | - if ($status === Http::STATUS_FORBIDDEN) { |
|
163 | - $this->logger->info($target . ' refused to ask for a shared secret.', ['app' => 'federation']); |
|
164 | - } else { |
|
165 | - $this->logger->logException($e, ['app' => 'federation']); |
|
166 | - } |
|
167 | - } catch (\Exception $e) { |
|
168 | - $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
|
169 | - $this->logger->logException($e, ['app' => 'federation']); |
|
170 | - } |
|
171 | - |
|
172 | - // if we received a unexpected response we try again later |
|
173 | - if ( |
|
174 | - $status !== Http::STATUS_OK |
|
175 | - && $status !== Http::STATUS_FORBIDDEN |
|
176 | - ) { |
|
177 | - $this->retainJob = true; |
|
178 | - } |
|
179 | - |
|
180 | - if ($status === Http::STATUS_FORBIDDEN) { |
|
181 | - // clear token if remote server refuses to ask for shared secret |
|
182 | - $this->dbHandler->addToken($target, ''); |
|
183 | - } |
|
184 | - |
|
185 | - } |
|
50 | + /** @var IClient */ |
|
51 | + private $httpClient; |
|
52 | + |
|
53 | + /** @var IJobList */ |
|
54 | + private $jobList; |
|
55 | + |
|
56 | + /** @var IURLGenerator */ |
|
57 | + private $urlGenerator; |
|
58 | + |
|
59 | + /** @var DbHandler */ |
|
60 | + private $dbHandler; |
|
61 | + |
|
62 | + /** @var TrustedServers */ |
|
63 | + private $trustedServers; |
|
64 | + |
|
65 | + private $endPoint = '/ocs/v2.php/apps/federation/api/v1/request-shared-secret?format=json'; |
|
66 | + |
|
67 | + /** @var ILogger */ |
|
68 | + private $logger; |
|
69 | + |
|
70 | + /** @var bool */ |
|
71 | + protected $retainJob = false; |
|
72 | + |
|
73 | + /** |
|
74 | + * RequestSharedSecret constructor. |
|
75 | + * |
|
76 | + * @param IClient $httpClient |
|
77 | + * @param IURLGenerator $urlGenerator |
|
78 | + * @param IJobList $jobList |
|
79 | + * @param TrustedServers $trustedServers |
|
80 | + * @param DbHandler $dbHandler |
|
81 | + */ |
|
82 | + public function __construct( |
|
83 | + IClient $httpClient = null, |
|
84 | + IURLGenerator $urlGenerator = null, |
|
85 | + IJobList $jobList = null, |
|
86 | + TrustedServers $trustedServers = null, |
|
87 | + DbHandler $dbHandler = null |
|
88 | + ) { |
|
89 | + $this->httpClient = $httpClient ? $httpClient : \OC::$server->getHTTPClientService()->newClient(); |
|
90 | + $this->jobList = $jobList ? $jobList : \OC::$server->getJobList(); |
|
91 | + $this->urlGenerator = $urlGenerator ? $urlGenerator : \OC::$server->getURLGenerator(); |
|
92 | + $this->dbHandler = $dbHandler ? $dbHandler : new DbHandler(\OC::$server->getDatabaseConnection(), \OC::$server->getL10N('federation')); |
|
93 | + $this->logger = \OC::$server->getLogger(); |
|
94 | + if ($trustedServers) { |
|
95 | + $this->trustedServers = $trustedServers; |
|
96 | + } else { |
|
97 | + $this->trustedServers = new TrustedServers( |
|
98 | + $this->dbHandler, |
|
99 | + \OC::$server->getHTTPClientService(), |
|
100 | + $this->logger, |
|
101 | + $this->jobList, |
|
102 | + \OC::$server->getSecureRandom(), |
|
103 | + \OC::$server->getConfig(), |
|
104 | + \OC::$server->getEventDispatcher() |
|
105 | + ); |
|
106 | + } |
|
107 | + } |
|
108 | + |
|
109 | + |
|
110 | + /** |
|
111 | + * run the job, then remove it from the joblist |
|
112 | + * |
|
113 | + * @param JobList $jobList |
|
114 | + * @param ILogger $logger |
|
115 | + */ |
|
116 | + public function execute($jobList, ILogger $logger = null) { |
|
117 | + $target = $this->argument['url']; |
|
118 | + // only execute if target is still in the list of trusted domains |
|
119 | + if ($this->trustedServers->isTrustedServer($target)) { |
|
120 | + $this->parentExecute($jobList, $logger); |
|
121 | + } |
|
122 | + |
|
123 | + if (!$this->retainJob) { |
|
124 | + $jobList->remove($this, $this->argument); |
|
125 | + } |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * call execute() method of parent |
|
130 | + * |
|
131 | + * @param JobList $jobList |
|
132 | + * @param ILogger $logger |
|
133 | + */ |
|
134 | + protected function parentExecute($jobList, $logger) { |
|
135 | + parent::execute($jobList, $logger); |
|
136 | + } |
|
137 | + |
|
138 | + protected function run($argument) { |
|
139 | + |
|
140 | + $target = $argument['url']; |
|
141 | + $source = $this->urlGenerator->getAbsoluteURL('/'); |
|
142 | + $source = rtrim($source, '/'); |
|
143 | + $token = $argument['token']; |
|
144 | + |
|
145 | + try { |
|
146 | + $result = $this->httpClient->post( |
|
147 | + $target . $this->endPoint, |
|
148 | + [ |
|
149 | + 'body' => [ |
|
150 | + 'url' => $source, |
|
151 | + 'token' => $token, |
|
152 | + ], |
|
153 | + 'timeout' => 3, |
|
154 | + 'connect_timeout' => 3, |
|
155 | + ] |
|
156 | + ); |
|
157 | + |
|
158 | + $status = $result->getStatusCode(); |
|
159 | + |
|
160 | + } catch (ClientException $e) { |
|
161 | + $status = $e->getCode(); |
|
162 | + if ($status === Http::STATUS_FORBIDDEN) { |
|
163 | + $this->logger->info($target . ' refused to ask for a shared secret.', ['app' => 'federation']); |
|
164 | + } else { |
|
165 | + $this->logger->logException($e, ['app' => 'federation']); |
|
166 | + } |
|
167 | + } catch (\Exception $e) { |
|
168 | + $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
|
169 | + $this->logger->logException($e, ['app' => 'federation']); |
|
170 | + } |
|
171 | + |
|
172 | + // if we received a unexpected response we try again later |
|
173 | + if ( |
|
174 | + $status !== Http::STATUS_OK |
|
175 | + && $status !== Http::STATUS_FORBIDDEN |
|
176 | + ) { |
|
177 | + $this->retainJob = true; |
|
178 | + } |
|
179 | + |
|
180 | + if ($status === Http::STATUS_FORBIDDEN) { |
|
181 | + // clear token if remote server refuses to ask for shared secret |
|
182 | + $this->dbHandler->addToken($target, ''); |
|
183 | + } |
|
184 | + |
|
185 | + } |
|
186 | 186 | } |
@@ -29,41 +29,41 @@ |
||
29 | 29 | |
30 | 30 | class Admin implements ISettings { |
31 | 31 | |
32 | - /** @var TrustedServers */ |
|
33 | - private $trustedServers; |
|
32 | + /** @var TrustedServers */ |
|
33 | + private $trustedServers; |
|
34 | 34 | |
35 | - public function __construct(TrustedServers $trustedServers) { |
|
36 | - $this->trustedServers = $trustedServers; |
|
37 | - } |
|
35 | + public function __construct(TrustedServers $trustedServers) { |
|
36 | + $this->trustedServers = $trustedServers; |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * @return TemplateResponse |
|
41 | - */ |
|
42 | - public function getForm() { |
|
43 | - $parameters = [ |
|
44 | - 'trustedServers' => $this->trustedServers->getServers(), |
|
45 | - 'autoAddServers' => $this->trustedServers->getAutoAddServers(), |
|
46 | - ]; |
|
39 | + /** |
|
40 | + * @return TemplateResponse |
|
41 | + */ |
|
42 | + public function getForm() { |
|
43 | + $parameters = [ |
|
44 | + 'trustedServers' => $this->trustedServers->getServers(), |
|
45 | + 'autoAddServers' => $this->trustedServers->getAutoAddServers(), |
|
46 | + ]; |
|
47 | 47 | |
48 | - return new TemplateResponse('federation', 'settings-admin', $parameters, ''); |
|
49 | - } |
|
48 | + return new TemplateResponse('federation', 'settings-admin', $parameters, ''); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * @return string the section ID, e.g. 'sharing' |
|
53 | - */ |
|
54 | - public function getSection() { |
|
55 | - return 'sharing'; |
|
56 | - } |
|
51 | + /** |
|
52 | + * @return string the section ID, e.g. 'sharing' |
|
53 | + */ |
|
54 | + public function getSection() { |
|
55 | + return 'sharing'; |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * @return int whether the form should be rather on the top or bottom of |
|
60 | - * the admin section. The forms are arranged in ascending order of the |
|
61 | - * priority values. It is required to return a value between 0 and 100. |
|
62 | - * |
|
63 | - * E.g.: 70 |
|
64 | - */ |
|
65 | - public function getPriority() { |
|
66 | - return 30; |
|
67 | - } |
|
58 | + /** |
|
59 | + * @return int whether the form should be rather on the top or bottom of |
|
60 | + * the admin section. The forms are arranged in ascending order of the |
|
61 | + * priority values. It is required to return a value between 0 and 100. |
|
62 | + * |
|
63 | + * E.g.: 70 |
|
64 | + */ |
|
65 | + public function getPriority() { |
|
66 | + return 30; |
|
67 | + } |
|
68 | 68 | |
69 | 69 | } |
@@ -27,25 +27,25 @@ |
||
27 | 27 | |
28 | 28 | class Hooks { |
29 | 29 | |
30 | - /** @var TrustedServers */ |
|
31 | - private $trustedServers; |
|
32 | - |
|
33 | - public function __construct(TrustedServers $trustedServers) { |
|
34 | - $this->trustedServers = $trustedServers; |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * add servers to the list of trusted servers once a federated share was established |
|
39 | - * |
|
40 | - * @param array $params |
|
41 | - */ |
|
42 | - public function addServerHook($params) { |
|
43 | - if ( |
|
44 | - $this->trustedServers->getAutoAddServers() === true && |
|
45 | - $this->trustedServers->isTrustedServer($params['server']) === false |
|
46 | - ) { |
|
47 | - $this->trustedServers->addServer($params['server']); |
|
48 | - } |
|
49 | - } |
|
30 | + /** @var TrustedServers */ |
|
31 | + private $trustedServers; |
|
32 | + |
|
33 | + public function __construct(TrustedServers $trustedServers) { |
|
34 | + $this->trustedServers = $trustedServers; |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * add servers to the list of trusted servers once a federated share was established |
|
39 | + * |
|
40 | + * @param array $params |
|
41 | + */ |
|
42 | + public function addServerHook($params) { |
|
43 | + if ( |
|
44 | + $this->trustedServers->getAutoAddServers() === true && |
|
45 | + $this->trustedServers->isTrustedServer($params['server']) === false |
|
46 | + ) { |
|
47 | + $this->trustedServers->addServer($params['server']); |
|
48 | + } |
|
49 | + } |
|
50 | 50 | |
51 | 51 | } |
@@ -33,42 +33,42 @@ |
||
33 | 33 | |
34 | 34 | class AddServerMiddleware extends Middleware { |
35 | 35 | |
36 | - /** @var string */ |
|
37 | - protected $appName; |
|
36 | + /** @var string */ |
|
37 | + protected $appName; |
|
38 | 38 | |
39 | - /** @var IL10N */ |
|
40 | - protected $l; |
|
39 | + /** @var IL10N */ |
|
40 | + protected $l; |
|
41 | 41 | |
42 | - /** @var ILogger */ |
|
43 | - protected $logger; |
|
42 | + /** @var ILogger */ |
|
43 | + protected $logger; |
|
44 | 44 | |
45 | - public function __construct($appName, IL10N $l, ILogger $logger) { |
|
46 | - $this->appName = $appName; |
|
47 | - $this->l = $l; |
|
48 | - $this->logger = $logger; |
|
49 | - } |
|
45 | + public function __construct($appName, IL10N $l, ILogger $logger) { |
|
46 | + $this->appName = $appName; |
|
47 | + $this->l = $l; |
|
48 | + $this->logger = $logger; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Log error message and return a response which can be displayed to the user |
|
53 | - * |
|
54 | - * @param \OCP\AppFramework\Controller $controller |
|
55 | - * @param string $methodName |
|
56 | - * @param \Exception $exception |
|
57 | - * @return JSONResponse |
|
58 | - */ |
|
59 | - public function afterException($controller, $methodName, \Exception $exception) { |
|
60 | - $this->logger->error($exception->getMessage(), ['app' => $this->appName]); |
|
61 | - if ($exception instanceof HintException) { |
|
62 | - $message = $exception->getHint(); |
|
63 | - } else { |
|
64 | - $message = $exception->getMessage(); |
|
65 | - } |
|
51 | + /** |
|
52 | + * Log error message and return a response which can be displayed to the user |
|
53 | + * |
|
54 | + * @param \OCP\AppFramework\Controller $controller |
|
55 | + * @param string $methodName |
|
56 | + * @param \Exception $exception |
|
57 | + * @return JSONResponse |
|
58 | + */ |
|
59 | + public function afterException($controller, $methodName, \Exception $exception) { |
|
60 | + $this->logger->error($exception->getMessage(), ['app' => $this->appName]); |
|
61 | + if ($exception instanceof HintException) { |
|
62 | + $message = $exception->getHint(); |
|
63 | + } else { |
|
64 | + $message = $exception->getMessage(); |
|
65 | + } |
|
66 | 66 | |
67 | - return new JSONResponse( |
|
68 | - ['message' => $message], |
|
69 | - Http::STATUS_BAD_REQUEST |
|
70 | - ); |
|
67 | + return new JSONResponse( |
|
68 | + ['message' => $message], |
|
69 | + Http::STATUS_BAD_REQUEST |
|
70 | + ); |
|
71 | 71 | |
72 | - } |
|
72 | + } |
|
73 | 73 | |
74 | 74 | } |
@@ -42,280 +42,280 @@ |
||
42 | 42 | */ |
43 | 43 | class DbHandler { |
44 | 44 | |
45 | - /** @var IDBConnection */ |
|
46 | - private $connection; |
|
47 | - |
|
48 | - /** @var IL10N */ |
|
49 | - private $IL10N; |
|
50 | - |
|
51 | - /** @var string */ |
|
52 | - private $dbTable = 'trusted_servers'; |
|
53 | - |
|
54 | - /** |
|
55 | - * @param IDBConnection $connection |
|
56 | - * @param IL10N $il10n |
|
57 | - */ |
|
58 | - public function __construct( |
|
59 | - IDBConnection $connection, |
|
60 | - IL10N $il10n |
|
61 | - ) { |
|
62 | - $this->connection = $connection; |
|
63 | - $this->IL10N = $il10n; |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * add server to the list of trusted servers |
|
68 | - * |
|
69 | - * @param string $url |
|
70 | - * @return int |
|
71 | - * @throws HintException |
|
72 | - */ |
|
73 | - public function addServer($url) { |
|
74 | - $hash = $this->hash($url); |
|
75 | - $url = rtrim($url, '/'); |
|
76 | - $query = $this->connection->getQueryBuilder(); |
|
77 | - $query->insert($this->dbTable) |
|
78 | - ->values( |
|
79 | - [ |
|
80 | - 'url' => $query->createParameter('url'), |
|
81 | - 'url_hash' => $query->createParameter('url_hash'), |
|
82 | - ] |
|
83 | - ) |
|
84 | - ->setParameter('url', $url) |
|
85 | - ->setParameter('url_hash', $hash); |
|
86 | - |
|
87 | - $result = $query->execute(); |
|
88 | - |
|
89 | - if ($result) { |
|
90 | - return (int)$this->connection->lastInsertId('*PREFIX*'.$this->dbTable); |
|
91 | - } else { |
|
92 | - $message = 'Internal failure, Could not add trusted server: ' . $url; |
|
93 | - $message_t = $this->IL10N->t('Could not add server'); |
|
94 | - throw new HintException($message, $message_t); |
|
95 | - } |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * remove server from the list of trusted servers |
|
100 | - * |
|
101 | - * @param int $id |
|
102 | - */ |
|
103 | - public function removeServer($id) { |
|
104 | - $query = $this->connection->getQueryBuilder(); |
|
105 | - $query->delete($this->dbTable) |
|
106 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
107 | - ->setParameter('id', $id); |
|
108 | - $query->execute(); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * get trusted server with given ID |
|
113 | - * |
|
114 | - * @param int $id |
|
115 | - * @return array |
|
116 | - * @throws \Exception |
|
117 | - */ |
|
118 | - public function getServerById($id) { |
|
119 | - $query = $this->connection->getQueryBuilder(); |
|
120 | - $query->select('*')->from($this->dbTable) |
|
121 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
122 | - ->setParameter('id', $id); |
|
123 | - $query->execute(); |
|
124 | - $result = $query->execute()->fetchAll(); |
|
125 | - |
|
126 | - if (empty($result)) { |
|
127 | - throw new \Exception('No Server found with ID: ' . $id); |
|
128 | - } |
|
129 | - |
|
130 | - return $result[0]; |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * get all trusted servers |
|
135 | - * |
|
136 | - * @return array |
|
137 | - */ |
|
138 | - public function getAllServer() { |
|
139 | - $query = $this->connection->getQueryBuilder(); |
|
140 | - $query->select(['url', 'url_hash', 'id', 'status', 'shared_secret', 'sync_token'])->from($this->dbTable); |
|
141 | - $result = $query->execute()->fetchAll(); |
|
142 | - return $result; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * check if server already exists in the database table |
|
147 | - * |
|
148 | - * @param string $url |
|
149 | - * @return bool |
|
150 | - */ |
|
151 | - public function serverExists($url) { |
|
152 | - $hash = $this->hash($url); |
|
153 | - $query = $this->connection->getQueryBuilder(); |
|
154 | - $query->select('url')->from($this->dbTable) |
|
155 | - ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
156 | - ->setParameter('url_hash', $hash); |
|
157 | - $result = $query->execute()->fetchAll(); |
|
158 | - |
|
159 | - return !empty($result); |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * write token to database. Token is used to exchange the secret |
|
164 | - * |
|
165 | - * @param string $url |
|
166 | - * @param string $token |
|
167 | - */ |
|
168 | - public function addToken($url, $token) { |
|
169 | - $hash = $this->hash($url); |
|
170 | - $query = $this->connection->getQueryBuilder(); |
|
171 | - $query->update($this->dbTable) |
|
172 | - ->set('token', $query->createParameter('token')) |
|
173 | - ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
174 | - ->setParameter('url_hash', $hash) |
|
175 | - ->setParameter('token', $token); |
|
176 | - $query->execute(); |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * get token stored in database |
|
181 | - * |
|
182 | - * @param string $url |
|
183 | - * @return string |
|
184 | - * @throws \Exception |
|
185 | - */ |
|
186 | - public function getToken($url) { |
|
187 | - $hash = $this->hash($url); |
|
188 | - $query = $this->connection->getQueryBuilder(); |
|
189 | - $query->select('token')->from($this->dbTable) |
|
190 | - ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
191 | - ->setParameter('url_hash', $hash); |
|
192 | - |
|
193 | - $result = $query->execute()->fetch(); |
|
194 | - |
|
195 | - if (!isset($result['token'])) { |
|
196 | - throw new \Exception('No token found for: ' . $url); |
|
197 | - } |
|
198 | - |
|
199 | - return $result['token']; |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * add shared Secret to database |
|
204 | - * |
|
205 | - * @param string $url |
|
206 | - * @param string $sharedSecret |
|
207 | - */ |
|
208 | - public function addSharedSecret($url, $sharedSecret) { |
|
209 | - $hash = $this->hash($url); |
|
210 | - $query = $this->connection->getQueryBuilder(); |
|
211 | - $query->update($this->dbTable) |
|
212 | - ->set('shared_secret', $query->createParameter('sharedSecret')) |
|
213 | - ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
214 | - ->setParameter('url_hash', $hash) |
|
215 | - ->setParameter('sharedSecret', $sharedSecret); |
|
216 | - $query->execute(); |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * get shared secret from database |
|
221 | - * |
|
222 | - * @param string $url |
|
223 | - * @return string |
|
224 | - */ |
|
225 | - public function getSharedSecret($url) { |
|
226 | - $hash = $this->hash($url); |
|
227 | - $query = $this->connection->getQueryBuilder(); |
|
228 | - $query->select('shared_secret')->from($this->dbTable) |
|
229 | - ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
230 | - ->setParameter('url_hash', $hash); |
|
231 | - |
|
232 | - $result = $query->execute()->fetch(); |
|
233 | - return $result['shared_secret']; |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * set server status |
|
238 | - * |
|
239 | - * @param string $url |
|
240 | - * @param int $status |
|
241 | - * @param string|null $token |
|
242 | - */ |
|
243 | - public function setServerStatus($url, $status, $token = null) { |
|
244 | - $hash = $this->hash($url); |
|
245 | - $query = $this->connection->getQueryBuilder(); |
|
246 | - $query->update($this->dbTable) |
|
247 | - ->set('status', $query->createNamedParameter($status)) |
|
248 | - ->where($query->expr()->eq('url_hash', $query->createNamedParameter($hash))); |
|
249 | - if (!is_null($token)) { |
|
250 | - $query->set('sync_token', $query->createNamedParameter($token)); |
|
251 | - } |
|
252 | - $query->execute(); |
|
253 | - } |
|
254 | - |
|
255 | - /** |
|
256 | - * get server status |
|
257 | - * |
|
258 | - * @param string $url |
|
259 | - * @return int |
|
260 | - */ |
|
261 | - public function getServerStatus($url) { |
|
262 | - $hash = $this->hash($url); |
|
263 | - $query = $this->connection->getQueryBuilder(); |
|
264 | - $query->select('status')->from($this->dbTable) |
|
265 | - ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
266 | - ->setParameter('url_hash', $hash); |
|
267 | - |
|
268 | - $result = $query->execute()->fetch(); |
|
269 | - return (int)$result['status']; |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * create hash from URL |
|
274 | - * |
|
275 | - * @param string $url |
|
276 | - * @return string |
|
277 | - */ |
|
278 | - protected function hash($url) { |
|
279 | - $normalized = $this->normalizeUrl($url); |
|
280 | - return sha1($normalized); |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * normalize URL, used to create the sha1 hash |
|
285 | - * |
|
286 | - * @param string $url |
|
287 | - * @return string |
|
288 | - */ |
|
289 | - protected function normalizeUrl($url) { |
|
290 | - $normalized = $url; |
|
291 | - |
|
292 | - if (strpos($url, 'https://') === 0) { |
|
293 | - $normalized = substr($url, strlen('https://')); |
|
294 | - } else if (strpos($url, 'http://') === 0) { |
|
295 | - $normalized = substr($url, strlen('http://')); |
|
296 | - } |
|
297 | - |
|
298 | - $normalized = Filesystem::normalizePath($normalized); |
|
299 | - $normalized = trim($normalized, '/'); |
|
300 | - |
|
301 | - return $normalized; |
|
302 | - } |
|
303 | - |
|
304 | - /** |
|
305 | - * @param $username |
|
306 | - * @param $password |
|
307 | - * @return bool |
|
308 | - */ |
|
309 | - public function auth($username, $password) { |
|
310 | - if ($username !== 'system') { |
|
311 | - return false; |
|
312 | - } |
|
313 | - $query = $this->connection->getQueryBuilder(); |
|
314 | - $query->select('url')->from($this->dbTable) |
|
315 | - ->where($query->expr()->eq('shared_secret', $query->createNamedParameter($password))); |
|
316 | - |
|
317 | - $result = $query->execute()->fetch(); |
|
318 | - return !empty($result); |
|
319 | - } |
|
45 | + /** @var IDBConnection */ |
|
46 | + private $connection; |
|
47 | + |
|
48 | + /** @var IL10N */ |
|
49 | + private $IL10N; |
|
50 | + |
|
51 | + /** @var string */ |
|
52 | + private $dbTable = 'trusted_servers'; |
|
53 | + |
|
54 | + /** |
|
55 | + * @param IDBConnection $connection |
|
56 | + * @param IL10N $il10n |
|
57 | + */ |
|
58 | + public function __construct( |
|
59 | + IDBConnection $connection, |
|
60 | + IL10N $il10n |
|
61 | + ) { |
|
62 | + $this->connection = $connection; |
|
63 | + $this->IL10N = $il10n; |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * add server to the list of trusted servers |
|
68 | + * |
|
69 | + * @param string $url |
|
70 | + * @return int |
|
71 | + * @throws HintException |
|
72 | + */ |
|
73 | + public function addServer($url) { |
|
74 | + $hash = $this->hash($url); |
|
75 | + $url = rtrim($url, '/'); |
|
76 | + $query = $this->connection->getQueryBuilder(); |
|
77 | + $query->insert($this->dbTable) |
|
78 | + ->values( |
|
79 | + [ |
|
80 | + 'url' => $query->createParameter('url'), |
|
81 | + 'url_hash' => $query->createParameter('url_hash'), |
|
82 | + ] |
|
83 | + ) |
|
84 | + ->setParameter('url', $url) |
|
85 | + ->setParameter('url_hash', $hash); |
|
86 | + |
|
87 | + $result = $query->execute(); |
|
88 | + |
|
89 | + if ($result) { |
|
90 | + return (int)$this->connection->lastInsertId('*PREFIX*'.$this->dbTable); |
|
91 | + } else { |
|
92 | + $message = 'Internal failure, Could not add trusted server: ' . $url; |
|
93 | + $message_t = $this->IL10N->t('Could not add server'); |
|
94 | + throw new HintException($message, $message_t); |
|
95 | + } |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * remove server from the list of trusted servers |
|
100 | + * |
|
101 | + * @param int $id |
|
102 | + */ |
|
103 | + public function removeServer($id) { |
|
104 | + $query = $this->connection->getQueryBuilder(); |
|
105 | + $query->delete($this->dbTable) |
|
106 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
107 | + ->setParameter('id', $id); |
|
108 | + $query->execute(); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * get trusted server with given ID |
|
113 | + * |
|
114 | + * @param int $id |
|
115 | + * @return array |
|
116 | + * @throws \Exception |
|
117 | + */ |
|
118 | + public function getServerById($id) { |
|
119 | + $query = $this->connection->getQueryBuilder(); |
|
120 | + $query->select('*')->from($this->dbTable) |
|
121 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
122 | + ->setParameter('id', $id); |
|
123 | + $query->execute(); |
|
124 | + $result = $query->execute()->fetchAll(); |
|
125 | + |
|
126 | + if (empty($result)) { |
|
127 | + throw new \Exception('No Server found with ID: ' . $id); |
|
128 | + } |
|
129 | + |
|
130 | + return $result[0]; |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * get all trusted servers |
|
135 | + * |
|
136 | + * @return array |
|
137 | + */ |
|
138 | + public function getAllServer() { |
|
139 | + $query = $this->connection->getQueryBuilder(); |
|
140 | + $query->select(['url', 'url_hash', 'id', 'status', 'shared_secret', 'sync_token'])->from($this->dbTable); |
|
141 | + $result = $query->execute()->fetchAll(); |
|
142 | + return $result; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * check if server already exists in the database table |
|
147 | + * |
|
148 | + * @param string $url |
|
149 | + * @return bool |
|
150 | + */ |
|
151 | + public function serverExists($url) { |
|
152 | + $hash = $this->hash($url); |
|
153 | + $query = $this->connection->getQueryBuilder(); |
|
154 | + $query->select('url')->from($this->dbTable) |
|
155 | + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
156 | + ->setParameter('url_hash', $hash); |
|
157 | + $result = $query->execute()->fetchAll(); |
|
158 | + |
|
159 | + return !empty($result); |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * write token to database. Token is used to exchange the secret |
|
164 | + * |
|
165 | + * @param string $url |
|
166 | + * @param string $token |
|
167 | + */ |
|
168 | + public function addToken($url, $token) { |
|
169 | + $hash = $this->hash($url); |
|
170 | + $query = $this->connection->getQueryBuilder(); |
|
171 | + $query->update($this->dbTable) |
|
172 | + ->set('token', $query->createParameter('token')) |
|
173 | + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
174 | + ->setParameter('url_hash', $hash) |
|
175 | + ->setParameter('token', $token); |
|
176 | + $query->execute(); |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * get token stored in database |
|
181 | + * |
|
182 | + * @param string $url |
|
183 | + * @return string |
|
184 | + * @throws \Exception |
|
185 | + */ |
|
186 | + public function getToken($url) { |
|
187 | + $hash = $this->hash($url); |
|
188 | + $query = $this->connection->getQueryBuilder(); |
|
189 | + $query->select('token')->from($this->dbTable) |
|
190 | + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
191 | + ->setParameter('url_hash', $hash); |
|
192 | + |
|
193 | + $result = $query->execute()->fetch(); |
|
194 | + |
|
195 | + if (!isset($result['token'])) { |
|
196 | + throw new \Exception('No token found for: ' . $url); |
|
197 | + } |
|
198 | + |
|
199 | + return $result['token']; |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * add shared Secret to database |
|
204 | + * |
|
205 | + * @param string $url |
|
206 | + * @param string $sharedSecret |
|
207 | + */ |
|
208 | + public function addSharedSecret($url, $sharedSecret) { |
|
209 | + $hash = $this->hash($url); |
|
210 | + $query = $this->connection->getQueryBuilder(); |
|
211 | + $query->update($this->dbTable) |
|
212 | + ->set('shared_secret', $query->createParameter('sharedSecret')) |
|
213 | + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
214 | + ->setParameter('url_hash', $hash) |
|
215 | + ->setParameter('sharedSecret', $sharedSecret); |
|
216 | + $query->execute(); |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * get shared secret from database |
|
221 | + * |
|
222 | + * @param string $url |
|
223 | + * @return string |
|
224 | + */ |
|
225 | + public function getSharedSecret($url) { |
|
226 | + $hash = $this->hash($url); |
|
227 | + $query = $this->connection->getQueryBuilder(); |
|
228 | + $query->select('shared_secret')->from($this->dbTable) |
|
229 | + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
230 | + ->setParameter('url_hash', $hash); |
|
231 | + |
|
232 | + $result = $query->execute()->fetch(); |
|
233 | + return $result['shared_secret']; |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * set server status |
|
238 | + * |
|
239 | + * @param string $url |
|
240 | + * @param int $status |
|
241 | + * @param string|null $token |
|
242 | + */ |
|
243 | + public function setServerStatus($url, $status, $token = null) { |
|
244 | + $hash = $this->hash($url); |
|
245 | + $query = $this->connection->getQueryBuilder(); |
|
246 | + $query->update($this->dbTable) |
|
247 | + ->set('status', $query->createNamedParameter($status)) |
|
248 | + ->where($query->expr()->eq('url_hash', $query->createNamedParameter($hash))); |
|
249 | + if (!is_null($token)) { |
|
250 | + $query->set('sync_token', $query->createNamedParameter($token)); |
|
251 | + } |
|
252 | + $query->execute(); |
|
253 | + } |
|
254 | + |
|
255 | + /** |
|
256 | + * get server status |
|
257 | + * |
|
258 | + * @param string $url |
|
259 | + * @return int |
|
260 | + */ |
|
261 | + public function getServerStatus($url) { |
|
262 | + $hash = $this->hash($url); |
|
263 | + $query = $this->connection->getQueryBuilder(); |
|
264 | + $query->select('status')->from($this->dbTable) |
|
265 | + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
266 | + ->setParameter('url_hash', $hash); |
|
267 | + |
|
268 | + $result = $query->execute()->fetch(); |
|
269 | + return (int)$result['status']; |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * create hash from URL |
|
274 | + * |
|
275 | + * @param string $url |
|
276 | + * @return string |
|
277 | + */ |
|
278 | + protected function hash($url) { |
|
279 | + $normalized = $this->normalizeUrl($url); |
|
280 | + return sha1($normalized); |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * normalize URL, used to create the sha1 hash |
|
285 | + * |
|
286 | + * @param string $url |
|
287 | + * @return string |
|
288 | + */ |
|
289 | + protected function normalizeUrl($url) { |
|
290 | + $normalized = $url; |
|
291 | + |
|
292 | + if (strpos($url, 'https://') === 0) { |
|
293 | + $normalized = substr($url, strlen('https://')); |
|
294 | + } else if (strpos($url, 'http://') === 0) { |
|
295 | + $normalized = substr($url, strlen('http://')); |
|
296 | + } |
|
297 | + |
|
298 | + $normalized = Filesystem::normalizePath($normalized); |
|
299 | + $normalized = trim($normalized, '/'); |
|
300 | + |
|
301 | + return $normalized; |
|
302 | + } |
|
303 | + |
|
304 | + /** |
|
305 | + * @param $username |
|
306 | + * @param $password |
|
307 | + * @return bool |
|
308 | + */ |
|
309 | + public function auth($username, $password) { |
|
310 | + if ($username !== 'system') { |
|
311 | + return false; |
|
312 | + } |
|
313 | + $query = $this->connection->getQueryBuilder(); |
|
314 | + $query->select('url')->from($this->dbTable) |
|
315 | + ->where($query->expr()->eq('shared_secret', $query->createNamedParameter($password))); |
|
316 | + |
|
317 | + $result = $query->execute()->fetch(); |
|
318 | + return !empty($result); |
|
319 | + } |
|
320 | 320 | |
321 | 321 | } |