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/private | |
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/private')
-rw-r--r-- | lib/private/Remote/Api/ApiBase.php | 97 | ||||
-rw-r--r-- | lib/private/Remote/Api/ApiCollection.php | 51 | ||||
-rw-r--r-- | lib/private/Remote/Api/ApiFactory.php | 41 | ||||
-rw-r--r-- | lib/private/Remote/Api/NotFoundException.php | 27 | ||||
-rw-r--r-- | lib/private/Remote/Api/OCS.php | 99 | ||||
-rw-r--r-- | lib/private/Remote/Credentials.php | 55 | ||||
-rw-r--r-- | lib/private/Remote/Instance.php | 147 | ||||
-rw-r--r-- | lib/private/Remote/InstanceFactory.php | 43 | ||||
-rw-r--r-- | lib/private/Remote/User.php | 138 | ||||
-rw-r--r-- | lib/private/Server.php | 27 |
10 files changed, 725 insertions, 0 deletions
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); + } } |