Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit f6414a43 authored by Bjoern Schiessle's avatar Bjoern Schiessle
Browse files

start implementing federated calendar sharing

parent 5c2996d8
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
 */
namespace OCA\Calendar\AppInfo;

use OCA\Calendar\CloudFederationProviderCalendar;
use OCA\Calendar\Controller;

use OCP\AppFramework\App;
@@ -53,6 +54,22 @@ class Application extends App {
			return new Controller\EmailController($c->getAppName(), $request, $userSession, $config, $mailer, $l10n, $defaults);
		});

		$container->registerService('FederationController', function(IAppContainer $c) {
			$request = $c->query('Request');
			$server = $c->getServer();

			return new Controller\FederationController(
				$c->getAppName(),
				$request,
				$server->getCloudIdManager(),
				$server->getCloudFederationProviderManager(),
				$server->getCloudFederationFactory(),
				$server->getSecureRandom(),
				$server->getUserSession()
			);
		});


		$container->registerService('ProxyController', function(IAppContainer $c) {
			$request = $c->query('Request');
			$client = $c->getServer()->getHTTPClientService();
@@ -84,6 +101,14 @@ class Application extends App {

			return new Controller\ViewController($c->getAppName(), $request, $userSession, $config, $urlGenerator);
		});

		$cloudFederationManager = $container->getServer()->getCloudFederationProviderManager();
		$cloudFederationManager->addCloudFederationProvider('calendar',
			'Federated calendar Sharing',
			function() {
				return new CloudFederationProviderCalendar();
			});

	}

	/**
+139 −0
Original line number Diff line number Diff line
<?php
/**
 * @copyright Copyright (c) 2018 Bjoern Schiessle <bjoern@schiessle.org>
 *
 * @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 OCA\Calendar\Controller;

use OCP\AppFramework\Controller;
use OCP\Federation\ICloudFederationFactory;
use OCP\Federation\ICloudFederationProviderManager;
use OCP\Federation\ICloudIdManager;
use OCP\IRequest;
use OCP\IUserSession;
use OCP\Security\ISecureRandom;

class FederationController extends Controller {

	/** @var ICloudIdManager */
	private $cloudIdManager;

	/** @var ICloudFederationProviderManager */
	private $federationProviderManager;

	/** @var ICloudFederationFactory */
	private $cloudFederationFactory;

	/** @var ISecureRandom */
	private $secureRandom;

	/** @var \OCP\IUser */
	private $user;

	public function __construct(string $appName,
								IRequest $request,
								ICloudIdManager $cloudIdManager,
								ICloudFederationProviderManager $federationProviderManager,
								ICloudFederationFactory $cloudFederationFactory,
								ISecureRandom $secureRandom,
								IUserSession $userSession) {
		parent::__construct($appName, $request);

		$this->cloudIdManager = $cloudIdManager;
		$this->federationProviderManager = $federationProviderManager;
		$this->cloudFederationFactory = $cloudFederationFactory;
		$this->secureRandom = $secureRandom;
		$this->user = $userSession->getUser();
	}

	/**
	 * @param string $url (should be the subscriber url, e.g. webcal://nextcloudserver/remote.php/dav/public-calendars/fkhskfhksf?export)
	 * @param string $calendarName
	 * @param string $recipient (the federated cloud ID of the recipient)
	 * @return bool
	 */
	public function createFederatedShare(string $url, string $calendarName, string $recipient) {
		if (!$this->cloudIdManager->isValidCloudId($recipient)) {
			return false;
		}

		$sharedSecret = $this->generateSharedSecret();

		// ToDo store share in a db table
		$providerId =$this->addShareToDb($url, $calendarName, $recipient, $sharedSecret, $this->user->getUID());

		$share = $this->cloudFederationFactory->getCloudFederationShare(
			$recipient,
			$calendarName,
			'',
			$providerId,
			$this->user->getCloudId(),
			$this->user->getCloudId(),
			$this->user->getCloudId(),
			$this->user->getCloudId(),
			$sharedSecret,
			'user',
			'calendar');

		$share->setProtocol([
			'name' => 'carddav',
			'options' => [
				'sharedSecret' => $sharedSecret,
				'url' => $url
			]
		]);

		$this->federationProviderManager->sendShare($share);
	}

	/**
	 * generate to token used to authenticate federated shares
	 *
	 * @return string
	 */
	private function generateSharedSecret() {
		$token = $this->secureRandom->generate(15,
			ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);
		return $token;
	}

	/**
	 * write share to a database table
	 *
	 * @param $url
	 * @param $calendarName
	 * @param $recipient
	 * @param $sharedSecret
	 * @param $owner
	 * @return int
	 */
	private function addShareToDb(string $url,
								  string $calendarName,
								  string $recipient,
								  string $sharedSecret,
								  string $owner) {

		// Todo We still need to define a table for it and then write it to the table


		// ToDo should be the insert ID
		return 1;
	}

}
+94 −0
Original line number Diff line number Diff line
<?php
/**
 * @copyright Copyright (c) 2018 Bjoern Schiessle <bjoern@schiessle.org>
 *
 * @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 OCA\Calendar;

use OCP\Federation\Exceptions\ActionNotSupportedException;
use OCP\Federation\ICloudFederationProvider;
use OCP\Federation\ICloudFederationShare;

class CloudFederationProviderCalendar implements ICloudFederationProvider {

	/**
	 * get the name of the share type, handled by this provider
	 *
	 * @return string
	 *
	 * @since 14.0.0
	 */
	public function getShareType() {
		return 'calendar';
	}

	/**
	 * share received from another server
	 *
	 * @param ICloudFederationShare $share
	 * @return string provider specific unique ID of the share
	 *
	 * @throws \OCP\Federation\Exceptions\ProviderCouldNotAddShareException
	 *
	 * @since 14.0.0
	 */
	public function shareReceived(ICloudFederationShare $share) {
		// TODO: Implement shareReceived() method.
		// Here we should to the same as we do if the user add a new calendar
		// subscription in the user interface
		// We might consider to first create a notification to ask the user whether
		// they want to accept the share or not
		//
		// Additionally we should store it in a db table so that we have information
		// like the shared secret, the provider id, etc in case we exchange notifications
		// also in the user interface it might be nice to show some additional information like
		// from whom the share comes from
	}

	/**
	 * notification received from another server
	 *
	 * @param string $notificationType (e.g SHARE_ACCEPTED)
	 * @param string $providerId share ID
	 * @param array $notification provider specific notification
	 * @return array $data send back to sender
	 *
	 * @throws ActionNotSupportedException
	 *
	 * @since 14.0.0
	 */
	public function notificationReceived($notificationType, $providerId, array $notification) {
		// TODO: Implement notificationReceived() method.
		// let's start with a small subset of notification, I think at least a unshare
		// notification from the owner would be nice
		throw new ActionNotSupportedException($notificationType);
		return [];
	}

	/**
	 * get the supported share types, e.g. "user", "group", etc.
	 *
	 * @return array
	 *
	 * @since 14.0.0
	 */
	public function getSupportedShareTypes() {
		return ['user'];
	}
}