Passed
Pull Request — master (#878)
by Björn
07:12
created

FederationController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
ccs 0
cts 14
cp 0
cc 1
nc 1
nop 7
crap 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018 Bjoern Schiessle <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\Calendar\Controller;
23
24
use OCP\AppFramework\Controller;
25
use OCP\Federation\ICloudFederationFactory;
26
use OCP\Federation\ICloudFederationProviderManager;
27
use OCP\Federation\ICloudIdManager;
28
use OCP\IRequest;
29
use OCP\IUserSession;
30
use OCP\Security\ISecureRandom;
31
32
class FederationController extends Controller {
33
34
	/** @var ICloudIdManager */
35
	private $cloudIdManager;
36
37
	/** @var ICloudFederationProviderManager */
38
	private $federationProviderManager;
39
40
	/** @var ICloudFederationFactory */
41
	private $cloudFederationFactory;
42
43
	/** @var ISecureRandom */
44
	private $secureRandom;
45
46
	/** @var \OCP\IUser */
47
	private $user;
48
49
	public function __construct(string $appName,
50
								IRequest $request,
51
								ICloudIdManager $cloudIdManager,
52
								ICloudFederationProviderManager $federationProviderManager,
53
								ICloudFederationFactory $cloudFederationFactory,
54
								ISecureRandom $secureRandom,
55
								IUserSession $userSession) {
56
		parent::__construct($appName, $request);
57
58
		$this->cloudIdManager = $cloudIdManager;
59
		$this->federationProviderManager = $federationProviderManager;
60
		$this->cloudFederationFactory = $cloudFederationFactory;
61
		$this->secureRandom = $secureRandom;
62
		$this->user = $userSession->getUser();
63
	}
64
65
	/**
66
	 * @param string $url (should be the subscriber url, e.g. webcal://nextcloudserver/remote.php/dav/public-calendars/fkhskfhksf?export)
67
	 * @param string $calendarName
68
	 * @param string $recipient (the federated cloud ID of the recipient)
69
	 * @return bool
70
	 */
71
	public function createFederatedShare(string $url, string $calendarName, string $recipient) {
72
		if (!$this->cloudIdManager->isValidCloudId($recipient)) {
73
			return false;
74
		}
75
76
		$sharedSecret = $this->generateSharedSecret();
77
78
		// ToDo store share in a db table
79
		$providerId =$this->addShareToDb($url, $calendarName, $recipient, $sharedSecret, $this->user->getUID());
80
81
		$share = $this->cloudFederationFactory->getCloudFederationShare(
82
			$recipient,
83
			$calendarName,
84
			'',
85
			$providerId,
86
			$this->user->getCloudId(),
87
			$this->user->getCloudId(),
88
			$this->user->getCloudId(),
89
			$this->user->getCloudId(),
90
			$sharedSecret,
91
			'user',
92
			'calendar');
93
94
		$share->setProtocol([
95
			'name' => 'carddav',
96
			'options' => [
97
				'sharedSecret' => $sharedSecret,
98
				'url' => $url
99
			]
100
		]);
101
102
		$this->federationProviderManager->sendShare($share);
103
	}
104
105
	/**
106
	 * generate to token used to authenticate federated shares
107
	 *
108
	 * @return string
109
	 */
110
	private function generateSharedSecret() {
111
		$token = $this->secureRandom->generate(15,
112
			ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);
113
		return $token;
114
	}
115
116
	/**
117
	 * write share to a database table
118
	 *
119
	 * @param $url
120
	 * @param $calendarName
121
	 * @param $recipient
122
	 * @param $sharedSecret
123
	 * @param $owner
124
	 * @return int
125
	 */
126
	private function addShareToDb(string $url,
127
								  string $calendarName,
128
								  string $recipient,
129
								  string $sharedSecret,
130
								  string $owner) {
131
132
		// Todo We still need to define a table for it and then write it to the table
133
134
135
		// ToDo should be the insert ID
136
		return 1;
137
	}
138
139
}
140