diff options
Diffstat (limited to 'apps/federation/lib/trustedservers.php')
-rw-r--r-- | apps/federation/lib/trustedservers.php | 105 |
1 files changed, 85 insertions, 20 deletions
diff --git a/apps/federation/lib/trustedservers.php b/apps/federation/lib/trustedservers.php index bf31277b2a8..f3ac6e24fc6 100644 --- a/apps/federation/lib/trustedservers.php +++ b/apps/federation/lib/trustedservers.php @@ -22,15 +22,21 @@ namespace OCA\Federation; - -use OC\Files\Filesystem; use OCP\AppFramework\Http; +use OCP\BackgroundJob\IJobList; use OCP\Http\Client\IClientService; -use OCP\IDBConnection; use OCP\ILogger; +use OCP\Security\ISecureRandom; class TrustedServers { + /** after a user list was exchanged at least once successfully */ + const STATUS_OK = 1; + /** waiting for shared secret or initial user list exchange */ + const STATUS_PENDING = 2; + /** something went wrong, misconfigured server, software bug,... user interaction needed */ + const STATUS_FAILURE = 3; + /** @var dbHandler */ private $dbHandler; @@ -40,21 +46,31 @@ class TrustedServers { /** @var ILogger */ private $logger; - private $dbTable = 'trusted_servers'; + /** @var IJobList */ + private $jobList; + + /** @var ISecureRandom */ + private $secureRandom; /** * @param DbHandler $dbHandler * @param IClientService $httpClientService * @param ILogger $logger + * @param IJobList $jobList + * @param ISecureRandom $secureRandom */ public function __construct( DbHandler $dbHandler, IClientService $httpClientService, - ILogger $logger + ILogger $logger, + IJobList $jobList, + ISecureRandom $secureRandom ) { $this->dbHandler = $dbHandler; $this->httpClientService = $httpClientService; $this->logger = $logger; + $this->jobList = $jobList; + $this->secureRandom = $secureRandom; } /** @@ -64,7 +80,41 @@ class TrustedServers { * @return int server id */ public function addServer($url) { - return $this->dbHandler->add($this->normalizeUrl($url)); + $url = $this->updateProtocol($url); + $result = $this->dbHandler->addServer($url); + if ($result) { + $token = $this->secureRandom->getMediumStrengthGenerator()->generate(16); + $this->dbHandler->addToken($url, $token); + $this->jobList->add( + 'OCA\Federation\BackgroundJob\RequestSharedSecret', + [ + 'url' => $url, + 'token' => $token + ] + ); + } + + return $result; + } + + /** + * get shared secret for the given server + * + * @param string $url + * @return string + */ + public function getSharedSecret($url) { + return $this->dbHandler->getSharedSecret($url); + } + + /** + * add shared secret for the given server + * + * @param string $url + * @param $sharedSecret + */ + public function addSharedSecret($url, $sharedSecret) { + $this->dbHandler->addSharedSecret($url, $sharedSecret); } /** @@ -73,7 +123,7 @@ class TrustedServers { * @param int $id */ public function removeServer($id) { - $this->dbHandler->remove($id); + $this->dbHandler->removeServer($id); } /** @@ -82,7 +132,7 @@ class TrustedServers { * @return array */ public function getServers() { - return $this->dbHandler->getAll(); + return $this->dbHandler->getAllServer(); } /** @@ -92,7 +142,25 @@ class TrustedServers { * @return bool */ public function isTrustedServer($url) { - return $this->dbHandler->exists($this->normalizeUrl($url)); + return $this->dbHandler->serverExists($url); + } + + /** + * set server status + * + * @param string $url + * @param int $status + */ + public function setServerStatus($url, $status) { + $this->dbHandler->setServerStatus($url, $status); + } + + /** + * @param string $url + * @return int + */ + public function getServerStatus($url) { + return $this->dbHandler->getServerStatus($url); } /** @@ -137,24 +205,21 @@ class TrustedServers { } /** - * normalize URL + * check if the URL contain a protocol, if not add https * * @param string $url * @return string */ - protected function normalizeUrl($url) { + protected function updateProtocol($url) { + if ( + strpos($url, 'https://') === 0 + || strpos($url, 'http://') === 0 + ) { - $normalized = $url; + return $url; - if (strpos($url, 'https://') === 0) { - $normalized = substr($url, strlen('https://')); - } else if (strpos($url, 'http://') === 0) { - $normalized = substr($url, strlen('http://')); } - $normalized = Filesystem::normalizePath($normalized); - $normalized = trim($normalized, '/'); - - return $normalized; + return 'https://' . $url; } } |