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