Skip to content

Commit 15515e4

Browse files
committed
feat: add token array to webhook definition and db entries
Signed-off-by: Jana Peper <[email protected]>
1 parent 6c362cb commit 15515e4

File tree

9 files changed

+104
-8
lines changed

9 files changed

+104
-8
lines changed

apps/webhook_listeners/composer/autoload.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
echo $err;
1515
}
1616
}
17-
trigger_error(
18-
$err,
19-
E_USER_ERROR
20-
);
17+
throw new RuntimeException($err);
2118
}
2219

2320
require_once __DIR__ . '/composer/autoload_real.php';

apps/webhook_listeners/composer/composer/InstalledVersions.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,23 @@
2626
*/
2727
class InstalledVersions
2828
{
29+
/**
30+
* @var string|null if set (by reflection by Composer), this should be set to the path where this class is being copied to
31+
* @internal
32+
*/
33+
private static $selfDir = null;
34+
2935
/**
3036
* @var mixed[]|null
3137
* @psalm-var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}|array{}|null
3238
*/
3339
private static $installed;
3440

41+
/**
42+
* @var bool
43+
*/
44+
private static $installedIsLocalDir;
45+
3546
/**
3647
* @var bool|null
3748
*/
@@ -309,6 +320,24 @@ public static function reload($data)
309320
{
310321
self::$installed = $data;
311322
self::$installedByVendor = array();
323+
324+
// when using reload, we disable the duplicate protection to ensure that self::$installed data is
325+
// always returned, but we cannot know whether it comes from the installed.php in __DIR__ or not,
326+
// so we have to assume it does not, and that may result in duplicate data being returned when listing
327+
// all installed packages for example
328+
self::$installedIsLocalDir = false;
329+
}
330+
331+
/**
332+
* @return string
333+
*/
334+
private static function getSelfDir()
335+
{
336+
if (self::$selfDir === null) {
337+
self::$selfDir = strtr(__DIR__, '\\', '/');
338+
}
339+
340+
return self::$selfDir;
312341
}
313342

314343
/**
@@ -322,19 +351,27 @@ private static function getInstalled()
322351
}
323352

324353
$installed = array();
354+
$copiedLocalDir = false;
325355

326356
if (self::$canGetVendors) {
357+
$selfDir = self::getSelfDir();
327358
foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) {
359+
$vendorDir = strtr($vendorDir, '\\', '/');
328360
if (isset(self::$installedByVendor[$vendorDir])) {
329361
$installed[] = self::$installedByVendor[$vendorDir];
330362
} elseif (is_file($vendorDir.'/composer/installed.php')) {
331363
/** @var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>} $required */
332364
$required = require $vendorDir.'/composer/installed.php';
333-
$installed[] = self::$installedByVendor[$vendorDir] = $required;
334-
if (null === self::$installed && strtr($vendorDir.'/composer', '\\', '/') === strtr(__DIR__, '\\', '/')) {
335-
self::$installed = $installed[count($installed) - 1];
365+
self::$installedByVendor[$vendorDir] = $required;
366+
$installed[] = $required;
367+
if (self::$installed === null && $vendorDir.'/composer' === $selfDir) {
368+
self::$installed = $required;
369+
self::$installedIsLocalDir = true;
336370
}
337371
}
372+
if (self::$installedIsLocalDir && $vendorDir.'/composer' === $selfDir) {
373+
$copiedLocalDir = true;
374+
}
338375
}
339376
}
340377

@@ -350,7 +387,7 @@ private static function getInstalled()
350387
}
351388
}
352389

