Skip to content

Commit 1219a04

Browse files
committed
cloud_federation_api: Introduce OpenAPI spec
Signed-off-by: jld3103 <jld3103yt@gmail.com>
1 parent 9476711 commit 1219a04

File tree

3 files changed

+408
-41
lines changed

3 files changed

+408
-41
lines changed

apps/cloud_federation_api/lib/Controller/RequestHandlerController.php

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* @author Bjoern Schiessle <bjoern@schiessle.org>
66
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
77
* @author Roeland Jago Douma <roeland@famdouma.nl>
8+
* @author Kate Döen <kate.doeen@nextcloud.com>
89
*
910
* @license GNU AGPL version 3 or any later version
1011
*
@@ -22,9 +23,12 @@
2223
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2324
*
2425
*/
26+
2527
namespace OCA\CloudFederationAPI\Controller;
2628

29+
use Exception;
2730
use OCA\CloudFederationAPI\Config;
31+
use OCA\CloudFederationAPI\ResponseDefinitions;
2832
use OCP\AppFramework\Controller;
2933
use OCP\AppFramework\Http;
3034
use OCP\AppFramework\Http\JSONResponse;
@@ -41,6 +45,7 @@
4145
use OCP\IURLGenerator;
4246
use OCP\IUserManager;
4347
use OCP\Share\Exceptions\ShareNotFound;
48+
use OCP\Util;
4449
use Psr\Log\LoggerInterface;
4550

