aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/OCM/Model/OCMProvider.php
blob: be13d65a40f86b19d141bb1cadf8380eb3aabc86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace OC\OCM\Model;

use NCU\Security\Signature\Model\Signatory;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\OCM\Events\ResourceTypeRegisterEvent;
use OCP\OCM\Exceptions\OCMArgumentException;
use OCP\OCM\Exceptions\OCMProviderException;
use OCP\OCM\ICapabilityAwareOCMProvider;
use OCP\OCM\IOCMResource;

/**
 * @since 28.0.0
 */
class OCMProvider implements ICapabilityAwareOCMProvider {
	private string $provider;
	private bool $enabled = false;
	private string $apiVersion = '';
	private string $inviteAcceptDialog = '';
	private array $capabilities = [];
	private string $endPoint = '';
	/** @var IOCMResource[] */
	private array $resourceTypes = [];
	private ?Signatory $signatory = null;
	private bool $emittedEvent = false;

	public function __construct(
		protected IEventDispatcher $dispatcher,
		protected IConfig $config,
	) {
		$this->provider = 'Nextcloud ' . $config->getSystemValue('version');
	}

	/**
	 * @param bool $enabled
	 *
	 * @return $this
	 */
	public function setEnabled(bool $enabled): static {
		$this->enabled = $enabled;

		return $this;
	}

	/**
	 * @return bool
	 */
	public function isEnabled(): bool {
		return $this->enabled;
	}

	/**
	 * @param string $apiVersion
	 *
	 * @return $this
	 */
	public function setApiVersion(string $apiVersion): static {
		$this->apiVersion = $apiVersion;

		return $this;
	}

	/**
	 * @return string
	 */
	public function getApiVersion(): string {
		return $this->apiVersion;
	}

	/**
	 * returns the invite accept dialog
	 *
	 * @return string
	 * @since 32.0.0
	 */
	public function getInviteAcceptDialog(): string {
		return $this->inviteAcceptDialog;
	}

	/**
	 * set the invite accept dialog
	 *
	 * @param string $inviteAcceptDialog
	 *
	 * @return $this
	 * @since 32.0.0
	 */
	public function setInviteAcceptDialog(string $inviteAcceptDialog): static {
		$this->inviteAcceptDialog = $inviteAcceptDialog;

		return $this;
	}

	/**
	 * @param string $endPoint
	 *
	 * @return $this
	 */
	public function setEndPoint(string $endPoint): static {
		$this->endPoint = $endPoint;

		return $this;
	}

	/**
	 * @return string
	 */
	public function getEndPoint(): string {
		return $this->endPoint;
	}

	/**
	 * @return string
	 */
	public function getProvider(): string {
		return $this->provider;
	}

	/**
	 * @param array $capabilities
	 *
	 * @return $this
	 */
	public function setCapabilities(array $capabilities): static {
		foreach ($capabilities as $value) {
			if (!in_array($value, $this->capabilities)) {
				array_push($this->capabilities, $value);
			}
		}

		return $this;
	}

	/**
	 * @return array
	 */
	public function getCapabilities(): array {
		return $this->capabilities;
	}
	/**
	 * create a new resource to later add it with {@see IOCMProvider::addResourceType()}
	 * @return IOCMResource
	 */
	public function createNewResourceType(): IOCMResource {
		return new OCMResource();
	}

	/**
	 * @param IOCMResource $resource
	 *
	 * @return $this
	 */
	public function addResourceType(IOCMResource $resource): static {
		$this->resourceTypes[] = $resource;

		return $this;
	}

	/**
	 * @param IOCMResource[] $resourceTypes
	 *
	 * @return $this
	 */
	public function setResourceTypes(array $resourceTypes): static {
		$this->resourceTypes = $resourceTypes;

		return $this;
	}

	/**
	 * @return IOCMResource[]
	 */
	public function getResourceTypes(): array {
		if (!$this->emittedEvent) {
			$this->emittedEvent = true;
			$event = new ResourceTypeRegisterEvent($this);
			$this->dispatcher->dispatchTyped($event);
		}

		return $this->resourceTypes;
	}

	/**
	 * @param string $resourceName
	 * @param string $protocol
	 *
	 * @return string
	 * @throws OCMArgumentException
	 */
	public function extractProtocolEntry(string $resourceName, string $protocol): string {
		foreach ($this->getResourceTypes() as $resource) {
			if ($resource->getName() === $resourceName) {
				$entry = $resource->getProtocols()[$protocol] ?? null;
				if (is_null($entry)) {
					throw new OCMArgumentException('protocol not found');
				}

				return (string)$entry;
			}
		}

		throw new OCMArgumentException('resource not found');
	}

	public function setSignatory(Signatory $signatory): void {
		$this->signatory = $signatory;
	}

	public function getSignatory(): ?Signatory {
		return $this->signatory;
	}

	/**
	 * import data from an array
	 *
	 * @param array $data
	 *
	 * @return OCMProvider&static
	 * @throws OCMProviderException in case a descent provider cannot be generated from data
	 */
	public function import(array $data): static {
		$this->setEnabled(is_bool($data['enabled'] ?? '') ? $data['enabled'] : false)
			// Fall back to old apiVersion for Nextcloud 30 compatibility
			->setApiVersion((string)($data['version'] ?? $data['apiVersion'] ?? ''))
			->setEndPoint($data['endPoint'] ?? '');

		$resources = [];
		foreach (($data['resourceTypes'] ?? []) as $resourceData) {
			$resource = new OCMResource();
			$resources[] = $resource->import($resourceData);
		}
		$this->setResourceTypes($resources);

		if (isset($data['publicKey'])) {
			// import details about the remote request signing public key, if available
			$signatory = new Signatory();
			$signatory->setKeyId($data['publicKey']['keyId'] ?? '');
			$signatory->setPublicKey($data['publicKey']['publicKeyPem'] ?? '');
			if ($signatory->getKeyId() !== '' && $signatory->getPublicKey() !== '') {
				$this->setSignatory($signatory);
			}
		}

		if (!$this->looksValid()) {
			throw new OCMProviderException('remote provider does not look valid');
		}

		return $this;
	}


	/**
	 * @return bool
	 */
	private function looksValid(): bool {
		return ($this->getApiVersion() !== '' && $this->getEndPoint() !== '');
	}

	/**
	 * @since 28.0.0
	 */
	public function jsonSerialize(): array {
		$resourceTypes = [];
		foreach ($this->getResourceTypes() as $res) {
			$resourceTypes[] = $res->jsonSerialize();
		}

		$response = [
			'enabled' => $this->isEnabled(),
			'apiVersion' => '1.0-proposal1', // deprecated, but keep it to stay compatible with old version
			'version' => $this->getApiVersion(), // informative but real version
			'endPoint' => $this->getEndPoint(),
			'publicKey' => $this->getSignatory()?->jsonSerialize(),
			'resourceTypes' => $resourceTypes
		];

		$capabilities = $this->getCapabilities();
		$inviteAcceptDialog = $this->getInviteAcceptDialog();
		if ($capabilities) {
			$response['capabilities'] = $capabilities;
		}
		if ($inviteAcceptDialog) {
			$response['inviteAcceptDialog'] = $inviteAcceptDialog;
		}
		return $response;

	}
}