@@ -44,176 +44,176 @@ |
||
44 | 44 | use OCP\IUserManager; |
45 | 45 | |
46 | 46 | class RetryJob extends Job { |
47 | - /** @var IClientService */ |
|
48 | - private $clientService; |
|
49 | - /** @var string */ |
|
50 | - private $lookupServer; |
|
51 | - /** @var IConfig */ |
|
52 | - private $config; |
|
53 | - /** @var IUserManager */ |
|
54 | - private $userManager; |
|
55 | - /** @var IAccountManager */ |
|
56 | - private $accountManager; |
|
57 | - /** @var Signer */ |
|
58 | - private $signer; |
|
59 | - /** @var int */ |
|
60 | - protected $retries = 0; |
|
61 | - /** @var bool */ |
|
62 | - protected $retainJob = false; |
|
63 | - |
|
64 | - /** |
|
65 | - * @param ITimeFactory $time |
|
66 | - * @param IClientService $clientService |
|
67 | - * @param IConfig $config |
|
68 | - * @param IUserManager $userManager |
|
69 | - * @param IAccountManager $accountManager |
|
70 | - * @param Signer $signer |
|
71 | - */ |
|
72 | - public function __construct(ITimeFactory $time, |
|
73 | - IClientService $clientService, |
|
74 | - IConfig $config, |
|
75 | - IUserManager $userManager, |
|
76 | - IAccountManager $accountManager, |
|
77 | - Signer $signer) { |
|
78 | - parent::__construct($time); |
|
79 | - $this->clientService = $clientService; |
|
80 | - $this->config = $config; |
|
81 | - $this->userManager = $userManager; |
|
82 | - $this->accountManager = $accountManager; |
|
83 | - $this->signer = $signer; |
|
84 | - |
|
85 | - $this->lookupServer = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com'); |
|
86 | - if (!empty($this->lookupServer)) { |
|
87 | - $this->lookupServer = rtrim($this->lookupServer, '/'); |
|
88 | - $this->lookupServer .= '/users'; |
|
89 | - } |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * run the job, then remove it from the jobList |
|
94 | - * |
|
95 | - * @param IJobList $jobList |
|
96 | - * @param ILogger|null $logger |
|
97 | - */ |
|
98 | - public function execute(IJobList $jobList, ILogger $logger = null): void { |
|
99 | - if (!isset($this->argument['userId'])) { |
|
100 | - // Old background job without user id, just drop it. |
|
101 | - $jobList->remove($this, $this->argument); |
|
102 | - return; |
|
103 | - } |
|
104 | - |
|
105 | - $this->retries = (int) $this->config->getUserValue($this->argument['userId'], 'lookup_server_connector', 'update_retries', 0); |
|
106 | - |
|
107 | - if ($this->shouldRemoveBackgroundJob()) { |
|
108 | - $jobList->remove($this, $this->argument); |
|
109 | - return; |
|
110 | - } |
|
111 | - |
|
112 | - if ($this->shouldRun()) { |
|
113 | - parent::execute($jobList, $logger); |
|
114 | - if (!$this->retainJob) { |
|
115 | - $jobList->remove($this, $this->argument); |
|
116 | - } |
|
117 | - } |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Check if we should kill the background job: |
|
122 | - * |
|
123 | - * - internet connection is disabled |
|
124 | - * - no valid lookup server URL given |
|
125 | - * - lookup server was disabled by the admin |
|
126 | - * - max retries are reached (set to 5) |
|
127 | - * |
|
128 | - * @return bool |
|
129 | - */ |
|
130 | - protected function shouldRemoveBackgroundJob(): bool { |
|
131 | - return $this->config->getSystemValueBool('has_internet_connection', true) === false || |
|
132 | - $this->config->getSystemValueString('lookup_server', 'https://lookup.nextcloud.com') === '' || |
|
133 | - $this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'yes') !== 'yes' || |
|
134 | - $this->retries >= 5; |
|
135 | - } |
|
136 | - |
|
137 | - protected function shouldRun(): bool { |
|
138 | - $delay = 100 * 6 ** $this->retries; |
|
139 | - return ($this->time->getTime() - $this->lastRun) > $delay; |
|
140 | - } |
|
141 | - |
|
142 | - protected function run($argument): void { |
|
143 | - $user = $this->userManager->get($this->argument['userId']); |
|
144 | - if (!$user instanceof IUser) { |
|
145 | - // User does not exist anymore |
|
146 | - return; |
|
147 | - } |
|
148 | - |
|
149 | - $data = $this->getUserAccountData($user); |
|
150 | - $signedData = $this->signer->sign('lookupserver', $data, $user); |
|
151 | - $client = $this->clientService->newClient(); |
|
152 | - |
|
153 | - try { |
|
154 | - if (count($data) === 1) { |
|
155 | - // No public data, just the federation Id |
|
156 | - $client->delete($this->lookupServer, |
|
157 | - [ |
|
158 | - 'body' => json_encode($signedData), |
|
159 | - 'timeout' => 10, |
|
160 | - 'connect_timeout' => 3, |
|
161 | - ] |
|
162 | - ); |
|
163 | - } else { |
|
164 | - $client->post($this->lookupServer, |
|
165 | - [ |
|
166 | - 'body' => json_encode($signedData), |
|
167 | - 'timeout' => 10, |
|
168 | - 'connect_timeout' => 3, |
|
169 | - ] |
|
170 | - ); |
|
171 | - } |
|
172 | - |
|
173 | - // Reset retry counter |
|
174 | - $this->config->deleteUserValue( |
|
175 | - $user->getUID(), |
|
176 | - 'lookup_server_connector', |
|
177 | - 'update_retries' |
|
178 | - ); |
|
179 | - } catch (\Exception $e) { |
|
180 | - // An error occurred, retry later |
|
181 | - $this->retainJob = true; |
|
182 | - $this->config->setUserValue( |
|
183 | - $user->getUID(), |
|
184 | - 'lookup_server_connector', |
|
185 | - 'update_retries', |
|
186 | - $this->retries + 1 |
|
187 | - ); |
|
188 | - } |
|
189 | - } |
|
190 | - |
|
191 | - protected function getUserAccountData(IUser $user): array { |
|
192 | - $account = $this->accountManager->getAccount($user); |
|
193 | - |
|
194 | - $publicData = []; |
|
195 | - foreach ($account->getProperties() as $property) { |
|
196 | - if ($property->getScope() === IAccountManager::VISIBILITY_PUBLIC) { |
|
197 | - $publicData[$property->getName()] = $property->getValue(); |
|
198 | - } |
|
199 | - } |
|
200 | - |
|
201 | - $data = ['federationId' => $user->getCloudId()]; |
|
202 | - if (!empty($publicData)) { |
|
203 | - $data['name'] = $publicData[IAccountManager::PROPERTY_DISPLAYNAME]['value'] ?? ''; |
|
204 | - $data['email'] = $publicData[IAccountManager::PROPERTY_EMAIL]['value'] ?? ''; |
|
205 | - $data['address'] = $publicData[IAccountManager::PROPERTY_ADDRESS]['value'] ?? ''; |
|
206 | - $data['website'] = $publicData[IAccountManager::PROPERTY_WEBSITE]['value'] ?? ''; |
|
207 | - $data['twitter'] = $publicData[IAccountManager::PROPERTY_TWITTER]['value'] ?? ''; |
|
208 | - $data['phone'] = $publicData[IAccountManager::PROPERTY_PHONE]['value'] ?? ''; |
|
209 | - $data['twitter_signature'] = $publicData[IAccountManager::PROPERTY_TWITTER]['signature'] ?? ''; |
|
210 | - $data['website_signature'] = $publicData[IAccountManager::PROPERTY_WEBSITE]['signature'] ?? ''; |
|
211 | - $data['verificationStatus'] = [ |
|
212 | - IAccountManager::PROPERTY_WEBSITE => $publicData[IAccountManager::PROPERTY_WEBSITE]['verified'] ?? '', |
|
213 | - IAccountManager::PROPERTY_TWITTER => $publicData[IAccountManager::PROPERTY_TWITTER]['verified'] ?? '', |
|
214 | - ]; |
|
215 | - } |
|
216 | - |
|
217 | - return $data; |
|
218 | - } |
|
47 | + /** @var IClientService */ |
|
48 | + private $clientService; |
|
49 | + /** @var string */ |
|
50 | + private $lookupServer; |
|
51 | + /** @var IConfig */ |
|
52 | + private $config; |
|
53 | + /** @var IUserManager */ |
|
54 | + private $userManager; |
|
55 | + /** @var IAccountManager */ |
|
56 | + private $accountManager; |
|
57 | + /** @var Signer */ |
|
58 | + private $signer; |
|
59 | + /** @var int */ |
|
60 | + protected $retries = 0; |
|
61 | + /** @var bool */ |
|
62 | + protected $retainJob = false; |
|
63 | + |
|
64 | + /** |
|
65 | + * @param ITimeFactory $time |
|
66 | + * @param IClientService $clientService |
|
67 | + * @param IConfig $config |
|
68 | + * @param IUserManager $userManager |
|
69 | + * @param IAccountManager $accountManager |
|
70 | + * @param Signer $signer |
|
71 | + */ |
|
72 | + public function __construct(ITimeFactory $time, |
|
73 | + IClientService $clientService, |
|
74 | + IConfig $config, |
|
75 | + IUserManager $userManager, |
|
76 | + IAccountManager $accountManager, |
|
77 | + Signer $signer) { |
|
78 | + parent::__construct($time); |
|
79 | + $this->clientService = $clientService; |
|
80 | + $this->config = $config; |
|
81 | + $this->userManager = $userManager; |
|
82 | + $this->accountManager = $accountManager; |
|
83 | + $this->signer = $signer; |
|
84 | + |
|
85 | + $this->lookupServer = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com'); |
|
86 | + if (!empty($this->lookupServer)) { |
|
87 | + $this->lookupServer = rtrim($this->lookupServer, '/'); |
|
88 | + $this->lookupServer .= '/users'; |
|
89 | + } |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * run the job, then remove it from the jobList |
|
94 | + * |
|
95 | + * @param IJobList $jobList |
|
96 | + * @param ILogger|null $logger |
|
97 | + */ |
|
98 | + public function execute(IJobList $jobList, ILogger $logger = null): void { |
|
99 | + if (!isset($this->argument['userId'])) { |
|
100 | + // Old background job without user id, just drop it. |
|
101 | + $jobList->remove($this, $this->argument); |
|
102 | + return; |
|
103 | + } |
|
104 | + |
|
105 | + $this->retries = (int) $this->config->getUserValue($this->argument['userId'], 'lookup_server_connector', 'update_retries', 0); |
|
106 | + |
|
107 | + if ($this->shouldRemoveBackgroundJob()) { |
|
108 | + $jobList->remove($this, $this->argument); |
|
109 | + return; |
|
110 | + } |
|
111 | + |
|
112 | + if ($this->shouldRun()) { |
|
113 | + parent::execute($jobList, $logger); |
|
114 | + if (!$this->retainJob) { |
|
115 | + $jobList->remove($this, $this->argument); |
|
116 | + } |
|
117 | + } |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Check if we should kill the background job: |
|
122 | + * |
|
123 | + * - internet connection is disabled |
|
124 | + * - no valid lookup server URL given |
|
125 | + * - lookup server was disabled by the admin |
|
126 | + * - max retries are reached (set to 5) |
|
127 | + * |
|
128 | + * @return bool |
|
129 | + */ |
|
130 | + protected function shouldRemoveBackgroundJob(): bool { |
|
131 | + return $this->config->getSystemValueBool('has_internet_connection', true) === false || |
|
132 | + $this->config->getSystemValueString('lookup_server', 'https://lookup.nextcloud.com') === '' || |
|
133 | + $this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'yes') !== 'yes' || |
|
134 | + $this->retries >= 5; |
|
135 | + } |
|
136 | + |
|
137 | + protected function shouldRun(): bool { |
|
138 | + $delay = 100 * 6 ** $this->retries; |
|
139 | + return ($this->time->getTime() - $this->lastRun) > $delay; |
|
140 | + } |
|
141 | + |
|
142 | + protected function run($argument): void { |
|
143 | + $user = $this->userManager->get($this->argument['userId']); |
|
144 | + if (!$user instanceof IUser) { |
|
145 | + // User does not exist anymore |
|
146 | + return; |
|
147 | + } |
|
148 | + |
|
149 | + $data = $this->getUserAccountData($user); |
|
150 | + $signedData = $this->signer->sign('lookupserver', $data, $user); |
|
151 | + $client = $this->clientService->newClient(); |
|
152 | + |
|
153 | + try { |
|
154 | + if (count($data) === 1) { |
|
155 | + // No public data, just the federation Id |
|
156 | + $client->delete($this->lookupServer, |
|
157 | + [ |
|
158 | + 'body' => json_encode($signedData), |
|
159 | + 'timeout' => 10, |
|
160 | + 'connect_timeout' => 3, |
|
161 | + ] |
|
162 | + ); |
|
163 | + } else { |
|
164 | + $client->post($this->lookupServer, |
|
165 | + [ |
|
166 | + 'body' => json_encode($signedData), |
|
167 | + 'timeout' => 10, |
|
168 | + 'connect_timeout' => 3, |
|
169 | + ] |
|
170 | + ); |
|
171 | + } |
|
172 | + |
|
173 | + // Reset retry counter |
|
174 | + $this->config->deleteUserValue( |
|
175 | + $user->getUID(), |
|
176 | + 'lookup_server_connector', |
|
177 | + 'update_retries' |
|
178 | + ); |
|
179 | + } catch (\Exception $e) { |
|
180 | + // An error occurred, retry later |
|
181 | + $this->retainJob = true; |
|
182 | + $this->config->setUserValue( |
|
183 | + $user->getUID(), |
|
184 | + 'lookup_server_connector', |
|
185 | + 'update_retries', |
|
186 | + $this->retries + 1 |
|
187 | + ); |
|
188 | + } |
|
189 | + } |
|
190 | + |
|
191 | + protected function getUserAccountData(IUser $user): array { |
|
192 | + $account = $this->accountManager->getAccount($user); |
|
193 | + |
|
194 | + $publicData = []; |
|
195 | + foreach ($account->getProperties() as $property) { |
|
196 | + if ($property->getScope() === IAccountManager::VISIBILITY_PUBLIC) { |
|
197 | + $publicData[$property->getName()] = $property->getValue(); |
|
198 | + } |
|
199 | + } |
|
200 | + |
|
201 | + $data = ['federationId' => $user->getCloudId()]; |
|
202 | + if (!empty($publicData)) { |
|
203 | + $data['name'] = $publicData[IAccountManager::PROPERTY_DISPLAYNAME]['value'] ?? ''; |
|
204 | + $data['email'] = $publicData[IAccountManager::PROPERTY_EMAIL]['value'] ?? ''; |
|
205 | + $data['address'] = $publicData[IAccountManager::PROPERTY_ADDRESS]['value'] ?? ''; |
|
206 | + $data['website'] = $publicData[IAccountManager::PROPERTY_WEBSITE]['value'] ?? ''; |
|
207 | + $data['twitter'] = $publicData[IAccountManager::PROPERTY_TWITTER]['value'] ?? ''; |
|
208 | + $data['phone'] = $publicData[IAccountManager::PROPERTY_PHONE]['value'] ?? ''; |
|
209 | + $data['twitter_signature'] = $publicData[IAccountManager::PROPERTY_TWITTER]['signature'] ?? ''; |
|
210 | + $data['website_signature'] = $publicData[IAccountManager::PROPERTY_WEBSITE]['signature'] ?? ''; |
|
211 | + $data['verificationStatus'] = [ |
|
212 | + IAccountManager::PROPERTY_WEBSITE => $publicData[IAccountManager::PROPERTY_WEBSITE]['verified'] ?? '', |
|
213 | + IAccountManager::PROPERTY_TWITTER => $publicData[IAccountManager::PROPERTY_TWITTER]['verified'] ?? '', |
|
214 | + ]; |
|
215 | + } |
|
216 | + |
|
217 | + return $data; |
|
218 | + } |
|
219 | 219 | } |
@@ -51,176 +51,176 @@ |
||
51 | 51 | */ |
52 | 52 | class RequestSharedSecret extends Job { |
53 | 53 | |
54 | - /** @var IClient */ |
|
55 | - private $httpClient; |
|
56 | - |
|
57 | - /** @var IJobList */ |
|
58 | - private $jobList; |
|
59 | - |
|
60 | - /** @var IURLGenerator */ |
|
61 | - private $urlGenerator; |
|
62 | - |
|
63 | - /** @var TrustedServers */ |
|
64 | - private $trustedServers; |
|
65 | - |
|
66 | - /** @var IDiscoveryService */ |
|
67 | - private $ocsDiscoveryService; |
|
68 | - |
|
69 | - /** @var ILogger */ |
|
70 | - private $logger; |
|
71 | - |
|
72 | - /** @var ITimeFactory */ |
|
73 | - private $timeFactory; |
|
74 | - |
|
75 | - /** @var bool */ |
|
76 | - protected $retainJob = false; |
|
77 | - |
|
78 | - private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/request-shared-secret'; |
|
79 | - |
|
80 | - /** @var int 30 day = 2592000sec */ |
|
81 | - private $maxLifespan = 2592000; |
|
82 | - |
|
83 | - /** |
|
84 | - * RequestSharedSecret constructor. |
|
85 | - * |
|
86 | - * @param IClientService $httpClientService |
|
87 | - * @param IURLGenerator $urlGenerator |
|
88 | - * @param IJobList $jobList |
|
89 | - * @param TrustedServers $trustedServers |
|
90 | - * @param IDiscoveryService $ocsDiscoveryService |
|
91 | - * @param ILogger $logger |
|
92 | - * @param ITimeFactory $timeFactory |
|
93 | - */ |
|
94 | - public function __construct( |
|
95 | - IClientService $httpClientService, |
|
96 | - IURLGenerator $urlGenerator, |
|
97 | - IJobList $jobList, |
|
98 | - TrustedServers $trustedServers, |
|
99 | - IDiscoveryService $ocsDiscoveryService, |
|
100 | - ILogger $logger, |
|
101 | - ITimeFactory $timeFactory |
|
102 | - ) { |
|
103 | - $this->httpClient = $httpClientService->newClient(); |
|
104 | - $this->jobList = $jobList; |
|
105 | - $this->urlGenerator = $urlGenerator; |
|
106 | - $this->logger = $logger; |
|
107 | - $this->ocsDiscoveryService = $ocsDiscoveryService; |
|
108 | - $this->trustedServers = $trustedServers; |
|
109 | - $this->timeFactory = $timeFactory; |
|
110 | - } |
|
111 | - |
|
112 | - |
|
113 | - /** |
|
114 | - * run the job, then remove it from the joblist |
|
115 | - * |
|
116 | - * @param IJobList $jobList |
|
117 | - * @param ILogger|null $logger |
|
118 | - */ |
|
119 | - public function execute(IJobList $jobList, ILogger $logger = null) { |
|
120 | - $target = $this->argument['url']; |
|
121 | - // only execute if target is still in the list of trusted domains |
|
122 | - if ($this->trustedServers->isTrustedServer($target)) { |
|
123 | - $this->parentExecute($jobList, $logger); |
|
124 | - } |
|
125 | - |
|
126 | - $jobList->remove($this, $this->argument); |
|
127 | - |
|
128 | - if ($this->retainJob) { |
|
129 | - $this->reAddJob($this->argument); |
|
130 | - } |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * call execute() method of parent |
|
135 | - * |
|
136 | - * @param IJobList $jobList |
|
137 | - * @param ILogger $logger |
|
138 | - */ |
|
139 | - protected function parentExecute($jobList, $logger) { |
|
140 | - parent::execute($jobList, $logger); |
|
141 | - } |
|
142 | - |
|
143 | - protected function run($argument) { |
|
144 | - $target = $argument['url']; |
|
145 | - $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
146 | - $currentTime = $this->timeFactory->getTime(); |
|
147 | - $source = $this->urlGenerator->getAbsoluteURL('/'); |
|
148 | - $source = rtrim($source, '/'); |
|
149 | - $token = $argument['token']; |
|
150 | - |
|
151 | - // kill job after 30 days of trying |
|
152 | - $deadline = $currentTime - $this->maxLifespan; |
|
153 | - if ($created < $deadline) { |
|
154 | - $this->retainJob = false; |
|
155 | - $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
|
156 | - return; |
|
157 | - } |
|
158 | - |
|
159 | - $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); |
|
160 | - $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
|
161 | - |
|
162 | - // make sure that we have a well formated url |
|
163 | - $url = rtrim($target, '/') . '/' . trim($endPoint, '/'); |
|
164 | - |
|
165 | - try { |
|
166 | - $result = $this->httpClient->post( |
|
167 | - $url, |
|
168 | - [ |
|
169 | - 'body' => [ |
|
170 | - 'url' => $source, |
|
171 | - 'token' => $token, |
|
172 | - 'format' => 'json', |
|
173 | - ], |
|
174 | - 'timeout' => 3, |
|
175 | - 'connect_timeout' => 3, |
|
176 | - ] |
|
177 | - ); |
|
178 | - |
|
179 | - $status = $result->getStatusCode(); |
|
180 | - } catch (ClientException $e) { |
|
181 | - $status = $e->getCode(); |
|
182 | - if ($status === Http::STATUS_FORBIDDEN) { |
|
183 | - $this->logger->info($target . ' refused to ask for a shared secret.', ['app' => 'federation']); |
|
184 | - } else { |
|
185 | - $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
|
186 | - } |
|
187 | - } catch (RequestException $e) { |
|
188 | - $status = -1; // There is no status code if we could not connect |
|
189 | - $this->logger->info('Could not connect to ' . $target, ['app' => 'federation']); |
|
190 | - } catch (RingException $e) { |
|
191 | - $status = -1; // There is no status code if we could not connect |
|
192 | - $this->logger->info('Could not connect to ' . $target, ['app' => 'federation']); |
|
193 | - } catch (\Exception $e) { |
|
194 | - $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
|
195 | - $this->logger->logException($e, ['app' => 'federation']); |
|
196 | - } |
|
197 | - |
|
198 | - // if we received a unexpected response we try again later |
|
199 | - if ( |
|
200 | - $status !== Http::STATUS_OK |
|
201 | - && $status !== Http::STATUS_FORBIDDEN |
|
202 | - ) { |
|
203 | - $this->retainJob = true; |
|
204 | - } |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * re-add background job |
|
209 | - * |
|
210 | - * @param array $argument |
|
211 | - */ |
|
212 | - protected function reAddJob(array $argument) { |
|
213 | - $url = $argument['url']; |
|
214 | - $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
215 | - $token = $argument['token']; |
|
216 | - |
|
217 | - $this->jobList->add( |
|
218 | - RequestSharedSecret::class, |
|
219 | - [ |
|
220 | - 'url' => $url, |
|
221 | - 'token' => $token, |
|
222 | - 'created' => $created |
|
223 | - ] |
|
224 | - ); |
|
225 | - } |
|
54 | + /** @var IClient */ |
|
55 | + private $httpClient; |
|
56 | + |
|
57 | + /** @var IJobList */ |
|
58 | + private $jobList; |
|
59 | + |
|
60 | + /** @var IURLGenerator */ |
|
61 | + private $urlGenerator; |
|
62 | + |
|
63 | + /** @var TrustedServers */ |
|
64 | + private $trustedServers; |
|
65 | + |
|
66 | + /** @var IDiscoveryService */ |
|
67 | + private $ocsDiscoveryService; |
|
68 | + |
|
69 | + /** @var ILogger */ |
|
70 | + private $logger; |
|
71 | + |
|
72 | + /** @var ITimeFactory */ |
|
73 | + private $timeFactory; |
|
74 | + |
|
75 | + /** @var bool */ |
|
76 | + protected $retainJob = false; |
|
77 | + |
|
78 | + private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/request-shared-secret'; |
|
79 | + |
|
80 | + /** @var int 30 day = 2592000sec */ |
|
81 | + private $maxLifespan = 2592000; |
|
82 | + |
|
83 | + /** |
|
84 | + * RequestSharedSecret constructor. |
|
85 | + * |
|
86 | + * @param IClientService $httpClientService |
|
87 | + * @param IURLGenerator $urlGenerator |
|
88 | + * @param IJobList $jobList |
|
89 | + * @param TrustedServers $trustedServers |
|
90 | + * @param IDiscoveryService $ocsDiscoveryService |
|
91 | + * @param ILogger $logger |
|
92 | + * @param ITimeFactory $timeFactory |
|
93 | + */ |
|
94 | + public function __construct( |
|
95 | + IClientService $httpClientService, |
|
96 | + IURLGenerator $urlGenerator, |
|
97 | + IJobList $jobList, |
|
98 | + TrustedServers $trustedServers, |
|
99 | + IDiscoveryService $ocsDiscoveryService, |
|
100 | + ILogger $logger, |
|
101 | + ITimeFactory $timeFactory |
|
102 | + ) { |
|
103 | + $this->httpClient = $httpClientService->newClient(); |
|
104 | + $this->jobList = $jobList; |
|
105 | + $this->urlGenerator = $urlGenerator; |
|
106 | + $this->logger = $logger; |
|
107 | + $this->ocsDiscoveryService = $ocsDiscoveryService; |
|
108 | + $this->trustedServers = $trustedServers; |
|
109 | + $this->timeFactory = $timeFactory; |
|
110 | + } |
|
111 | + |
|
112 | + |
|
113 | + /** |
|
114 | + * run the job, then remove it from the joblist |
|
115 | + * |
|
116 | + * @param IJobList $jobList |
|
117 | + * @param ILogger|null $logger |
|
118 | + */ |
|
119 | + public function execute(IJobList $jobList, ILogger $logger = null) { |
|
120 | + $target = $this->argument['url']; |
|
121 | + // only execute if target is still in the list of trusted domains |
|
122 | + if ($this->trustedServers->isTrustedServer($target)) { |
|
123 | + $this->parentExecute($jobList, $logger); |
|
124 | + } |
|
125 | + |
|
126 | + $jobList->remove($this, $this->argument); |
|
127 | + |
|
128 | + if ($this->retainJob) { |
|
129 | + $this->reAddJob($this->argument); |
|
130 | + } |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * call execute() method of parent |
|
135 | + * |
|
136 | + * @param IJobList $jobList |
|
137 | + * @param ILogger $logger |
|
138 | + */ |
|
139 | + protected function parentExecute($jobList, $logger) { |
|
140 | + parent::execute($jobList, $logger); |
|
141 | + } |
|
142 | + |
|
143 | + protected function run($argument) { |
|
144 | + $target = $argument['url']; |
|
145 | + $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
146 | + $currentTime = $this->timeFactory->getTime(); |
|
147 | + $source = $this->urlGenerator->getAbsoluteURL('/'); |
|
148 | + $source = rtrim($source, '/'); |
|
149 | + $token = $argument['token']; |
|
150 | + |
|
151 | + // kill job after 30 days of trying |
|
152 | + $deadline = $currentTime - $this->maxLifespan; |
|
153 | + if ($created < $deadline) { |
|
154 | + $this->retainJob = false; |
|
155 | + $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
|
156 | + return; |
|
157 | + } |
|
158 | + |
|
159 | + $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); |
|
160 | + $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
|
161 | + |
|
162 | + // make sure that we have a well formated url |
|
163 | + $url = rtrim($target, '/') . '/' . trim($endPoint, '/'); |
|
164 | + |
|
165 | + try { |
|
166 | + $result = $this->httpClient->post( |
|
167 | + $url, |
|
168 | + [ |
|
169 | + 'body' => [ |
|
170 | + 'url' => $source, |
|
171 | + 'token' => $token, |
|
172 | + 'format' => 'json', |
|
173 | + ], |
|
174 | + 'timeout' => 3, |
|
175 | + 'connect_timeout' => 3, |
|
176 | + ] |
|
177 | + ); |
|
178 | + |
|
179 | + $status = $result->getStatusCode(); |
|
180 | + } catch (ClientException $e) { |
|
181 | + $status = $e->getCode(); |
|
182 | + if ($status === Http::STATUS_FORBIDDEN) { |
|
183 | + $this->logger->info($target . ' refused to ask for a shared secret.', ['app' => 'federation']); |
|
184 | + } else { |
|
185 | + $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
|
186 | + } |
|
187 | + } catch (RequestException $e) { |
|
188 | + $status = -1; // There is no status code if we could not connect |
|
189 | + $this->logger->info('Could not connect to ' . $target, ['app' => 'federation']); |
|
190 | + } catch (RingException $e) { |
|
191 | + $status = -1; // There is no status code if we could not connect |
|
192 | + $this->logger->info('Could not connect to ' . $target, ['app' => 'federation']); |
|
193 | + } catch (\Exception $e) { |
|
194 | + $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
|
195 | + $this->logger->logException($e, ['app' => 'federation']); |
|
196 | + } |
|
197 | + |
|
198 | + // if we received a unexpected response we try again later |
|
199 | + if ( |
|
200 | + $status !== Http::STATUS_OK |
|
201 | + && $status !== Http::STATUS_FORBIDDEN |
|
202 | + ) { |
|
203 | + $this->retainJob = true; |
|
204 | + } |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * re-add background job |
|
209 | + * |
|
210 | + * @param array $argument |
|
211 | + */ |
|
212 | + protected function reAddJob(array $argument) { |
|
213 | + $url = $argument['url']; |
|
214 | + $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
215 | + $token = $argument['token']; |
|
216 | + |
|
217 | + $this->jobList->add( |
|
218 | + RequestSharedSecret::class, |
|
219 | + [ |
|
220 | + 'url' => $url, |
|
221 | + 'token' => $token, |
|
222 | + 'created' => $created |
|
223 | + ] |
|
224 | + ); |
|
225 | + } |
|
226 | 226 | } |
@@ -53,201 +53,201 @@ |
||
53 | 53 | */ |
54 | 54 | class GetSharedSecret extends Job { |
55 | 55 | |
56 | - /** @var IClient */ |
|
57 | - private $httpClient; |
|
58 | - |
|
59 | - /** @var IJobList */ |
|
60 | - private $jobList; |
|
61 | - |
|
62 | - /** @var IURLGenerator */ |
|
63 | - private $urlGenerator; |
|
64 | - |
|
65 | - /** @var TrustedServers */ |
|
66 | - private $trustedServers; |
|
67 | - |
|
68 | - /** @var IDiscoveryService */ |
|
69 | - private $ocsDiscoveryService; |
|
70 | - |
|
71 | - /** @var ILogger */ |
|
72 | - private $logger; |
|
73 | - |
|
74 | - /** @var ITimeFactory */ |
|
75 | - private $timeFactory; |
|
76 | - |
|
77 | - /** @var bool */ |
|
78 | - protected $retainJob = false; |
|
79 | - |
|
80 | - private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/shared-secret'; |
|
81 | - |
|
82 | - /** @var int 30 day = 2592000sec */ |
|
83 | - private $maxLifespan = 2592000; |
|
84 | - |
|
85 | - /** |
|
86 | - * RequestSharedSecret constructor. |
|
87 | - * |
|
88 | - * @param IClientService $httpClientService |
|
89 | - * @param IURLGenerator $urlGenerator |
|
90 | - * @param IJobList $jobList |
|
91 | - * @param TrustedServers $trustedServers |
|
92 | - * @param ILogger $logger |
|
93 | - * @param IDiscoveryService $ocsDiscoveryService |
|
94 | - * @param ITimeFactory $timeFactory |
|
95 | - */ |
|
96 | - public function __construct( |
|
97 | - IClientService $httpClientService, |
|
98 | - IURLGenerator $urlGenerator, |
|
99 | - IJobList $jobList, |
|
100 | - TrustedServers $trustedServers, |
|
101 | - ILogger $logger, |
|
102 | - IDiscoveryService $ocsDiscoveryService, |
|
103 | - ITimeFactory $timeFactory |
|
104 | - ) { |
|
105 | - $this->logger = $logger; |
|
106 | - $this->httpClient = $httpClientService->newClient(); |
|
107 | - $this->jobList = $jobList; |
|
108 | - $this->urlGenerator = $urlGenerator; |
|
109 | - $this->ocsDiscoveryService = $ocsDiscoveryService; |
|
110 | - $this->trustedServers = $trustedServers; |
|
111 | - $this->timeFactory = $timeFactory; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * run the job, then remove it from the joblist |
|
116 | - * |
|
117 | - * @param IJobList $jobList |
|
118 | - * @param ILogger|null $logger |
|
119 | - */ |
|
120 | - public function execute(IJobList $jobList, ILogger $logger = null) { |
|
121 | - $target = $this->argument['url']; |
|
122 | - // only execute if target is still in the list of trusted domains |
|
123 | - if ($this->trustedServers->isTrustedServer($target)) { |
|
124 | - $this->parentExecute($jobList, $logger); |
|
125 | - } |
|
126 | - |
|
127 | - $jobList->remove($this, $this->argument); |
|
128 | - |
|
129 | - if ($this->retainJob) { |
|
130 | - $this->reAddJob($this->argument); |
|
131 | - } |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * call execute() method of parent |
|
136 | - * |
|
137 | - * @param IJobList $jobList |
|
138 | - * @param ILogger $logger |
|
139 | - */ |
|
140 | - protected function parentExecute($jobList, $logger = null) { |
|
141 | - parent::execute($jobList, $logger); |
|
142 | - } |
|
143 | - |
|
144 | - protected function run($argument) { |
|
145 | - $target = $argument['url']; |
|
146 | - $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
147 | - $currentTime = $this->timeFactory->getTime(); |
|
148 | - $source = $this->urlGenerator->getAbsoluteURL('/'); |
|
149 | - $source = rtrim($source, '/'); |
|
150 | - $token = $argument['token']; |
|
151 | - |
|
152 | - // kill job after 30 days of trying |
|
153 | - $deadline = $currentTime - $this->maxLifespan; |
|
154 | - if ($created < $deadline) { |
|
155 | - $this->retainJob = false; |
|
156 | - $this->trustedServers->setServerStatus($target,TrustedServers::STATUS_FAILURE); |
|
157 | - return; |
|
158 | - } |
|
159 | - |
|
160 | - $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); |
|
161 | - $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
|
162 | - |
|
163 | - // make sure that we have a well formatted url |
|
164 | - $url = rtrim($target, '/') . '/' . trim($endPoint, '/'); |
|
165 | - |
|
166 | - $result = null; |
|
167 | - try { |
|
168 | - $result = $this->httpClient->get( |
|
169 | - $url, |
|
170 | - [ |
|
171 | - 'query' => |
|
172 | - [ |
|
173 | - 'url' => $source, |
|
174 | - 'token' => $token, |
|
175 | - 'format' => 'json', |
|
176 | - ], |
|
177 | - 'timeout' => 3, |
|
178 | - 'connect_timeout' => 3, |
|
179 | - ] |
|
180 | - ); |
|
181 | - |
|
182 | - $status = $result->getStatusCode(); |
|
183 | - } catch (ClientException $e) { |
|
184 | - $status = $e->getCode(); |
|
185 | - if ($status === Http::STATUS_FORBIDDEN) { |
|
186 | - $this->logger->info($target . ' refused to exchange a shared secret with you.', ['app' => 'federation']); |
|
187 | - } else { |
|
188 | - $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
|
189 | - } |
|
190 | - } catch (RequestException $e) { |
|
191 | - $status = -1; // There is no status code if we could not connect |
|
192 | - $this->logger->logException($e, [ |
|
193 | - 'message' => 'Could not connect to ' . $target, |
|
194 | - 'level' => ILogger::INFO, |
|
195 | - 'app' => 'federation', |
|
196 | - ]); |
|
197 | - } catch (RingException $e) { |
|
198 | - $status = -1; // There is no status code if we could not connect |
|
199 | - $this->logger->logException($e, [ |
|
200 | - 'message' => 'Could not connect to ' . $target, |
|
201 | - 'level' => ILogger::INFO, |
|
202 | - 'app' => 'federation', |
|
203 | - ]); |
|
204 | - } catch (\Exception $e) { |
|
205 | - $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
|
206 | - $this->logger->logException($e, ['app' => 'federation']); |
|
207 | - } |
|
208 | - |
|
209 | - // if we received a unexpected response we try again later |
|
210 | - if ( |
|
211 | - $status !== Http::STATUS_OK |
|
212 | - && $status !== Http::STATUS_FORBIDDEN |
|
213 | - ) { |
|
214 | - $this->retainJob = true; |
|
215 | - } |
|
216 | - |
|
217 | - if ($status === Http::STATUS_OK && $result instanceof IResponse) { |
|
218 | - $body = $result->getBody(); |
|
219 | - $result = json_decode($body, true); |
|
220 | - if (isset($result['ocs']['data']['sharedSecret'])) { |
|
221 | - $this->trustedServers->addSharedSecret( |
|
222 | - $target, |
|
223 | - $result['ocs']['data']['sharedSecret'] |
|
224 | - ); |
|
225 | - } else { |
|
226 | - $this->logger->error( |
|
227 | - 'remote server "' . $target . '"" does not return a valid shared secret. Received data: ' . $body, |
|
228 | - ['app' => 'federation'] |
|
229 | - ); |
|
230 | - $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
|
231 | - } |
|
232 | - } |
|
233 | - } |
|
234 | - |
|
235 | - /** |
|
236 | - * re-add background job |
|
237 | - * |
|
238 | - * @param array $argument |
|
239 | - */ |
|
240 | - protected function reAddJob(array $argument) { |
|
241 | - $url = $argument['url']; |
|
242 | - $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
243 | - $token = $argument['token']; |
|
244 | - $this->jobList->add( |
|
245 | - GetSharedSecret::class, |
|
246 | - [ |
|
247 | - 'url' => $url, |
|
248 | - 'token' => $token, |
|
249 | - 'created' => $created |
|
250 | - ] |
|
251 | - ); |
|
252 | - } |
|
56 | + /** @var IClient */ |
|
57 | + private $httpClient; |
|
58 | + |
|
59 | + /** @var IJobList */ |
|
60 | + private $jobList; |
|
61 | + |
|
62 | + /** @var IURLGenerator */ |
|
63 | + private $urlGenerator; |
|
64 | + |
|
65 | + /** @var TrustedServers */ |
|
66 | + private $trustedServers; |
|
67 | + |
|
68 | + /** @var IDiscoveryService */ |
|
69 | + private $ocsDiscoveryService; |
|
70 | + |
|
71 | + /** @var ILogger */ |
|
72 | + private $logger; |
|
73 | + |
|
74 | + /** @var ITimeFactory */ |
|
75 | + private $timeFactory; |
|
76 | + |
|
77 | + /** @var bool */ |
|
78 | + protected $retainJob = false; |
|
79 | + |
|
80 | + private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/shared-secret'; |
|
81 | + |
|
82 | + /** @var int 30 day = 2592000sec */ |
|
83 | + private $maxLifespan = 2592000; |
|
84 | + |
|
85 | + /** |
|
86 | + * RequestSharedSecret constructor. |
|
87 | + * |
|
88 | + * @param IClientService $httpClientService |
|
89 | + * @param IURLGenerator $urlGenerator |
|
90 | + * @param IJobList $jobList |
|
91 | + * @param TrustedServers $trustedServers |
|
92 | + * @param ILogger $logger |
|
93 | + * @param IDiscoveryService $ocsDiscoveryService |
|
94 | + * @param ITimeFactory $timeFactory |
|
95 | + */ |
|
96 | + public function __construct( |
|
97 | + IClientService $httpClientService, |
|
98 | + IURLGenerator $urlGenerator, |
|
99 | + IJobList $jobList, |
|
100 | + TrustedServers $trustedServers, |
|
101 | + ILogger $logger, |
|
102 | + IDiscoveryService $ocsDiscoveryService, |
|
103 | + ITimeFactory $timeFactory |
|
104 | + ) { |
|
105 | + $this->logger = $logger; |
|
106 | + $this->httpClient = $httpClientService->newClient(); |
|
107 | + $this->jobList = $jobList; |
|
108 | + $this->urlGenerator = $urlGenerator; |
|
109 | + $this->ocsDiscoveryService = $ocsDiscoveryService; |
|
110 | + $this->trustedServers = $trustedServers; |
|
111 | + $this->timeFactory = $timeFactory; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * run the job, then remove it from the joblist |
|
116 | + * |
|
117 | + * @param IJobList $jobList |
|
118 | + * @param ILogger|null $logger |
|
119 | + */ |
|
120 | + public function execute(IJobList $jobList, ILogger $logger = null) { |
|
121 | + $target = $this->argument['url']; |
|
122 | + // only execute if target is still in the list of trusted domains |
|
123 | + if ($this->trustedServers->isTrustedServer($target)) { |
|
124 | + $this->parentExecute($jobList, $logger); |
|
125 | + } |
|
126 | + |
|
127 | + $jobList->remove($this, $this->argument); |
|
128 | + |
|
129 | + if ($this->retainJob) { |
|
130 | + $this->reAddJob($this->argument); |
|
131 | + } |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * call execute() method of parent |
|
136 | + * |
|
137 | + * @param IJobList $jobList |
|
138 | + * @param ILogger $logger |
|
139 | + */ |
|
140 | + protected function parentExecute($jobList, $logger = null) { |
|
141 | + parent::execute($jobList, $logger); |
|
142 | + } |
|
143 | + |
|
144 | + protected function run($argument) { |
|
145 | + $target = $argument['url']; |
|
146 | + $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
147 | + $currentTime = $this->timeFactory->getTime(); |
|
148 | + $source = $this->urlGenerator->getAbsoluteURL('/'); |
|
149 | + $source = rtrim($source, '/'); |
|
150 | + $token = $argument['token']; |
|
151 | + |
|
152 | + // kill job after 30 days of trying |
|
153 | + $deadline = $currentTime - $this->maxLifespan; |
|
154 | + if ($created < $deadline) { |
|
155 | + $this->retainJob = false; |
|
156 | + $this->trustedServers->setServerStatus($target,TrustedServers::STATUS_FAILURE); |
|
157 | + return; |
|
158 | + } |
|
159 | + |
|
160 | + $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); |
|
161 | + $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
|
162 | + |
|
163 | + // make sure that we have a well formatted url |
|
164 | + $url = rtrim($target, '/') . '/' . trim($endPoint, '/'); |
|
165 | + |
|
166 | + $result = null; |
|
167 | + try { |
|
168 | + $result = $this->httpClient->get( |
|
169 | + $url, |
|
170 | + [ |
|
171 | + 'query' => |
|
172 | + [ |
|
173 | + 'url' => $source, |
|
174 | + 'token' => $token, |
|
175 | + 'format' => 'json', |
|
176 | + ], |
|
177 | + 'timeout' => 3, |
|
178 | + 'connect_timeout' => 3, |
|
179 | + ] |
|
180 | + ); |
|
181 | + |
|
182 | + $status = $result->getStatusCode(); |
|
183 | + } catch (ClientException $e) { |
|
184 | + $status = $e->getCode(); |
|
185 | + if ($status === Http::STATUS_FORBIDDEN) { |
|
186 | + $this->logger->info($target . ' refused to exchange a shared secret with you.', ['app' => 'federation']); |
|
187 | + } else { |
|
188 | + $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
|
189 | + } |
|
190 | + } catch (RequestException $e) { |
|
191 | + $status = -1; // There is no status code if we could not connect |
|
192 | + $this->logger->logException($e, [ |
|
193 | + 'message' => 'Could not connect to ' . $target, |
|
194 | + 'level' => ILogger::INFO, |
|
195 | + 'app' => 'federation', |
|
196 | + ]); |
|
197 | + } catch (RingException $e) { |
|
198 | + $status = -1; // There is no status code if we could not connect |
|
199 | + $this->logger->logException($e, [ |
|
200 | + 'message' => 'Could not connect to ' . $target, |
|
201 | + 'level' => ILogger::INFO, |
|
202 | + 'app' => 'federation', |
|
203 | + ]); |
|
204 | + } catch (\Exception $e) { |
|
205 | + $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
|
206 | + $this->logger->logException($e, ['app' => 'federation']); |
|
207 | + } |
|
208 | + |
|
209 | + // if we received a unexpected response we try again later |
|
210 | + if ( |
|
211 | + $status !== Http::STATUS_OK |
|
212 | + && $status !== Http::STATUS_FORBIDDEN |
|
213 | + ) { |
|
214 | + $this->retainJob = true; |
|
215 | + } |
|
216 | + |
|
217 | + if ($status === Http::STATUS_OK && $result instanceof IResponse) { |
|
218 | + $body = $result->getBody(); |
|
219 | + $result = json_decode($body, true); |
|
220 | + if (isset($result['ocs']['data']['sharedSecret'])) { |
|
221 | + $this->trustedServers->addSharedSecret( |
|
222 | + $target, |
|
223 | + $result['ocs']['data']['sharedSecret'] |
|
224 | + ); |
|
225 | + } else { |
|
226 | + $this->logger->error( |
|
227 | + 'remote server "' . $target . '"" does not return a valid shared secret. Received data: ' . $body, |
|
228 | + ['app' => 'federation'] |
|
229 | + ); |
|
230 | + $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
|
231 | + } |
|
232 | + } |
|
233 | + } |
|
234 | + |
|
235 | + /** |
|
236 | + * re-add background job |
|
237 | + * |
|
238 | + * @param array $argument |
|
239 | + */ |
|
240 | + protected function reAddJob(array $argument) { |
|
241 | + $url = $argument['url']; |
|
242 | + $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
243 | + $token = $argument['token']; |
|
244 | + $this->jobList->add( |
|
245 | + GetSharedSecret::class, |
|
246 | + [ |
|
247 | + 'url' => $url, |
|
248 | + 'token' => $token, |
|
249 | + 'created' => $created |
|
250 | + ] |
|
251 | + ); |
|
252 | + } |
|
253 | 253 | } |
@@ -41,114 +41,114 @@ |
||
41 | 41 | |
42 | 42 | class RefreshWebcalJob extends Job { |
43 | 43 | |
44 | - /** |
|
45 | - * @var RefreshWebcalService |
|
46 | - */ |
|
47 | - private $refreshWebcalService; |
|
48 | - |
|
49 | - /** |
|
50 | - * @var IConfig |
|
51 | - */ |
|
52 | - private $config; |
|
53 | - |
|
54 | - /** @var ILogger */ |
|
55 | - private $logger; |
|
56 | - |
|
57 | - /** @var ITimeFactory */ |
|
58 | - private $timeFactory; |
|
59 | - |
|
60 | - /** |
|
61 | - * RefreshWebcalJob constructor. |
|
62 | - * |
|
63 | - * @param RefreshWebcalService $refreshWebcalService |
|
64 | - * @param IConfig $config |
|
65 | - * @param ILogger $logger |
|
66 | - * @param ITimeFactory $timeFactory |
|
67 | - */ |
|
68 | - public function __construct(RefreshWebcalService $refreshWebcalService, IConfig $config, ILogger $logger, ITimeFactory $timeFactory) { |
|
69 | - parent::__construct($timeFactory); |
|
70 | - $this->refreshWebcalService = $refreshWebcalService; |
|
71 | - $this->config = $config; |
|
72 | - $this->logger = $logger; |
|
73 | - $this->timeFactory = $timeFactory; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * this function is called at most every hour |
|
78 | - * |
|
79 | - * @inheritdoc |
|
80 | - */ |
|
81 | - public function execute(IJobList $jobList, ILogger $logger = null) { |
|
82 | - $subscription = $this->refreshWebcalService->getSubscription($this->argument['principaluri'], $this->argument['uri']); |
|
83 | - if (!$subscription) { |
|
84 | - return; |
|
85 | - } |
|
86 | - |
|
87 | - $this->fixSubscriptionRowTyping($subscription); |
|
88 | - |
|
89 | - // if no refresh rate was configured, just refresh once a week |
|
90 | - $defaultRefreshRate = $this->config->getAppValue('dav', 'calendarSubscriptionRefreshRate', 'P1W'); |
|
91 | - $refreshRate = $subscription[RefreshWebcalService::REFRESH_RATE] ?? $defaultRefreshRate; |
|
92 | - |
|
93 | - $subscriptionId = $subscription['id']; |
|
94 | - |
|
95 | - try { |
|
96 | - /** @var DateInterval $dateInterval */ |
|
97 | - $dateInterval = DateTimeParser::parseDuration($refreshRate); |
|
98 | - } catch (InvalidDataException $ex) { |
|
99 | - $this->logger->logException($ex); |
|
100 | - $this->logger->warning("Subscription $subscriptionId could not be refreshed, refreshrate in database is invalid"); |
|
101 | - return; |
|
102 | - } |
|
103 | - |
|
104 | - $interval = $this->getIntervalFromDateInterval($dateInterval); |
|
105 | - if (($this->timeFactory->getTime() - $this->lastRun) <= $interval) { |
|
106 | - return; |
|
107 | - } |
|
108 | - |
|
109 | - parent::execute($jobList, $logger); |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @param array $argument |
|
114 | - */ |
|
115 | - protected function run($argument) { |
|
116 | - $this->refreshWebcalService->refreshSubscription($argument['principaluri'], $argument['uri']); |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * get total number of seconds from DateInterval object |
|
121 | - * |
|
122 | - * @param DateInterval $interval |
|
123 | - * @return int |
|
124 | - */ |
|
125 | - private function getIntervalFromDateInterval(DateInterval $interval):int { |
|
126 | - return $interval->s |
|
127 | - + ($interval->i * 60) |
|
128 | - + ($interval->h * 60 * 60) |
|
129 | - + ($interval->d * 60 * 60 * 24) |
|
130 | - + ($interval->m * 60 * 60 * 24 * 30) |
|
131 | - + ($interval->y * 60 * 60 * 24 * 365); |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * Fixes types of rows |
|
136 | - * |
|
137 | - * @param array $row |
|
138 | - */ |
|
139 | - private function fixSubscriptionRowTyping(array &$row):void { |
|
140 | - $forceInt = [ |
|
141 | - 'id', |
|
142 | - 'lastmodified', |
|
143 | - RefreshWebcalService::STRIP_ALARMS, |
|
144 | - RefreshWebcalService::STRIP_ATTACHMENTS, |
|
145 | - RefreshWebcalService::STRIP_TODOS, |
|
146 | - ]; |
|
147 | - |
|
148 | - foreach ($forceInt as $column) { |
|
149 | - if (isset($row[$column])) { |
|
150 | - $row[$column] = (int) $row[$column]; |
|
151 | - } |
|
152 | - } |
|
153 | - } |
|
44 | + /** |
|
45 | + * @var RefreshWebcalService |
|
46 | + */ |
|
47 | + private $refreshWebcalService; |
|
48 | + |
|
49 | + /** |
|
50 | + * @var IConfig |
|
51 | + */ |
|
52 | + private $config; |
|
53 | + |
|
54 | + /** @var ILogger */ |
|
55 | + private $logger; |
|
56 | + |
|
57 | + /** @var ITimeFactory */ |
|
58 | + private $timeFactory; |
|
59 | + |
|
60 | + /** |
|
61 | + * RefreshWebcalJob constructor. |
|
62 | + * |
|
63 | + * @param RefreshWebcalService $refreshWebcalService |
|
64 | + * @param IConfig $config |
|
65 | + * @param ILogger $logger |
|
66 | + * @param ITimeFactory $timeFactory |
|
67 | + */ |
|
68 | + public function __construct(RefreshWebcalService $refreshWebcalService, IConfig $config, ILogger $logger, ITimeFactory $timeFactory) { |
|
69 | + parent::__construct($timeFactory); |
|
70 | + $this->refreshWebcalService = $refreshWebcalService; |
|
71 | + $this->config = $config; |
|
72 | + $this->logger = $logger; |
|
73 | + $this->timeFactory = $timeFactory; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * this function is called at most every hour |
|
78 | + * |
|
79 | + * @inheritdoc |
|
80 | + */ |
|
81 | + public function execute(IJobList $jobList, ILogger $logger = null) { |
|
82 | + $subscription = $this->refreshWebcalService->getSubscription($this->argument['principaluri'], $this->argument['uri']); |
|
83 | + if (!$subscription) { |
|
84 | + return; |
|
85 | + } |
|
86 | + |
|
87 | + $this->fixSubscriptionRowTyping($subscription); |
|
88 | + |
|
89 | + // if no refresh rate was configured, just refresh once a week |
|
90 | + $defaultRefreshRate = $this->config->getAppValue('dav', 'calendarSubscriptionRefreshRate', 'P1W'); |
|
91 | + $refreshRate = $subscription[RefreshWebcalService::REFRESH_RATE] ?? $defaultRefreshRate; |
|
92 | + |
|
93 | + $subscriptionId = $subscription['id']; |
|
94 | + |
|
95 | + try { |
|
96 | + /** @var DateInterval $dateInterval */ |
|
97 | + $dateInterval = DateTimeParser::parseDuration($refreshRate); |
|
98 | + } catch (InvalidDataException $ex) { |
|
99 | + $this->logger->logException($ex); |
|
100 | + $this->logger->warning("Subscription $subscriptionId could not be refreshed, refreshrate in database is invalid"); |
|
101 | + return; |
|
102 | + } |
|
103 | + |
|
104 | + $interval = $this->getIntervalFromDateInterval($dateInterval); |
|
105 | + if (($this->timeFactory->getTime() - $this->lastRun) <= $interval) { |
|
106 | + return; |
|
107 | + } |
|
108 | + |
|
109 | + parent::execute($jobList, $logger); |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @param array $argument |
|
114 | + */ |
|
115 | + protected function run($argument) { |
|
116 | + $this->refreshWebcalService->refreshSubscription($argument['principaluri'], $argument['uri']); |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * get total number of seconds from DateInterval object |
|
121 | + * |
|
122 | + * @param DateInterval $interval |
|
123 | + * @return int |
|
124 | + */ |
|
125 | + private function getIntervalFromDateInterval(DateInterval $interval):int { |
|
126 | + return $interval->s |
|
127 | + + ($interval->i * 60) |
|
128 | + + ($interval->h * 60 * 60) |
|
129 | + + ($interval->d * 60 * 60 * 24) |
|
130 | + + ($interval->m * 60 * 60 * 24 * 30) |
|
131 | + + ($interval->y * 60 * 60 * 24 * 365); |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * Fixes types of rows |
|
136 | + * |
|
137 | + * @param array $row |
|
138 | + */ |
|
139 | + private function fixSubscriptionRowTyping(array &$row):void { |
|
140 | + $forceInt = [ |
|
141 | + 'id', |
|
142 | + 'lastmodified', |
|
143 | + RefreshWebcalService::STRIP_ALARMS, |
|
144 | + RefreshWebcalService::STRIP_ATTACHMENTS, |
|
145 | + RefreshWebcalService::STRIP_TODOS, |
|
146 | + ]; |
|
147 | + |
|
148 | + foreach ($forceInt as $column) { |
|
149 | + if (isset($row[$column])) { |
|
150 | + $row[$column] = (int) $row[$column]; |
|
151 | + } |
|
152 | + } |
|
153 | + } |
|
154 | 154 | } |
@@ -40,257 +40,257 @@ |
||
40 | 40 | |
41 | 41 | class VerifyUserData extends Job { |
42 | 42 | |
43 | - /** @var bool */ |
|
44 | - private $retainJob = true; |
|
45 | - |
|
46 | - /** @var int max number of attempts to send the request */ |
|
47 | - private $maxTry = 24; |
|
48 | - |
|
49 | - /** @var int how much time should be between two tries (1 hour) */ |
|
50 | - private $interval = 3600; |
|
51 | - |
|
52 | - /** @var AccountManager */ |
|
53 | - private $accountManager; |
|
54 | - |
|
55 | - /** @var IUserManager */ |
|
56 | - private $userManager; |
|
57 | - |
|
58 | - /** @var IClientService */ |
|
59 | - private $httpClientService; |
|
60 | - |
|
61 | - /** @var ILogger */ |
|
62 | - private $logger; |
|
63 | - |
|
64 | - /** @var string */ |
|
65 | - private $lookupServerUrl; |
|
66 | - |
|
67 | - /** @var IConfig */ |
|
68 | - private $config; |
|
69 | - |
|
70 | - /** |
|
71 | - * VerifyUserData constructor. |
|
72 | - * |
|
73 | - * @param AccountManager $accountManager |
|
74 | - * @param IUserManager $userManager |
|
75 | - * @param IClientService $clientService |
|
76 | - * @param ILogger $logger |
|
77 | - * @param IConfig $config |
|
78 | - */ |
|
79 | - public function __construct(AccountManager $accountManager, |
|
80 | - IUserManager $userManager, |
|
81 | - IClientService $clientService, |
|
82 | - ILogger $logger, |
|
83 | - IConfig $config |
|
84 | - ) { |
|
85 | - $this->accountManager = $accountManager; |
|
86 | - $this->userManager = $userManager; |
|
87 | - $this->httpClientService = $clientService; |
|
88 | - $this->logger = $logger; |
|
89 | - |
|
90 | - $lookupServerUrl = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com'); |
|
91 | - $this->lookupServerUrl = rtrim($lookupServerUrl, '/'); |
|
92 | - $this->config = $config; |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * run the job, then remove it from the jobList |
|
97 | - * |
|
98 | - * @param IJobList $jobList |
|
99 | - * @param ILogger|null $logger |
|
100 | - */ |
|
101 | - public function execute(IJobList $jobList, ILogger $logger = null) { |
|
102 | - if ($this->shouldRun($this->argument)) { |
|
103 | - parent::execute($jobList, $logger); |
|
104 | - $jobList->remove($this, $this->argument); |
|
105 | - if ($this->retainJob) { |
|
106 | - $this->reAddJob($jobList, $this->argument); |
|
107 | - } else { |
|
108 | - $this->resetVerificationState(); |
|
109 | - } |
|
110 | - } |
|
111 | - } |
|
112 | - |
|
113 | - protected function run($argument) { |
|
114 | - $try = (int)$argument['try'] + 1; |
|
115 | - |
|
116 | - switch ($argument['type']) { |
|
117 | - case AccountManager::PROPERTY_WEBSITE: |
|
118 | - $result = $this->verifyWebsite($argument); |
|
119 | - break; |
|
120 | - case AccountManager::PROPERTY_TWITTER: |
|
121 | - case AccountManager::PROPERTY_EMAIL: |
|
122 | - $result = $this->verifyViaLookupServer($argument, $argument['type']); |
|
123 | - break; |
|
124 | - default: |
|
125 | - // no valid type given, no need to retry |
|
126 | - $this->logger->error($argument['type'] . ' is no valid type for user account data.'); |
|
127 | - $result = true; |
|
128 | - } |
|
129 | - |
|
130 | - if ($result === true || $try > $this->maxTry) { |
|
131 | - $this->retainJob = false; |
|
132 | - } |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * verify web page |
|
137 | - * |
|
138 | - * @param array $argument |
|
139 | - * @return bool true if we could check the verification code, otherwise false |
|
140 | - */ |
|
141 | - protected function verifyWebsite(array $argument) { |
|
142 | - $result = false; |
|
143 | - |
|
144 | - $url = rtrim($argument['data'], '/') . '/.well-known/' . 'CloudIdVerificationCode.txt'; |
|
145 | - |
|
146 | - $client = $this->httpClientService->newClient(); |
|
147 | - try { |
|
148 | - $response = $client->get($url); |
|
149 | - } catch (\Exception $e) { |
|
150 | - return false; |
|
151 | - } |
|
152 | - |
|
153 | - if ($response->getStatusCode() === Http::STATUS_OK) { |
|
154 | - $result = true; |
|
155 | - $publishedCode = $response->getBody(); |
|
156 | - // remove new lines and spaces |
|
157 | - $publishedCodeSanitized = trim(preg_replace('/\s\s+/', ' ', $publishedCode)); |
|
158 | - $user = $this->userManager->get($argument['uid']); |
|
159 | - // we don't check a valid user -> give up |
|
160 | - if ($user === null) { |
|
161 | - $this->logger->error($argument['uid'] . ' doesn\'t exist, can\'t verify user data.'); |
|
162 | - return $result; |
|
163 | - } |
|
164 | - $userData = $this->accountManager->getUser($user); |
|
165 | - |
|
166 | - if ($publishedCodeSanitized === $argument['verificationCode']) { |
|
167 | - $userData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::VERIFIED; |
|
168 | - } else { |
|
169 | - $userData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::NOT_VERIFIED; |
|
170 | - } |
|
171 | - |
|
172 | - $this->accountManager->updateUser($user, $userData); |
|
173 | - } |
|
174 | - |
|
175 | - return $result; |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * verify email address |
|
180 | - * |
|
181 | - * @param array $argument |
|
182 | - * @param string $dataType |
|
183 | - * @return bool true if we could check the verification code, otherwise false |
|
184 | - */ |
|
185 | - protected function verifyViaLookupServer(array $argument, $dataType) { |
|
186 | - if (empty($this->lookupServerUrl) || |
|
187 | - $this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'yes') !== 'yes' || |
|
188 | - $this->config->getSystemValue('has_internet_connection', true) === false) { |
|
189 | - return false; |
|
190 | - } |
|
191 | - |
|
192 | - $user = $this->userManager->get($argument['uid']); |
|
193 | - |
|
194 | - // we don't check a valid user -> give up |
|
195 | - if ($user === null) { |
|
196 | - $this->logger->info($argument['uid'] . ' doesn\'t exist, can\'t verify user data.'); |
|
197 | - return true; |
|
198 | - } |
|
199 | - |
|
200 | - $localUserData = $this->accountManager->getUser($user); |
|
201 | - $cloudId = $user->getCloudId(); |
|
202 | - |
|
203 | - // ask lookup-server for user data |
|
204 | - $lookupServerData = $this->queryLookupServer($cloudId); |
|
205 | - |
|
206 | - // for some reasons we couldn't read any data from the lookup server, try again later |
|
207 | - if (empty($lookupServerData) || empty($lookupServerData[$dataType])) { |
|
208 | - return false; |
|
209 | - } |
|
210 | - |
|
211 | - // lookup server has verification data for wrong user data (e.g. email address), try again later |
|
212 | - if ($lookupServerData[$dataType]['value'] !== $argument['data']) { |
|
213 | - return false; |
|
214 | - } |
|
215 | - |
|
216 | - // lookup server hasn't verified the email address so far, try again later |
|
217 | - if ($lookupServerData[$dataType]['verified'] === AccountManager::NOT_VERIFIED) { |
|
218 | - return false; |
|
219 | - } |
|
220 | - |
|
221 | - $localUserData[$dataType]['verified'] = AccountManager::VERIFIED; |
|
222 | - $this->accountManager->updateUser($user, $localUserData); |
|
223 | - |
|
224 | - return true; |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * @param string $cloudId |
|
229 | - * @return array |
|
230 | - */ |
|
231 | - protected function queryLookupServer($cloudId) { |
|
232 | - try { |
|
233 | - $client = $this->httpClientService->newClient(); |
|
234 | - $response = $client->get( |
|
235 | - $this->lookupServerUrl . '/users?search=' . urlencode($cloudId) . '&exactCloudId=1', |
|
236 | - [ |
|
237 | - 'timeout' => 10, |
|
238 | - 'connect_timeout' => 3, |
|
239 | - ] |
|
240 | - ); |
|
241 | - |
|
242 | - $body = json_decode($response->getBody(), true); |
|
243 | - |
|
244 | - if (is_array($body) && isset($body['federationId']) && $body['federationId'] === $cloudId) { |
|
245 | - return $body; |
|
246 | - } |
|
247 | - } catch (\Exception $e) { |
|
248 | - // do nothing, we will just re-try later |
|
249 | - } |
|
250 | - |
|
251 | - return []; |
|
252 | - } |
|
253 | - |
|
254 | - /** |
|
255 | - * re-add background job with new arguments |
|
256 | - * |
|
257 | - * @param IJobList $jobList |
|
258 | - * @param array $argument |
|
259 | - */ |
|
260 | - protected function reAddJob(IJobList $jobList, array $argument) { |
|
261 | - $jobList->add(VerifyUserData::class, |
|
262 | - [ |
|
263 | - 'verificationCode' => $argument['verificationCode'], |
|
264 | - 'data' => $argument['data'], |
|
265 | - 'type' => $argument['type'], |
|
266 | - 'uid' => $argument['uid'], |
|
267 | - 'try' => (int)$argument['try'] + 1, |
|
268 | - 'lastRun' => time() |
|
269 | - ] |
|
270 | - ); |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * test if it is time for the next run |
|
275 | - * |
|
276 | - * @param array $argument |
|
277 | - * @return bool |
|
278 | - */ |
|
279 | - protected function shouldRun(array $argument) { |
|
280 | - $lastRun = (int)$argument['lastRun']; |
|
281 | - return ((time() - $lastRun) > $this->interval); |
|
282 | - } |
|
283 | - |
|
284 | - |
|
285 | - /** |
|
286 | - * reset verification state after max tries are reached |
|
287 | - */ |
|
288 | - protected function resetVerificationState() { |
|
289 | - $user = $this->userManager->get($this->argument['uid']); |
|
290 | - if ($user !== null) { |
|
291 | - $accountData = $this->accountManager->getUser($user); |
|
292 | - $accountData[$this->argument['type']]['verified'] = AccountManager::NOT_VERIFIED; |
|
293 | - $this->accountManager->updateUser($user, $accountData); |
|
294 | - } |
|
295 | - } |
|
43 | + /** @var bool */ |
|
44 | + private $retainJob = true; |
|
45 | + |
|
46 | + /** @var int max number of attempts to send the request */ |
|
47 | + private $maxTry = 24; |
|
48 | + |
|
49 | + /** @var int how much time should be between two tries (1 hour) */ |
|
50 | + private $interval = 3600; |
|
51 | + |
|
52 | + /** @var AccountManager */ |
|
53 | + private $accountManager; |
|
54 | + |
|
55 | + /** @var IUserManager */ |
|
56 | + private $userManager; |
|
57 | + |
|
58 | + /** @var IClientService */ |
|
59 | + private $httpClientService; |
|
60 | + |
|
61 | + /** @var ILogger */ |
|
62 | + private $logger; |
|
63 | + |
|
64 | + /** @var string */ |
|
65 | + private $lookupServerUrl; |
|
66 | + |
|
67 | + /** @var IConfig */ |
|
68 | + private $config; |
|
69 | + |
|
70 | + /** |
|
71 | + * VerifyUserData constructor. |
|
72 | + * |
|
73 | + * @param AccountManager $accountManager |
|
74 | + * @param IUserManager $userManager |
|
75 | + * @param IClientService $clientService |
|
76 | + * @param ILogger $logger |
|
77 | + * @param IConfig $config |
|
78 | + */ |
|
79 | + public function __construct(AccountManager $accountManager, |
|
80 | + IUserManager $userManager, |
|
81 | + IClientService $clientService, |
|
82 | + ILogger $logger, |
|
83 | + IConfig $config |
|
84 | + ) { |
|
85 | + $this->accountManager = $accountManager; |
|
86 | + $this->userManager = $userManager; |
|
87 | + $this->httpClientService = $clientService; |
|
88 | + $this->logger = $logger; |
|
89 | + |
|
90 | + $lookupServerUrl = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com'); |
|
91 | + $this->lookupServerUrl = rtrim($lookupServerUrl, '/'); |
|
92 | + $this->config = $config; |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * run the job, then remove it from the jobList |
|
97 | + * |
|
98 | + * @param IJobList $jobList |
|
99 | + * @param ILogger|null $logger |
|
100 | + */ |
|
101 | + public function execute(IJobList $jobList, ILogger $logger = null) { |
|
102 | + if ($this->shouldRun($this->argument)) { |
|
103 | + parent::execute($jobList, $logger); |
|
104 | + $jobList->remove($this, $this->argument); |
|
105 | + if ($this->retainJob) { |
|
106 | + $this->reAddJob($jobList, $this->argument); |
|
107 | + } else { |
|
108 | + $this->resetVerificationState(); |
|
109 | + } |
|
110 | + } |
|
111 | + } |
|
112 | + |
|
113 | + protected function run($argument) { |
|
114 | + $try = (int)$argument['try'] + 1; |
|
115 | + |
|
116 | + switch ($argument['type']) { |
|
117 | + case AccountManager::PROPERTY_WEBSITE: |
|
118 | + $result = $this->verifyWebsite($argument); |
|
119 | + break; |
|
120 | + case AccountManager::PROPERTY_TWITTER: |
|
121 | + case AccountManager::PROPERTY_EMAIL: |
|
122 | + $result = $this->verifyViaLookupServer($argument, $argument['type']); |
|
123 | + break; |
|
124 | + default: |
|
125 | + // no valid type given, no need to retry |
|
126 | + $this->logger->error($argument['type'] . ' is no valid type for user account data.'); |
|
127 | + $result = true; |
|
128 | + } |
|
129 | + |
|
130 | + if ($result === true || $try > $this->maxTry) { |
|
131 | + $this->retainJob = false; |
|
132 | + } |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * verify web page |
|
137 | + * |
|
138 | + * @param array $argument |
|
139 | + * @return bool true if we could check the verification code, otherwise false |
|
140 | + */ |
|
141 | + protected function verifyWebsite(array $argument) { |
|
142 | + $result = false; |
|
143 | + |
|
144 | + $url = rtrim($argument['data'], '/') . '/.well-known/' . 'CloudIdVerificationCode.txt'; |
|
145 | + |
|
146 | + $client = $this->httpClientService->newClient(); |
|
147 | + try { |
|
148 | + $response = $client->get($url); |
|
149 | + } catch (\Exception $e) { |
|
150 | + return false; |
|
151 | + } |
|
152 | + |
|
153 | + if ($response->getStatusCode() === Http::STATUS_OK) { |
|
154 | + $result = true; |
|
155 | + $publishedCode = $response->getBody(); |
|
156 | + // remove new lines and spaces |
|
157 | + $publishedCodeSanitized = trim(preg_replace('/\s\s+/', ' ', $publishedCode)); |
|
158 | + $user = $this->userManager->get($argument['uid']); |
|
159 | + // we don't check a valid user -> give up |
|
160 | + if ($user === null) { |
|
161 | + $this->logger->error($argument['uid'] . ' doesn\'t exist, can\'t verify user data.'); |
|
162 | + return $result; |
|
163 | + } |
|
164 | + $userData = $this->accountManager->getUser($user); |
|
165 | + |
|
166 | + if ($publishedCodeSanitized === $argument['verificationCode']) { |
|
167 | + $userData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::VERIFIED; |
|
168 | + } else { |
|
169 | + $userData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::NOT_VERIFIED; |
|
170 | + } |
|
171 | + |
|
172 | + $this->accountManager->updateUser($user, $userData); |
|
173 | + } |
|
174 | + |
|
175 | + return $result; |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * verify email address |
|
180 | + * |
|
181 | + * @param array $argument |
|
182 | + * @param string $dataType |
|
183 | + * @return bool true if we could check the verification code, otherwise false |
|
184 | + */ |
|
185 | + protected function verifyViaLookupServer(array $argument, $dataType) { |
|
186 | + if (empty($this->lookupServerUrl) || |
|
187 | + $this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'yes') !== 'yes' || |
|
188 | + $this->config->getSystemValue('has_internet_connection', true) === false) { |
|
189 | + return false; |
|
190 | + } |
|
191 | + |
|
192 | + $user = $this->userManager->get($argument['uid']); |
|
193 | + |
|
194 | + // we don't check a valid user -> give up |
|
195 | + if ($user === null) { |
|
196 | + $this->logger->info($argument['uid'] . ' doesn\'t exist, can\'t verify user data.'); |
|
197 | + return true; |
|
198 | + } |
|
199 | + |
|
200 | + $localUserData = $this->accountManager->getUser($user); |
|
201 | + $cloudId = $user->getCloudId(); |
|
202 | + |
|
203 | + // ask lookup-server for user data |
|
204 | + $lookupServerData = $this->queryLookupServer($cloudId); |
|
205 | + |
|
206 | + // for some reasons we couldn't read any data from the lookup server, try again later |
|
207 | + if (empty($lookupServerData) || empty($lookupServerData[$dataType])) { |
|
208 | + return false; |
|
209 | + } |
|
210 | + |
|
211 | + // lookup server has verification data for wrong user data (e.g. email address), try again later |
|
212 | + if ($lookupServerData[$dataType]['value'] !== $argument['data']) { |
|
213 | + return false; |
|
214 | + } |
|
215 | + |
|
216 | + // lookup server hasn't verified the email address so far, try again later |
|
217 | + if ($lookupServerData[$dataType]['verified'] === AccountManager::NOT_VERIFIED) { |
|
218 | + return false; |
|
219 | + } |
|
220 | + |
|
221 | + $localUserData[$dataType]['verified'] = AccountManager::VERIFIED; |
|
222 | + $this->accountManager->updateUser($user, $localUserData); |
|
223 | + |
|
224 | + return true; |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * @param string $cloudId |
|
229 | + * @return array |
|
230 | + */ |
|
231 | + protected function queryLookupServer($cloudId) { |
|
232 | + try { |
|
233 | + $client = $this->httpClientService->newClient(); |
|
234 | + $response = $client->get( |
|
235 | + $this->lookupServerUrl . '/users?search=' . urlencode($cloudId) . '&exactCloudId=1', |
|
236 | + [ |
|
237 | + 'timeout' => 10, |
|
238 | + 'connect_timeout' => 3, |
|
239 | + ] |
|
240 | + ); |
|
241 | + |
|
242 | + $body = json_decode($response->getBody(), true); |
|
243 | + |
|
244 | + if (is_array($body) && isset($body['federationId']) && $body['federationId'] === $cloudId) { |
|
245 | + return $body; |
|
246 | + } |
|
247 | + } catch (\Exception $e) { |
|
248 | + // do nothing, we will just re-try later |
|
249 | + } |
|
250 | + |
|
251 | + return []; |
|
252 | + } |
|
253 | + |
|
254 | + /** |
|
255 | + * re-add background job with new arguments |
|
256 | + * |
|
257 | + * @param IJobList $jobList |
|
258 | + * @param array $argument |
|
259 | + */ |
|
260 | + protected function reAddJob(IJobList $jobList, array $argument) { |
|
261 | + $jobList->add(VerifyUserData::class, |
|
262 | + [ |
|
263 | + 'verificationCode' => $argument['verificationCode'], |
|
264 | + 'data' => $argument['data'], |
|
265 | + 'type' => $argument['type'], |
|
266 | + 'uid' => $argument['uid'], |
|
267 | + 'try' => (int)$argument['try'] + 1, |
|
268 | + 'lastRun' => time() |
|
269 | + ] |
|
270 | + ); |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * test if it is time for the next run |
|
275 | + * |
|
276 | + * @param array $argument |
|
277 | + * @return bool |
|
278 | + */ |
|
279 | + protected function shouldRun(array $argument) { |
|
280 | + $lastRun = (int)$argument['lastRun']; |
|
281 | + return ((time() - $lastRun) > $this->interval); |
|
282 | + } |
|
283 | + |
|
284 | + |
|
285 | + /** |
|
286 | + * reset verification state after max tries are reached |
|
287 | + */ |
|
288 | + protected function resetVerificationState() { |
|
289 | + $user = $this->userManager->get($this->argument['uid']); |
|
290 | + if ($user !== null) { |
|
291 | + $accountData = $this->accountManager->getUser($user); |
|
292 | + $accountData[$this->argument['type']]['verified'] = AccountManager::NOT_VERIFIED; |
|
293 | + $this->accountManager->updateUser($user, $accountData); |
|
294 | + } |
|
295 | + } |
|
296 | 296 | } |
@@ -43,102 +43,102 @@ |
||
43 | 43 | */ |
44 | 44 | class RetryJob extends Job { |
45 | 45 | |
46 | - /** @var bool */ |
|
47 | - private $retainJob = true; |
|
46 | + /** @var bool */ |
|
47 | + private $retainJob = true; |
|
48 | 48 | |
49 | - /** @var Notifications */ |
|
50 | - private $notifications; |
|
49 | + /** @var Notifications */ |
|
50 | + private $notifications; |
|
51 | 51 | |
52 | - /** @var int max number of attempts to send the request */ |
|
53 | - private $maxTry = 20; |
|
52 | + /** @var int max number of attempts to send the request */ |
|
53 | + private $maxTry = 20; |
|
54 | 54 | |
55 | - /** @var int how much time should be between two tries (10 minutes) */ |
|
56 | - private $interval = 600; |
|
55 | + /** @var int how much time should be between two tries (10 minutes) */ |
|
56 | + private $interval = 600; |
|
57 | 57 | |
58 | - /** |
|
59 | - * UnShare constructor. |
|
60 | - * |
|
61 | - * @param Notifications $notifications |
|
62 | - */ |
|
63 | - public function __construct(Notifications $notifications = null) { |
|
64 | - if ($notifications) { |
|
65 | - $this->notifications = $notifications; |
|
66 | - } else { |
|
67 | - $addressHandler = new AddressHandler( |
|
68 | - \OC::$server->getURLGenerator(), |
|
69 | - \OC::$server->getL10N('federatedfilesharing'), |
|
70 | - \OC::$server->getCloudIdManager() |
|
71 | - ); |
|
72 | - $this->notifications = new Notifications( |
|
73 | - $addressHandler, |
|
74 | - \OC::$server->getHTTPClientService(), |
|
75 | - \OC::$server->query(\OCP\OCS\IDiscoveryService::class), |
|
76 | - \OC::$server->getJobList(), |
|
77 | - \OC::$server->getCloudFederationProviderManager(), |
|
78 | - \OC::$server->getCloudFederationFactory() |
|
79 | - ); |
|
80 | - } |
|
81 | - } |
|
58 | + /** |
|
59 | + * UnShare constructor. |
|
60 | + * |
|
61 | + * @param Notifications $notifications |
|
62 | + */ |
|
63 | + public function __construct(Notifications $notifications = null) { |
|
64 | + if ($notifications) { |
|
65 | + $this->notifications = $notifications; |
|
66 | + } else { |
|
67 | + $addressHandler = new AddressHandler( |
|
68 | + \OC::$server->getURLGenerator(), |
|
69 | + \OC::$server->getL10N('federatedfilesharing'), |
|
70 | + \OC::$server->getCloudIdManager() |
|
71 | + ); |
|
72 | + $this->notifications = new Notifications( |
|
73 | + $addressHandler, |
|
74 | + \OC::$server->getHTTPClientService(), |
|
75 | + \OC::$server->query(\OCP\OCS\IDiscoveryService::class), |
|
76 | + \OC::$server->getJobList(), |
|
77 | + \OC::$server->getCloudFederationProviderManager(), |
|
78 | + \OC::$server->getCloudFederationFactory() |
|
79 | + ); |
|
80 | + } |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * run the job, then remove it from the jobList |
|
85 | - * |
|
86 | - * @param IJobList $jobList |
|
87 | - * @param ILogger|null $logger |
|
88 | - */ |
|
89 | - public function execute(IJobList $jobList, ILogger $logger = null) { |
|
90 | - if ($this->shouldRun($this->argument)) { |
|
91 | - parent::execute($jobList, $logger); |
|
92 | - $jobList->remove($this, $this->argument); |
|
93 | - if ($this->retainJob) { |
|
94 | - $this->reAddJob($jobList, $this->argument); |
|
95 | - } |
|
96 | - } |
|
97 | - } |
|
83 | + /** |
|
84 | + * run the job, then remove it from the jobList |
|
85 | + * |
|
86 | + * @param IJobList $jobList |
|
87 | + * @param ILogger|null $logger |
|
88 | + */ |
|
89 | + public function execute(IJobList $jobList, ILogger $logger = null) { |
|
90 | + if ($this->shouldRun($this->argument)) { |
|
91 | + parent::execute($jobList, $logger); |
|
92 | + $jobList->remove($this, $this->argument); |
|
93 | + if ($this->retainJob) { |
|
94 | + $this->reAddJob($jobList, $this->argument); |
|
95 | + } |
|
96 | + } |
|
97 | + } |
|
98 | 98 | |
99 | - protected function run($argument) { |
|
100 | - $remote = $argument['remote']; |
|
101 | - $remoteId = $argument['remoteId']; |
|
102 | - $token = $argument['token']; |
|
103 | - $action = $argument['action']; |
|
104 | - $data = json_decode($argument['data'], true); |
|
105 | - $try = (int)$argument['try'] + 1; |
|
99 | + protected function run($argument) { |
|
100 | + $remote = $argument['remote']; |
|
101 | + $remoteId = $argument['remoteId']; |
|
102 | + $token = $argument['token']; |
|
103 | + $action = $argument['action']; |
|
104 | + $data = json_decode($argument['data'], true); |
|
105 | + $try = (int)$argument['try'] + 1; |
|
106 | 106 | |
107 | - $result = $this->notifications->sendUpdateToRemote($remote, $remoteId, $token, $action, $data, $try); |
|
107 | + $result = $this->notifications->sendUpdateToRemote($remote, $remoteId, $token, $action, $data, $try); |
|
108 | 108 | |
109 | - if ($result === true || $try > $this->maxTry) { |
|
110 | - $this->retainJob = false; |
|
111 | - } |
|
112 | - } |
|
109 | + if ($result === true || $try > $this->maxTry) { |
|
110 | + $this->retainJob = false; |
|
111 | + } |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * re-add background job with new arguments |
|
116 | - * |
|
117 | - * @param IJobList $jobList |
|
118 | - * @param array $argument |
|
119 | - */ |
|
120 | - protected function reAddJob(IJobList $jobList, array $argument) { |
|
121 | - $jobList->add(RetryJob::class, |
|
122 | - [ |
|
123 | - 'remote' => $argument['remote'], |
|
124 | - 'remoteId' => $argument['remoteId'], |
|
125 | - 'token' => $argument['token'], |
|
126 | - 'data' => $argument['data'], |
|
127 | - 'action' => $argument['action'], |
|
128 | - 'try' => (int)$argument['try'] + 1, |
|
129 | - 'lastRun' => time() |
|
130 | - ] |
|
131 | - ); |
|
132 | - } |
|
114 | + /** |
|
115 | + * re-add background job with new arguments |
|
116 | + * |
|
117 | + * @param IJobList $jobList |
|
118 | + * @param array $argument |
|
119 | + */ |
|
120 | + protected function reAddJob(IJobList $jobList, array $argument) { |
|
121 | + $jobList->add(RetryJob::class, |
|
122 | + [ |
|
123 | + 'remote' => $argument['remote'], |
|
124 | + 'remoteId' => $argument['remoteId'], |
|
125 | + 'token' => $argument['token'], |
|
126 | + 'data' => $argument['data'], |
|
127 | + 'action' => $argument['action'], |
|
128 | + 'try' => (int)$argument['try'] + 1, |
|
129 | + 'lastRun' => time() |
|
130 | + ] |
|
131 | + ); |
|
132 | + } |
|
133 | 133 | |
134 | - /** |
|
135 | - * test if it is time for the next run |
|
136 | - * |
|
137 | - * @param array $argument |
|
138 | - * @return bool |
|
139 | - */ |
|
140 | - protected function shouldRun(array $argument) { |
|
141 | - $lastRun = (int)$argument['lastRun']; |
|
142 | - return ((time() - $lastRun) > $this->interval); |
|
143 | - } |
|
134 | + /** |
|
135 | + * test if it is time for the next run |
|
136 | + * |
|
137 | + * @param array $argument |
|
138 | + * @return bool |
|
139 | + */ |
|
140 | + protected function shouldRun(array $argument) { |
|
141 | + $lastRun = (int)$argument['lastRun']; |
|
142 | + return ((time() - $lastRun) > $this->interval); |
|
143 | + } |
|
144 | 144 | } |