4651
/**
@@ -77,15 +82,15 @@ class RequestHandlerController extends Controller {
7782
private $cloudIdManager;
7883

7984
public function __construct($appName,
80-
IRequest $request,
81-
LoggerInterface $logger,
82-
IUserManager $userManager,
83-
IGroupManager $groupManager,
84-
IURLGenerator $urlGenerator,
85-
ICloudFederationProviderManager $cloudFederationProviderManager,
86-
Config $config,
87-
ICloudFederationFactory $factory,
88-
ICloudIdManager $cloudIdManager
85+
IRequest $request,
86+
LoggerInterface $logger,
87+
IUserManager $userManager,
88+
IGroupManager $groupManager,
89+
IURLGenerator $urlGenerator,
90+
ICloudFederationProviderManager $cloudFederationProviderManager,
91+
Config $config,
92+
ICloudFederationFactory $factory,
93+
ICloudIdManager $cloudIdManager
8994
) {
9095
parent::__construct($appName, $request);
9196

@@ -108,30 +113,29 @@ public function __construct($appName,
108113
*
109114
* @param string $shareWith
110115
* @param string $name resource name (e.g. document.odt)
111-
* @param string $description share description (optional)
116+
* @param string|null $description share description
112117
* @param string $providerId resource UID on the provider side
113118
* @param string $owner provider specific UID of the user who owns the resource
114-
* @param string $ownerDisplayName display name of the user who shared the item
115-
* @param string $sharedBy provider specific UID of the user who shared the resource
116-
* @param string $sharedByDisplayName display name of the user who shared the resource
117-
* @param array $protocol (e,.g. ['name' => 'webdav', 'options' => ['username' => 'john', 'permissions' => 31]])
118-
* @param string $shareType ('group' or 'user' share)
119-
* @param $resourceType ('file', 'calendar',...)
120-
* @return Http\DataResponse|JSONResponse
119+
* @param string|null $ownerDisplayName display name of the user who shared the item
120+
* @param string|null $sharedBy provider specific UID of the user who shared the resource
121+
* @param string|null $sharedByDisplayName display name of the user who shared the resource
122+
* @param array{name: string[], options: array{}} $protocol e,.g. ['name' => 'webdav', 'options' => ['username' => 'john', 'permissions' => 31]]
123+
* @param string $shareType 'group' or 'user' share
124+
* @param string $resourceType 'file', 'calendar',...
125+
*
126+
* @psalm-import-type CloudFederationAddShare from ResponseDefinitions
127+
* @psalm-import-type CloudFederationValidationError from ResponseDefinitions
128+
* @psalm-import-type CloudFederationError from ResponseDefinitions
129+
* @return JSONResponse<CloudFederationAddShare> 201 The notification was successfully received. The display name of the recepient might be returned in the body.
130+
* @return JSONResponse<CloudFederationValidationError> 400 Bad request due to invalid parameters, e.g. when `shareWith` is not found or required properties are missing.
131+
* @return JSONResponse<CloudFederationError> 501 Share type or the resource type is not supported.
121132
*
122133
* Example: curl -H "Content-Type: application/json" -X POST -d '{"shareWith":"admin1@serve1","name":"welcome server2.txt","description":"desc","providerId":"2","owner":"admin2@http://localhost/server2","ownerDisplayName":"admin2 display","shareType":"user","resourceType":"file","protocol":{"name":"webdav","options":{"sharedSecret":"secret","permissions":"webdav-property"}}}' http://localhost/server/index.php/ocm/shares
123134
*/
124-
public function addShare($shareWith, $name, $description, $providerId, $owner, $ownerDisplayName, $sharedBy, $sharedByDisplayName, $protocol, $shareType, $resourceType) {
135+
public function addShare(string $shareWith, string $name, ?string $description, string $providerId, string $owner, ?string $ownerDisplayName, ?string $sharedBy, ?string $sharedByDisplayName, array $protocol, string $shareType, string $resourceType): JSONResponse {
125136

126137
// check if all required parameters are set
127-
if ($shareWith === null ||
128-
$name === null ||
129-
$providerId === null ||
130-
$owner === null ||
131-
$resourceType === null ||
132-
$shareType === null ||
133-
!is_array($protocol) ||
134-
!isset($protocol['name']) ||
138+
if (!isset($protocol['name']) ||
135139
!isset($protocol['options']) ||
136140
!is_array($protocol['options']) ||
137141
!isset($protocol['options']['sharedSecret'])
@@ -202,7 +206,7 @@ public function addShare($shareWith, $name, $description, $providerId, $owner, $
202206
['message' => $e->getMessage()],
203207
$e->getCode()
204208
);
205-
} catch (\Exception $e) {
209+
} catch (Exception $e) {
206210
$this->logger->error($e->getMessage(), ['exception' => $e]);
207211
return new JSONResponse(
208212
['message' => 'Internal error at ' . $this->urlGenerator->getBaseUrl()],
@@ -228,20 +232,21 @@ public function addShare($shareWith, $name, $description, $providerId, $owner, $
228232
* @PublicPage
229233
* @BruteForceProtection(action=receiveFederatedShareNotification)
230234
*
231-
* @param string $notificationType (notification type, e.g. SHARE_ACCEPTED)
232-
* @param string $resourceType (calendar, file, contact,...)
233-
* @param string $providerId id of the share
234-
* @param array $notification the actual payload of the notification
235-
* @return JSONResponse
235+
* @param string $notificationType notification type, e.g. SHARE_ACCEPTED
236+
* @param string $resourceType calendar, file, contact,...
237+
* @param string|null $providerId id of the share
238+
* @param array{}|null $notification the actual payload of the notification
239+
* @return JSONResponse<array{}> 201 The notification was successfully received
240+
*
241+
* @psalm-import-type CloudFederationValidationError from ResponseDefinitions
242+
* @psalm-import-type CloudFederationError from ResponseDefinitions
243+
* @return JSONResponse<CloudFederationValidationError> 400 Bad request due to invalid parameters, e.g. when `type` is invalid or missing.
244+
* @return JSONResponse<CloudFederationError> 501 The resource type is not supported.
236245
*/
237-
public function receiveNotification($notificationType, $resourceType, $providerId, array $notification) {
246+
public function receiveNotification(string $notificationType, string $resourceType, ?string $providerId, ?array $notification): JSONResponse {
238247

239248
// check if all required parameters are set
240-
if ($notificationType === null ||
241-
$resourceType === null ||
242-
$providerId === null ||
243-
!is_array($notification)
244-
) {
249+
if ($providerId === null || !is_array($notification)) {
245250
return new JSONResponse(
246251
['message' => 'Missing arguments'],
247252
Http::STATUS_BAD_REQUEST
@@ -274,14 +279,14 @@ public function receiveNotification($notificationType, $resourceType, $providerI
274279
$response = new JSONResponse(['message' => 'RESOURCE_NOT_FOUND'], Http::STATUS_FORBIDDEN);
275280
$response->throttle();
276281
return $response;
277-
} catch (\Exception $e) {
282+
} catch (Exception $e) {
278283
return new JSONResponse(
279284
['message' => 'Internal error at ' . $this->urlGenerator->getBaseUrl()],
280285
Http::STATUS_BAD_REQUEST
281286
);
282287
}
283288

284-
return new JSONResponse($result,Http::STATUS_CREATED);
289+
return new JSONResponse($result, Http::STATUS_CREATED);
285290
}
286291

287292
/**
@@ -293,7 +298,7 @@ public function receiveNotification($notificationType, $resourceType, $providerI
293298
private function mapUid($uid) {
294299
// FIXME this should be a method in the user management instead
295300
$this->logger->debug('shareWith before, ' . $uid, ['app' => $this->appName]);
296-
\OCP\Util::emitHook(
301+
Util::emitHook(
297302
'\OCA\Files_Sharing\API\Server2Server',
298303
'preLoginNameUsedAsUserName',
299304
['uid' => &$uid]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* @copyright Copyright (c) 2023 Kate Döen <kate.doeen@nextcloud.com>
6+
*
7+
* @author Kate Döen <kate.doeen@nextcloud.com>
8+
*
9+
* @license GNU AGPL version 3 or any later version
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Affero General Public License as
13+
* published by the Free Software Foundation, either version 3 of the
14+
* License, or (at your option) any later version.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*
24+
*/
25+
26+
namespace OCA\CloudFederationAPI;
27+
28+
/**
29+
* @psalm-type CloudFederationAddShare = array{
30+
* recipientDisplayName: ?string,
31+
* }
32+
*
33+
* @psalm-type CloudFederationError = array{
34+
* message: string,
35+
* }
36+
*
37+
* @psalm-type CloudFederationValidationError = CloudFederationError&array{
38+
* validationErrors: array{
39+
* name: string,
40+
* message: string|null,
41+
* }[],
42+
* }
43+
*/
44+
class ResponseDefinitions {
45+
}

0 commit comments

Comments
 (0)