@@ -49,194 +49,194 @@ |
||
49 | 49 | */ |
50 | 50 | class GetSharedSecret extends Job{ |
51 | 51 | |
52 | - /** @var IClient */ |
|
53 | - private $httpClient; |
|
54 | - |
|
55 | - /** @var IJobList */ |
|
56 | - private $jobList; |
|
57 | - |
|
58 | - /** @var IURLGenerator */ |
|
59 | - private $urlGenerator; |
|
60 | - |
|
61 | - /** @var TrustedServers */ |
|
62 | - private $trustedServers; |
|
63 | - |
|
64 | - /** @var DbHandler */ |
|
65 | - private $dbHandler; |
|
66 | - |
|
67 | - /** @var IDiscoveryService */ |
|
68 | - private $ocsDiscoveryService; |
|
69 | - |
|
70 | - /** @var ILogger */ |
|
71 | - private $logger; |
|
72 | - |
|
73 | - /** @var bool */ |
|
74 | - protected $retainJob = false; |
|
75 | - |
|
76 | - private $format = '?format=json'; |
|
77 | - |
|
78 | - private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/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 ILogger $logger |
|
91 | - * @param DbHandler $dbHandler |
|
92 | - * @param IDiscoveryService $ocsDiscoveryService |
|
93 | - */ |
|
94 | - public function __construct( |
|
95 | - IClientService $httpClientService, |
|
96 | - IURLGenerator $urlGenerator, |
|
97 | - IJobList $jobList, |
|
98 | - TrustedServers $trustedServers, |
|
99 | - ILogger $logger, |
|
100 | - DbHandler $dbHandler, |
|
101 | - IDiscoveryService $ocsDiscoveryService |
|
102 | - ) { |
|
103 | - $this->logger = $logger; |
|
104 | - $this->httpClient = $httpClientService->newClient(); |
|
105 | - $this->jobList = $jobList; |
|
106 | - $this->urlGenerator = $urlGenerator; |
|
107 | - $this->dbHandler = $dbHandler; |
|
108 | - $this->ocsDiscoveryService = $ocsDiscoveryService; |
|
109 | - $this->trustedServers = $trustedServers; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * run the job, then remove it from the joblist |
|
114 | - * |
|
115 | - * @param JobList $jobList |
|
116 | - * @param ILogger $logger |
|
117 | - */ |
|
118 | - public function execute($jobList, ILogger $logger = null) { |
|
119 | - $target = $this->argument['url']; |
|
120 | - // only execute if target is still in the list of trusted domains |
|
121 | - if ($this->trustedServers->isTrustedServer($target)) { |
|
122 | - $this->parentExecute($jobList, $logger); |
|
123 | - } |
|
124 | - |
|
125 | - $jobList->remove($this, $this->argument); |
|
126 | - |
|
127 | - if ($this->retainJob) { |
|
128 | - $this->reAddJob($jobList, $this->argument); |
|
129 | - } |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * call execute() method of parent |
|
134 | - * |
|
135 | - * @param JobList $jobList |
|
136 | - * @param ILogger $logger |
|
137 | - */ |
|
138 | - protected function parentExecute($jobList, $logger = null) { |
|
139 | - parent::execute($jobList, $logger); |
|
140 | - } |
|
141 | - |
|
142 | - protected function run($argument) { |
|
143 | - $target = $argument['url']; |
|
144 | - $created = isset($argument['created']) ? (int)$argument['created'] : time(); |
|
145 | - $currentTime = time(); |
|
146 | - $source = $this->urlGenerator->getAbsoluteURL('/'); |
|
147 | - $source = rtrim($source, '/'); |
|
148 | - $token = $argument['token']; |
|
149 | - |
|
150 | - // kill job after 30 days of trying |
|
151 | - $deadline = $currentTime - $this->maxLifespan; |
|
152 | - if ($created < $deadline) { |
|
153 | - $this->retainJob = false; |
|
154 | - $this->trustedServers->setServerStatus($target,TrustedServers::STATUS_FAILURE); |
|
155 | - return; |
|
156 | - } |
|
157 | - |
|
158 | - $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); |
|
159 | - $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
|
160 | - |
|
161 | - // make sure that we have a well formated url |
|
162 | - $url = rtrim($target, '/') . '/' . trim($endPoint, '/') . $this->format; |
|
163 | - |
|
164 | - $result = null; |
|
165 | - try { |
|
166 | - $result = $this->httpClient->get( |
|
167 | - $url, |
|
168 | - [ |
|
169 | - 'query' => |
|
170 | - [ |
|
171 | - 'url' => $source, |
|
172 | - 'token' => $token |
|
173 | - ], |
|
174 | - 'timeout' => 3, |
|
175 | - 'connect_timeout' => 3, |
|
176 | - ] |
|
177 | - ); |
|
178 | - |
|
179 | - $status = $result->getStatusCode(); |
|
180 | - |
|
181 | - } catch (ClientException $e) { |
|
182 | - $status = $e->getCode(); |
|
183 | - if ($status === Http::STATUS_FORBIDDEN) { |
|
184 | - $this->logger->info($target . ' refused to exchange a shared secret with you.', ['app' => 'federation']); |
|
185 | - } else { |
|
186 | - $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
|
187 | - } |
|
188 | - } catch (\Exception $e) { |
|
189 | - $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
|
190 | - $this->logger->logException($e, ['app' => 'federation']); |
|
191 | - } |
|
192 | - |
|
193 | - // if we received a unexpected response we try again later |
|
194 | - if ( |
|
195 | - $status !== Http::STATUS_OK |
|
196 | - && $status !== Http::STATUS_FORBIDDEN |
|
197 | - ) { |
|
198 | - $this->retainJob = true; |
|
199 | - } else { |
|
200 | - // reset token if we received a valid response |
|
201 | - $this->dbHandler->addToken($target, ''); |
|
202 | - } |
|
203 | - |
|
204 | - if ($status === Http::STATUS_OK && $result instanceof IResponse) { |
|
205 | - $body = $result->getBody(); |
|
206 | - $result = json_decode($body, true); |
|
207 | - if (isset($result['ocs']['data']['sharedSecret'])) { |
|
208 | - $this->trustedServers->addSharedSecret( |
|
209 | - $target, |
|
210 | - $result['ocs']['data']['sharedSecret'] |
|
211 | - ); |
|
212 | - } else { |
|
213 | - $this->logger->error( |
|
214 | - 'remote server "' . $target . '"" does not return a valid shared secret', |
|
215 | - ['app' => 'federation'] |
|
216 | - ); |
|
217 | - $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
|
218 | - } |
|
219 | - } |
|
220 | - |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * re-add background job |
|
225 | - * |
|
226 | - * @param IJobList $jobList |
|
227 | - * @param array $argument |
|
228 | - */ |
|
229 | - protected function reAddJob(IJobList $jobList, array $argument) { |
|
230 | - $url = $argument['url']; |
|
231 | - $created = isset($argument['created']) ? (int)$argument['created'] : time(); |
|
232 | - $token = $argument['token']; |
|
233 | - $this->jobList->add( |
|
234 | - GetSharedSecret::class, |
|
235 | - [ |
|
236 | - 'url' => $url, |
|
237 | - 'token' => $token, |
|
238 | - 'created' => $created |
|
239 | - ] |
|
240 | - ); |
|
241 | - } |
|
52 | + /** @var IClient */ |
|
53 | + private $httpClient; |
|
54 | + |
|
55 | + /** @var IJobList */ |
|
56 | + private $jobList; |
|
57 | + |
|
58 | + /** @var IURLGenerator */ |
|
59 | + private $urlGenerator; |
|
60 | + |
|
61 | + /** @var TrustedServers */ |
|
62 | + private $trustedServers; |
|
63 | + |
|
64 | + /** @var DbHandler */ |
|
65 | + private $dbHandler; |
|
66 | + |
|
67 | + /** @var IDiscoveryService */ |
|
68 | + private $ocsDiscoveryService; |
|
69 | + |
|
70 | + /** @var ILogger */ |
|
71 | + private $logger; |
|
72 | + |
|
73 | + /** @var bool */ |
|
74 | + protected $retainJob = false; |
|
75 | + |
|
76 | + private $format = '?format=json'; |
|
77 | + |
|
78 | + private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/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 ILogger $logger |
|
91 | + * @param DbHandler $dbHandler |
|
92 | + * @param IDiscoveryService $ocsDiscoveryService |
|
93 | + */ |
|
94 | + public function __construct( |
|
95 | + IClientService $httpClientService, |
|
96 | + IURLGenerator $urlGenerator, |
|
97 | + IJobList $jobList, |
|
98 | + TrustedServers $trustedServers, |
|
99 | + ILogger $logger, |
|
100 | + DbHandler $dbHandler, |
|
101 | + IDiscoveryService $ocsDiscoveryService |
|
102 | + ) { |
|
103 | + $this->logger = $logger; |
|
104 | + $this->httpClient = $httpClientService->newClient(); |
|
105 | + $this->jobList = $jobList; |
|
106 | + $this->urlGenerator = $urlGenerator; |
|
107 | + $this->dbHandler = $dbHandler; |
|
108 | + $this->ocsDiscoveryService = $ocsDiscoveryService; |
|
109 | + $this->trustedServers = $trustedServers; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * run the job, then remove it from the joblist |
|
114 | + * |
|
115 | + * @param JobList $jobList |
|
116 | + * @param ILogger $logger |
|
117 | + */ |
|
118 | + public function execute($jobList, ILogger $logger = null) { |
|
119 | + $target = $this->argument['url']; |
|
120 | + // only execute if target is still in the list of trusted domains |
|
121 | + if ($this->trustedServers->isTrustedServer($target)) { |
|
122 | + $this->parentExecute($jobList, $logger); |
|
123 | + } |
|
124 | + |
|
125 | + $jobList->remove($this, $this->argument); |
|
126 | + |
|
127 | + if ($this->retainJob) { |
|
128 | + $this->reAddJob($jobList, $this->argument); |
|
129 | + } |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * call execute() method of parent |
|
134 | + * |
|
135 | + * @param JobList $jobList |
|
136 | + * @param ILogger $logger |
|
137 | + */ |
|
138 | + protected function parentExecute($jobList, $logger = null) { |
|
139 | + parent::execute($jobList, $logger); |
|
140 | + } |
|
141 | + |
|
142 | + protected function run($argument) { |
|
143 | + $target = $argument['url']; |
|
144 | + $created = isset($argument['created']) ? (int)$argument['created'] : time(); |
|
145 | + $currentTime = time(); |
|
146 | + $source = $this->urlGenerator->getAbsoluteURL('/'); |
|
147 | + $source = rtrim($source, '/'); |
|
148 | + $token = $argument['token']; |
|
149 | + |
|
150 | + // kill job after 30 days of trying |
|
151 | + $deadline = $currentTime - $this->maxLifespan; |
|
152 | + if ($created < $deadline) { |
|
153 | + $this->retainJob = false; |
|
154 | + $this->trustedServers->setServerStatus($target,TrustedServers::STATUS_FAILURE); |
|
155 | + return; |
|
156 | + } |
|
157 | + |
|
158 | + $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); |
|
159 | + $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
|
160 | + |
|
161 | + // make sure that we have a well formated url |
|
162 | + $url = rtrim($target, '/') . '/' . trim($endPoint, '/') . $this->format; |
|
163 | + |
|
164 | + $result = null; |
|
165 | + try { |
|
166 | + $result = $this->httpClient->get( |
|
167 | + $url, |
|
168 | + [ |
|
169 | + 'query' => |
|
170 | + [ |
|
171 | + 'url' => $source, |
|
172 | + 'token' => $token |
|
173 | + ], |
|
174 | + 'timeout' => 3, |
|
175 | + 'connect_timeout' => 3, |
|
176 | + ] |
|
177 | + ); |
|
178 | + |
|
179 | + $status = $result->getStatusCode(); |
|
180 | + |
|
181 | + } catch (ClientException $e) { |
|
182 | + $status = $e->getCode(); |
|
183 | + if ($status === Http::STATUS_FORBIDDEN) { |
|
184 | + $this->logger->info($target . ' refused to exchange a shared secret with you.', ['app' => 'federation']); |
|
185 | + } else { |
|
186 | + $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
|
187 | + } |
|
188 | + } catch (\Exception $e) { |
|
189 | + $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
|
190 | + $this->logger->logException($e, ['app' => 'federation']); |
|
191 | + } |
|
192 | + |
|
193 | + // if we received a unexpected response we try again later |
|
194 | + if ( |
|
195 | + $status !== Http::STATUS_OK |
|
196 | + && $status !== Http::STATUS_FORBIDDEN |
|
197 | + ) { |
|
198 | + $this->retainJob = true; |
|
199 | + } else { |
|
200 | + // reset token if we received a valid response |
|
201 | + $this->dbHandler->addToken($target, ''); |
|
202 | + } |
|
203 | + |
|
204 | + if ($status === Http::STATUS_OK && $result instanceof IResponse) { |
|
205 | + $body = $result->getBody(); |
|
206 | + $result = json_decode($body, true); |
|
207 | + if (isset($result['ocs']['data']['sharedSecret'])) { |
|
208 | + $this->trustedServers->addSharedSecret( |
|
209 | + $target, |
|
210 | + $result['ocs']['data']['sharedSecret'] |
|
211 | + ); |
|
212 | + } else { |
|
213 | + $this->logger->error( |
|
214 | + 'remote server "' . $target . '"" does not return a valid shared secret', |
|
215 | + ['app' => 'federation'] |
|
216 | + ); |
|
217 | + $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
|
218 | + } |
|
219 | + } |
|
220 | + |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * re-add background job |
|
225 | + * |
|
226 | + * @param IJobList $jobList |
|
227 | + * @param array $argument |
|
228 | + */ |
|
229 | + protected function reAddJob(IJobList $jobList, array $argument) { |
|
230 | + $url = $argument['url']; |
|
231 | + $created = isset($argument['created']) ? (int)$argument['created'] : time(); |
|
232 | + $token = $argument['token']; |
|
233 | + $this->jobList->add( |
|
234 | + GetSharedSecret::class, |
|
235 | + [ |
|
236 | + 'url' => $url, |
|
237 | + 'token' => $token, |
|
238 | + 'created' => $created |
|
239 | + ] |
|
240 | + ); |
|
241 | + } |
|
242 | 242 | } |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | * |
48 | 48 | * @package OCA\Federation\Backgroundjob |
49 | 49 | */ |
50 | -class GetSharedSecret extends Job{ |
|
50 | +class GetSharedSecret extends Job { |
|
51 | 51 | |
52 | 52 | /** @var IClient */ |
53 | 53 | private $httpClient; |
@@ -141,7 +141,7 @@ discard block |
||
141 | 141 | |
142 | 142 | protected function run($argument) { |
143 | 143 | $target = $argument['url']; |
144 | - $created = isset($argument['created']) ? (int)$argument['created'] : time(); |
|
144 | + $created = isset($argument['created']) ? (int) $argument['created'] : time(); |
|
145 | 145 | $currentTime = time(); |
146 | 146 | $source = $this->urlGenerator->getAbsoluteURL('/'); |
147 | 147 | $source = rtrim($source, '/'); |
@@ -151,7 +151,7 @@ discard block |
||
151 | 151 | $deadline = $currentTime - $this->maxLifespan; |
152 | 152 | if ($created < $deadline) { |
153 | 153 | $this->retainJob = false; |
154 | - $this->trustedServers->setServerStatus($target,TrustedServers::STATUS_FAILURE); |
|
154 | + $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
|
155 | 155 | return; |
156 | 156 | } |
157 | 157 | |
@@ -159,7 +159,7 @@ discard block |
||
159 | 159 | $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
160 | 160 | |
161 | 161 | // make sure that we have a well formated url |
162 | - $url = rtrim($target, '/') . '/' . trim($endPoint, '/') . $this->format; |
|
162 | + $url = rtrim($target, '/').'/'.trim($endPoint, '/').$this->format; |
|
163 | 163 | |
164 | 164 | $result = null; |
165 | 165 | try { |
@@ -181,9 +181,9 @@ discard block |
||
181 | 181 | } catch (ClientException $e) { |
182 | 182 | $status = $e->getCode(); |
183 | 183 | if ($status === Http::STATUS_FORBIDDEN) { |
184 | - $this->logger->info($target . ' refused to exchange a shared secret with you.', ['app' => 'federation']); |
|
184 | + $this->logger->info($target.' refused to exchange a shared secret with you.', ['app' => 'federation']); |
|
185 | 185 | } else { |
186 | - $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
|
186 | + $this->logger->info($target.' responded with a '.$status.' containing: '.$e->getMessage(), ['app' => 'federation']); |
|
187 | 187 | } |
188 | 188 | } catch (\Exception $e) { |
189 | 189 | $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
@@ -196,7 +196,7 @@ discard block |
||
196 | 196 | && $status !== Http::STATUS_FORBIDDEN |
197 | 197 | ) { |
198 | 198 | $this->retainJob = true; |
199 | - } else { |
|
199 | + } else { |
|
200 | 200 | // reset token if we received a valid response |
201 | 201 | $this->dbHandler->addToken($target, ''); |
202 | 202 | } |
@@ -211,7 +211,7 @@ discard block |
||
211 | 211 | ); |
212 | 212 | } else { |
213 | 213 | $this->logger->error( |
214 | - 'remote server "' . $target . '"" does not return a valid shared secret', |
|
214 | + 'remote server "'.$target.'"" does not return a valid shared secret', |
|
215 | 215 | ['app' => 'federation'] |
216 | 216 | ); |
217 | 217 | $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
@@ -228,7 +228,7 @@ discard block |
||
228 | 228 | */ |
229 | 229 | protected function reAddJob(IJobList $jobList, array $argument) { |
230 | 230 | $url = $argument['url']; |
231 | - $created = isset($argument['created']) ? (int)$argument['created'] : time(); |
|
231 | + $created = isset($argument['created']) ? (int) $argument['created'] : time(); |
|
232 | 232 | $token = $argument['token']; |
233 | 233 | $this->jobList->add( |
234 | 234 | GetSharedSecret::class, |
@@ -49,181 +49,181 @@ |
||
49 | 49 | */ |
50 | 50 | class RequestSharedSecret extends Job { |
51 | 51 | |
52 | - /** @var IClient */ |
|
53 | - private $httpClient; |
|
54 | - |
|
55 | - /** @var IJobList */ |
|
56 | - private $jobList; |
|
57 | - |
|
58 | - /** @var IURLGenerator */ |
|
59 | - private $urlGenerator; |
|
60 | - |
|
61 | - /** @var DbHandler */ |
|
62 | - private $dbHandler; |
|
63 | - |
|
64 | - /** @var TrustedServers */ |
|
65 | - private $trustedServers; |
|
66 | - |
|
67 | - /** @var IDiscoveryService */ |
|
68 | - private $ocsDiscoveryService; |
|
69 | - |
|
70 | - /** @var ILogger */ |
|
71 | - private $logger; |
|
72 | - |
|
73 | - /** @var bool */ |
|
74 | - protected $retainJob = false; |
|
75 | - |
|
76 | - private $format = '?format=json'; |
|
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 DbHandler $dbHandler |
|
91 | - * @param IDiscoveryService $ocsDiscoveryService |
|
92 | - * @param ILogger $logger |
|
93 | - */ |
|
94 | - public function __construct( |
|
95 | - IClientService $httpClientService, |
|
96 | - IURLGenerator $urlGenerator, |
|
97 | - IJobList $jobList, |
|
98 | - TrustedServers $trustedServers, |
|
99 | - DbHandler $dbHandler, |
|
100 | - IDiscoveryService $ocsDiscoveryService, |
|
101 | - ILogger $logger |
|
102 | - ) { |
|
103 | - $this->httpClient = $httpClientService->newClient(); |
|
104 | - $this->jobList = $jobList; |
|
105 | - $this->urlGenerator = $urlGenerator; |
|
106 | - $this->dbHandler = $dbHandler; |
|
107 | - $this->logger = $logger; |
|
108 | - $this->ocsDiscoveryService = $ocsDiscoveryService; |
|
109 | - $this->trustedServers = $trustedServers; |
|
110 | - } |
|
111 | - |
|
112 | - |
|
113 | - /** |
|
114 | - * run the job, then remove it from the joblist |
|
115 | - * |
|
116 | - * @param JobList $jobList |
|
117 | - * @param ILogger $logger |
|
118 | - */ |
|
119 | - public function execute($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($jobList, $this->argument); |
|
130 | - } |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * call execute() method of parent |
|
135 | - * |
|
136 | - * @param JobList $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 | - |
|
145 | - $target = $argument['url']; |
|
146 | - $created = isset($argument['created']) ? (int)$argument['created'] : time(); |
|
147 | - $currentTime = time(); |
|
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 formated url |
|
164 | - $url = rtrim($target, '/') . '/' . trim($endPoint, '/') . $this->format; |
|
165 | - |
|
166 | - try { |
|
167 | - $result = $this->httpClient->post( |
|
168 | - $url, |
|
169 | - [ |
|
170 | - 'body' => [ |
|
171 | - 'url' => $source, |
|
172 | - 'token' => $token, |
|
173 | - ], |
|
174 | - 'timeout' => 3, |
|
175 | - 'connect_timeout' => 3, |
|
176 | - ] |
|
177 | - ); |
|
178 | - |
|
179 | - $status = $result->getStatusCode(); |
|
180 | - |
|
181 | - } catch (ClientException $e) { |
|
182 | - $status = $e->getCode(); |
|
183 | - if ($status === Http::STATUS_FORBIDDEN) { |
|
184 | - $this->logger->info($target . ' refused to ask for a shared secret.', ['app' => 'federation']); |
|
185 | - } else { |
|
186 | - $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
|
187 | - } |
|
188 | - } catch (\Exception $e) { |
|
189 | - $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
|
190 | - $this->logger->logException($e, ['app' => 'federation']); |
|
191 | - } |
|
192 | - |
|
193 | - // if we received a unexpected response we try again later |
|
194 | - if ( |
|
195 | - $status !== Http::STATUS_OK |
|
196 | - && $status !== Http::STATUS_FORBIDDEN |
|
197 | - ) { |
|
198 | - $this->retainJob = true; |
|
199 | - } |
|
200 | - |
|
201 | - if ($status === Http::STATUS_FORBIDDEN) { |
|
202 | - // clear token if remote server refuses to ask for shared secret |
|
203 | - $this->dbHandler->addToken($target, ''); |
|
204 | - } |
|
205 | - |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * re-add background job |
|
210 | - * |
|
211 | - * @param IJobList $jobList |
|
212 | - * @param array $argument |
|
213 | - */ |
|
214 | - protected function reAddJob(IJobList $jobList, array $argument) { |
|
215 | - |
|
216 | - $url = $argument['url']; |
|
217 | - $created = isset($argument['created']) ? (int)$argument['created'] : time(); |
|
218 | - $token = $argument['token']; |
|
219 | - |
|
220 | - $jobList->add( |
|
221 | - RequestSharedSecret::class, |
|
222 | - [ |
|
223 | - 'url' => $url, |
|
224 | - 'token' => $token, |
|
225 | - 'created' => $created |
|
226 | - ] |
|
227 | - ); |
|
228 | - } |
|
52 | + /** @var IClient */ |
|
53 | + private $httpClient; |
|
54 | + |
|
55 | + /** @var IJobList */ |
|
56 | + private $jobList; |
|
57 | + |
|
58 | + /** @var IURLGenerator */ |
|
59 | + private $urlGenerator; |
|
60 | + |
|
61 | + /** @var DbHandler */ |
|
62 | + private $dbHandler; |
|
63 | + |
|
64 | + /** @var TrustedServers */ |
|
65 | + private $trustedServers; |
|
66 | + |
|
67 | + /** @var IDiscoveryService */ |
|
68 | + private $ocsDiscoveryService; |
|
69 | + |
|
70 | + /** @var ILogger */ |
|
71 | + private $logger; |
|
72 | + |
|
73 | + /** @var bool */ |
|
74 | + protected $retainJob = false; |
|
75 | + |
|
76 | + private $format = '?format=json'; |
|
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 DbHandler $dbHandler |
|
91 | + * @param IDiscoveryService $ocsDiscoveryService |
|
92 | + * @param ILogger $logger |
|
93 | + */ |
|
94 | + public function __construct( |
|
95 | + IClientService $httpClientService, |
|
96 | + IURLGenerator $urlGenerator, |
|
97 | + IJobList $jobList, |
|
98 | + TrustedServers $trustedServers, |
|
99 | + DbHandler $dbHandler, |
|
100 | + IDiscoveryService $ocsDiscoveryService, |
|
101 | + ILogger $logger |
|
102 | + ) { |
|
103 | + $this->httpClient = $httpClientService->newClient(); |
|
104 | + $this->jobList = $jobList; |
|
105 | + $this->urlGenerator = $urlGenerator; |
|
106 | + $this->dbHandler = $dbHandler; |
|
107 | + $this->logger = $logger; |
|
108 | + $this->ocsDiscoveryService = $ocsDiscoveryService; |
|
109 | + $this->trustedServers = $trustedServers; |
|
110 | + } |
|
111 | + |
|
112 | + |
|
113 | + /** |
|
114 | + * run the job, then remove it from the joblist |
|
115 | + * |
|
116 | + * @param JobList $jobList |
|
117 | + * @param ILogger $logger |
|
118 | + */ |
|
119 | + public function execute($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($jobList, $this->argument); |
|
130 | + } |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * call execute() method of parent |
|
135 | + * |
|
136 | + * @param JobList $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 | + |
|
145 | + $target = $argument['url']; |
|
146 | + $created = isset($argument['created']) ? (int)$argument['created'] : time(); |
|
147 | + $currentTime = time(); |
|
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 formated url |
|
164 | + $url = rtrim($target, '/') . '/' . trim($endPoint, '/') . $this->format; |
|
165 | + |
|
166 | + try { |
|
167 | + $result = $this->httpClient->post( |
|
168 | + $url, |
|
169 | + [ |
|
170 | + 'body' => [ |
|
171 | + 'url' => $source, |
|
172 | + 'token' => $token, |
|
173 | + ], |
|
174 | + 'timeout' => 3, |
|
175 | + 'connect_timeout' => 3, |
|
176 | + ] |
|
177 | + ); |
|
178 | + |
|
179 | + $status = $result->getStatusCode(); |
|
180 | + |
|
181 | + } catch (ClientException $e) { |
|
182 | + $status = $e->getCode(); |
|
183 | + if ($status === Http::STATUS_FORBIDDEN) { |
|
184 | + $this->logger->info($target . ' refused to ask for a shared secret.', ['app' => 'federation']); |
|
185 | + } else { |
|
186 | + $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
|
187 | + } |
|
188 | + } catch (\Exception $e) { |
|
189 | + $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
|
190 | + $this->logger->logException($e, ['app' => 'federation']); |
|
191 | + } |
|
192 | + |
|
193 | + // if we received a unexpected response we try again later |
|
194 | + if ( |
|
195 | + $status !== Http::STATUS_OK |
|
196 | + && $status !== Http::STATUS_FORBIDDEN |
|
197 | + ) { |
|
198 | + $this->retainJob = true; |
|
199 | + } |
|
200 | + |
|
201 | + if ($status === Http::STATUS_FORBIDDEN) { |
|
202 | + // clear token if remote server refuses to ask for shared secret |
|
203 | + $this->dbHandler->addToken($target, ''); |
|
204 | + } |
|
205 | + |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * re-add background job |
|
210 | + * |
|
211 | + * @param IJobList $jobList |
|
212 | + * @param array $argument |
|
213 | + */ |
|
214 | + protected function reAddJob(IJobList $jobList, array $argument) { |
|
215 | + |
|
216 | + $url = $argument['url']; |
|
217 | + $created = isset($argument['created']) ? (int)$argument['created'] : time(); |
|
218 | + $token = $argument['token']; |
|
219 | + |
|
220 | + $jobList->add( |
|
221 | + RequestSharedSecret::class, |
|
222 | + [ |
|
223 | + 'url' => $url, |
|
224 | + 'token' => $token, |
|
225 | + 'created' => $created |
|
226 | + ] |
|
227 | + ); |
|
228 | + } |
|
229 | 229 | } |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | protected function run($argument) { |
144 | 144 | |
145 | 145 | $target = $argument['url']; |
146 | - $created = isset($argument['created']) ? (int)$argument['created'] : time(); |
|
146 | + $created = isset($argument['created']) ? (int) $argument['created'] : time(); |
|
147 | 147 | $currentTime = time(); |
148 | 148 | $source = $this->urlGenerator->getAbsoluteURL('/'); |
149 | 149 | $source = rtrim($source, '/'); |
@@ -161,7 +161,7 @@ discard block |
||
161 | 161 | $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
162 | 162 | |
163 | 163 | // make sure that we have a well formated url |
164 | - $url = rtrim($target, '/') . '/' . trim($endPoint, '/') . $this->format; |
|
164 | + $url = rtrim($target, '/').'/'.trim($endPoint, '/').$this->format; |
|
165 | 165 | |
166 | 166 | try { |
167 | 167 | $result = $this->httpClient->post( |
@@ -181,9 +181,9 @@ discard block |
||
181 | 181 | } catch (ClientException $e) { |
182 | 182 | $status = $e->getCode(); |
183 | 183 | if ($status === Http::STATUS_FORBIDDEN) { |
184 | - $this->logger->info($target . ' refused to ask for a shared secret.', ['app' => 'federation']); |
|
184 | + $this->logger->info($target.' refused to ask for a shared secret.', ['app' => 'federation']); |
|
185 | 185 | } else { |
186 | - $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
|
186 | + $this->logger->info($target.' responded with a '.$status.' containing: '.$e->getMessage(), ['app' => 'federation']); |
|
187 | 187 | } |
188 | 188 | } catch (\Exception $e) { |
189 | 189 | $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
@@ -214,7 +214,7 @@ discard block |
||
214 | 214 | protected function reAddJob(IJobList $jobList, array $argument) { |
215 | 215 | |
216 | 216 | $url = $argument['url']; |
217 | - $created = isset($argument['created']) ? (int)$argument['created'] : time(); |
|
217 | + $created = isset($argument['created']) ? (int) $argument['created'] : time(); |
|
218 | 218 | $token = $argument['token']; |
219 | 219 | |
220 | 220 | $jobList->add( |
@@ -38,246 +38,246 @@ |
||
38 | 38 | |
39 | 39 | class TrustedServers { |
40 | 40 | |
41 | - /** after a user list was exchanged at least once successfully */ |
|
42 | - const STATUS_OK = 1; |
|
43 | - /** waiting for shared secret or initial user list exchange */ |
|
44 | - const STATUS_PENDING = 2; |
|
45 | - /** something went wrong, misconfigured server, software bug,... user interaction needed */ |
|
46 | - const STATUS_FAILURE = 3; |
|
47 | - /** remote server revoked access */ |
|
48 | - const STATUS_ACCESS_REVOKED = 4; |
|
49 | - |
|
50 | - /** @var dbHandler */ |
|
51 | - private $dbHandler; |
|
52 | - |
|
53 | - /** @var IClientService */ |
|
54 | - private $httpClientService; |
|
55 | - |
|
56 | - /** @var ILogger */ |
|
57 | - private $logger; |
|
58 | - |
|
59 | - /** @var IJobList */ |
|
60 | - private $jobList; |
|
61 | - |
|
62 | - /** @var ISecureRandom */ |
|
63 | - private $secureRandom; |
|
64 | - |
|
65 | - /** @var IConfig */ |
|
66 | - private $config; |
|
67 | - |
|
68 | - /** @var EventDispatcherInterface */ |
|
69 | - private $dispatcher; |
|
70 | - |
|
71 | - /** |
|
72 | - * @param DbHandler $dbHandler |
|
73 | - * @param IClientService $httpClientService |
|
74 | - * @param ILogger $logger |
|
75 | - * @param IJobList $jobList |
|
76 | - * @param ISecureRandom $secureRandom |
|
77 | - * @param IConfig $config |
|
78 | - * @param EventDispatcherInterface $dispatcher |
|
79 | - */ |
|
80 | - public function __construct( |
|
81 | - DbHandler $dbHandler, |
|
82 | - IClientService $httpClientService, |
|
83 | - ILogger $logger, |
|
84 | - IJobList $jobList, |
|
85 | - ISecureRandom $secureRandom, |
|
86 | - IConfig $config, |
|
87 | - EventDispatcherInterface $dispatcher |
|
88 | - ) { |
|
89 | - $this->dbHandler = $dbHandler; |
|
90 | - $this->httpClientService = $httpClientService; |
|
91 | - $this->logger = $logger; |
|
92 | - $this->jobList = $jobList; |
|
93 | - $this->secureRandom = $secureRandom; |
|
94 | - $this->config = $config; |
|
95 | - $this->dispatcher = $dispatcher; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * add server to the list of trusted servers |
|
100 | - * |
|
101 | - * @param $url |
|
102 | - * @return int server id |
|
103 | - */ |
|
104 | - public function addServer($url) { |
|
105 | - $url = $this->updateProtocol($url); |
|
106 | - $result = $this->dbHandler->addServer($url); |
|
107 | - if ($result) { |
|
108 | - $token = $this->secureRandom->generate(16); |
|
109 | - $this->dbHandler->addToken($url, $token); |
|
110 | - $this->jobList->add( |
|
111 | - 'OCA\Federation\BackgroundJob\RequestSharedSecret', |
|
112 | - [ |
|
113 | - 'url' => $url, |
|
114 | - 'token' => $token, |
|
115 | - 'created' => $this->getTimestamp() |
|
116 | - ] |
|
117 | - ); |
|
118 | - } |
|
119 | - |
|
120 | - return $result; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * enable/disable to automatically add servers to the list of trusted servers |
|
125 | - * once a federated share was created and accepted successfully |
|
126 | - * |
|
127 | - * @param bool $status |
|
128 | - */ |
|
129 | - public function setAutoAddServers($status) { |
|
130 | - $value = $status ? '1' : '0'; |
|
131 | - $this->config->setAppValue('federation', 'autoAddServers', $value); |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * return if we automatically add servers to the list of trusted servers |
|
136 | - * once a federated share was created and accepted successfully |
|
137 | - * |
|
138 | - * @return bool |
|
139 | - */ |
|
140 | - public function getAutoAddServers() { |
|
141 | - $value = $this->config->getAppValue('federation', 'autoAddServers', '0'); |
|
142 | - return $value === '1'; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * get shared secret for the given server |
|
147 | - * |
|
148 | - * @param string $url |
|
149 | - * @return string |
|
150 | - */ |
|
151 | - public function getSharedSecret($url) { |
|
152 | - return $this->dbHandler->getSharedSecret($url); |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * add shared secret for the given server |
|
157 | - * |
|
158 | - * @param string $url |
|
159 | - * @param $sharedSecret |
|
160 | - */ |
|
161 | - public function addSharedSecret($url, $sharedSecret) { |
|
162 | - $this->dbHandler->addSharedSecret($url, $sharedSecret); |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * remove server from the list of trusted servers |
|
167 | - * |
|
168 | - * @param int $id |
|
169 | - */ |
|
170 | - public function removeServer($id) { |
|
171 | - $server = $this->dbHandler->getServerById($id); |
|
172 | - $this->dbHandler->removeServer($id); |
|
173 | - $event = new GenericEvent($server['url_hash']); |
|
174 | - $this->dispatcher->dispatch('OCP\Federation\TrustedServerEvent::remove', $event); |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * get all trusted servers |
|
179 | - * |
|
180 | - * @return array |
|
181 | - */ |
|
182 | - public function getServers() { |
|
183 | - return $this->dbHandler->getAllServer(); |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * check if given server is a trusted Nextcloud server |
|
188 | - * |
|
189 | - * @param string $url |
|
190 | - * @return bool |
|
191 | - */ |
|
192 | - public function isTrustedServer($url) { |
|
193 | - return $this->dbHandler->serverExists($url); |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * set server status |
|
198 | - * |
|
199 | - * @param string $url |
|
200 | - * @param int $status |
|
201 | - */ |
|
202 | - public function setServerStatus($url, $status) { |
|
203 | - $this->dbHandler->setServerStatus($url, $status); |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * @param string $url |
|
208 | - * @return int |
|
209 | - */ |
|
210 | - public function getServerStatus($url) { |
|
211 | - return $this->dbHandler->getServerStatus($url); |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * check if URL point to a ownCloud/Nextcloud server |
|
216 | - * |
|
217 | - * @param string $url |
|
218 | - * @return bool |
|
219 | - */ |
|
220 | - public function isOwnCloudServer($url) { |
|
221 | - $isValidOwnCloud = false; |
|
222 | - $client = $this->httpClientService->newClient(); |
|
223 | - try { |
|
224 | - $result = $client->get( |
|
225 | - $url . '/status.php', |
|
226 | - [ |
|
227 | - 'timeout' => 3, |
|
228 | - 'connect_timeout' => 3, |
|
229 | - ] |
|
230 | - ); |
|
231 | - if ($result->getStatusCode() === Http::STATUS_OK) { |
|
232 | - $isValidOwnCloud = $this->checkOwnCloudVersion($result->getBody()); |
|
233 | - |
|
234 | - } |
|
235 | - } catch (\Exception $e) { |
|
236 | - $this->logger->debug('No Nextcloud server: ' . $e->getMessage()); |
|
237 | - return false; |
|
238 | - } |
|
239 | - |
|
240 | - return $isValidOwnCloud; |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * check if ownCloud version is >= 9.0 |
|
245 | - * |
|
246 | - * @param $status |
|
247 | - * @return bool |
|
248 | - * @throws HintException |
|
249 | - */ |
|
250 | - protected function checkOwnCloudVersion($status) { |
|
251 | - $decoded = json_decode($status, true); |
|
252 | - if (!empty($decoded) && isset($decoded['version'])) { |
|
253 | - if (!version_compare($decoded['version'], '9.0.0', '>=')) { |
|
254 | - throw new HintException('Remote server version is too low. 9.0 is required.'); |
|
255 | - } |
|
256 | - return true; |
|
257 | - } |
|
258 | - return false; |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * check if the URL contain a protocol, if not add https |
|
263 | - * |
|
264 | - * @param string $url |
|
265 | - * @return string |
|
266 | - */ |
|
267 | - protected function updateProtocol($url) { |
|
268 | - if ( |
|
269 | - strpos($url, 'https://') === 0 |
|
270 | - || strpos($url, 'http://') === 0 |
|
271 | - ) { |
|
272 | - |
|
273 | - return $url; |
|
274 | - |
|
275 | - } |
|
276 | - |
|
277 | - return 'https://' . $url; |
|
278 | - } |
|
279 | - |
|
280 | - protected function getTimestamp() { |
|
281 | - return time(); |
|
282 | - } |
|
41 | + /** after a user list was exchanged at least once successfully */ |
|
42 | + const STATUS_OK = 1; |
|
43 | + /** waiting for shared secret or initial user list exchange */ |
|
44 | + const STATUS_PENDING = 2; |
|
45 | + /** something went wrong, misconfigured server, software bug,... user interaction needed */ |
|
46 | + const STATUS_FAILURE = 3; |
|
47 | + /** remote server revoked access */ |
|
48 | + const STATUS_ACCESS_REVOKED = 4; |
|
49 | + |
|
50 | + /** @var dbHandler */ |
|
51 | + private $dbHandler; |
|
52 | + |
|
53 | + /** @var IClientService */ |
|
54 | + private $httpClientService; |
|
55 | + |
|
56 | + /** @var ILogger */ |
|
57 | + private $logger; |
|
58 | + |
|
59 | + /** @var IJobList */ |
|
60 | + private $jobList; |
|
61 | + |
|
62 | + /** @var ISecureRandom */ |
|
63 | + private $secureRandom; |
|
64 | + |
|
65 | + /** @var IConfig */ |
|
66 | + private $config; |
|
67 | + |
|
68 | + /** @var EventDispatcherInterface */ |
|
69 | + private $dispatcher; |
|
70 | + |
|
71 | + /** |
|
72 | + * @param DbHandler $dbHandler |
|
73 | + * @param IClientService $httpClientService |
|
74 | + * @param ILogger $logger |
|
75 | + * @param IJobList $jobList |
|
76 | + * @param ISecureRandom $secureRandom |
|
77 | + * @param IConfig $config |
|
78 | + * @param EventDispatcherInterface $dispatcher |
|
79 | + */ |
|
80 | + public function __construct( |
|
81 | + DbHandler $dbHandler, |
|
82 | + IClientService $httpClientService, |
|
83 | + ILogger $logger, |
|
84 | + IJobList $jobList, |
|
85 | + ISecureRandom $secureRandom, |
|
86 | + IConfig $config, |
|
87 | + EventDispatcherInterface $dispatcher |
|
88 | + ) { |
|
89 | + $this->dbHandler = $dbHandler; |
|
90 | + $this->httpClientService = $httpClientService; |
|
91 | + $this->logger = $logger; |
|
92 | + $this->jobList = $jobList; |
|
93 | + $this->secureRandom = $secureRandom; |
|
94 | + $this->config = $config; |
|
95 | + $this->dispatcher = $dispatcher; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * add server to the list of trusted servers |
|
100 | + * |
|
101 | + * @param $url |
|
102 | + * @return int server id |
|
103 | + */ |
|
104 | + public function addServer($url) { |
|
105 | + $url = $this->updateProtocol($url); |
|
106 | + $result = $this->dbHandler->addServer($url); |
|
107 | + if ($result) { |
|
108 | + $token = $this->secureRandom->generate(16); |
|
109 | + $this->dbHandler->addToken($url, $token); |
|
110 | + $this->jobList->add( |
|
111 | + 'OCA\Federation\BackgroundJob\RequestSharedSecret', |
|
112 | + [ |
|
113 | + 'url' => $url, |
|
114 | + 'token' => $token, |
|
115 | + 'created' => $this->getTimestamp() |
|
116 | + ] |
|
117 | + ); |
|
118 | + } |
|
119 | + |
|
120 | + return $result; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * enable/disable to automatically add servers to the list of trusted servers |
|
125 | + * once a federated share was created and accepted successfully |
|
126 | + * |
|
127 | + * @param bool $status |
|
128 | + */ |
|
129 | + public function setAutoAddServers($status) { |
|
130 | + $value = $status ? '1' : '0'; |
|
131 | + $this->config->setAppValue('federation', 'autoAddServers', $value); |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * return if we automatically add servers to the list of trusted servers |
|
136 | + * once a federated share was created and accepted successfully |
|
137 | + * |
|
138 | + * @return bool |
|
139 | + */ |
|
140 | + public function getAutoAddServers() { |
|
141 | + $value = $this->config->getAppValue('federation', 'autoAddServers', '0'); |
|
142 | + return $value === '1'; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * get shared secret for the given server |
|
147 | + * |
|
148 | + * @param string $url |
|
149 | + * @return string |
|
150 | + */ |
|
151 | + public function getSharedSecret($url) { |
|
152 | + return $this->dbHandler->getSharedSecret($url); |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * add shared secret for the given server |
|
157 | + * |
|
158 | + * @param string $url |
|
159 | + * @param $sharedSecret |
|
160 | + */ |
|
161 | + public function addSharedSecret($url, $sharedSecret) { |
|
162 | + $this->dbHandler->addSharedSecret($url, $sharedSecret); |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * remove server from the list of trusted servers |
|
167 | + * |
|
168 | + * @param int $id |
|
169 | + */ |
|
170 | + public function removeServer($id) { |
|
171 | + $server = $this->dbHandler->getServerById($id); |
|
172 | + $this->dbHandler->removeServer($id); |
|
173 | + $event = new GenericEvent($server['url_hash']); |
|
174 | + $this->dispatcher->dispatch('OCP\Federation\TrustedServerEvent::remove', $event); |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * get all trusted servers |
|
179 | + * |
|
180 | + * @return array |
|
181 | + */ |
|
182 | + public function getServers() { |
|
183 | + return $this->dbHandler->getAllServer(); |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * check if given server is a trusted Nextcloud server |
|
188 | + * |
|
189 | + * @param string $url |
|
190 | + * @return bool |
|
191 | + */ |
|
192 | + public function isTrustedServer($url) { |
|
193 | + return $this->dbHandler->serverExists($url); |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * set server status |
|
198 | + * |
|
199 | + * @param string $url |
|
200 | + * @param int $status |
|
201 | + */ |
|
202 | + public function setServerStatus($url, $status) { |
|
203 | + $this->dbHandler->setServerStatus($url, $status); |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * @param string $url |
|
208 | + * @return int |
|
209 | + */ |
|
210 | + public function getServerStatus($url) { |
|
211 | + return $this->dbHandler->getServerStatus($url); |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * check if URL point to a ownCloud/Nextcloud server |
|
216 | + * |
|
217 | + * @param string $url |
|
218 | + * @return bool |
|
219 | + */ |
|
220 | + public function isOwnCloudServer($url) { |
|
221 | + $isValidOwnCloud = false; |
|
222 | + $client = $this->httpClientService->newClient(); |
|
223 | + try { |
|
224 | + $result = $client->get( |
|
225 | + $url . '/status.php', |
|
226 | + [ |
|
227 | + 'timeout' => 3, |
|
228 | + 'connect_timeout' => 3, |
|
229 | + ] |
|
230 | + ); |
|
231 | + if ($result->getStatusCode() === Http::STATUS_OK) { |
|
232 | + $isValidOwnCloud = $this->checkOwnCloudVersion($result->getBody()); |
|
233 | + |
|
234 | + } |
|
235 | + } catch (\Exception $e) { |
|
236 | + $this->logger->debug('No Nextcloud server: ' . $e->getMessage()); |
|
237 | + return false; |
|
238 | + } |
|
239 | + |
|
240 | + return $isValidOwnCloud; |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * check if ownCloud version is >= 9.0 |
|
245 | + * |
|
246 | + * @param $status |
|
247 | + * @return bool |
|
248 | + * @throws HintException |
|
249 | + */ |
|
250 | + protected function checkOwnCloudVersion($status) { |
|
251 | + $decoded = json_decode($status, true); |
|
252 | + if (!empty($decoded) && isset($decoded['version'])) { |
|
253 | + if (!version_compare($decoded['version'], '9.0.0', '>=')) { |
|
254 | + throw new HintException('Remote server version is too low. 9.0 is required.'); |
|
255 | + } |
|
256 | + return true; |
|
257 | + } |
|
258 | + return false; |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * check if the URL contain a protocol, if not add https |
|
263 | + * |
|
264 | + * @param string $url |
|
265 | + * @return string |
|
266 | + */ |
|
267 | + protected function updateProtocol($url) { |
|
268 | + if ( |
|
269 | + strpos($url, 'https://') === 0 |
|
270 | + || strpos($url, 'http://') === 0 |
|
271 | + ) { |
|
272 | + |
|
273 | + return $url; |
|
274 | + |
|
275 | + } |
|
276 | + |
|
277 | + return 'https://' . $url; |
|
278 | + } |
|
279 | + |
|
280 | + protected function getTimestamp() { |
|
281 | + return time(); |
|
282 | + } |
|
283 | 283 | } |
@@ -222,7 +222,7 @@ discard block |
||
222 | 222 | $client = $this->httpClientService->newClient(); |
223 | 223 | try { |
224 | 224 | $result = $client->get( |
225 | - $url . '/status.php', |
|
225 | + $url.'/status.php', |
|
226 | 226 | [ |
227 | 227 | 'timeout' => 3, |
228 | 228 | 'connect_timeout' => 3, |
@@ -233,7 +233,7 @@ discard block |
||
233 | 233 | |
234 | 234 | } |
235 | 235 | } catch (\Exception $e) { |
236 | - $this->logger->debug('No Nextcloud server: ' . $e->getMessage()); |
|
236 | + $this->logger->debug('No Nextcloud server: '.$e->getMessage()); |
|
237 | 237 | return false; |
238 | 238 | } |
239 | 239 | |
@@ -274,7 +274,7 @@ discard block |
||
274 | 274 | |
275 | 275 | } |
276 | 276 | |
277 | - return 'https://' . $url; |
|
277 | + return 'https://'.$url; |
|
278 | 278 | } |
279 | 279 | |
280 | 280 | protected function getTimestamp() { |
@@ -46,165 +46,165 @@ |
||
46 | 46 | */ |
47 | 47 | class OCSAuthAPIController extends OCSController{ |
48 | 48 | |
49 | - /** @var ISecureRandom */ |
|
50 | - private $secureRandom; |
|
51 | - |
|
52 | - /** @var IJobList */ |
|
53 | - private $jobList; |
|
54 | - |
|
55 | - /** @var TrustedServers */ |
|
56 | - private $trustedServers; |
|
57 | - |
|
58 | - /** @var DbHandler */ |
|
59 | - private $dbHandler; |
|
60 | - |
|
61 | - /** @var ILogger */ |
|
62 | - private $logger; |
|
63 | - |
|
64 | - /** |
|
65 | - * OCSAuthAPI constructor. |
|
66 | - * |
|
67 | - * @param string $appName |
|
68 | - * @param IRequest $request |
|
69 | - * @param ISecureRandom $secureRandom |
|
70 | - * @param IJobList $jobList |
|
71 | - * @param TrustedServers $trustedServers |
|
72 | - * @param DbHandler $dbHandler |
|
73 | - * @param ILogger $logger |
|
74 | - */ |
|
75 | - public function __construct( |
|
76 | - $appName, |
|
77 | - IRequest $request, |
|
78 | - ISecureRandom $secureRandom, |
|
79 | - IJobList $jobList, |
|
80 | - TrustedServers $trustedServers, |
|
81 | - DbHandler $dbHandler, |
|
82 | - ILogger $logger |
|
83 | - ) { |
|
84 | - parent::__construct($appName, $request); |
|
85 | - |
|
86 | - $this->secureRandom = $secureRandom; |
|
87 | - $this->jobList = $jobList; |
|
88 | - $this->trustedServers = $trustedServers; |
|
89 | - $this->dbHandler = $dbHandler; |
|
90 | - $this->logger = $logger; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @NoCSRFRequired |
|
95 | - * @PublicPage |
|
96 | - * |
|
97 | - * request received to ask remote server for a shared secret, for legacy end-points |
|
98 | - * |
|
99 | - * @param string $url |
|
100 | - * @param string $token |
|
101 | - * @return Http\DataResponse |
|
102 | - * @throws OCSForbiddenException |
|
103 | - */ |
|
104 | - public function requestSharedSecretLegacy($url, $token) { |
|
105 | - return $this->requestSharedSecret($url, $token); |
|
106 | - } |
|
107 | - |
|
108 | - |
|
109 | - /** |
|
110 | - * @NoCSRFRequired |
|
111 | - * @PublicPage |
|
112 | - * |
|
113 | - * create shared secret and return it, for legacy end-points |
|
114 | - * |
|
115 | - * @param string $url |
|
116 | - * @param string $token |
|
117 | - * @return Http\DataResponse |
|
118 | - * @throws OCSForbiddenException |
|
119 | - */ |
|
120 | - public function getSharedSecretLegacy($url, $token) { |
|
121 | - return $this->getSharedSecret($url, $token); |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * @NoCSRFRequired |
|
126 | - * @PublicPage |
|
127 | - * |
|
128 | - * request received to ask remote server for a shared secret |
|
129 | - * |
|
130 | - * @param string $url |
|
131 | - * @param string $token |
|
132 | - * @return Http\DataResponse |
|
133 | - * @throws OCSForbiddenException |
|
134 | - */ |
|
135 | - public function requestSharedSecret($url, $token) { |
|
136 | - if ($this->trustedServers->isTrustedServer($url) === false) { |
|
137 | - $this->logger->error('remote server not trusted (' . $url . ') while requesting shared secret', ['app' => 'federation']); |
|
138 | - throw new OCSForbiddenException(); |
|
139 | - } |
|
140 | - |
|
141 | - // if both server initiated the exchange of the shared secret the greater |
|
142 | - // token wins |
|
143 | - $localToken = $this->dbHandler->getToken($url); |
|
144 | - if (strcmp($localToken, $token) > 0) { |
|
145 | - $this->logger->info( |
|
146 | - 'remote server (' . $url . ') presented lower token. We will initiate the exchange of the shared secret.', |
|
147 | - ['app' => 'federation'] |
|
148 | - ); |
|
149 | - throw new OCSForbiddenException(); |
|
150 | - } |
|
151 | - |
|
152 | - $this->jobList->add( |
|
153 | - 'OCA\Federation\BackgroundJob\GetSharedSecret', |
|
154 | - [ |
|
155 | - 'url' => $url, |
|
156 | - 'token' => $token, |
|
157 | - 'created' => $this->getTimestamp() |
|
158 | - ] |
|
159 | - ); |
|
160 | - |
|
161 | - return new Http\DataResponse(); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * @NoCSRFRequired |
|
166 | - * @PublicPage |
|
167 | - * |
|
168 | - * create shared secret and return it |
|
169 | - * |
|
170 | - * @param string $url |
|
171 | - * @param string $token |
|
172 | - * @return Http\DataResponse |
|
173 | - * @throws OCSForbiddenException |
|
174 | - */ |
|
175 | - public function getSharedSecret($url, $token) { |
|
176 | - if ($this->trustedServers->isTrustedServer($url) === false) { |
|
177 | - $this->logger->error('remote server not trusted (' . $url . ') while getting shared secret', ['app' => 'federation']); |
|
178 | - throw new OCSForbiddenException(); |
|
179 | - } |
|
180 | - |
|
181 | - if ($this->isValidToken($url, $token) === false) { |
|
182 | - $expectedToken = $this->dbHandler->getToken($url); |
|
183 | - $this->logger->error( |
|
184 | - 'remote server (' . $url . ') didn\'t send a valid token (got "' . $token . '" but expected "'. $expectedToken . '") while getting shared secret', |
|
185 | - ['app' => 'federation'] |
|
186 | - ); |
|
187 | - throw new OCSForbiddenException(); |
|
188 | - } |
|
189 | - |
|
190 | - $sharedSecret = $this->secureRandom->generate(32); |
|
191 | - |
|
192 | - $this->trustedServers->addSharedSecret($url, $sharedSecret); |
|
193 | - // reset token after the exchange of the shared secret was successful |
|
194 | - $this->dbHandler->addToken($url, ''); |
|
195 | - |
|
196 | - return new Http\DataResponse([ |
|
197 | - 'sharedSecret' => $sharedSecret |
|
198 | - ]); |
|
199 | - } |
|
200 | - |
|
201 | - protected function isValidToken($url, $token) { |
|
202 | - $storedToken = $this->dbHandler->getToken($url); |
|
203 | - return hash_equals($storedToken, $token); |
|
204 | - } |
|
205 | - |
|
206 | - protected function getTimestamp() { |
|
207 | - return time(); |
|
208 | - } |
|
49 | + /** @var ISecureRandom */ |
|
50 | + private $secureRandom; |
|
51 | + |
|
52 | + /** @var IJobList */ |
|
53 | + private $jobList; |
|
54 | + |
|
55 | + /** @var TrustedServers */ |
|
56 | + private $trustedServers; |
|
57 | + |
|
58 | + /** @var DbHandler */ |
|
59 | + private $dbHandler; |
|
60 | + |
|
61 | + /** @var ILogger */ |
|
62 | + private $logger; |
|
63 | + |
|
64 | + /** |
|
65 | + * OCSAuthAPI constructor. |
|
66 | + * |
|
67 | + * @param string $appName |
|
68 | + * @param IRequest $request |
|
69 | + * @param ISecureRandom $secureRandom |
|
70 | + * @param IJobList $jobList |
|
71 | + * @param TrustedServers $trustedServers |
|
72 | + * @param DbHandler $dbHandler |
|
73 | + * @param ILogger $logger |
|
74 | + */ |
|
75 | + public function __construct( |
|
76 | + $appName, |
|
77 | + IRequest $request, |
|
78 | + ISecureRandom $secureRandom, |
|
79 | + IJobList $jobList, |
|
80 | + TrustedServers $trustedServers, |
|
81 | + DbHandler $dbHandler, |
|
82 | + ILogger $logger |
|
83 | + ) { |
|
84 | + parent::__construct($appName, $request); |
|
85 | + |
|
86 | + $this->secureRandom = $secureRandom; |
|
87 | + $this->jobList = $jobList; |
|
88 | + $this->trustedServers = $trustedServers; |
|
89 | + $this->dbHandler = $dbHandler; |
|
90 | + $this->logger = $logger; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @NoCSRFRequired |
|
95 | + * @PublicPage |
|
96 | + * |
|
97 | + * request received to ask remote server for a shared secret, for legacy end-points |
|
98 | + * |
|
99 | + * @param string $url |
|
100 | + * @param string $token |
|
101 | + * @return Http\DataResponse |
|
102 | + * @throws OCSForbiddenException |
|
103 | + */ |
|
104 | + public function requestSharedSecretLegacy($url, $token) { |
|
105 | + return $this->requestSharedSecret($url, $token); |
|
106 | + } |
|
107 | + |
|
108 | + |
|
109 | + /** |
|
110 | + * @NoCSRFRequired |
|
111 | + * @PublicPage |
|
112 | + * |
|
113 | + * create shared secret and return it, for legacy end-points |
|
114 | + * |
|
115 | + * @param string $url |
|
116 | + * @param string $token |
|
117 | + * @return Http\DataResponse |
|
118 | + * @throws OCSForbiddenException |
|
119 | + */ |
|
120 | + public function getSharedSecretLegacy($url, $token) { |
|
121 | + return $this->getSharedSecret($url, $token); |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * @NoCSRFRequired |
|
126 | + * @PublicPage |
|
127 | + * |
|
128 | + * request received to ask remote server for a shared secret |
|
129 | + * |
|
130 | + * @param string $url |
|
131 | + * @param string $token |
|
132 | + * @return Http\DataResponse |
|
133 | + * @throws OCSForbiddenException |
|
134 | + */ |
|
135 | + public function requestSharedSecret($url, $token) { |
|
136 | + if ($this->trustedServers->isTrustedServer($url) === false) { |
|
137 | + $this->logger->error('remote server not trusted (' . $url . ') while requesting shared secret', ['app' => 'federation']); |
|
138 | + throw new OCSForbiddenException(); |
|
139 | + } |
|
140 | + |
|
141 | + // if both server initiated the exchange of the shared secret the greater |
|
142 | + // token wins |
|
143 | + $localToken = $this->dbHandler->getToken($url); |
|
144 | + if (strcmp($localToken, $token) > 0) { |
|
145 | + $this->logger->info( |
|
146 | + 'remote server (' . $url . ') presented lower token. We will initiate the exchange of the shared secret.', |
|
147 | + ['app' => 'federation'] |
|
148 | + ); |
|
149 | + throw new OCSForbiddenException(); |
|
150 | + } |
|
151 | + |
|
152 | + $this->jobList->add( |
|
153 | + 'OCA\Federation\BackgroundJob\GetSharedSecret', |
|
154 | + [ |
|
155 | + 'url' => $url, |
|
156 | + 'token' => $token, |
|
157 | + 'created' => $this->getTimestamp() |
|
158 | + ] |
|
159 | + ); |
|
160 | + |
|
161 | + return new Http\DataResponse(); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * @NoCSRFRequired |
|
166 | + * @PublicPage |
|
167 | + * |
|
168 | + * create shared secret and return it |
|
169 | + * |
|
170 | + * @param string $url |
|
171 | + * @param string $token |
|
172 | + * @return Http\DataResponse |
|
173 | + * @throws OCSForbiddenException |
|
174 | + */ |
|
175 | + public function getSharedSecret($url, $token) { |
|
176 | + if ($this->trustedServers->isTrustedServer($url) === false) { |
|
177 | + $this->logger->error('remote server not trusted (' . $url . ') while getting shared secret', ['app' => 'federation']); |
|
178 | + throw new OCSForbiddenException(); |
|
179 | + } |
|
180 | + |
|
181 | + if ($this->isValidToken($url, $token) === false) { |
|
182 | + $expectedToken = $this->dbHandler->getToken($url); |
|
183 | + $this->logger->error( |
|
184 | + 'remote server (' . $url . ') didn\'t send a valid token (got "' . $token . '" but expected "'. $expectedToken . '") while getting shared secret', |
|
185 | + ['app' => 'federation'] |
|
186 | + ); |
|
187 | + throw new OCSForbiddenException(); |
|
188 | + } |
|
189 | + |
|
190 | + $sharedSecret = $this->secureRandom->generate(32); |
|
191 | + |
|
192 | + $this->trustedServers->addSharedSecret($url, $sharedSecret); |
|
193 | + // reset token after the exchange of the shared secret was successful |
|
194 | + $this->dbHandler->addToken($url, ''); |
|
195 | + |
|
196 | + return new Http\DataResponse([ |
|
197 | + 'sharedSecret' => $sharedSecret |
|
198 | + ]); |
|
199 | + } |
|
200 | + |
|
201 | + protected function isValidToken($url, $token) { |
|
202 | + $storedToken = $this->dbHandler->getToken($url); |
|
203 | + return hash_equals($storedToken, $token); |
|
204 | + } |
|
205 | + |
|
206 | + protected function getTimestamp() { |
|
207 | + return time(); |
|
208 | + } |
|
209 | 209 | |
210 | 210 | } |