@@ -51,173 +51,173 @@ |
||
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 bool */ |
|
73 | - protected $retainJob = false; |
|
74 | - |
|
75 | - private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/request-shared-secret'; |
|
76 | - |
|
77 | - /** @var int 30 day = 2592000sec */ |
|
78 | - private $maxLifespan = 2592000; |
|
79 | - |
|
80 | - /** |
|
81 | - * RequestSharedSecret constructor. |
|
82 | - * |
|
83 | - * @param IClientService $httpClientService |
|
84 | - * @param IURLGenerator $urlGenerator |
|
85 | - * @param IJobList $jobList |
|
86 | - * @param TrustedServers $trustedServers |
|
87 | - * @param IDiscoveryService $ocsDiscoveryService |
|
88 | - * @param ILogger $logger |
|
89 | - * @param ITimeFactory $timeFactory |
|
90 | - */ |
|
91 | - public function __construct( |
|
92 | - IClientService $httpClientService, |
|
93 | - IURLGenerator $urlGenerator, |
|
94 | - IJobList $jobList, |
|
95 | - TrustedServers $trustedServers, |
|
96 | - IDiscoveryService $ocsDiscoveryService, |
|
97 | - ILogger $logger, |
|
98 | - ITimeFactory $timeFactory |
|
99 | - ) { |
|
100 | - parent::__construct($timeFactory); |
|
101 | - $this->httpClient = $httpClientService->newClient(); |
|
102 | - $this->jobList = $jobList; |
|
103 | - $this->urlGenerator = $urlGenerator; |
|
104 | - $this->logger = $logger; |
|
105 | - $this->ocsDiscoveryService = $ocsDiscoveryService; |
|
106 | - $this->trustedServers = $trustedServers; |
|
107 | - } |
|
108 | - |
|
109 | - |
|
110 | - /** |
|
111 | - * run the job, then remove it from the joblist |
|
112 | - * |
|
113 | - * @param IJobList $jobList |
|
114 | - * @param ILogger|null $logger |
|
115 | - */ |
|
116 | - public function execute(IJobList $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 | - $jobList->remove($this, $this->argument); |
|
124 | - |
|
125 | - if ($this->retainJob) { |
|
126 | - $this->reAddJob($this->argument); |
|
127 | - } |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * call execute() method of parent |
|
132 | - * |
|
133 | - * @param IJobList $jobList |
|
134 | - * @param ILogger $logger |
|
135 | - */ |
|
136 | - protected function parentExecute($jobList, $logger) { |
|
137 | - parent::execute($jobList, $logger); |
|
138 | - } |
|
139 | - |
|
140 | - protected function run($argument) { |
|
141 | - $target = $argument['url']; |
|
142 | - $created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime(); |
|
143 | - $currentTime = $this->time->getTime(); |
|
144 | - $source = $this->urlGenerator->getAbsoluteURL('/'); |
|
145 | - $source = rtrim($source, '/'); |
|
146 | - $token = $argument['token']; |
|
147 | - |
|
148 | - // kill job after 30 days of trying |
|
149 | - $deadline = $currentTime - $this->maxLifespan; |
|
150 | - if ($created < $deadline) { |
|
151 | - $this->retainJob = false; |
|
152 | - $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
|
153 | - return; |
|
154 | - } |
|
155 | - |
|
156 | - $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); |
|
157 | - $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
|
158 | - |
|
159 | - // make sure that we have a well formated url |
|
160 | - $url = rtrim($target, '/') . '/' . trim($endPoint, '/'); |
|
161 | - |
|
162 | - try { |
|
163 | - $result = $this->httpClient->post( |
|
164 | - $url, |
|
165 | - [ |
|
166 | - 'body' => [ |
|
167 | - 'url' => $source, |
|
168 | - 'token' => $token, |
|
169 | - 'format' => 'json', |
|
170 | - ], |
|
171 | - 'timeout' => 3, |
|
172 | - 'connect_timeout' => 3, |
|
173 | - ] |
|
174 | - ); |
|
175 | - |
|
176 | - $status = $result->getStatusCode(); |
|
177 | - } catch (ClientException $e) { |
|
178 | - $status = $e->getCode(); |
|
179 | - if ($status === Http::STATUS_FORBIDDEN) { |
|
180 | - $this->logger->info($target . ' refused to ask for a shared secret.', ['app' => 'federation']); |
|
181 | - } else { |
|
182 | - $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
|
183 | - } |
|
184 | - } catch (RequestException $e) { |
|
185 | - $status = -1; // There is no status code if we could not connect |
|
186 | - $this->logger->info('Could not connect to ' . $target, ['app' => 'federation']); |
|
187 | - } catch (RingException $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 (\Exception $e) { |
|
191 | - $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
|
192 | - $this->logger->logException($e, ['app' => 'federation']); |
|
193 | - } |
|
194 | - |
|
195 | - // if we received a unexpected response we try again later |
|
196 | - if ( |
|
197 | - $status !== Http::STATUS_OK |
|
198 | - && $status !== Http::STATUS_FORBIDDEN |
|
199 | - ) { |
|
200 | - $this->retainJob = true; |
|
201 | - } |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * re-add background job |
|
206 | - * |
|
207 | - * @param array $argument |
|
208 | - */ |
|
209 | - protected function reAddJob(array $argument) { |
|
210 | - $url = $argument['url']; |
|
211 | - $created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime(); |
|
212 | - $token = $argument['token']; |
|
213 | - |
|
214 | - $this->jobList->add( |
|
215 | - RequestSharedSecret::class, |
|
216 | - [ |
|
217 | - 'url' => $url, |
|
218 | - 'token' => $token, |
|
219 | - 'created' => $created |
|
220 | - ] |
|
221 | - ); |
|
222 | - } |
|
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 bool */ |
|
73 | + protected $retainJob = false; |
|
74 | + |
|
75 | + private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/request-shared-secret'; |
|
76 | + |
|
77 | + /** @var int 30 day = 2592000sec */ |
|
78 | + private $maxLifespan = 2592000; |
|
79 | + |
|
80 | + /** |
|
81 | + * RequestSharedSecret constructor. |
|
82 | + * |
|
83 | + * @param IClientService $httpClientService |
|
84 | + * @param IURLGenerator $urlGenerator |
|
85 | + * @param IJobList $jobList |
|
86 | + * @param TrustedServers $trustedServers |
|
87 | + * @param IDiscoveryService $ocsDiscoveryService |
|
88 | + * @param ILogger $logger |
|
89 | + * @param ITimeFactory $timeFactory |
|
90 | + */ |
|
91 | + public function __construct( |
|
92 | + IClientService $httpClientService, |
|
93 | + IURLGenerator $urlGenerator, |
|
94 | + IJobList $jobList, |
|
95 | + TrustedServers $trustedServers, |
|
96 | + IDiscoveryService $ocsDiscoveryService, |
|
97 | + ILogger $logger, |
|
98 | + ITimeFactory $timeFactory |
|
99 | + ) { |
|
100 | + parent::__construct($timeFactory); |
|
101 | + $this->httpClient = $httpClientService->newClient(); |
|
102 | + $this->jobList = $jobList; |
|
103 | + $this->urlGenerator = $urlGenerator; |
|
104 | + $this->logger = $logger; |
|
105 | + $this->ocsDiscoveryService = $ocsDiscoveryService; |
|
106 | + $this->trustedServers = $trustedServers; |
|
107 | + } |
|
108 | + |
|
109 | + |
|
110 | + /** |
|
111 | + * run the job, then remove it from the joblist |
|
112 | + * |
|
113 | + * @param IJobList $jobList |
|
114 | + * @param ILogger|null $logger |
|
115 | + */ |
|
116 | + public function execute(IJobList $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 | + $jobList->remove($this, $this->argument); |
|
124 | + |
|
125 | + if ($this->retainJob) { |
|
126 | + $this->reAddJob($this->argument); |
|
127 | + } |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * call execute() method of parent |
|
132 | + * |
|
133 | + * @param IJobList $jobList |
|
134 | + * @param ILogger $logger |
|
135 | + */ |
|
136 | + protected function parentExecute($jobList, $logger) { |
|
137 | + parent::execute($jobList, $logger); |
|
138 | + } |
|
139 | + |
|
140 | + protected function run($argument) { |
|
141 | + $target = $argument['url']; |
|
142 | + $created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime(); |
|
143 | + $currentTime = $this->time->getTime(); |
|
144 | + $source = $this->urlGenerator->getAbsoluteURL('/'); |
|
145 | + $source = rtrim($source, '/'); |
|
146 | + $token = $argument['token']; |
|
147 | + |
|
148 | + // kill job after 30 days of trying |
|
149 | + $deadline = $currentTime - $this->maxLifespan; |
|
150 | + if ($created < $deadline) { |
|
151 | + $this->retainJob = false; |
|
152 | + $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
|
153 | + return; |
|
154 | + } |
|
155 | + |
|
156 | + $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); |
|
157 | + $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
|
158 | + |
|
159 | + // make sure that we have a well formated url |
|
160 | + $url = rtrim($target, '/') . '/' . trim($endPoint, '/'); |
|
161 | + |
|
162 | + try { |
|
163 | + $result = $this->httpClient->post( |
|
164 | + $url, |
|
165 | + [ |
|
166 | + 'body' => [ |
|
167 | + 'url' => $source, |
|
168 | + 'token' => $token, |
|
169 | + 'format' => 'json', |
|
170 | + ], |
|
171 | + 'timeout' => 3, |
|
172 | + 'connect_timeout' => 3, |
|
173 | + ] |
|
174 | + ); |
|
175 | + |
|
176 | + $status = $result->getStatusCode(); |
|
177 | + } catch (ClientException $e) { |
|
178 | + $status = $e->getCode(); |
|
179 | + if ($status === Http::STATUS_FORBIDDEN) { |
|
180 | + $this->logger->info($target . ' refused to ask for a shared secret.', ['app' => 'federation']); |
|
181 | + } else { |
|
182 | + $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
|
183 | + } |
|
184 | + } catch (RequestException $e) { |
|
185 | + $status = -1; // There is no status code if we could not connect |
|
186 | + $this->logger->info('Could not connect to ' . $target, ['app' => 'federation']); |
|
187 | + } catch (RingException $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 (\Exception $e) { |
|
191 | + $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
|
192 | + $this->logger->logException($e, ['app' => 'federation']); |
|
193 | + } |
|
194 | + |
|
195 | + // if we received a unexpected response we try again later |
|
196 | + if ( |
|
197 | + $status !== Http::STATUS_OK |
|
198 | + && $status !== Http::STATUS_FORBIDDEN |
|
199 | + ) { |
|
200 | + $this->retainJob = true; |
|
201 | + } |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * re-add background job |
|
206 | + * |
|
207 | + * @param array $argument |
|
208 | + */ |
|
209 | + protected function reAddJob(array $argument) { |
|
210 | + $url = $argument['url']; |
|
211 | + $created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime(); |
|
212 | + $token = $argument['token']; |
|
213 | + |
|
214 | + $this->jobList->add( |
|
215 | + RequestSharedSecret::class, |
|
216 | + [ |
|
217 | + 'url' => $url, |
|
218 | + 'token' => $token, |
|
219 | + 'created' => $created |
|
220 | + ] |
|
221 | + ); |
|
222 | + } |
|
223 | 223 | } |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | |
140 | 140 | protected function run($argument) { |
141 | 141 | $target = $argument['url']; |
142 | - $created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime(); |
|
142 | + $created = isset($argument['created']) ? (int) $argument['created'] : $this->time->getTime(); |
|
143 | 143 | $currentTime = $this->time->getTime(); |
144 | 144 | $source = $this->urlGenerator->getAbsoluteURL('/'); |
145 | 145 | $source = rtrim($source, '/'); |
@@ -157,7 +157,7 @@ discard block |
||
157 | 157 | $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
158 | 158 | |
159 | 159 | // make sure that we have a well formated url |
160 | - $url = rtrim($target, '/') . '/' . trim($endPoint, '/'); |
|
160 | + $url = rtrim($target, '/').'/'.trim($endPoint, '/'); |
|
161 | 161 | |
162 | 162 | try { |
163 | 163 | $result = $this->httpClient->post( |
@@ -177,16 +177,16 @@ discard block |
||
177 | 177 | } catch (ClientException $e) { |
178 | 178 | $status = $e->getCode(); |
179 | 179 | if ($status === Http::STATUS_FORBIDDEN) { |
180 | - $this->logger->info($target . ' refused to ask for a shared secret.', ['app' => 'federation']); |
|
180 | + $this->logger->info($target.' refused to ask for a shared secret.', ['app' => 'federation']); |
|
181 | 181 | } else { |
182 | - $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
|
182 | + $this->logger->info($target.' responded with a '.$status.' containing: '.$e->getMessage(), ['app' => 'federation']); |
|
183 | 183 | } |
184 | 184 | } catch (RequestException $e) { |
185 | 185 | $status = -1; // There is no status code if we could not connect |
186 | - $this->logger->info('Could not connect to ' . $target, ['app' => 'federation']); |
|
186 | + $this->logger->info('Could not connect to '.$target, ['app' => 'federation']); |
|
187 | 187 | } catch (RingException $e) { |
188 | 188 | $status = -1; // There is no status code if we could not connect |
189 | - $this->logger->info('Could not connect to ' . $target, ['app' => 'federation']); |
|
189 | + $this->logger->info('Could not connect to '.$target, ['app' => 'federation']); |
|
190 | 190 | } catch (\Exception $e) { |
191 | 191 | $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
192 | 192 | $this->logger->logException($e, ['app' => 'federation']); |
@@ -208,7 +208,7 @@ discard block |
||
208 | 208 | */ |
209 | 209 | protected function reAddJob(array $argument) { |
210 | 210 | $url = $argument['url']; |
211 | - $created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime(); |
|
211 | + $created = isset($argument['created']) ? (int) $argument['created'] : $this->time->getTime(); |
|
212 | 212 | $token = $argument['token']; |
213 | 213 | |
214 | 214 | $this->jobList->add( |
@@ -53,198 +53,198 @@ |
||
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 bool */ |
|
75 | - protected $retainJob = false; |
|
76 | - |
|
77 | - private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/shared-secret'; |
|
78 | - |
|
79 | - /** @var int 30 day = 2592000sec */ |
|
80 | - private $maxLifespan = 2592000; |
|
81 | - |
|
82 | - /** |
|
83 | - * RequestSharedSecret constructor. |
|
84 | - * |
|
85 | - * @param IClientService $httpClientService |
|
86 | - * @param IURLGenerator $urlGenerator |
|
87 | - * @param IJobList $jobList |
|
88 | - * @param TrustedServers $trustedServers |
|
89 | - * @param ILogger $logger |
|
90 | - * @param IDiscoveryService $ocsDiscoveryService |
|
91 | - * @param ITimeFactory $timeFactory |
|
92 | - */ |
|
93 | - public function __construct( |
|
94 | - IClientService $httpClientService, |
|
95 | - IURLGenerator $urlGenerator, |
|
96 | - IJobList $jobList, |
|
97 | - TrustedServers $trustedServers, |
|
98 | - ILogger $logger, |
|
99 | - IDiscoveryService $ocsDiscoveryService, |
|
100 | - ITimeFactory $timeFactory |
|
101 | - ) { |
|
102 | - parent::__construct($timeFactory); |
|
103 | - $this->logger = $logger; |
|
104 | - $this->httpClient = $httpClientService->newClient(); |
|
105 | - $this->jobList = $jobList; |
|
106 | - $this->urlGenerator = $urlGenerator; |
|
107 | - $this->ocsDiscoveryService = $ocsDiscoveryService; |
|
108 | - $this->trustedServers = $trustedServers; |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * run the job, then remove it from the joblist |
|
113 | - * |
|
114 | - * @param IJobList $jobList |
|
115 | - * @param ILogger|null $logger |
|
116 | - */ |
|
117 | - public function execute(IJobList $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 | - $jobList->remove($this, $this->argument); |
|
125 | - |
|
126 | - if ($this->retainJob) { |
|
127 | - $this->reAddJob($this->argument); |
|
128 | - } |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * call execute() method of parent |
|
133 | - * |
|
134 | - * @param IJobList $jobList |
|
135 | - * @param ILogger $logger |
|
136 | - */ |
|
137 | - protected function parentExecute($jobList, $logger = null) { |
|
138 | - parent::execute($jobList, $logger); |
|
139 | - } |
|
140 | - |
|
141 | - protected function run($argument) { |
|
142 | - $target = $argument['url']; |
|
143 | - $created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime(); |
|
144 | - $currentTime = $this->time->getTime(); |
|
145 | - $source = $this->urlGenerator->getAbsoluteURL('/'); |
|
146 | - $source = rtrim($source, '/'); |
|
147 | - $token = $argument['token']; |
|
148 | - |
|
149 | - // kill job after 30 days of trying |
|
150 | - $deadline = $currentTime - $this->maxLifespan; |
|
151 | - if ($created < $deadline) { |
|
152 | - $this->retainJob = false; |
|
153 | - $this->trustedServers->setServerStatus($target,TrustedServers::STATUS_FAILURE); |
|
154 | - return; |
|
155 | - } |
|
156 | - |
|
157 | - $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); |
|
158 | - $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
|
159 | - |
|
160 | - // make sure that we have a well formatted url |
|
161 | - $url = rtrim($target, '/') . '/' . trim($endPoint, '/'); |
|
162 | - |
|
163 | - $result = null; |
|
164 | - try { |
|
165 | - $result = $this->httpClient->get( |
|
166 | - $url, |
|
167 | - [ |
|
168 | - 'query' => |
|
169 | - [ |
|
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 exchange a shared secret with you.', ['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->logException($e, [ |
|
190 | - 'message' => 'Could not connect to ' . $target, |
|
191 | - 'level' => ILogger::INFO, |
|
192 | - 'app' => 'federation', |
|
193 | - ]); |
|
194 | - } catch (RingException $e) { |
|
195 | - $status = -1; // There is no status code if we could not connect |
|
196 | - $this->logger->logException($e, [ |
|
197 | - 'message' => 'Could not connect to ' . $target, |
|
198 | - 'level' => ILogger::INFO, |
|
199 | - 'app' => 'federation', |
|
200 | - ]); |
|
201 | - } catch (\Exception $e) { |
|
202 | - $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
|
203 | - $this->logger->logException($e, ['app' => 'federation']); |
|
204 | - } |
|
205 | - |
|
206 | - // if we received a unexpected response we try again later |
|
207 | - if ( |
|
208 | - $status !== Http::STATUS_OK |
|
209 | - && $status !== Http::STATUS_FORBIDDEN |
|
210 | - ) { |
|
211 | - $this->retainJob = true; |
|
212 | - } |
|
213 | - |
|
214 | - if ($status === Http::STATUS_OK && $result instanceof IResponse) { |
|
215 | - $body = $result->getBody(); |
|
216 | - $result = json_decode($body, true); |
|
217 | - if (isset($result['ocs']['data']['sharedSecret'])) { |
|
218 | - $this->trustedServers->addSharedSecret( |
|
219 | - $target, |
|
220 | - $result['ocs']['data']['sharedSecret'] |
|
221 | - ); |
|
222 | - } else { |
|
223 | - $this->logger->error( |
|
224 | - 'remote server "' . $target . '"" does not return a valid shared secret. Received data: ' . $body, |
|
225 | - ['app' => 'federation'] |
|
226 | - ); |
|
227 | - $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
|
228 | - } |
|
229 | - } |
|
230 | - } |
|
231 | - |
|
232 | - /** |
|
233 | - * re-add background job |
|
234 | - * |
|
235 | - * @param array $argument |
|
236 | - */ |
|
237 | - protected function reAddJob(array $argument) { |
|
238 | - $url = $argument['url']; |
|
239 | - $created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime(); |
|
240 | - $token = $argument['token']; |
|
241 | - $this->jobList->add( |
|
242 | - GetSharedSecret::class, |
|
243 | - [ |
|
244 | - 'url' => $url, |
|
245 | - 'token' => $token, |
|
246 | - 'created' => $created |
|
247 | - ] |
|
248 | - ); |
|
249 | - } |
|
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 bool */ |
|
75 | + protected $retainJob = false; |
|
76 | + |
|
77 | + private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/shared-secret'; |
|
78 | + |
|
79 | + /** @var int 30 day = 2592000sec */ |
|
80 | + private $maxLifespan = 2592000; |
|
81 | + |
|
82 | + /** |
|
83 | + * RequestSharedSecret constructor. |
|
84 | + * |
|
85 | + * @param IClientService $httpClientService |
|
86 | + * @param IURLGenerator $urlGenerator |
|
87 | + * @param IJobList $jobList |
|
88 | + * @param TrustedServers $trustedServers |
|
89 | + * @param ILogger $logger |
|
90 | + * @param IDiscoveryService $ocsDiscoveryService |
|
91 | + * @param ITimeFactory $timeFactory |
|
92 | + */ |
|
93 | + public function __construct( |
|
94 | + IClientService $httpClientService, |
|
95 | + IURLGenerator $urlGenerator, |
|
96 | + IJobList $jobList, |
|
97 | + TrustedServers $trustedServers, |
|
98 | + ILogger $logger, |
|
99 | + IDiscoveryService $ocsDiscoveryService, |
|
100 | + ITimeFactory $timeFactory |
|
101 | + ) { |
|
102 | + parent::__construct($timeFactory); |
|
103 | + $this->logger = $logger; |
|
104 | + $this->httpClient = $httpClientService->newClient(); |
|
105 | + $this->jobList = $jobList; |
|
106 | + $this->urlGenerator = $urlGenerator; |
|
107 | + $this->ocsDiscoveryService = $ocsDiscoveryService; |
|
108 | + $this->trustedServers = $trustedServers; |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * run the job, then remove it from the joblist |
|
113 | + * |
|
114 | + * @param IJobList $jobList |
|
115 | + * @param ILogger|null $logger |
|
116 | + */ |
|
117 | + public function execute(IJobList $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 | + $jobList->remove($this, $this->argument); |
|
125 | + |
|
126 | + if ($this->retainJob) { |
|
127 | + $this->reAddJob($this->argument); |
|
128 | + } |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * call execute() method of parent |
|
133 | + * |
|
134 | + * @param IJobList $jobList |
|
135 | + * @param ILogger $logger |
|
136 | + */ |
|
137 | + protected function parentExecute($jobList, $logger = null) { |
|
138 | + parent::execute($jobList, $logger); |
|
139 | + } |
|
140 | + |
|
141 | + protected function run($argument) { |
|
142 | + $target = $argument['url']; |
|
143 | + $created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime(); |
|
144 | + $currentTime = $this->time->getTime(); |
|
145 | + $source = $this->urlGenerator->getAbsoluteURL('/'); |
|
146 | + $source = rtrim($source, '/'); |
|
147 | + $token = $argument['token']; |
|
148 | + |
|
149 | + // kill job after 30 days of trying |
|
150 | + $deadline = $currentTime - $this->maxLifespan; |
|
151 | + if ($created < $deadline) { |
|
152 | + $this->retainJob = false; |
|
153 | + $this->trustedServers->setServerStatus($target,TrustedServers::STATUS_FAILURE); |
|
154 | + return; |
|
155 | + } |
|
156 | + |
|
157 | + $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); |
|
158 | + $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
|
159 | + |
|
160 | + // make sure that we have a well formatted url |
|
161 | + $url = rtrim($target, '/') . '/' . trim($endPoint, '/'); |
|
162 | + |
|
163 | + $result = null; |
|
164 | + try { |
|
165 | + $result = $this->httpClient->get( |
|
166 | + $url, |
|
167 | + [ |
|
168 | + 'query' => |
|
169 | + [ |
|
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 exchange a shared secret with you.', ['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->logException($e, [ |
|
190 | + 'message' => 'Could not connect to ' . $target, |
|
191 | + 'level' => ILogger::INFO, |
|
192 | + 'app' => 'federation', |
|
193 | + ]); |
|
194 | + } catch (RingException $e) { |
|
195 | + $status = -1; // There is no status code if we could not connect |
|
196 | + $this->logger->logException($e, [ |
|
197 | + 'message' => 'Could not connect to ' . $target, |
|
198 | + 'level' => ILogger::INFO, |
|
199 | + 'app' => 'federation', |
|
200 | + ]); |
|
201 | + } catch (\Exception $e) { |
|
202 | + $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
|
203 | + $this->logger->logException($e, ['app' => 'federation']); |
|
204 | + } |
|
205 | + |
|
206 | + // if we received a unexpected response we try again later |
|
207 | + if ( |
|
208 | + $status !== Http::STATUS_OK |
|
209 | + && $status !== Http::STATUS_FORBIDDEN |
|
210 | + ) { |
|
211 | + $this->retainJob = true; |
|
212 | + } |
|
213 | + |
|
214 | + if ($status === Http::STATUS_OK && $result instanceof IResponse) { |
|
215 | + $body = $result->getBody(); |
|
216 | + $result = json_decode($body, true); |
|
217 | + if (isset($result['ocs']['data']['sharedSecret'])) { |
|
218 | + $this->trustedServers->addSharedSecret( |
|
219 | + $target, |
|
220 | + $result['ocs']['data']['sharedSecret'] |
|
221 | + ); |
|
222 | + } else { |
|
223 | + $this->logger->error( |
|
224 | + 'remote server "' . $target . '"" does not return a valid shared secret. Received data: ' . $body, |
|
225 | + ['app' => 'federation'] |
|
226 | + ); |
|
227 | + $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
|
228 | + } |
|
229 | + } |
|
230 | + } |
|
231 | + |
|
232 | + /** |
|
233 | + * re-add background job |
|
234 | + * |
|
235 | + * @param array $argument |
|
236 | + */ |
|
237 | + protected function reAddJob(array $argument) { |
|
238 | + $url = $argument['url']; |
|
239 | + $created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime(); |
|
240 | + $token = $argument['token']; |
|
241 | + $this->jobList->add( |
|
242 | + GetSharedSecret::class, |
|
243 | + [ |
|
244 | + 'url' => $url, |
|
245 | + 'token' => $token, |
|
246 | + 'created' => $created |
|
247 | + ] |
|
248 | + ); |
|
249 | + } |
|
250 | 250 | } |
@@ -140,7 +140,7 @@ discard block |
||
140 | 140 | |
141 | 141 | protected function run($argument) { |
142 | 142 | $target = $argument['url']; |
143 | - $created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime(); |
|
143 | + $created = isset($argument['created']) ? (int) $argument['created'] : $this->time->getTime(); |
|
144 | 144 | $currentTime = $this->time->getTime(); |
145 | 145 | $source = $this->urlGenerator->getAbsoluteURL('/'); |
146 | 146 | $source = rtrim($source, '/'); |
@@ -150,7 +150,7 @@ discard block |
||
150 | 150 | $deadline = $currentTime - $this->maxLifespan; |
151 | 151 | if ($created < $deadline) { |
152 | 152 | $this->retainJob = false; |
153 | - $this->trustedServers->setServerStatus($target,TrustedServers::STATUS_FAILURE); |
|
153 | + $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
|
154 | 154 | return; |
155 | 155 | } |
156 | 156 | |
@@ -158,7 +158,7 @@ discard block |
||
158 | 158 | $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
159 | 159 | |
160 | 160 | // make sure that we have a well formatted url |
161 | - $url = rtrim($target, '/') . '/' . trim($endPoint, '/'); |
|
161 | + $url = rtrim($target, '/').'/'.trim($endPoint, '/'); |
|
162 | 162 | |
163 | 163 | $result = null; |
164 | 164 | try { |
@@ -180,21 +180,21 @@ discard block |
||
180 | 180 | } catch (ClientException $e) { |
181 | 181 | $status = $e->getCode(); |
182 | 182 | if ($status === Http::STATUS_FORBIDDEN) { |
183 | - $this->logger->info($target . ' refused to exchange a shared secret with you.', ['app' => 'federation']); |
|
183 | + $this->logger->info($target.' refused to exchange a shared secret with you.', ['app' => 'federation']); |
|
184 | 184 | } else { |
185 | - $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
|
185 | + $this->logger->info($target.' responded with a '.$status.' containing: '.$e->getMessage(), ['app' => 'federation']); |
|
186 | 186 | } |
187 | 187 | } catch (RequestException $e) { |
188 | 188 | $status = -1; // There is no status code if we could not connect |
189 | 189 | $this->logger->logException($e, [ |
190 | - 'message' => 'Could not connect to ' . $target, |
|
190 | + 'message' => 'Could not connect to '.$target, |
|
191 | 191 | 'level' => ILogger::INFO, |
192 | 192 | 'app' => 'federation', |
193 | 193 | ]); |
194 | 194 | } catch (RingException $e) { |
195 | 195 | $status = -1; // There is no status code if we could not connect |
196 | 196 | $this->logger->logException($e, [ |
197 | - 'message' => 'Could not connect to ' . $target, |
|
197 | + 'message' => 'Could not connect to '.$target, |
|
198 | 198 | 'level' => ILogger::INFO, |
199 | 199 | 'app' => 'federation', |
200 | 200 | ]); |
@@ -221,7 +221,7 @@ discard block |
||
221 | 221 | ); |
222 | 222 | } else { |
223 | 223 | $this->logger->error( |
224 | - 'remote server "' . $target . '"" does not return a valid shared secret. Received data: ' . $body, |
|
224 | + 'remote server "'.$target.'"" does not return a valid shared secret. Received data: '.$body, |
|
225 | 225 | ['app' => 'federation'] |
226 | 226 | ); |
227 | 227 | $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
@@ -236,7 +236,7 @@ discard block |
||
236 | 236 | */ |
237 | 237 | protected function reAddJob(array $argument) { |
238 | 238 | $url = $argument['url']; |
239 | - $created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime(); |
|
239 | + $created = isset($argument['created']) ? (int) $argument['created'] : $this->time->getTime(); |
|
240 | 240 | $token = $argument['token']; |
241 | 241 | $this->jobList->add( |
242 | 242 | GetSharedSecret::class, |
@@ -42,250 +42,250 @@ |
||
42 | 42 | |
43 | 43 | class VerifyUserData extends Job { |
44 | 44 | |
45 | - /** @var bool */ |
|
46 | - private $retainJob = true; |
|
47 | - |
|
48 | - /** @var int max number of attempts to send the request */ |
|
49 | - private $maxTry = 24; |
|
50 | - |
|
51 | - /** @var int how much time should be between two tries (1 hour) */ |
|
52 | - private $interval = 3600; |
|
53 | - |
|
54 | - /** @var AccountManager */ |
|
55 | - private $accountManager; |
|
56 | - |
|
57 | - /** @var IUserManager */ |
|
58 | - private $userManager; |
|
59 | - |
|
60 | - /** @var IClientService */ |
|
61 | - private $httpClientService; |
|
62 | - |
|
63 | - /** @var ILogger */ |
|
64 | - private $logger; |
|
65 | - |
|
66 | - /** @var string */ |
|
67 | - private $lookupServerUrl; |
|
68 | - |
|
69 | - /** @var IConfig */ |
|
70 | - private $config; |
|
71 | - |
|
72 | - public function __construct(AccountManager $accountManager, |
|
73 | - IUserManager $userManager, |
|
74 | - IClientService $clientService, |
|
75 | - ILogger $logger, |
|
76 | - ITimeFactory $timeFactory, |
|
77 | - IConfig $config |
|
78 | - ) { |
|
79 | - parent::__construct($timeFactory); |
|
80 | - $this->accountManager = $accountManager; |
|
81 | - $this->userManager = $userManager; |
|
82 | - $this->httpClientService = $clientService; |
|
83 | - $this->logger = $logger; |
|
84 | - |
|
85 | - $lookupServerUrl = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com'); |
|
86 | - $this->lookupServerUrl = rtrim($lookupServerUrl, '/'); |
|
87 | - $this->config = $config; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * run the job, then remove it from the jobList |
|
92 | - * |
|
93 | - * @param IJobList $jobList |
|
94 | - * @param ILogger|null $logger |
|
95 | - */ |
|
96 | - public function execute(IJobList $jobList, ILogger $logger = null) { |
|
97 | - if ($this->shouldRun($this->argument)) { |
|
98 | - parent::execute($jobList, $logger); |
|
99 | - $jobList->remove($this, $this->argument); |
|
100 | - if ($this->retainJob) { |
|
101 | - $this->reAddJob($jobList, $this->argument); |
|
102 | - } else { |
|
103 | - $this->resetVerificationState(); |
|
104 | - } |
|
105 | - } |
|
106 | - } |
|
107 | - |
|
108 | - protected function run($argument) { |
|
109 | - $try = (int)$argument['try'] + 1; |
|
110 | - |
|
111 | - switch ($argument['type']) { |
|
112 | - case IAccountManager::PROPERTY_WEBSITE: |
|
113 | - $result = $this->verifyWebsite($argument); |
|
114 | - break; |
|
115 | - case IAccountManager::PROPERTY_TWITTER: |
|
116 | - case IAccountManager::PROPERTY_EMAIL: |
|
117 | - $result = $this->verifyViaLookupServer($argument, $argument['type']); |
|
118 | - break; |
|
119 | - default: |
|
120 | - // no valid type given, no need to retry |
|
121 | - $this->logger->error($argument['type'] . ' is no valid type for user account data.'); |
|
122 | - $result = true; |
|
123 | - } |
|
124 | - |
|
125 | - if ($result === true || $try > $this->maxTry) { |
|
126 | - $this->retainJob = false; |
|
127 | - } |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * verify web page |
|
132 | - * |
|
133 | - * @param array $argument |
|
134 | - * @return bool true if we could check the verification code, otherwise false |
|
135 | - */ |
|
136 | - protected function verifyWebsite(array $argument) { |
|
137 | - $result = false; |
|
138 | - |
|
139 | - $url = rtrim($argument['data'], '/') . '/.well-known/' . 'CloudIdVerificationCode.txt'; |
|
140 | - |
|
141 | - $client = $this->httpClientService->newClient(); |
|
142 | - try { |
|
143 | - $response = $client->get($url); |
|
144 | - } catch (\Exception $e) { |
|
145 | - return false; |
|
146 | - } |
|
147 | - |
|
148 | - if ($response->getStatusCode() === Http::STATUS_OK) { |
|
149 | - $result = true; |
|
150 | - $publishedCode = $response->getBody(); |
|
151 | - // remove new lines and spaces |
|
152 | - $publishedCodeSanitized = trim(preg_replace('/\s\s+/', ' ', $publishedCode)); |
|
153 | - $user = $this->userManager->get($argument['uid']); |
|
154 | - // we don't check a valid user -> give up |
|
155 | - if ($user === null) { |
|
156 | - $this->logger->error($argument['uid'] . ' doesn\'t exist, can\'t verify user data.'); |
|
157 | - return $result; |
|
158 | - } |
|
159 | - $userData = $this->accountManager->getUser($user); |
|
160 | - |
|
161 | - if ($publishedCodeSanitized === $argument['verificationCode']) { |
|
162 | - $userData[IAccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::VERIFIED; |
|
163 | - } else { |
|
164 | - $userData[IAccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::NOT_VERIFIED; |
|
165 | - } |
|
166 | - |
|
167 | - $this->accountManager->updateUser($user, $userData); |
|
168 | - } |
|
169 | - |
|
170 | - return $result; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * verify email address |
|
175 | - * |
|
176 | - * @param array $argument |
|
177 | - * @param string $dataType |
|
178 | - * @return bool true if we could check the verification code, otherwise false |
|
179 | - */ |
|
180 | - protected function verifyViaLookupServer(array $argument, $dataType) { |
|
181 | - if (empty($this->lookupServerUrl) || |
|
182 | - $this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'yes') !== 'yes' || |
|
183 | - $this->config->getSystemValue('has_internet_connection', true) === false) { |
|
184 | - return false; |
|
185 | - } |
|
186 | - |
|
187 | - $user = $this->userManager->get($argument['uid']); |
|
188 | - |
|
189 | - // we don't check a valid user -> give up |
|
190 | - if ($user === null) { |
|
191 | - $this->logger->info($argument['uid'] . ' doesn\'t exist, can\'t verify user data.'); |
|
192 | - return true; |
|
193 | - } |
|
194 | - |
|
195 | - $localUserData = $this->accountManager->getUser($user); |
|
196 | - $cloudId = $user->getCloudId(); |
|
197 | - |
|
198 | - // ask lookup-server for user data |
|
199 | - $lookupServerData = $this->queryLookupServer($cloudId); |
|
200 | - |
|
201 | - // for some reasons we couldn't read any data from the lookup server, try again later |
|
202 | - if (empty($lookupServerData) || empty($lookupServerData[$dataType])) { |
|
203 | - return false; |
|
204 | - } |
|
205 | - |
|
206 | - // lookup server has verification data for wrong user data (e.g. email address), try again later |
|
207 | - if ($lookupServerData[$dataType]['value'] !== $argument['data']) { |
|
208 | - return false; |
|
209 | - } |
|
210 | - |
|
211 | - // lookup server hasn't verified the email address so far, try again later |
|
212 | - if ($lookupServerData[$dataType]['verified'] === AccountManager::NOT_VERIFIED) { |
|
213 | - return false; |
|
214 | - } |
|
215 | - |
|
216 | - $localUserData[$dataType]['verified'] = AccountManager::VERIFIED; |
|
217 | - $this->accountManager->updateUser($user, $localUserData); |
|
218 | - |
|
219 | - return true; |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * @param string $cloudId |
|
224 | - * @return array |
|
225 | - */ |
|
226 | - protected function queryLookupServer($cloudId) { |
|
227 | - try { |
|
228 | - $client = $this->httpClientService->newClient(); |
|
229 | - $response = $client->get( |
|
230 | - $this->lookupServerUrl . '/users?search=' . urlencode($cloudId) . '&exactCloudId=1', |
|
231 | - [ |
|
232 | - 'timeout' => 10, |
|
233 | - 'connect_timeout' => 3, |
|
234 | - ] |
|
235 | - ); |
|
236 | - |
|
237 | - $body = json_decode($response->getBody(), true); |
|
238 | - |
|
239 | - if (is_array($body) && isset($body['federationId']) && $body['federationId'] === $cloudId) { |
|
240 | - return $body; |
|
241 | - } |
|
242 | - } catch (\Exception $e) { |
|
243 | - // do nothing, we will just re-try later |
|
244 | - } |
|
245 | - |
|
246 | - return []; |
|
247 | - } |
|
248 | - |
|
249 | - /** |
|
250 | - * re-add background job with new arguments |
|
251 | - * |
|
252 | - * @param IJobList $jobList |
|
253 | - * @param array $argument |
|
254 | - */ |
|
255 | - protected function reAddJob(IJobList $jobList, array $argument) { |
|
256 | - $jobList->add(VerifyUserData::class, |
|
257 | - [ |
|
258 | - 'verificationCode' => $argument['verificationCode'], |
|
259 | - 'data' => $argument['data'], |
|
260 | - 'type' => $argument['type'], |
|
261 | - 'uid' => $argument['uid'], |
|
262 | - 'try' => (int)$argument['try'] + 1, |
|
263 | - 'lastRun' => time() |
|
264 | - ] |
|
265 | - ); |
|
266 | - } |
|
267 | - |
|
268 | - /** |
|
269 | - * test if it is time for the next run |
|
270 | - * |
|
271 | - * @param array $argument |
|
272 | - * @return bool |
|
273 | - */ |
|
274 | - protected function shouldRun(array $argument) { |
|
275 | - $lastRun = (int)$argument['lastRun']; |
|
276 | - return ((time() - $lastRun) > $this->interval); |
|
277 | - } |
|
278 | - |
|
279 | - |
|
280 | - /** |
|
281 | - * reset verification state after max tries are reached |
|
282 | - */ |
|
283 | - protected function resetVerificationState() { |
|
284 | - $user = $this->userManager->get($this->argument['uid']); |
|
285 | - if ($user !== null) { |
|
286 | - $accountData = $this->accountManager->getUser($user); |
|
287 | - $accountData[$this->argument['type']]['verified'] = AccountManager::NOT_VERIFIED; |
|
288 | - $this->accountManager->updateUser($user, $accountData); |
|
289 | - } |
|
290 | - } |
|
45 | + /** @var bool */ |
|
46 | + private $retainJob = true; |
|
47 | + |
|
48 | + /** @var int max number of attempts to send the request */ |
|
49 | + private $maxTry = 24; |
|
50 | + |
|
51 | + /** @var int how much time should be between two tries (1 hour) */ |
|
52 | + private $interval = 3600; |
|
53 | + |
|
54 | + /** @var AccountManager */ |
|
55 | + private $accountManager; |
|
56 | + |
|
57 | + /** @var IUserManager */ |
|
58 | + private $userManager; |
|
59 | + |
|
60 | + /** @var IClientService */ |
|
61 | + private $httpClientService; |
|
62 | + |
|
63 | + /** @var ILogger */ |
|
64 | + private $logger; |
|
65 | + |
|
66 | + /** @var string */ |
|
67 | + private $lookupServerUrl; |
|
68 | + |
|
69 | + /** @var IConfig */ |
|
70 | + private $config; |
|
71 | + |
|
72 | + public function __construct(AccountManager $accountManager, |
|
73 | + IUserManager $userManager, |
|
74 | + IClientService $clientService, |
|
75 | + ILogger $logger, |
|
76 | + ITimeFactory $timeFactory, |
|
77 | + IConfig $config |
|
78 | + ) { |
|
79 | + parent::__construct($timeFactory); |
|
80 | + $this->accountManager = $accountManager; |
|
81 | + $this->userManager = $userManager; |
|
82 | + $this->httpClientService = $clientService; |
|
83 | + $this->logger = $logger; |
|
84 | + |
|
85 | + $lookupServerUrl = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com'); |
|
86 | + $this->lookupServerUrl = rtrim($lookupServerUrl, '/'); |
|
87 | + $this->config = $config; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * run the job, then remove it from the jobList |
|
92 | + * |
|
93 | + * @param IJobList $jobList |
|
94 | + * @param ILogger|null $logger |
|
95 | + */ |
|
96 | + public function execute(IJobList $jobList, ILogger $logger = null) { |
|
97 | + if ($this->shouldRun($this->argument)) { |
|
98 | + parent::execute($jobList, $logger); |
|
99 | + $jobList->remove($this, $this->argument); |
|
100 | + if ($this->retainJob) { |
|
101 | + $this->reAddJob($jobList, $this->argument); |
|
102 | + } else { |
|
103 | + $this->resetVerificationState(); |
|
104 | + } |
|
105 | + } |
|
106 | + } |
|
107 | + |
|
108 | + protected function run($argument) { |
|
109 | + $try = (int)$argument['try'] + 1; |
|
110 | + |
|
111 | + switch ($argument['type']) { |
|
112 | + case IAccountManager::PROPERTY_WEBSITE: |
|
113 | + $result = $this->verifyWebsite($argument); |
|
114 | + break; |
|
115 | + case IAccountManager::PROPERTY_TWITTER: |
|
116 | + case IAccountManager::PROPERTY_EMAIL: |
|
117 | + $result = $this->verifyViaLookupServer($argument, $argument['type']); |
|
118 | + break; |
|
119 | + default: |
|
120 | + // no valid type given, no need to retry |
|
121 | + $this->logger->error($argument['type'] . ' is no valid type for user account data.'); |
|
122 | + $result = true; |
|
123 | + } |
|
124 | + |
|
125 | + if ($result === true || $try > $this->maxTry) { |
|
126 | + $this->retainJob = false; |
|
127 | + } |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * verify web page |
|
132 | + * |
|
133 | + * @param array $argument |
|
134 | + * @return bool true if we could check the verification code, otherwise false |
|
135 | + */ |
|
136 | + protected function verifyWebsite(array $argument) { |
|
137 | + $result = false; |
|
138 | + |
|
139 | + $url = rtrim($argument['data'], '/') . '/.well-known/' . 'CloudIdVerificationCode.txt'; |
|
140 | + |
|
141 | + $client = $this->httpClientService->newClient(); |
|
142 | + try { |
|
143 | + $response = $client->get($url); |
|
144 | + } catch (\Exception $e) { |
|
145 | + return false; |
|
146 | + } |
|
147 | + |
|
148 | + if ($response->getStatusCode() === Http::STATUS_OK) { |
|
149 | + $result = true; |
|
150 | + $publishedCode = $response->getBody(); |
|
151 | + // remove new lines and spaces |
|
152 | + $publishedCodeSanitized = trim(preg_replace('/\s\s+/', ' ', $publishedCode)); |
|
153 | + $user = $this->userManager->get($argument['uid']); |
|
154 | + // we don't check a valid user -> give up |
|
155 | + if ($user === null) { |
|
156 | + $this->logger->error($argument['uid'] . ' doesn\'t exist, can\'t verify user data.'); |
|
157 | + return $result; |
|
158 | + } |
|
159 | + $userData = $this->accountManager->getUser($user); |
|
160 | + |
|
161 | + if ($publishedCodeSanitized === $argument['verificationCode']) { |
|
162 | + $userData[IAccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::VERIFIED; |
|
163 | + } else { |
|
164 | + $userData[IAccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::NOT_VERIFIED; |
|
165 | + } |
|
166 | + |
|
167 | + $this->accountManager->updateUser($user, $userData); |
|
168 | + } |
|
169 | + |
|
170 | + return $result; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * verify email address |
|
175 | + * |
|
176 | + * @param array $argument |
|
177 | + * @param string $dataType |
|
178 | + * @return bool true if we could check the verification code, otherwise false |
|
179 | + */ |
|
180 | + protected function verifyViaLookupServer(array $argument, $dataType) { |
|
181 | + if (empty($this->lookupServerUrl) || |
|
182 | + $this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'yes') !== 'yes' || |
|
183 | + $this->config->getSystemValue('has_internet_connection', true) === false) { |
|
184 | + return false; |
|
185 | + } |
|
186 | + |
|
187 | + $user = $this->userManager->get($argument['uid']); |
|
188 | + |
|
189 | + // we don't check a valid user -> give up |
|
190 | + if ($user === null) { |
|
191 | + $this->logger->info($argument['uid'] . ' doesn\'t exist, can\'t verify user data.'); |
|
192 | + return true; |
|
193 | + } |
|
194 | + |
|
195 | + $localUserData = $this->accountManager->getUser($user); |
|
196 | + $cloudId = $user->getCloudId(); |
|
197 | + |
|
198 | + // ask lookup-server for user data |
|
199 | + $lookupServerData = $this->queryLookupServer($cloudId); |
|
200 | + |
|
201 | + // for some reasons we couldn't read any data from the lookup server, try again later |
|
202 | + if (empty($lookupServerData) || empty($lookupServerData[$dataType])) { |
|
203 | + return false; |
|
204 | + } |
|
205 | + |
|
206 | + // lookup server has verification data for wrong user data (e.g. email address), try again later |
|
207 | + if ($lookupServerData[$dataType]['value'] !== $argument['data']) { |
|
208 | + return false; |
|
209 | + } |
|
210 | + |
|
211 | + // lookup server hasn't verified the email address so far, try again later |
|
212 | + if ($lookupServerData[$dataType]['verified'] === AccountManager::NOT_VERIFIED) { |
|
213 | + return false; |
|
214 | + } |
|
215 | + |
|
216 | + $localUserData[$dataType]['verified'] = AccountManager::VERIFIED; |
|
217 | + $this->accountManager->updateUser($user, $localUserData); |
|
218 | + |
|
219 | + return true; |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * @param string $cloudId |
|
224 | + * @return array |
|
225 | + */ |
|
226 | + protected function queryLookupServer($cloudId) { |
|
227 | + try { |
|
228 | + $client = $this->httpClientService->newClient(); |
|
229 | + $response = $client->get( |
|
230 | + $this->lookupServerUrl . '/users?search=' . urlencode($cloudId) . '&exactCloudId=1', |
|
231 | + [ |
|
232 | + 'timeout' => 10, |
|
233 | + 'connect_timeout' => 3, |
|
234 | + ] |
|
235 | + ); |
|
236 | + |
|
237 | + $body = json_decode($response->getBody(), true); |
|
238 | + |
|
239 | + if (is_array($body) && isset($body['federationId']) && $body['federationId'] === $cloudId) { |
|
240 | + return $body; |
|
241 | + } |
|
242 | + } catch (\Exception $e) { |
|
243 | + // do nothing, we will just re-try later |
|
244 | + } |
|
245 | + |
|
246 | + return []; |
|
247 | + } |
|
248 | + |
|
249 | + /** |
|
250 | + * re-add background job with new arguments |
|
251 | + * |
|
252 | + * @param IJobList $jobList |
|
253 | + * @param array $argument |
|
254 | + */ |
|
255 | + protected function reAddJob(IJobList $jobList, array $argument) { |
|
256 | + $jobList->add(VerifyUserData::class, |
|
257 | + [ |
|
258 | + 'verificationCode' => $argument['verificationCode'], |
|
259 | + 'data' => $argument['data'], |
|
260 | + 'type' => $argument['type'], |
|
261 | + 'uid' => $argument['uid'], |
|
262 | + 'try' => (int)$argument['try'] + 1, |
|
263 | + 'lastRun' => time() |
|
264 | + ] |
|
265 | + ); |
|
266 | + } |
|
267 | + |
|
268 | + /** |
|
269 | + * test if it is time for the next run |
|
270 | + * |
|
271 | + * @param array $argument |
|
272 | + * @return bool |
|
273 | + */ |
|
274 | + protected function shouldRun(array $argument) { |
|
275 | + $lastRun = (int)$argument['lastRun']; |
|
276 | + return ((time() - $lastRun) > $this->interval); |
|
277 | + } |
|
278 | + |
|
279 | + |
|
280 | + /** |
|
281 | + * reset verification state after max tries are reached |
|
282 | + */ |
|
283 | + protected function resetVerificationState() { |
|
284 | + $user = $this->userManager->get($this->argument['uid']); |
|
285 | + if ($user !== null) { |
|
286 | + $accountData = $this->accountManager->getUser($user); |
|
287 | + $accountData[$this->argument['type']]['verified'] = AccountManager::NOT_VERIFIED; |
|
288 | + $this->accountManager->updateUser($user, $accountData); |
|
289 | + } |
|
290 | + } |
|
291 | 291 | } |
@@ -43,84 +43,84 @@ |
||
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 | 58 | |
59 | - public function __construct(Notifications $notifications, |
|
60 | - ITimeFactory $timeFactory) { |
|
61 | - parent::__construct($timeFactory); |
|
62 | - $this->notifications = $notifications; |
|
63 | - } |
|
59 | + public function __construct(Notifications $notifications, |
|
60 | + ITimeFactory $timeFactory) { |
|
61 | + parent::__construct($timeFactory); |
|
62 | + $this->notifications = $notifications; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * run the job, then remove it from the jobList |
|
67 | - * |
|
68 | - * @param IJobList $jobList |
|
69 | - * @param ILogger|null $logger |
|
70 | - */ |
|
71 | - public function execute(IJobList $jobList, ILogger $logger = null) { |
|
72 | - if ($this->shouldRun($this->argument)) { |
|
73 | - parent::execute($jobList, $logger); |
|
74 | - $jobList->remove($this, $this->argument); |
|
75 | - if ($this->retainJob) { |
|
76 | - $this->reAddJob($jobList, $this->argument); |
|
77 | - } |
|
78 | - } |
|
79 | - } |
|
65 | + /** |
|
66 | + * run the job, then remove it from the jobList |
|
67 | + * |
|
68 | + * @param IJobList $jobList |
|
69 | + * @param ILogger|null $logger |
|
70 | + */ |
|
71 | + public function execute(IJobList $jobList, ILogger $logger = null) { |
|
72 | + if ($this->shouldRun($this->argument)) { |
|
73 | + parent::execute($jobList, $logger); |
|
74 | + $jobList->remove($this, $this->argument); |
|
75 | + if ($this->retainJob) { |
|
76 | + $this->reAddJob($jobList, $this->argument); |
|
77 | + } |
|
78 | + } |
|
79 | + } |
|
80 | 80 | |
81 | - protected function run($argument) { |
|
82 | - $remote = $argument['remote']; |
|
83 | - $remoteId = $argument['remoteId']; |
|
84 | - $token = $argument['token']; |
|
85 | - $action = $argument['action']; |
|
86 | - $data = json_decode($argument['data'], true); |
|
87 | - $try = (int)$argument['try'] + 1; |
|
81 | + protected function run($argument) { |
|
82 | + $remote = $argument['remote']; |
|
83 | + $remoteId = $argument['remoteId']; |
|
84 | + $token = $argument['token']; |
|
85 | + $action = $argument['action']; |
|
86 | + $data = json_decode($argument['data'], true); |
|
87 | + $try = (int)$argument['try'] + 1; |
|
88 | 88 | |
89 | - $result = $this->notifications->sendUpdateToRemote($remote, $remoteId, $token, $action, $data, $try); |
|
89 | + $result = $this->notifications->sendUpdateToRemote($remote, $remoteId, $token, $action, $data, $try); |
|
90 | 90 | |
91 | - if ($result === true || $try > $this->maxTry) { |
|
92 | - $this->retainJob = false; |
|
93 | - } |
|
94 | - } |
|
91 | + if ($result === true || $try > $this->maxTry) { |
|
92 | + $this->retainJob = false; |
|
93 | + } |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * re-add background job with new arguments |
|
98 | - * |
|
99 | - * @param IJobList $jobList |
|
100 | - * @param array $argument |
|
101 | - */ |
|
102 | - protected function reAddJob(IJobList $jobList, array $argument) { |
|
103 | - $jobList->add(RetryJob::class, |
|
104 | - [ |
|
105 | - 'remote' => $argument['remote'], |
|
106 | - 'remoteId' => $argument['remoteId'], |
|
107 | - 'token' => $argument['token'], |
|
108 | - 'data' => $argument['data'], |
|
109 | - 'action' => $argument['action'], |
|
110 | - 'try' => (int)$argument['try'] + 1, |
|
111 | - 'lastRun' => $this->time->getTime() |
|
112 | - ] |
|
113 | - ); |
|
114 | - } |
|
96 | + /** |
|
97 | + * re-add background job with new arguments |
|
98 | + * |
|
99 | + * @param IJobList $jobList |
|
100 | + * @param array $argument |
|
101 | + */ |
|
102 | + protected function reAddJob(IJobList $jobList, array $argument) { |
|
103 | + $jobList->add(RetryJob::class, |
|
104 | + [ |
|
105 | + 'remote' => $argument['remote'], |
|
106 | + 'remoteId' => $argument['remoteId'], |
|
107 | + 'token' => $argument['token'], |
|
108 | + 'data' => $argument['data'], |
|
109 | + 'action' => $argument['action'], |
|
110 | + 'try' => (int)$argument['try'] + 1, |
|
111 | + 'lastRun' => $this->time->getTime() |
|
112 | + ] |
|
113 | + ); |
|
114 | + } |
|
115 | 115 | |
116 | - /** |
|
117 | - * test if it is time for the next run |
|
118 | - * |
|
119 | - * @param array $argument |
|
120 | - * @return bool |
|
121 | - */ |
|
122 | - protected function shouldRun(array $argument) { |
|
123 | - $lastRun = (int)$argument['lastRun']; |
|
124 | - return (($this->time->getTime() - $lastRun) > $this->interval); |
|
125 | - } |
|
116 | + /** |
|
117 | + * test if it is time for the next run |
|
118 | + * |
|
119 | + * @param array $argument |
|
120 | + * @return bool |
|
121 | + */ |
|
122 | + protected function shouldRun(array $argument) { |
|
123 | + $lastRun = (int)$argument['lastRun']; |
|
124 | + return (($this->time->getTime() - $lastRun) > $this->interval); |
|
125 | + } |
|
126 | 126 | } |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | $token = $argument['token']; |
85 | 85 | $action = $argument['action']; |
86 | 86 | $data = json_decode($argument['data'], true); |
87 | - $try = (int)$argument['try'] + 1; |
|
87 | + $try = (int) $argument['try'] + 1; |
|
88 | 88 | |
89 | 89 | $result = $this->notifications->sendUpdateToRemote($remote, $remoteId, $token, $action, $data, $try); |
90 | 90 | |
@@ -107,7 +107,7 @@ discard block |
||
107 | 107 | 'token' => $argument['token'], |
108 | 108 | 'data' => $argument['data'], |
109 | 109 | 'action' => $argument['action'], |
110 | - 'try' => (int)$argument['try'] + 1, |
|
110 | + 'try' => (int) $argument['try'] + 1, |
|
111 | 111 | 'lastRun' => $this->time->getTime() |
112 | 112 | ] |
113 | 113 | ); |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | * @return bool |
121 | 121 | */ |
122 | 122 | protected function shouldRun(array $argument) { |
123 | - $lastRun = (int)$argument['lastRun']; |
|
123 | + $lastRun = (int) $argument['lastRun']; |
|
124 | 124 | return (($this->time->getTime() - $lastRun) > $this->interval); |
125 | 125 | } |
126 | 126 | } |