353-
if (self::$installed !== array()) {
390+
if (self::$installed !== array() && !$copiedLocalDir) {
354391
$installed[] = self::$installed;
355392
}
356393

apps/webhook_listeners/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
'OCA\\WebhookListeners\\Listener\\WebhooksEventListener' => $baseDir . '/../lib/Listener/WebhooksEventListener.php',
1818
'OCA\\WebhookListeners\\Migration\\Version1000Date20240527153425' => $baseDir . '/../lib/Migration/Version1000Date20240527153425.php',
1919
'OCA\\WebhookListeners\\Migration\\Version1001Date20240716184935' => $baseDir . '/../lib/Migration/Version1001Date20240716184935.php',
20+
'OCA\\WebhookListeners\\Migration\\Version1500Date20251007130000' => $baseDir . '/../lib/Migration/Version1500Date20251007130000.php',
2021
'OCA\\WebhookListeners\\ResponseDefinitions' => $baseDir . '/../lib/ResponseDefinitions.php',
2122
'OCA\\WebhookListeners\\Service\\PHPMongoQuery' => $baseDir . '/../lib/Service/PHPMongoQuery.php',
2223
'OCA\\WebhookListeners\\Settings\\Admin' => $baseDir . '/../lib/Settings/Admin.php',

apps/webhook_listeners/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class ComposerStaticInitWebhookListeners
3232
'OCA\\WebhookListeners\\Listener\\WebhooksEventListener' => __DIR__ . '/..' . '/../lib/Listener/WebhooksEventListener.php',
3333
'OCA\\WebhookListeners\\Migration\\Version1000Date20240527153425' => __DIR__ . '/..' . '/../lib/Migration/Version1000Date20240527153425.php',
3434
'OCA\\WebhookListeners\\Migration\\Version1001Date20240716184935' => __DIR__ . '/..' . '/../lib/Migration/Version1001Date20240716184935.php',
35+
'OCA\\WebhookListeners\\Migration\\Version1500Date20251007130000' => __DIR__ . '/..' . '/../lib/Migration/Version1500Date20251007130000.php',
3536
'OCA\\WebhookListeners\\ResponseDefinitions' => __DIR__ . '/..' . '/../lib/ResponseDefinitions.php',
3637
'OCA\\WebhookListeners\\Service\\PHPMongoQuery' => __DIR__ . '/..' . '/../lib/Service/PHPMongoQuery.php',
3738
'OCA\\WebhookListeners\\Settings\\Admin' => __DIR__ . '/..' . '/../lib/Settings/Admin.php',

apps/webhook_listeners/lib/Controller/WebhooksController.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public function show(int $id): DataResponse {
112112
* @param ?array<string,string> $headers Array of headers to send
113113
* @param "none"|"header"|null $authMethod Authentication method to use
114114
* @param ?array<string,mixed> $authData Array of data for authentication
115+
* @param ?array<string,mixed> $tokenNeeded List of user ids for which to include auth tokens in the event
115116
*
116117
* @return DataResponse<Http::STATUS_OK, WebhookListenersWebhookInfo, array{}>
117118
*
@@ -134,6 +135,7 @@ public function create(
134135
?string $authMethod,
135136
#[\SensitiveParameter]
136137
?array $authData,
138+
?array $tokenNeeded = null,
137139
): DataResponse {
138140
$appId = null;
139141
if ($this->session->get('app_api') === true) {
@@ -156,6 +158,7 @@ public function create(
156158
$headers,
157159
$authMethod,
158160
$authData,
161+
$tokenNeeded,
159162
);
160163
return new DataResponse($webhookListener->jsonSerialize());
161164
} catch (\UnexpectedValueException $e) {
@@ -180,6 +183,7 @@ public function create(
180183
* @param ?array<string,string> $headers Array of headers to send
181184
* @param "none"|"header"|null $authMethod Authentication method to use
182185
* @param ?array<string,mixed> $authData Array of data for authentication
186+
* @param ?array<string,mixed> $tokenNeeded List of user ids for which to include auth tokens in the event
183187
*
184188
* @return DataResponse<Http::STATUS_OK, WebhookListenersWebhookInfo, array{}>
185189
*
@@ -203,6 +207,7 @@ public function update(
203207
?string $authMethod,
204208
#[\SensitiveParameter]
205209
?array $authData,
210+
?array $tokenNeeded = null,
206211
): DataResponse {
207212
$appId = null;
208213
if ($this->session->get('app_api') === true) {
@@ -226,6 +231,7 @@ public function update(
226231
$headers,
227232
$authMethod,
228233
$authData,
234+
$tokenNeeded,
229235
);
230236
return new DataResponse($webhookListener->jsonSerialize());
231237
} catch (\UnexpectedValueException $e) {

apps/webhook_listeners/lib/Db/WebhookListener.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ class WebhookListener extends Entity implements \JsonSerializable {
8484
*/
8585
protected $authData = null;
8686

87+
/**
88+
* @var array
89+
* @psalm-suppress PropertyNotSetInConstructor
90+
*/
91+
protected $tokenNeeded;
92+
8793
private ICrypto $crypto;
8894

8995
public function __construct(
@@ -103,6 +109,7 @@ public function __construct(
103109
$this->addType('headers', 'json');
104110
$this->addType('authMethod', 'string');
105111
$this->addType('authData', 'string');
112+
$this->addType('tokenNeeded', 'json');
106113
}
107114

108115
public function getAuthMethodEnum(): AuthMethod {

apps/webhook_listeners/lib/Db/WebhookListenerMapper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public function addWebhookListener(
8282
AuthMethod $authMethod,
8383
#[\SensitiveParameter]
8484
?array $authData,
85+
?array $tokenNeeded
8586
): WebhookListener {
8687
/* Remove any superfluous antislash */
8788
$event = ltrim($event, '\\');
@@ -99,6 +100,7 @@ public function addWebhookListener(
99100
'userIdFilter' => $userIdFilter ?? '',
100101
'headers' => $headers,
101102
'authMethod' => $authMethod->value,
103+
'tokenNeeded' => $tokenNeeded ?? [],
102104
]
103105
);
104106
$webhookListener->setAuthDataClear($authData);
@@ -122,6 +124,7 @@ public function updateWebhookListener(
122124
AuthMethod $authMethod,
123125
#[\SensitiveParameter]
124126
?array $authData,
127+
?array $tokenNeeded,
125128
): WebhookListener {
126129
/* Remove any superfluous antislash */
127130
$event = ltrim($event, '\\');
@@ -140,6 +143,7 @@ public function updateWebhookListener(
140143
'userIdFilter' => $userIdFilter ?? '',
141144
'headers' => $headers,
142145
'authMethod' => $authMethod->value,
146+
'tokenNeeded' => $tokenNeeded ?? [],
143147
]
144148
);
145149
$webhookListener->setAuthDataClear($authData);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\WebhookListeners\Migration;
11+
12+
use Closure;
13+
use OCA\WebhookListeners\Db\WebhookListenerMapper;
14+
use OCP\DB\ISchemaWrapper;
15+
use OCP\Migration\IOutput;
16+
use OCP\Migration\SimpleMigrationStep;
17+
use OCP\DB\Types;
18+
19+
class Version1500Date20251007130000 extends SimpleMigrationStep {
20+
21+
/**
22+
* @param IOutput $output
23+
* @param Closure(): ISchemaWrapper $schemaClosure
24+
* @param array $options
25+
* @return null|ISchemaWrapper
26+
*/
27+
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
28+
$schema = $schemaClosure();
29+
30+
if ($schema->hasTable(WebhookListenerMapper::TABLE_NAME)) {
31+
$table = $schema->getTable(WebhookListenerMapper::TABLE_NAME);
32+
if(!$table->hasColumn('token_needed')){
33+
$table->addColumn('token_needed', Types::TEXT, [
34+
'notnull' => false,
35+
]);
36+
}
37+
38+
return $schema;
39+
}
40+
return null;
41+
}
42+
}

apps/webhook_listeners/lib/ResponseDefinitions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* headers?: array<string,string>,
2222
* authMethod: string,
2323
* authData?: array<string,mixed>,
24+
* tokenNeeded?: array<string,mixed>,
2425
* }
2526
*/
2627
class ResponseDefinitions {

0 commit comments

Comments
 (0)