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
298
299
300
|
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\WebhookListeners\Controller;
use OCA\WebhookListeners\Db\AuthMethod;
use OCA\WebhookListeners\Db\WebhookListener;
use OCA\WebhookListeners\Db\WebhookListenerMapper;
use OCA\WebhookListeners\ResponseDefinitions;
use OCA\WebhookListeners\Settings\Admin;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\ApiRoute;
use OCP\AppFramework\Http\Attribute\AppApiAdminAccessWithoutUser;
use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSBadRequestException;
use OCP\AppFramework\OCS\OCSException;
use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
use OCP\IRequest;
use OCP\ISession;
use Psr\Log\LoggerInterface;
/**
* @psalm-import-type WebhookListenersWebhookInfo from ResponseDefinitions
*/
#[OpenAPI(scope: OpenAPI::SCOPE_ADMINISTRATION)]
class WebhooksController extends OCSController {
public function __construct(
string $appName,
IRequest $request,
private LoggerInterface $logger,
private WebhookListenerMapper $mapper,
private ?string $userId,
private ISession $session,
) {
parent::__construct($appName, $request);
}
/**
* List registered webhooks
*
* @param string|null $uri The callback URI to filter by
* @return DataResponse<Http::STATUS_OK, list<WebhookListenersWebhookInfo>, array{}>
* @throws OCSException Other internal error
*
* 200: Webhook registrations returned
*/
#[ApiRoute(verb: 'GET', url: '/api/v1/webhooks')]
#[AuthorizedAdminSetting(settings:Admin::class)]
#[AppApiAdminAccessWithoutUser]
public function index(?string $uri = null): DataResponse {
try {
if ($uri !== null) {
$webhookListeners = $this->mapper->getByUri($uri);
} else {
$webhookListeners = $this->mapper->getAll();
}
return new DataResponse(array_values(array_map(
fn (WebhookListener $listener): array => $listener->jsonSerialize(),
$webhookListeners
)));
} catch (\Exception $e) {
$this->logger->error('Error when listing webhooks', ['exception' => $e]);
throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e);
}
}
/**
* Get details on a registered webhook
*
* @param int $id id of the webhook
*
* @return DataResponse<Http::STATUS_OK, WebhookListenersWebhookInfo, array{}>
* @throws OCSNotFoundException Webhook not found
* @throws OCSException Other internal error
*
* 200: Webhook registration returned
*/
#[ApiRoute(verb: 'GET', url: '/api/v1/webhooks/{id}')]
#[AuthorizedAdminSetting(settings:Admin::class)]
#[AppApiAdminAccessWithoutUser]
public function show(int $id): DataResponse {
try {
return new DataResponse($this->mapper->getById($id)->jsonSerialize());
} catch (DoesNotExistException $e) {
throw new OCSNotFoundException($e->getMessage(), $e);
} catch (\Exception $e) {
$this->logger->error('Error when getting webhook', ['exception' => $e]);
throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e);
}
}
/**
* Register a new webhook
*
* @param string $httpMethod HTTP method to use to contact the webhook
* @param string $uri Webhook URI endpoint
* @param string $event Event class name to listen to
* @param ?array<string,mixed> $eventFilter Mongo filter to apply to the serialized data to decide if firing
* @param ?string $userIdFilter User id to filter on. The webhook will only be called by requests from this user. Empty or null means no filtering.
* @param ?array<string,string> $headers Array of headers to send
* @param "none"|"header"|null $authMethod Authentication method to use
* @param ?array<string,mixed> $authData Array of data for authentication
*
* @return DataResponse<Http::STATUS_OK, WebhookListenersWebhookInfo, array{}>
*
* 200: Webhook registration returned
*
* @throws OCSBadRequestException Bad request
* @throws OCSForbiddenException Insufficient permissions
* @throws OCSException Other error
*/
#[ApiRoute(verb: 'POST', url: '/api/v1/webhooks')]
#[AuthorizedAdminSetting(settings:Admin::class)]
#[AppApiAdminAccessWithoutUser]
public function create(
string $httpMethod,
string $uri,
string $event,
?array $eventFilter,
?string $userIdFilter,
?array $headers,
?string $authMethod,
#[\SensitiveParameter]
?array $authData,
): DataResponse {
$appId = null;
if ($this->session->get('app_api') === true) {
$appId = $this->request->getHeader('ex-app-id');
}
try {
$authMethod = AuthMethod::from($authMethod ?? AuthMethod::None->value);
} catch (\ValueError $e) {
throw new OCSBadRequestException('This auth method does not exist');
}
try {
$webhookListener = $this->mapper->addWebhookListener(
$appId,
$this->userId,
$httpMethod,
$uri,
$event,
$eventFilter,
$userIdFilter,
$headers,
$authMethod,
$authData,
);
return new DataResponse($webhookListener->jsonSerialize());
} catch (\UnexpectedValueException $e) {
throw new OCSBadRequestException($e->getMessage(), $e);
} catch (\DomainException $e) {
throw new OCSForbiddenException($e->getMessage(), $e);
} catch (\Exception $e) {
$this->logger->error('Error when inserting webhook', ['exception' => $e]);
throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e);
}
}
/**
* Update an existing webhook registration
*
* @param int $id id of the webhook
* @param string $httpMethod HTTP method to use to contact the webhook
* @param string $uri Webhook URI endpoint
* @param string $event Event class name to listen to
* @param ?array<string,mixed> $eventFilter Mongo filter to apply to the serialized data to decide if firing
* @param ?string $userIdFilter User id to filter on. The webhook will only be called by requests from this user. Empty or null means no filtering.
* @param ?array<string,string> $headers Array of headers to send
* @param "none"|"header"|null $authMethod Authentication method to use
* @param ?array<string,mixed> $authData Array of data for authentication
*
* @return DataResponse<Http::STATUS_OK, WebhookListenersWebhookInfo, array{}>
*
* 200: Webhook registration returned
*
* @throws OCSBadRequestException Bad request
* @throws OCSForbiddenException Insufficient permissions
* @throws OCSException Other error
*/
#[ApiRoute(verb: 'POST', url: '/api/v1/webhooks/{id}')]
#[AuthorizedAdminSetting(settings:Admin::class)]
#[AppApiAdminAccessWithoutUser]
public function update(
int $id,
string $httpMethod,
string $uri,
string $event,
?array $eventFilter,
?string $userIdFilter,
?array $headers,
?string $authMethod,
#[\SensitiveParameter]
?array $authData,
): DataResponse {
$appId = null;
if ($this->session->get('app_api') === true) {
$appId = $this->request->getHeader('ex-app-id');
}
try {
$authMethod = AuthMethod::from($authMethod ?? AuthMethod::None->value);
} catch (\ValueError $e) {
throw new OCSBadRequestException('This auth method does not exist');
}
try {
$webhookListener = $this->mapper->updateWebhookListener(
$id,
$appId,
$this->userId,
$httpMethod,
$uri,
$event,
$eventFilter,
$userIdFilter,
$headers,
$authMethod,
$authData,
);
return new DataResponse($webhookListener->jsonSerialize());
} catch (\UnexpectedValueException $e) {
throw new OCSBadRequestException($e->getMessage(), $e);
} catch (\DomainException $e) {
throw new OCSForbiddenException($e->getMessage(), $e);
} catch (\Exception $e) {
$this->logger->error('Error when updating flow with id ' . $id, ['exception' => $e]);
throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e);
}
}
/**
* Remove an existing webhook registration
*
* @param int $id id of the webhook
*
* @return DataResponse<Http::STATUS_OK, bool, array{}>
*
* 200: Boolean returned whether something was deleted
*
* @throws OCSBadRequestException Bad request
* @throws OCSForbiddenException Insufficient permissions
* @throws OCSException Other error
*/
#[ApiRoute(verb: 'DELETE', url: '/api/v1/webhooks/{id}')]
#[AuthorizedAdminSetting(settings:Admin::class)]
#[AppApiAdminAccessWithoutUser]
public function destroy(int $id): DataResponse {
try {
$deleted = $this->mapper->deleteById($id);
return new DataResponse($deleted);
} catch (\UnexpectedValueException $e) {
throw new OCSBadRequestException($e->getMessage(), $e);
} catch (\DomainException $e) {
throw new OCSForbiddenException($e->getMessage(), $e);
} catch (\Exception $e) {
$this->logger->error('Error when deleting flow with id ' . $id, ['exception' => $e]);
throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e);
}
}
/**
* Remove all existing webhook registration mapped to an AppAPI app id
*
* @param string $appid id of the app, as in the ex-app-id for creation
*
* @return DataResponse<Http::STATUS_OK, int, array{}>
*
* 200: Integer number of registrations deleted
*
* @throws OCSBadRequestException Bad request
* @throws OCSForbiddenException Insufficient permissions
* @throws OCSException Other error
*/
#[ApiRoute(verb: 'DELETE', url: '/api/v1/webhooks/byappid/{appid}')]
#[AuthorizedAdminSetting(settings:Admin::class)]
#[AppApiAdminAccessWithoutUser]
public function deleteByAppId(string $appid): DataResponse {
try {
$deletedCount = $this->mapper->deleteByAppId($appid);
return new DataResponse($deletedCount);
} catch (\UnexpectedValueException $e) {
throw new OCSBadRequestException($e->getMessage(), $e);
} catch (\DomainException $e) {
throw new OCSForbiddenException($e->getMessage(), $e);
} catch (\Exception $e) {
$this->logger->error('Error when deleting flows for app id ' . $appid, ['exception' => $e]);
throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e);
}
}
}
|