@@ -56,211 +56,211 @@ |
||
56 | 56 | */ |
57 | 57 | class GetSharedSecret extends Job { |
58 | 58 | |
59 | - /** @var IClient */ |
|
60 | - private $httpClient; |
|
61 | - |
|
62 | - /** @var IJobList */ |
|
63 | - private $jobList; |
|
64 | - |
|
65 | - /** @var IURLGenerator */ |
|
66 | - private $urlGenerator; |
|
67 | - |
|
68 | - /** @var TrustedServers */ |
|
69 | - private $trustedServers; |
|
70 | - |
|
71 | - /** @var DbHandler */ |
|
72 | - private $dbHandler; |
|
73 | - |
|
74 | - /** @var IDiscoveryService */ |
|
75 | - private $ocsDiscoveryService; |
|
76 | - |
|
77 | - /** @var ILogger */ |
|
78 | - private $logger; |
|
79 | - |
|
80 | - /** @var ITimeFactory */ |
|
81 | - private $timeFactory; |
|
82 | - |
|
83 | - /** @var bool */ |
|
84 | - protected $retainJob = false; |
|
85 | - |
|
86 | - private $format = 'json'; |
|
87 | - |
|
88 | - private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/shared-secret'; |
|
89 | - |
|
90 | - /** @var int 30 day = 2592000sec */ |
|
91 | - private $maxLifespan = 2592000; |
|
92 | - |
|
93 | - /** |
|
94 | - * RequestSharedSecret constructor. |
|
95 | - * |
|
96 | - * @param IClientService $httpClientService |
|
97 | - * @param IURLGenerator $urlGenerator |
|
98 | - * @param IJobList $jobList |
|
99 | - * @param TrustedServers $trustedServers |
|
100 | - * @param ILogger $logger |
|
101 | - * @param DbHandler $dbHandler |
|
102 | - * @param IDiscoveryService $ocsDiscoveryService |
|
103 | - * @param ITimeFactory $timeFactory |
|
104 | - */ |
|
105 | - public function __construct( |
|
106 | - IClientService $httpClientService, |
|
107 | - IURLGenerator $urlGenerator, |
|
108 | - IJobList $jobList, |
|
109 | - TrustedServers $trustedServers, |
|
110 | - ILogger $logger, |
|
111 | - DbHandler $dbHandler, |
|
112 | - IDiscoveryService $ocsDiscoveryService, |
|
113 | - ITimeFactory $timeFactory |
|
114 | - ) { |
|
115 | - $this->logger = $logger; |
|
116 | - $this->httpClient = $httpClientService->newClient(); |
|
117 | - $this->jobList = $jobList; |
|
118 | - $this->urlGenerator = $urlGenerator; |
|
119 | - $this->dbHandler = $dbHandler; |
|
120 | - $this->ocsDiscoveryService = $ocsDiscoveryService; |
|
121 | - $this->trustedServers = $trustedServers; |
|
122 | - $this->timeFactory = $timeFactory; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * run the job, then remove it from the joblist |
|
127 | - * |
|
128 | - * @param JobList $jobList |
|
129 | - * @param ILogger|null $logger |
|
130 | - */ |
|
131 | - public function execute($jobList, ILogger $logger = null) { |
|
132 | - $target = $this->argument['url']; |
|
133 | - // only execute if target is still in the list of trusted domains |
|
134 | - if ($this->trustedServers->isTrustedServer($target)) { |
|
135 | - $this->parentExecute($jobList, $logger); |
|
136 | - } |
|
137 | - |
|
138 | - $jobList->remove($this, $this->argument); |
|
139 | - |
|
140 | - if ($this->retainJob) { |
|
141 | - $this->reAddJob($this->argument); |
|
142 | - } |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * call execute() method of parent |
|
147 | - * |
|
148 | - * @param JobList $jobList |
|
149 | - * @param ILogger $logger |
|
150 | - */ |
|
151 | - protected function parentExecute($jobList, $logger = null) { |
|
152 | - parent::execute($jobList, $logger); |
|
153 | - } |
|
154 | - |
|
155 | - protected function run($argument) { |
|
156 | - $target = $argument['url']; |
|
157 | - $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
158 | - $currentTime = $this->timeFactory->getTime(); |
|
159 | - $source = $this->urlGenerator->getAbsoluteURL('/'); |
|
160 | - $source = rtrim($source, '/'); |
|
161 | - $token = $argument['token']; |
|
162 | - |
|
163 | - // kill job after 30 days of trying |
|
164 | - $deadline = $currentTime - $this->maxLifespan; |
|
165 | - if ($created < $deadline) { |
|
166 | - $this->retainJob = false; |
|
167 | - $this->trustedServers->setServerStatus($target,TrustedServers::STATUS_FAILURE); |
|
168 | - return; |
|
169 | - } |
|
170 | - |
|
171 | - $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); |
|
172 | - $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
|
173 | - |
|
174 | - // make sure that we have a well formatted url |
|
175 | - $url = rtrim($target, '/') . '/' . trim($endPoint, '/'); |
|
176 | - |
|
177 | - $result = null; |
|
178 | - try { |
|
179 | - $result = $this->httpClient->get( |
|
180 | - $url, |
|
181 | - [ |
|
182 | - 'query' => |
|
183 | - [ |
|
184 | - 'url' => $source, |
|
185 | - 'token' => $token, |
|
186 | - 'format' => $this->format |
|
187 | - ], |
|
188 | - 'timeout' => 3, |
|
189 | - 'connect_timeout' => 3, |
|
190 | - ] |
|
191 | - ); |
|
192 | - |
|
193 | - $status = $result->getStatusCode(); |
|
194 | - |
|
195 | - } catch (ClientException $e) { |
|
196 | - $status = $e->getCode(); |
|
197 | - if ($status === Http::STATUS_FORBIDDEN) { |
|
198 | - $this->logger->info($target . ' refused to exchange a shared secret with you.', ['app' => 'federation']); |
|
199 | - } else { |
|
200 | - $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
|
201 | - } |
|
202 | - } catch (RequestException $e) { |
|
203 | - $status = -1; // There is no status code if we could not connect |
|
204 | - $this->logger->logException($e, [ |
|
205 | - 'message' => 'Could not connect to ' . $target, |
|
206 | - 'level' => \OCP\Util::INFO, |
|
207 | - 'app' => 'federation', |
|
208 | - ]); |
|
209 | - } catch (RingException $e) { |
|
210 | - $status = -1; // There is no status code if we could not connect |
|
211 | - $this->logger->logException($e, [ |
|
212 | - 'message' => 'Could not connect to ' . $target, |
|
213 | - 'level' => \OCP\Util::INFO, |
|
214 | - 'app' => 'federation', |
|
215 | - ]); |
|
216 | - } catch (\Exception $e) { |
|
217 | - $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
|
218 | - $this->logger->logException($e, ['app' => 'federation']); |
|
219 | - } |
|
220 | - |
|
221 | - // if we received a unexpected response we try again later |
|
222 | - if ( |
|
223 | - $status !== Http::STATUS_OK |
|
224 | - && $status !== Http::STATUS_FORBIDDEN |
|
225 | - ) { |
|
226 | - $this->retainJob = true; |
|
227 | - } |
|
228 | - |
|
229 | - if ($status === Http::STATUS_OK && $result instanceof IResponse) { |
|
230 | - $body = $result->getBody(); |
|
231 | - $result = json_decode($body, true); |
|
232 | - if (isset($result['ocs']['data']['sharedSecret'])) { |
|
233 | - $this->trustedServers->addSharedSecret( |
|
234 | - $target, |
|
235 | - $result['ocs']['data']['sharedSecret'] |
|
236 | - ); |
|
237 | - } else { |
|
238 | - $this->logger->error( |
|
239 | - 'remote server "' . $target . '"" does not return a valid shared secret. Received data: ' . $body, |
|
240 | - ['app' => 'federation'] |
|
241 | - ); |
|
242 | - $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
|
243 | - } |
|
244 | - } |
|
245 | - |
|
246 | - } |
|
247 | - |
|
248 | - /** |
|
249 | - * re-add background job |
|
250 | - * |
|
251 | - * @param array $argument |
|
252 | - */ |
|
253 | - protected function reAddJob(array $argument) { |
|
254 | - $url = $argument['url']; |
|
255 | - $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
256 | - $token = $argument['token']; |
|
257 | - $this->jobList->add( |
|
258 | - GetSharedSecret::class, |
|
259 | - [ |
|
260 | - 'url' => $url, |
|
261 | - 'token' => $token, |
|
262 | - 'created' => $created |
|
263 | - ] |
|
264 | - ); |
|
265 | - } |
|
59 | + /** @var IClient */ |
|
60 | + private $httpClient; |
|
61 | + |
|
62 | + /** @var IJobList */ |
|
63 | + private $jobList; |
|
64 | + |
|
65 | + /** @var IURLGenerator */ |
|
66 | + private $urlGenerator; |
|
67 | + |
|
68 | + /** @var TrustedServers */ |
|
69 | + private $trustedServers; |
|
70 | + |
|
71 | + /** @var DbHandler */ |
|
72 | + private $dbHandler; |
|
73 | + |
|
74 | + /** @var IDiscoveryService */ |
|
75 | + private $ocsDiscoveryService; |
|
76 | + |
|
77 | + /** @var ILogger */ |
|
78 | + private $logger; |
|
79 | + |
|
80 | + /** @var ITimeFactory */ |
|
81 | + private $timeFactory; |
|
82 | + |
|
83 | + /** @var bool */ |
|
84 | + protected $retainJob = false; |
|
85 | + |
|
86 | + private $format = 'json'; |
|
87 | + |
|
88 | + private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/shared-secret'; |
|
89 | + |
|
90 | + /** @var int 30 day = 2592000sec */ |
|
91 | + private $maxLifespan = 2592000; |
|
92 | + |
|
93 | + /** |
|
94 | + * RequestSharedSecret constructor. |
|
95 | + * |
|
96 | + * @param IClientService $httpClientService |
|
97 | + * @param IURLGenerator $urlGenerator |
|
98 | + * @param IJobList $jobList |
|
99 | + * @param TrustedServers $trustedServers |
|
100 | + * @param ILogger $logger |
|
101 | + * @param DbHandler $dbHandler |
|
102 | + * @param IDiscoveryService $ocsDiscoveryService |
|
103 | + * @param ITimeFactory $timeFactory |
|
104 | + */ |
|
105 | + public function __construct( |
|
106 | + IClientService $httpClientService, |
|
107 | + IURLGenerator $urlGenerator, |
|
108 | + IJobList $jobList, |
|
109 | + TrustedServers $trustedServers, |
|
110 | + ILogger $logger, |
|
111 | + DbHandler $dbHandler, |
|
112 | + IDiscoveryService $ocsDiscoveryService, |
|
113 | + ITimeFactory $timeFactory |
|
114 | + ) { |
|
115 | + $this->logger = $logger; |
|
116 | + $this->httpClient = $httpClientService->newClient(); |
|
117 | + $this->jobList = $jobList; |
|
118 | + $this->urlGenerator = $urlGenerator; |
|
119 | + $this->dbHandler = $dbHandler; |
|
120 | + $this->ocsDiscoveryService = $ocsDiscoveryService; |
|
121 | + $this->trustedServers = $trustedServers; |
|
122 | + $this->timeFactory = $timeFactory; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * run the job, then remove it from the joblist |
|
127 | + * |
|
128 | + * @param JobList $jobList |
|
129 | + * @param ILogger|null $logger |
|
130 | + */ |
|
131 | + public function execute($jobList, ILogger $logger = null) { |
|
132 | + $target = $this->argument['url']; |
|
133 | + // only execute if target is still in the list of trusted domains |
|
134 | + if ($this->trustedServers->isTrustedServer($target)) { |
|
135 | + $this->parentExecute($jobList, $logger); |
|
136 | + } |
|
137 | + |
|
138 | + $jobList->remove($this, $this->argument); |
|
139 | + |
|
140 | + if ($this->retainJob) { |
|
141 | + $this->reAddJob($this->argument); |
|
142 | + } |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * call execute() method of parent |
|
147 | + * |
|
148 | + * @param JobList $jobList |
|
149 | + * @param ILogger $logger |
|
150 | + */ |
|
151 | + protected function parentExecute($jobList, $logger = null) { |
|
152 | + parent::execute($jobList, $logger); |
|
153 | + } |
|
154 | + |
|
155 | + protected function run($argument) { |
|
156 | + $target = $argument['url']; |
|
157 | + $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
158 | + $currentTime = $this->timeFactory->getTime(); |
|
159 | + $source = $this->urlGenerator->getAbsoluteURL('/'); |
|
160 | + $source = rtrim($source, '/'); |
|
161 | + $token = $argument['token']; |
|
162 | + |
|
163 | + // kill job after 30 days of trying |
|
164 | + $deadline = $currentTime - $this->maxLifespan; |
|
165 | + if ($created < $deadline) { |
|
166 | + $this->retainJob = false; |
|
167 | + $this->trustedServers->setServerStatus($target,TrustedServers::STATUS_FAILURE); |
|
168 | + return; |
|
169 | + } |
|
170 | + |
|
171 | + $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); |
|
172 | + $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
|
173 | + |
|
174 | + // make sure that we have a well formatted url |
|
175 | + $url = rtrim($target, '/') . '/' . trim($endPoint, '/'); |
|
176 | + |
|
177 | + $result = null; |
|
178 | + try { |
|
179 | + $result = $this->httpClient->get( |
|
180 | + $url, |
|
181 | + [ |
|
182 | + 'query' => |
|
183 | + [ |
|
184 | + 'url' => $source, |
|
185 | + 'token' => $token, |
|
186 | + 'format' => $this->format |
|
187 | + ], |
|
188 | + 'timeout' => 3, |
|
189 | + 'connect_timeout' => 3, |
|
190 | + ] |
|
191 | + ); |
|
192 | + |
|
193 | + $status = $result->getStatusCode(); |
|
194 | + |
|
195 | + } catch (ClientException $e) { |
|
196 | + $status = $e->getCode(); |
|
197 | + if ($status === Http::STATUS_FORBIDDEN) { |
|
198 | + $this->logger->info($target . ' refused to exchange a shared secret with you.', ['app' => 'federation']); |
|
199 | + } else { |
|
200 | + $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
|
201 | + } |
|
202 | + } catch (RequestException $e) { |
|
203 | + $status = -1; // There is no status code if we could not connect |
|
204 | + $this->logger->logException($e, [ |
|
205 | + 'message' => 'Could not connect to ' . $target, |
|
206 | + 'level' => \OCP\Util::INFO, |
|
207 | + 'app' => 'federation', |
|
208 | + ]); |
|
209 | + } catch (RingException $e) { |
|
210 | + $status = -1; // There is no status code if we could not connect |
|
211 | + $this->logger->logException($e, [ |
|
212 | + 'message' => 'Could not connect to ' . $target, |
|
213 | + 'level' => \OCP\Util::INFO, |
|
214 | + 'app' => 'federation', |
|
215 | + ]); |
|
216 | + } catch (\Exception $e) { |
|
217 | + $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
|
218 | + $this->logger->logException($e, ['app' => 'federation']); |
|
219 | + } |
|
220 | + |
|
221 | + // if we received a unexpected response we try again later |
|
222 | + if ( |
|
223 | + $status !== Http::STATUS_OK |
|
224 | + && $status !== Http::STATUS_FORBIDDEN |
|
225 | + ) { |
|
226 | + $this->retainJob = true; |
|
227 | + } |
|
228 | + |
|
229 | + if ($status === Http::STATUS_OK && $result instanceof IResponse) { |
|
230 | + $body = $result->getBody(); |
|
231 | + $result = json_decode($body, true); |
|
232 | + if (isset($result['ocs']['data']['sharedSecret'])) { |
|
233 | + $this->trustedServers->addSharedSecret( |
|
234 | + $target, |
|
235 | + $result['ocs']['data']['sharedSecret'] |
|
236 | + ); |
|
237 | + } else { |
|
238 | + $this->logger->error( |
|
239 | + 'remote server "' . $target . '"" does not return a valid shared secret. Received data: ' . $body, |
|
240 | + ['app' => 'federation'] |
|
241 | + ); |
|
242 | + $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
|
243 | + } |
|
244 | + } |
|
245 | + |
|
246 | + } |
|
247 | + |
|
248 | + /** |
|
249 | + * re-add background job |
|
250 | + * |
|
251 | + * @param array $argument |
|
252 | + */ |
|
253 | + protected function reAddJob(array $argument) { |
|
254 | + $url = $argument['url']; |
|
255 | + $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
256 | + $token = $argument['token']; |
|
257 | + $this->jobList->add( |
|
258 | + GetSharedSecret::class, |
|
259 | + [ |
|
260 | + 'url' => $url, |
|
261 | + 'token' => $token, |
|
262 | + 'created' => $created |
|
263 | + ] |
|
264 | + ); |
|
265 | + } |
|
266 | 266 | } |
@@ -154,7 +154,7 @@ discard block |
||
154 | 154 | |
155 | 155 | protected function run($argument) { |
156 | 156 | $target = $argument['url']; |
157 | - $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
157 | + $created = isset($argument['created']) ? (int) $argument['created'] : $this->timeFactory->getTime(); |
|
158 | 158 | $currentTime = $this->timeFactory->getTime(); |
159 | 159 | $source = $this->urlGenerator->getAbsoluteURL('/'); |
160 | 160 | $source = rtrim($source, '/'); |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | $deadline = $currentTime - $this->maxLifespan; |
165 | 165 | if ($created < $deadline) { |
166 | 166 | $this->retainJob = false; |
167 | - $this->trustedServers->setServerStatus($target,TrustedServers::STATUS_FAILURE); |
|
167 | + $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
|
168 | 168 | return; |
169 | 169 | } |
170 | 170 | |
@@ -172,7 +172,7 @@ discard block |
||
172 | 172 | $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
173 | 173 | |
174 | 174 | // make sure that we have a well formatted url |
175 | - $url = rtrim($target, '/') . '/' . trim($endPoint, '/'); |
|
175 | + $url = rtrim($target, '/').'/'.trim($endPoint, '/'); |
|
176 | 176 | |
177 | 177 | $result = null; |
178 | 178 | try { |
@@ -195,21 +195,21 @@ discard block |
||
195 | 195 | } catch (ClientException $e) { |
196 | 196 | $status = $e->getCode(); |
197 | 197 | if ($status === Http::STATUS_FORBIDDEN) { |
198 | - $this->logger->info($target . ' refused to exchange a shared secret with you.', ['app' => 'federation']); |
|
198 | + $this->logger->info($target.' refused to exchange a shared secret with you.', ['app' => 'federation']); |
|
199 | 199 | } else { |
200 | - $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
|
200 | + $this->logger->info($target.' responded with a '.$status.' containing: '.$e->getMessage(), ['app' => 'federation']); |
|
201 | 201 | } |
202 | 202 | } catch (RequestException $e) { |
203 | 203 | $status = -1; // There is no status code if we could not connect |
204 | 204 | $this->logger->logException($e, [ |
205 | - 'message' => 'Could not connect to ' . $target, |
|
205 | + 'message' => 'Could not connect to '.$target, |
|
206 | 206 | 'level' => \OCP\Util::INFO, |
207 | 207 | 'app' => 'federation', |
208 | 208 | ]); |
209 | 209 | } catch (RingException $e) { |
210 | 210 | $status = -1; // There is no status code if we could not connect |
211 | 211 | $this->logger->logException($e, [ |
212 | - 'message' => 'Could not connect to ' . $target, |
|
212 | + 'message' => 'Could not connect to '.$target, |
|
213 | 213 | 'level' => \OCP\Util::INFO, |
214 | 214 | 'app' => 'federation', |
215 | 215 | ]); |
@@ -236,7 +236,7 @@ discard block |
||
236 | 236 | ); |
237 | 237 | } else { |
238 | 238 | $this->logger->error( |
239 | - 'remote server "' . $target . '"" does not return a valid shared secret. Received data: ' . $body, |
|
239 | + 'remote server "'.$target.'"" does not return a valid shared secret. Received data: '.$body, |
|
240 | 240 | ['app' => 'federation'] |
241 | 241 | ); |
242 | 242 | $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
@@ -252,7 +252,7 @@ discard block |
||
252 | 252 | */ |
253 | 253 | protected function reAddJob(array $argument) { |
254 | 254 | $url = $argument['url']; |
255 | - $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
255 | + $created = isset($argument['created']) ? (int) $argument['created'] : $this->timeFactory->getTime(); |
|
256 | 256 | $token = $argument['token']; |
257 | 257 | $this->jobList->add( |
258 | 258 | GetSharedSecret::class, |
@@ -49,165 +49,165 @@ |
||
49 | 49 | */ |
50 | 50 | class OCSAuthAPIController extends OCSController{ |
51 | 51 | |
52 | - /** @var ISecureRandom */ |
|
53 | - private $secureRandom; |
|
54 | - |
|
55 | - /** @var IJobList */ |
|
56 | - private $jobList; |
|
57 | - |
|
58 | - /** @var TrustedServers */ |
|
59 | - private $trustedServers; |
|
60 | - |
|
61 | - /** @var DbHandler */ |
|
62 | - private $dbHandler; |
|
63 | - |
|
64 | - /** @var ILogger */ |
|
65 | - private $logger; |
|
66 | - |
|
67 | - /** @var ITimeFactory */ |
|
68 | - private $timeFactory; |
|
69 | - |
|
70 | - /** |
|
71 | - * OCSAuthAPI constructor. |
|
72 | - * |
|
73 | - * @param string $appName |
|
74 | - * @param IRequest $request |
|
75 | - * @param ISecureRandom $secureRandom |
|
76 | - * @param IJobList $jobList |
|
77 | - * @param TrustedServers $trustedServers |
|
78 | - * @param DbHandler $dbHandler |
|
79 | - * @param ILogger $logger |
|
80 | - * @param ITimeFactory $timeFactory |
|
81 | - */ |
|
82 | - public function __construct( |
|
83 | - $appName, |
|
84 | - IRequest $request, |
|
85 | - ISecureRandom $secureRandom, |
|
86 | - IJobList $jobList, |
|
87 | - TrustedServers $trustedServers, |
|
88 | - DbHandler $dbHandler, |
|
89 | - ILogger $logger, |
|
90 | - ITimeFactory $timeFactory |
|
91 | - ) { |
|
92 | - parent::__construct($appName, $request); |
|
93 | - |
|
94 | - $this->secureRandom = $secureRandom; |
|
95 | - $this->jobList = $jobList; |
|
96 | - $this->trustedServers = $trustedServers; |
|
97 | - $this->dbHandler = $dbHandler; |
|
98 | - $this->logger = $logger; |
|
99 | - $this->timeFactory = $timeFactory; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * @NoCSRFRequired |
|
104 | - * @PublicPage |
|
105 | - * |
|
106 | - * request received to ask remote server for a shared secret, for legacy end-points |
|
107 | - * |
|
108 | - * @param string $url |
|
109 | - * @param string $token |
|
110 | - * @return Http\DataResponse |
|
111 | - * @throws OCSForbiddenException |
|
112 | - */ |
|
113 | - public function requestSharedSecretLegacy($url, $token) { |
|
114 | - return $this->requestSharedSecret($url, $token); |
|
115 | - } |
|
116 | - |
|
117 | - |
|
118 | - /** |
|
119 | - * @NoCSRFRequired |
|
120 | - * @PublicPage |
|
121 | - * |
|
122 | - * create shared secret and return it, for legacy end-points |
|
123 | - * |
|
124 | - * @param string $url |
|
125 | - * @param string $token |
|
126 | - * @return Http\DataResponse |
|
127 | - * @throws OCSForbiddenException |
|
128 | - */ |
|
129 | - public function getSharedSecretLegacy($url, $token) { |
|
130 | - return $this->getSharedSecret($url, $token); |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * @NoCSRFRequired |
|
135 | - * @PublicPage |
|
136 | - * |
|
137 | - * request received to ask remote server for a shared secret |
|
138 | - * |
|
139 | - * @param string $url |
|
140 | - * @param string $token |
|
141 | - * @return Http\DataResponse |
|
142 | - * @throws OCSForbiddenException |
|
143 | - */ |
|
144 | - public function requestSharedSecret($url, $token) { |
|
145 | - if ($this->trustedServers->isTrustedServer($url) === false) { |
|
146 | - $this->logger->error('remote server not trusted (' . $url . ') while requesting shared secret', ['app' => 'federation']); |
|
147 | - throw new OCSForbiddenException(); |
|
148 | - } |
|
149 | - |
|
150 | - // if both server initiated the exchange of the shared secret the greater |
|
151 | - // token wins |
|
152 | - $localToken = $this->dbHandler->getToken($url); |
|
153 | - if (strcmp($localToken, $token) > 0) { |
|
154 | - $this->logger->info( |
|
155 | - 'remote server (' . $url . ') presented lower token. We will initiate the exchange of the shared secret.', |
|
156 | - ['app' => 'federation'] |
|
157 | - ); |
|
158 | - throw new OCSForbiddenException(); |
|
159 | - } |
|
160 | - |
|
161 | - $this->jobList->add( |
|
162 | - 'OCA\Federation\BackgroundJob\GetSharedSecret', |
|
163 | - [ |
|
164 | - 'url' => $url, |
|
165 | - 'token' => $token, |
|
166 | - 'created' => $this->timeFactory->getTime() |
|
167 | - ] |
|
168 | - ); |
|
169 | - |
|
170 | - return new Http\DataResponse(); |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * @NoCSRFRequired |
|
175 | - * @PublicPage |
|
176 | - * |
|
177 | - * create shared secret and return it |
|
178 | - * |
|
179 | - * @param string $url |
|
180 | - * @param string $token |
|
181 | - * @return Http\DataResponse |
|
182 | - * @throws OCSForbiddenException |
|
183 | - */ |
|
184 | - public function getSharedSecret($url, $token) { |
|
185 | - |
|
186 | - if ($this->trustedServers->isTrustedServer($url) === false) { |
|
187 | - $this->logger->error('remote server not trusted (' . $url . ') while getting shared secret', ['app' => 'federation']); |
|
188 | - throw new OCSForbiddenException(); |
|
189 | - } |
|
190 | - |
|
191 | - if ($this->isValidToken($url, $token) === false) { |
|
192 | - $expectedToken = $this->dbHandler->getToken($url); |
|
193 | - $this->logger->error( |
|
194 | - 'remote server (' . $url . ') didn\'t send a valid token (got "' . $token . '" but expected "'. $expectedToken . '") while getting shared secret', |
|
195 | - ['app' => 'federation'] |
|
196 | - ); |
|
197 | - throw new OCSForbiddenException(); |
|
198 | - } |
|
199 | - |
|
200 | - $sharedSecret = $this->secureRandom->generate(32); |
|
201 | - |
|
202 | - $this->trustedServers->addSharedSecret($url, $sharedSecret); |
|
203 | - |
|
204 | - return new Http\DataResponse([ |
|
205 | - 'sharedSecret' => $sharedSecret |
|
206 | - ]); |
|
207 | - } |
|
208 | - |
|
209 | - protected function isValidToken($url, $token) { |
|
210 | - $storedToken = $this->dbHandler->getToken($url); |
|
211 | - return hash_equals($storedToken, $token); |
|
212 | - } |
|
52 | + /** @var ISecureRandom */ |
|
53 | + private $secureRandom; |
|
54 | + |
|
55 | + /** @var IJobList */ |
|
56 | + private $jobList; |
|
57 | + |
|
58 | + /** @var TrustedServers */ |
|
59 | + private $trustedServers; |
|
60 | + |
|
61 | + /** @var DbHandler */ |
|
62 | + private $dbHandler; |
|
63 | + |
|
64 | + /** @var ILogger */ |
|
65 | + private $logger; |
|
66 | + |
|
67 | + /** @var ITimeFactory */ |
|
68 | + private $timeFactory; |
|
69 | + |
|
70 | + /** |
|
71 | + * OCSAuthAPI constructor. |
|
72 | + * |
|
73 | + * @param string $appName |
|
74 | + * @param IRequest $request |
|
75 | + * @param ISecureRandom $secureRandom |
|
76 | + * @param IJobList $jobList |
|
77 | + * @param TrustedServers $trustedServers |
|
78 | + * @param DbHandler $dbHandler |
|
79 | + * @param ILogger $logger |
|
80 | + * @param ITimeFactory $timeFactory |
|
81 | + */ |
|
82 | + public function __construct( |
|
83 | + $appName, |
|
84 | + IRequest $request, |
|
85 | + ISecureRandom $secureRandom, |
|
86 | + IJobList $jobList, |
|
87 | + TrustedServers $trustedServers, |
|
88 | + DbHandler $dbHandler, |
|
89 | + ILogger $logger, |
|
90 | + ITimeFactory $timeFactory |
|
91 | + ) { |
|
92 | + parent::__construct($appName, $request); |
|
93 | + |
|
94 | + $this->secureRandom = $secureRandom; |
|
95 | + $this->jobList = $jobList; |
|
96 | + $this->trustedServers = $trustedServers; |
|
97 | + $this->dbHandler = $dbHandler; |
|
98 | + $this->logger = $logger; |
|
99 | + $this->timeFactory = $timeFactory; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * @NoCSRFRequired |
|
104 | + * @PublicPage |
|
105 | + * |
|
106 | + * request received to ask remote server for a shared secret, for legacy end-points |
|
107 | + * |
|
108 | + * @param string $url |
|
109 | + * @param string $token |
|
110 | + * @return Http\DataResponse |
|
111 | + * @throws OCSForbiddenException |
|
112 | + */ |
|
113 | + public function requestSharedSecretLegacy($url, $token) { |
|
114 | + return $this->requestSharedSecret($url, $token); |
|
115 | + } |
|
116 | + |
|
117 | + |
|
118 | + /** |
|
119 | + * @NoCSRFRequired |
|
120 | + * @PublicPage |
|
121 | + * |
|
122 | + * create shared secret and return it, for legacy end-points |
|
123 | + * |
|
124 | + * @param string $url |
|
125 | + * @param string $token |
|
126 | + * @return Http\DataResponse |
|
127 | + * @throws OCSForbiddenException |
|
128 | + */ |
|
129 | + public function getSharedSecretLegacy($url, $token) { |
|
130 | + return $this->getSharedSecret($url, $token); |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * @NoCSRFRequired |
|
135 | + * @PublicPage |
|
136 | + * |
|
137 | + * request received to ask remote server for a shared secret |
|
138 | + * |
|
139 | + * @param string $url |
|
140 | + * @param string $token |
|
141 | + * @return Http\DataResponse |
|
142 | + * @throws OCSForbiddenException |
|
143 | + */ |
|
144 | + public function requestSharedSecret($url, $token) { |
|
145 | + if ($this->trustedServers->isTrustedServer($url) === false) { |
|
146 | + $this->logger->error('remote server not trusted (' . $url . ') while requesting shared secret', ['app' => 'federation']); |
|
147 | + throw new OCSForbiddenException(); |
|
148 | + } |
|
149 | + |
|
150 | + // if both server initiated the exchange of the shared secret the greater |
|
151 | + // token wins |
|
152 | + $localToken = $this->dbHandler->getToken($url); |
|
153 | + if (strcmp($localToken, $token) > 0) { |
|
154 | + $this->logger->info( |
|
155 | + 'remote server (' . $url . ') presented lower token. We will initiate the exchange of the shared secret.', |
|
156 | + ['app' => 'federation'] |
|
157 | + ); |
|
158 | + throw new OCSForbiddenException(); |
|
159 | + } |
|
160 | + |
|
161 | + $this->jobList->add( |
|
162 | + 'OCA\Federation\BackgroundJob\GetSharedSecret', |
|
163 | + [ |
|
164 | + 'url' => $url, |
|
165 | + 'token' => $token, |
|
166 | + 'created' => $this->timeFactory->getTime() |
|
167 | + ] |
|
168 | + ); |
|
169 | + |
|
170 | + return new Http\DataResponse(); |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * @NoCSRFRequired |
|
175 | + * @PublicPage |
|
176 | + * |
|
177 | + * create shared secret and return it |
|
178 | + * |
|
179 | + * @param string $url |
|
180 | + * @param string $token |
|
181 | + * @return Http\DataResponse |
|
182 | + * @throws OCSForbiddenException |
|
183 | + */ |
|
184 | + public function getSharedSecret($url, $token) { |
|
185 | + |
|
186 | + if ($this->trustedServers->isTrustedServer($url) === false) { |
|
187 | + $this->logger->error('remote server not trusted (' . $url . ') while getting shared secret', ['app' => 'federation']); |
|
188 | + throw new OCSForbiddenException(); |
|
189 | + } |
|
190 | + |
|
191 | + if ($this->isValidToken($url, $token) === false) { |
|
192 | + $expectedToken = $this->dbHandler->getToken($url); |
|
193 | + $this->logger->error( |
|
194 | + 'remote server (' . $url . ') didn\'t send a valid token (got "' . $token . '" but expected "'. $expectedToken . '") while getting shared secret', |
|
195 | + ['app' => 'federation'] |
|
196 | + ); |
|
197 | + throw new OCSForbiddenException(); |
|
198 | + } |
|
199 | + |
|
200 | + $sharedSecret = $this->secureRandom->generate(32); |
|
201 | + |
|
202 | + $this->trustedServers->addSharedSecret($url, $sharedSecret); |
|
203 | + |
|
204 | + return new Http\DataResponse([ |
|
205 | + 'sharedSecret' => $sharedSecret |
|
206 | + ]); |
|
207 | + } |
|
208 | + |
|
209 | + protected function isValidToken($url, $token) { |
|
210 | + $storedToken = $this->dbHandler->getToken($url); |
|
211 | + return hash_equals($storedToken, $token); |
|
212 | + } |
|
213 | 213 | } |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | * |
48 | 48 | * @package OCA\Federation\Controller |
49 | 49 | */ |
50 | -class OCSAuthAPIController extends OCSController{ |
|
50 | +class OCSAuthAPIController extends OCSController { |
|
51 | 51 | |
52 | 52 | /** @var ISecureRandom */ |
53 | 53 | private $secureRandom; |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | */ |
144 | 144 | public function requestSharedSecret($url, $token) { |
145 | 145 | if ($this->trustedServers->isTrustedServer($url) === false) { |
146 | - $this->logger->error('remote server not trusted (' . $url . ') while requesting shared secret', ['app' => 'federation']); |
|
146 | + $this->logger->error('remote server not trusted ('.$url.') while requesting shared secret', ['app' => 'federation']); |
|
147 | 147 | throw new OCSForbiddenException(); |
148 | 148 | } |
149 | 149 | |
@@ -152,7 +152,7 @@ discard block |
||
152 | 152 | $localToken = $this->dbHandler->getToken($url); |
153 | 153 | if (strcmp($localToken, $token) > 0) { |
154 | 154 | $this->logger->info( |
155 | - 'remote server (' . $url . ') presented lower token. We will initiate the exchange of the shared secret.', |
|
155 | + 'remote server ('.$url.') presented lower token. We will initiate the exchange of the shared secret.', |
|
156 | 156 | ['app' => 'federation'] |
157 | 157 | ); |
158 | 158 | throw new OCSForbiddenException(); |
@@ -184,14 +184,14 @@ discard block |
||
184 | 184 | public function getSharedSecret($url, $token) { |
185 | 185 | |
186 | 186 | if ($this->trustedServers->isTrustedServer($url) === false) { |
187 | - $this->logger->error('remote server not trusted (' . $url . ') while getting shared secret', ['app' => 'federation']); |
|
187 | + $this->logger->error('remote server not trusted ('.$url.') while getting shared secret', ['app' => 'federation']); |
|
188 | 188 | throw new OCSForbiddenException(); |
189 | 189 | } |
190 | 190 | |
191 | 191 | if ($this->isValidToken($url, $token) === false) { |
192 | 192 | $expectedToken = $this->dbHandler->getToken($url); |
193 | 193 | $this->logger->error( |
194 | - 'remote server (' . $url . ') didn\'t send a valid token (got "' . $token . '" but expected "'. $expectedToken . '") while getting shared secret', |
|
194 | + 'remote server ('.$url.') didn\'t send a valid token (got "'.$token.'" but expected "'.$expectedToken.'") while getting shared secret', |
|
195 | 195 | ['app' => 'federation'] |
196 | 196 | ); |
197 | 197 | throw new OCSForbiddenException(); |