diff options
author | Roeland Jago Douma <rullzer@users.noreply.github.com> | 2017-12-11 15:28:39 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-11 15:28:39 +0100 |
commit | e8acf448eb9615dd9b1f523d3e033fab82991b31 (patch) | |
tree | 0245fc0e3963dbbb5baf05769b9fcf4e55bb1358 /lib | |
parent | e47137c7d4a57b20bc2ed337aaee4cf99ee94e08 (diff) | |
parent | 5ce69e7c426059474c1ac59a2086ac66f672e8b8 (diff) | |
download | nextcloud-server-e8acf448eb9615dd9b1f523d3e033fab82991b31.tar.gz nextcloud-server-e8acf448eb9615dd9b1f523d3e033fab82991b31.zip |
Merge pull request #6651 from nextcloud/remote-cloud-client
Add api clients for talking to remote clouds
Diffstat (limited to 'lib')
21 files changed, 1188 insertions, 0 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 81dad9890b5..b3fb04ff6d9 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -246,6 +246,14 @@ return array( 'OCP\\OCS\\IDiscoveryService' => $baseDir . '/lib/public/OCS/IDiscoveryService.php', 'OCP\\PreConditionNotMetException' => $baseDir . '/lib/public/PreConditionNotMetException.php', 'OCP\\Preview\\IProvider' => $baseDir . '/lib/public/Preview/IProvider.php', + 'OCP\\Remote\\Api\\IApiCollection' => $baseDir . '/lib/public/Remote/Api/IApiCollection.php', + 'OCP\\Remote\\Api\\IApiFactory' => $baseDir . '/lib/public/Remote/Api/IApiFactory.php', + 'OCP\\Remote\\Api\\ICapabilitiesApi' => $baseDir . '/lib/public/Remote/Api/ICapabilitiesApi.php', + 'OCP\\Remote\\Api\\IUserApi' => $baseDir . '/lib/public/Remote/Api/IUserApi.php', + 'OCP\\Remote\\ICredentials' => $baseDir . '/lib/public/Remote/ICredentials.php', + 'OCP\\Remote\\IInstance' => $baseDir . '/lib/public/Remote/IInstance.php', + 'OCP\\Remote\\IInstanceFactory' => $baseDir . '/lib/public/Remote/IInstanceFactory.php', + 'OCP\\Remote\\IUser' => $baseDir . '/lib/public/Remote/IUser.php', 'OCP\\Response' => $baseDir . '/lib/public/Response.php', 'OCP\\RichObjectStrings\\Definitions' => $baseDir . '/lib/public/RichObjectStrings/Definitions.php', 'OCP\\RichObjectStrings\\IValidator' => $baseDir . '/lib/public/RichObjectStrings/IValidator.php', @@ -779,6 +787,15 @@ return array( 'OC\\Preview\\WatcherConnector' => $baseDir . '/lib/private/Preview/WatcherConnector.php', 'OC\\Preview\\XBitmap' => $baseDir . '/lib/private/Preview/XBitmap.php', 'OC\\RedisFactory' => $baseDir . '/lib/private/RedisFactory.php', + 'OC\\Remote\\Api\\ApiBase' => $baseDir . '/lib/private/Remote/Api/ApiBase.php', + 'OC\\Remote\\Api\\ApiCollection' => $baseDir . '/lib/private/Remote/Api/ApiCollection.php', + 'OC\\Remote\\Api\\ApiFactory' => $baseDir . '/lib/private/Remote/Api/ApiFactory.php', + 'OC\\Remote\\Api\\NotFoundException' => $baseDir . '/lib/private/Remote/Api/NotFoundException.php', + 'OC\\Remote\\Api\\OCS' => $baseDir . '/lib/private/Remote/Api/OCS.php', + 'OC\\Remote\\Credentials' => $baseDir . '/lib/private/Remote/Credentials.php', + 'OC\\Remote\\Instance' => $baseDir . '/lib/private/Remote/Instance.php', + 'OC\\Remote\\InstanceFactory' => $baseDir . '/lib/private/Remote/InstanceFactory.php', + 'OC\\Remote\\User' => $baseDir . '/lib/private/Remote/User.php', 'OC\\Repair' => $baseDir . '/lib/private/Repair.php', 'OC\\RepairException' => $baseDir . '/lib/private/RepairException.php', 'OC\\Repair\\CleanTags' => $baseDir . '/lib/private/Repair/CleanTags.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index b926100365b..a905134ce9f 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -276,6 +276,14 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OCP\\OCS\\IDiscoveryService' => __DIR__ . '/../../..' . '/lib/public/OCS/IDiscoveryService.php', 'OCP\\PreConditionNotMetException' => __DIR__ . '/../../..' . '/lib/public/PreConditionNotMetException.php', 'OCP\\Preview\\IProvider' => __DIR__ . '/../../..' . '/lib/public/Preview/IProvider.php', + 'OCP\\Remote\\Api\\IApiCollection' => __DIR__ . '/../../..' . '/lib/public/Remote/Api/IApiCollection.php', + 'OCP\\Remote\\Api\\IApiFactory' => __DIR__ . '/../../..' . '/lib/public/Remote/Api/IApiFactory.php', + 'OCP\\Remote\\Api\\ICapabilitiesApi' => __DIR__ . '/../../..' . '/lib/public/Remote/Api/ICapabilitiesApi.php', + 'OCP\\Remote\\Api\\IUserApi' => __DIR__ . '/../../..' . '/lib/public/Remote/Api/IUserApi.php', + 'OCP\\Remote\\ICredentials' => __DIR__ . '/../../..' . '/lib/public/Remote/ICredentials.php', + 'OCP\\Remote\\IInstance' => __DIR__ . '/../../..' . '/lib/public/Remote/IInstance.php', + 'OCP\\Remote\\IInstanceFactory' => __DIR__ . '/../../..' . '/lib/public/Remote/IInstanceFactory.php', + 'OCP\\Remote\\IUser' => __DIR__ . '/../../..' . '/lib/public/Remote/IUser.php', 'OCP\\Response' => __DIR__ . '/../../..' . '/lib/public/Response.php', 'OCP\\RichObjectStrings\\Definitions' => __DIR__ . '/../../..' . '/lib/public/RichObjectStrings/Definitions.php', 'OCP\\RichObjectStrings\\IValidator' => __DIR__ . '/../../..' . '/lib/public/RichObjectStrings/IValidator.php', @@ -809,6 +817,15 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OC\\Preview\\WatcherConnector' => __DIR__ . '/../../..' . '/lib/private/Preview/WatcherConnector.php', 'OC\\Preview\\XBitmap' => __DIR__ . '/../../..' . '/lib/private/Preview/XBitmap.php', 'OC\\RedisFactory' => __DIR__ . '/../../..' . '/lib/private/RedisFactory.php', + 'OC\\Remote\\Api\\ApiBase' => __DIR__ . '/../../..' . '/lib/private/Remote/Api/ApiBase.php', + 'OC\\Remote\\Api\\ApiCollection' => __DIR__ . '/../../..' . '/lib/private/Remote/Api/ApiCollection.php', + 'OC\\Remote\\Api\\ApiFactory' => __DIR__ . '/../../..' . '/lib/private/Remote/Api/ApiFactory.php', + 'OC\\Remote\\Api\\NotFoundException' => __DIR__ . '/../../..' . '/lib/private/Remote/Api/NotFoundException.php', + 'OC\\Remote\\Api\\OCS' => __DIR__ . '/../../..' . '/lib/private/Remote/Api/OCS.php', + 'OC\\Remote\\Credentials' => __DIR__ . '/../../..' . '/lib/private/Remote/Credentials.php', + 'OC\\Remote\\Instance' => __DIR__ . '/../../..' . '/lib/private/Remote/Instance.php', + 'OC\\Remote\\InstanceFactory' => __DIR__ . '/../../..' . '/lib/private/Remote/InstanceFactory.php', + 'OC\\Remote\\User' => __DIR__ . '/../../..' . '/lib/private/Remote/User.php', 'OC\\Repair' => __DIR__ . '/../../..' . '/lib/private/Repair.php', 'OC\\RepairException' => __DIR__ . '/../../..' . '/lib/private/RepairException.php', 'OC\\Repair\\CleanTags' => __DIR__ . '/../../..' . '/lib/private/Repair/CleanTags.php', diff --git a/lib/private/Remote/Api/ApiBase.php b/lib/private/Remote/Api/ApiBase.php new file mode 100644 index 00000000000..70292a977f4 --- /dev/null +++ b/lib/private/Remote/Api/ApiBase.php @@ -0,0 +1,97 @@ +<?php +/** + * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\Remote\Api; + +use OCP\Http\Client\IClientService; +use OCP\Remote\ICredentials; +use OCP\Remote\IInstance; + +class ApiBase { + /** @var IInstance */ + private $instance; + /** @var ICredentials */ + private $credentials; + /** @var IClientService */ + private $clientService; + + public function __construct(IInstance $instance, ICredentials $credentials, IClientService $clientService) { + $this->instance = $instance; + $this->credentials = $credentials; + $this->clientService = $clientService; + } + + protected function getHttpClient() { + return $this->clientService->newClient(); + } + + protected function addDefaultHeaders(array $headers) { + return array_merge([ + 'OCS-APIREQUEST' => 'true', + 'Accept' => 'application/json' + ], $headers); + } + + /** + * @param string $method + * @param string $url + * @param array $body + * @param array $query + * @param array $headers + * @return resource|string + * @throws \InvalidArgumentException + */ + protected function request($method, $url, array $body = [], array $query = [], array $headers = []) { + $fullUrl = trim($this->instance->getFullUrl(), '/') . '/' . $url; + $options = [ + 'query' => $query, + 'headers' => $this->addDefaultHeaders($headers), + 'auth' => [$this->credentials->getUsername(), $this->credentials->getPassword()] + ]; + if ($body) { + $options['body'] = $body; + } + + $client = $this->getHttpClient(); + + switch ($method) { + case 'get': + $response = $client->get($fullUrl, $options); + break; + case 'post': + $response = $client->post($fullUrl, $options); + break; + case 'put': + $response = $client->put($fullUrl, $options); + break; + case 'delete': + $response = $client->delete($fullUrl, $options); + break; + case 'options': + $response = $client->options($fullUrl, $options); + break; + default: + throw new \InvalidArgumentException('Invalid method ' . $method); + } + + return $response->getBody(); + } +} diff --git a/lib/private/Remote/Api/ApiCollection.php b/lib/private/Remote/Api/ApiCollection.php new file mode 100644 index 00000000000..5ce97621dbb --- /dev/null +++ b/lib/private/Remote/Api/ApiCollection.php @@ -0,0 +1,51 @@ +<?php +/** + * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\Remote\Api; + + +use OCP\Http\Client\IClientService; +use OCP\Remote\Api\IApiCollection; +use OCP\Remote\ICredentials; +use OCP\Remote\IInstance; + +class ApiCollection implements IApiCollection { + /** @var IInstance */ + private $instance; + /** @var ICredentials */ + private $credentials; + /** @var IClientService */ + private $clientService; + + public function __construct(IInstance $instance, ICredentials $credentials, IClientService $clientService) { + $this->instance = $instance; + $this->credentials = $credentials; + $this->clientService = $clientService; + } + + public function getCapabilitiesApi() { + return new OCS($this->instance, $this->credentials, $this->clientService); + } + + public function getUserApi() { + return new OCS($this->instance, $this->credentials, $this->clientService); + } +} diff --git a/lib/private/Remote/Api/ApiFactory.php b/lib/private/Remote/Api/ApiFactory.php new file mode 100644 index 00000000000..19b8e8eb50c --- /dev/null +++ b/lib/private/Remote/Api/ApiFactory.php @@ -0,0 +1,41 @@ +<?php +/** + * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\Remote\Api; + + +use OCP\Http\Client\IClientService; +use OCP\Remote\Api\IApiFactory; +use OCP\Remote\ICredentials; +use OCP\Remote\IInstance; + +class ApiFactory implements IApiFactory { + /** @var IClientService */ + private $clientService; + + public function __construct(IClientService $clientService) { + $this->clientService = $clientService; + } + + public function getApiCollection(IInstance $instance, ICredentials $credentials) { + return new ApiCollection($instance, $credentials, $this->clientService); + } +} diff --git a/lib/private/Remote/Api/NotFoundException.php b/lib/private/Remote/Api/NotFoundException.php new file mode 100644 index 00000000000..e660beb70d0 --- /dev/null +++ b/lib/private/Remote/Api/NotFoundException.php @@ -0,0 +1,27 @@ +<?php +/** + * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\Remote\Api; + + +class NotFoundException extends \Exception { + +} diff --git a/lib/private/Remote/Api/OCS.php b/lib/private/Remote/Api/OCS.php new file mode 100644 index 00000000000..a3a15302810 --- /dev/null +++ b/lib/private/Remote/Api/OCS.php @@ -0,0 +1,99 @@ +<?php +/** + * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\Remote\Api; + + +use GuzzleHttp\Exception\ClientException; +use OC\ForbiddenException; +use OC\Remote\User; +use OCP\API; +use OCP\Remote\Api\ICapabilitiesApi; +use OCP\Remote\Api\IUserApi; + +class OCS extends ApiBase implements ICapabilitiesApi, IUserApi { + /** + * @param string $method + * @param string $url + * @param array $body + * @param array $query + * @param array $headers + * @return array + * @throws ForbiddenException + * @throws NotFoundException + * @throws \Exception + */ + protected function request($method, $url, array $body = [], array $query = [], array $headers = []) { + try { + $response = json_decode(parent::request($method, 'ocs/v2.php/' . $url, $body, $query, $headers), true); + } catch (ClientException $e) { + if ($e->getResponse()->getStatusCode() === 404) { + throw new NotFoundException(); + } else if ($e->getResponse()->getStatusCode() === 403 || $e->getResponse()->getStatusCode() === 401) { + throw new ForbiddenException(); + } else { + throw $e; + } + } + if (!isset($response['ocs']) || !isset($response['ocs']['meta'])) { + throw new \Exception('Invalid ocs response'); + } + if ($response['ocs']['meta']['statuscode'] === API::RESPOND_UNAUTHORISED) { + throw new ForbiddenException(); + } + if ($response['ocs']['meta']['statuscode'] === API::RESPOND_NOT_FOUND) { + throw new NotFoundException(); + } + if ($response['ocs']['meta']['status'] !== 'ok') { + throw new \Exception('Unknown ocs error ' . $response['ocs']['meta']['message']); + } + + return $response['ocs']['data']; + } + + /** + * @param array $data + * @param string $type + * @param string[] $keys + * @throws \Exception + */ + private function checkResponseArray(array $data, $type, array $keys) { + foreach ($keys as $key) { + if (!array_key_exists($key, $data)) { + throw new \Exception('Invalid ' . $type . ' response, expected field ' . $key . ' not found'); + } + } + } + + public function getUser($userId) { + $result = $this->request('get', 'cloud/users/' . $userId); + $this->checkResponseArray($result, 'user', User::EXPECTED_KEYS); + return new User($result); + } + + /** + * @return array The capabilities in the form of [$appId => [$capability => $value]] + */ + public function getCapabilities() { + $result = $this->request('get', 'cloud/capabilities'); + return $result['capabilities']; + } +} diff --git a/lib/private/Remote/Credentials.php b/lib/private/Remote/Credentials.php new file mode 100644 index 00000000000..419b8b21c95 --- /dev/null +++ b/lib/private/Remote/Credentials.php @@ -0,0 +1,55 @@ +<?php +/** + * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\Remote; + + +use OCP\Remote\ICredentials; + +class Credentials implements ICredentials { + /** @var string */ + private $user; + /** @var string */ + private $password; + + /** + * @param string $user + * @param string $password + */ + public function __construct($user, $password) { + $this->user = $user; + $this->password = $password; + } + + /** + * @return string + */ + public function getUsername() { + return $this->user; + } + + /** + * @return string + */ + public function getPassword() { + return $this->password; + } +} diff --git a/lib/private/Remote/Instance.php b/lib/private/Remote/Instance.php new file mode 100644 index 00000000000..3e77226edad --- /dev/null +++ b/lib/private/Remote/Instance.php @@ -0,0 +1,147 @@ +<?php +/** + * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\Remote; + +use OC\Remote\Api\NotFoundException; +use OCP\Http\Client\IClientService; +use OCP\ICache; +use OCP\Remote\IInstance; + +/** + * Provides some basic info about a remote Nextcloud instance + */ +class Instance implements IInstance { + /** @var string */ + private $url; + + /** @var ICache */ + private $cache; + + /** @var IClientService */ + private $clientService; + + /** @var array|null */ + private $status; + + /** + * @param string $url + * @param ICache $cache + * @param IClientService $clientService + */ + public function __construct($url, ICache $cache, IClientService $clientService) { + $url = str_replace('https://', '', $url); + $this->url = str_replace('http://', '', $url); + $this->cache = $cache; + $this->clientService = $clientService; + } + + /** + * @return string The url of the remote server without protocol + */ + public function getUrl() { + return $this->url; + } + + /** + * @return string The of of the remote server with protocol + */ + public function getFullUrl() { + return $this->getProtocol() . '://' . $this->getUrl(); + } + + /** + * @return string The full version string in '13.1.2.3' format + */ + public function getVersion() { + $status = $this->getStatus(); + return $status['version']; + } + + /** + * @return string 'http' or 'https' + */ + public function getProtocol() { + $status = $this->getStatus(); + return $status['protocol']; + } + + /** + * Check that the remote server is installed and not in maintenance mode + * + * @return bool + */ + public function isActive() { + $status = $this->getStatus(); + return $status['installed'] && !$status['maintenance']; + } + + /** + * @return array + * @throws NotFoundException + * @throws \Exception + */ + private function getStatus() { + if ($this->status) { + return $this->status; + } + $key = 'remote/' . $this->url . '/status'; + $httpsKey = 'remote/' . $this->url . '/https'; + $status = $this->cache->get($key); + if (!$status) { + $response = $this->downloadStatus('https://' . $this->getUrl() . '/status.php'); + $protocol = 'https'; + if (!$response) { + if ($status = $this->cache->get($httpsKey)) { + throw new \Exception('refusing to connect to remote instance(' . $this->url . ') over http that was previously accessible over https'); + } + $response = $this->downloadStatus('http://' . $this->getUrl() . '/status.php'); + $protocol = 'http'; + } else { + $this->cache->set($httpsKey, true, 60 * 60 * 24 * 365); + } + $status = json_decode($response, true); + if ($status) { + $status['protocol'] = $protocol; + } + if ($status) { + $this->cache->set($key, $status, 5 * 60); + $this->status = $status; + } else { + throw new NotFoundException('Remote server not found at address ' . $this->url); + } + } + return $status; + } + + /** + * @param string $url + * @return bool|string + */ + private function downloadStatus($url) { + try { + $request = $this->clientService->newClient()->get($url); + return $request->getBody(); + } catch (\Exception $e) { + return false; + } + } +} diff --git a/lib/private/Remote/InstanceFactory.php b/lib/private/Remote/InstanceFactory.php new file mode 100644 index 00000000000..72baa433615 --- /dev/null +++ b/lib/private/Remote/InstanceFactory.php @@ -0,0 +1,43 @@ +<?php +/** + * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\Remote; + + +use OCP\Http\Client\IClientService; +use OCP\ICache; +use OCP\Remote\IInstanceFactory; + +class InstanceFactory implements IInstanceFactory { + /** @var ICache */ + private $cache; + /** @var IClientService */ + private $clientService; + + public function __construct(ICache $cache, IClientService $clientService) { + $this->cache = $cache; + $this->clientService = $clientService; + } + + public function getInstance($url) { + return new Instance($url, $this->cache, $this->clientService); + } +} diff --git a/lib/private/Remote/User.php b/lib/private/Remote/User.php new file mode 100644 index 00000000000..1f31965f5e7 --- /dev/null +++ b/lib/private/Remote/User.php @@ -0,0 +1,138 @@ +<?php +/** + * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\Remote; + + +use OCP\Remote\IUser; + +class User implements IUser { + const EXPECTED_KEYS = [ + 'id', + 'email', + 'displayname', + 'phone', + 'address', + 'website', + 'groups', + 'language', + 'quota' + ]; + + /** @var array */ + private $data; + + public function __construct(array $data) { + $this->data = $data; + } + + + /** + * @return string + */ + public function getUserId() { + return $this->data['id']; + } + + /** + * @return string + */ + public function getEmail() { + return $this->data['email']; + } + + /** + * @return string + */ + public function getDisplayName() { + return $this->data['displayname']; + } + + /** + * @return string + */ + public function getPhone() { + return $this->data['phone']; + } + + /** + * @return string + */ + public function getAddress() { + return $this->data['address']; + } + + /** + * @return string + */ + public function getWebsite() { + return $this->data['website']; + } + + /** + * @return string + */ + public function getTwitter() { + return isset($this->data['twitter']) ? $this->data['twitter'] : ''; + } + + /** + * @return string[] + */ + public function getGroups() { + return $this->data['groups']; + } + + /** + * @return string + */ + public function getLanguage() { + return $this->data['language']; + } + + /** + * @return int + */ + public function getUsedSpace() { + return $this->data['quota']['used']; + } + + /** + * @return int + */ + public function getFreeSpace() { + return $this->data['quota']['free']; + } + + /** + * @return int + */ + public function getTotalSpace() { + return $this->data['quota']['total']; + } + + /** + * @return int + */ + public function getQuota() { + return $this->data['quota']['quota']; + } +} diff --git a/lib/private/Server.php b/lib/private/Server.php index 0c6338f6a4c..f4f7cb75ad1 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -89,6 +89,8 @@ use OC\Memcache\ArrayCache; use OC\Memcache\Factory; use OC\Notification\Manager; use OC\OCS\DiscoveryService; +use OC\Remote\Api\ApiFactory; +use OC\Remote\InstanceFactory; use OC\Repair\NC11\CleanPreviewsBackgroundJob; use OC\RichObjectStrings\Validator; use OC\Security\Bruteforce\Throttler; @@ -123,6 +125,8 @@ use OCP\IServerContainer; use OCP\ITempManager; use OCP\Contacts\ContactsMenu\IActionFactory; use OCP\Lock\ILockingProvider; +use OCP\Remote\Api\IApiFactory; +use OCP\Remote\IInstanceFactory; use OCP\RichObjectStrings\IValidator; use OCP\Security\IContentSecurityPolicyManager; use OCP\Share; @@ -1109,6 +1113,15 @@ class Server extends ServerContainer implements IServerContainer { $c->getConfig() ); }); + + $this->registerService(IApiFactory::class, function(Server $c) { + return new ApiFactory($c->getHTTPClientService()); + }); + + $this->registerService(IInstanceFactory::class, function(Server $c) { + $memcacheFactory = $c->getMemCacheFactory(); + return new InstanceFactory($memcacheFactory->createLocal('remoteinstance.'), $c->getHTTPClientService()); + }); } /** @@ -1878,4 +1891,18 @@ class Server extends ServerContainer implements IServerContainer { public function getCloudIdManager() { return $this->query(ICloudIdManager::class); } + + /** + * @return \OCP\Remote\Api\IApiFactory + */ + public function getRemoteApiFactory() { + return $this->query(IApiFactory::class); + } + + /** + * @return \OCP\Remote\IInstanceFactory + */ + public function getRemoteInstanceFactory() { + return $this->query(IInstanceFactory::class); + } } diff --git a/lib/public/IServerContainer.php b/lib/public/IServerContainer.php index 8c33a765d5e..851a3c7e2bb 100644 --- a/lib/public/IServerContainer.php +++ b/lib/public/IServerContainer.php @@ -541,4 +541,16 @@ interface IServerContainer extends IContainer { * @since 12.0.0 */ public function getCloudIdManager(); + + /** + * @return \OCP\Remote\Api\IApiFactory + * @since 13.0.0 + */ + public function getRemoteApiFactory(); + + /** + * @return \OCP\Remote\IInstanceFactory + * @since 13.0.0 + */ + public function getRemoteInstanceFactory(); } diff --git a/lib/public/Remote/Api/IApiCollection.php b/lib/public/Remote/Api/IApiCollection.php new file mode 100644 index 00000000000..c2bb11114d1 --- /dev/null +++ b/lib/public/Remote/Api/IApiCollection.php @@ -0,0 +1,43 @@ +<?php +/** + * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCP\Remote\Api; + +/** + * Provides access to the various apis of a remote instance + * + * @since 13.0.0 + */ +interface IApiCollection { + /** + * @return IUserApi + * + * @since 13.0.0 + */ + public function getUserApi(); + + /** + * @return ICapabilitiesApi + * + * @since 13.0.0 + */ + public function getCapabilitiesApi(); +} diff --git a/lib/public/Remote/Api/IApiFactory.php b/lib/public/Remote/Api/IApiFactory.php new file mode 100644 index 00000000000..f1830f4c04a --- /dev/null +++ b/lib/public/Remote/Api/IApiFactory.php @@ -0,0 +1,39 @@ +<?php +/** + * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCP\Remote\Api; + +use OCP\Remote\ICredentials; +use OCP\Remote\IInstance; + +/** + * @since 13.0.0 + */ +interface IApiFactory { + /** + * @param IInstance $instance + * @param ICredentials $credentials + * @return IApiCollection + * + * @since 13.0.0 + */ + public function getApiCollection(IInstance $instance, ICredentials $credentials); +} diff --git a/lib/public/Remote/Api/ICapabilitiesApi.php b/lib/public/Remote/Api/ICapabilitiesApi.php new file mode 100644 index 00000000000..855be7b520b --- /dev/null +++ b/lib/public/Remote/Api/ICapabilitiesApi.php @@ -0,0 +1,34 @@ +<?php +/** + * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCP\Remote\Api; + +/** + * @since 13.0.0 + */ +interface ICapabilitiesApi { + /** + * @return array The capabilities in the form of [$appId => [$capability => $value]] + * + * @since 13.0.0 + */ + public function getCapabilities(); +} diff --git a/lib/public/Remote/Api/IUserApi.php b/lib/public/Remote/Api/IUserApi.php new file mode 100644 index 00000000000..9fa05dee01a --- /dev/null +++ b/lib/public/Remote/Api/IUserApi.php @@ -0,0 +1,37 @@ +<?php +/** + * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCP\Remote\Api; + +use OCP\Remote\IUser; + +/** + * @since 13.0.0 + */ +interface IUserApi { + /** + * @param string $userId + * @return IUser + * + * @since 13.0.0 + */ + public function getUser($userId); +} diff --git a/lib/public/Remote/ICredentials.php b/lib/public/Remote/ICredentials.php new file mode 100644 index 00000000000..587bb4d5930 --- /dev/null +++ b/lib/public/Remote/ICredentials.php @@ -0,0 +1,43 @@ +<?php +/** + * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCP\Remote; + +/** + * The credentials for a remote user + * + * @since 13.0.0 + */ +interface ICredentials { + /** + * @return string + * + * @since 13.0.0 + */ + public function getUsername(); + + /** + * @return string + * + * @since 13.0.0 + */ + public function getPassword(); +} diff --git a/lib/public/Remote/IInstance.php b/lib/public/Remote/IInstance.php new file mode 100644 index 00000000000..08973308aba --- /dev/null +++ b/lib/public/Remote/IInstance.php @@ -0,0 +1,66 @@ +<?php +/** + * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCP\Remote; + +/** + * Provides some basic info about a remote Nextcloud instance + * + * @since 13.0.0 + */ +interface IInstance { + /** + * @return string The url of the remote server without protocol + * + * @since 13.0.0 + */ + public function getUrl(); + + /** + * @return string The of of the remote server with protocol + * + * @since 13.0.0 + */ + public function getFullUrl(); + + /** + * @return string The full version string in '13.1.2.3' format + * + * @since 13.0.0 + */ + public function getVersion(); + + /** + * @return string 'http' or 'https' + * + * @since 13.0.0 + */ + public function getProtocol(); + + /** + * Check that the remote server is installed and not in maintenance mode + * + * @since 13.0.0 + * + * @return bool + */ + public function isActive(); +} diff --git a/lib/public/Remote/IInstanceFactory.php b/lib/public/Remote/IInstanceFactory.php new file mode 100644 index 00000000000..6aae463a897 --- /dev/null +++ b/lib/public/Remote/IInstanceFactory.php @@ -0,0 +1,35 @@ +<?php +/** + * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCP\Remote; + +/** + * @since 13.0.0 + */ +interface IInstanceFactory { + /** + * @param string $url + * @return IInstance + * + * @since 13.0.0 + */ + public function getInstance($url); +} diff --git a/lib/public/Remote/IUser.php b/lib/public/Remote/IUser.php new file mode 100644 index 00000000000..c34531d3847 --- /dev/null +++ b/lib/public/Remote/IUser.php @@ -0,0 +1,120 @@ +<?php +/** + * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCP\Remote; + +/** + * User info for a remote user + * + * @since 13.0.0 + */ +interface IUser { + /** + * @return string + * + * @since 13.0.0 + */ + public function getUserId(); + + /** + * @return string + * + * @since 13.0.0 + */ + public function getEmail(); + + /** + * @return string + * + * @since 13.0.0 + */ + public function getDisplayName(); + + /** + * @return string + * + * @since 13.0.0 + */ + public function getPhone(); + + /** + * @return string + * + * @since 13.0.0 + */ + public function getAddress(); + + /** + * @return string + * + * @since 13.0.0 + */ + public function getWebsite(); + + /** + * @return string + * + * @since 13.0.0 + */ + public function getTwitter(); + + /** + * @return string[] + * + * @since 13.0.0 + */ + public function getGroups(); + + /** + * @return string + * + * @since 13.0.0 + */ + public function getLanguage(); + + /** + * @return int + * + * @since 13.0.0 + */ + public function getUsedSpace(); + + /** + * @return int + * + * @since 13.0.0 + */ + public function getFreeSpace(); + + /** + * @return int + * + * @since 13.0.0 + */ + public function getTotalSpace(); + + /** + * @return int + * + * @since 13.0.0 + */ + public function getQuota(); +} |