From 35fbdfabfa6ca104bb5c8d2964d6f65a41a5d4cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Mon, 3 Jun 2024 16:10:55 +0200 Subject: [PATCH] feat: Add app_api app id to saved information about webhook MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- .../lib/Controller/WebhooksController.php | 12 +++++++++ apps/webhooks/lib/Db/WebhookListener.php | 6 ++++- .../webhooks/lib/Db/WebhookListenerMapper.php | 4 +++ .../Version1000Date20240527153425.php | 4 +++ apps/webhooks/openapi.json | 26 +++++++++++++++++-- .../tests/Db/WebhookListenerMapperTest.php | 1 + 6 files changed, 50 insertions(+), 3 deletions(-) diff --git a/apps/webhooks/lib/Controller/WebhooksController.php b/apps/webhooks/lib/Controller/WebhooksController.php index 21a06f5e563c6..0893743cdb951 100644 --- a/apps/webhooks/lib/Controller/WebhooksController.php +++ b/apps/webhooks/lib/Controller/WebhooksController.php @@ -20,6 +20,7 @@ use OCP\AppFramework\OCS\OCSForbiddenException; use OCP\AppFramework\OCSController; use OCP\IRequest; +use OCP\ISession; use Psr\Log\LoggerInterface; /** @@ -33,6 +34,7 @@ public function __construct( private LoggerInterface $logger, private WebhookListenerMapper $mapper, private ?string $userId, + private ISession $session, ) { parent::__construct($appName, $request); } @@ -97,8 +99,13 @@ public function create( ?string $authMethod, ?array $authData, ): DataResponse { + $appId = null; + if ($this->session->get('app_api') === true) { + $appId = $this->request->getHeader('EX-APP-ID'); + } try { $webhookListener = $this->mapper->addWebhookListener( + $appId, $this->userId, $httpMethod, $uri, @@ -151,9 +158,14 @@ public function update( ?string $authMethod, ?array $authData, ): DataResponse { + $appId = null; + if ($this->session->get('app_api') === true) { + $appId = $this->request->getHeader('EX-APP-ID'); + } try { $webhookListener = $this->mapper->updateWebhookListener( $id, + $appId, $this->userId, $httpMethod, $uri, diff --git a/apps/webhooks/lib/Db/WebhookListener.php b/apps/webhooks/lib/Db/WebhookListener.php index 8e16249b678ab..20d00959f8c9a 100644 --- a/apps/webhooks/lib/Db/WebhookListener.php +++ b/apps/webhooks/lib/Db/WebhookListener.php @@ -16,7 +16,10 @@ * @method string getUserId() */ class WebhookListener extends Entity implements \JsonSerializable { - /** @var string id of the user who added the webhook listener */ + /** @var ?string id of the app_api application who added the webhook listener */ + protected $appId; + + /** @var string id of the user who added the webhook listener */ protected $userId; /** @var string */ @@ -41,6 +44,7 @@ class WebhookListener extends Entity implements \JsonSerializable { protected $authData; public function __construct() { + $this->addType('appId', 'string'); $this->addType('userId', 'string'); $this->addType('httpMethod', 'string'); $this->addType('uri', 'string'); diff --git a/apps/webhooks/lib/Db/WebhookListenerMapper.php b/apps/webhooks/lib/Db/WebhookListenerMapper.php index 5ce824893abcf..3b472231e37eb 100644 --- a/apps/webhooks/lib/Db/WebhookListenerMapper.php +++ b/apps/webhooks/lib/Db/WebhookListenerMapper.php @@ -57,6 +57,7 @@ public function getAll(): array { } public function addWebhookListener( + ?string $appId, string $userId, string $httpMethod, string $uri, @@ -68,6 +69,7 @@ public function addWebhookListener( ) { $webhookListener = WebhookListener::fromParams( [ + 'appId' => $appId, 'userId' => $userId, 'httpMethod' => $httpMethod, 'uri' => $uri, @@ -83,6 +85,7 @@ public function addWebhookListener( public function updateWebhookListener( int $id, + ?string $appId, string $userId, string $httpMethod, string $uri, @@ -95,6 +98,7 @@ public function updateWebhookListener( $webhookListener = WebhookListener::fromParams( [ 'id' => $id, + 'appId' => $appId, 'userId' => $userId, 'httpMethod' => $httpMethod, 'uri' => $uri, diff --git a/apps/webhooks/lib/Migration/Version1000Date20240527153425.php b/apps/webhooks/lib/Migration/Version1000Date20240527153425.php index b6c345a22e2a9..1b06414353382 100755 --- a/apps/webhooks/lib/Migration/Version1000Date20240527153425.php +++ b/apps/webhooks/lib/Migration/Version1000Date20240527153425.php @@ -31,6 +31,10 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt 'notnull' => true, 'length' => 4, ]); + $table->addColumn('app_id', Types::STRING, [ + 'notnull' => false, + 'length' => 64, + ]); $table->addColumn('user_id', Types::STRING, [ 'notnull' => true, 'length' => 64, diff --git a/apps/webhooks/openapi.json b/apps/webhooks/openapi.json index eeed54d780569..3a2c641b0a638 100644 --- a/apps/webhooks/openapi.json +++ b/apps/webhooks/openapi.json @@ -26,7 +26,8 @@ "id", "userId", "httpMethod", - "uri" + "uri", + "authMethod" ], "properties": { "id": { @@ -43,6 +44,27 @@ }, "event": { "type": "string" + }, + "eventFilter": { + "type": "array", + "items": { + "type": "object" + } + }, + "headers": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "authMethod": { + "type": "string" + }, + "authData": { + "type": "object", + "additionalProperties": { + "type": "object" + } } } }, @@ -709,4 +731,4 @@ } }, "tags": [] -} +} \ No newline at end of file diff --git a/apps/webhooks/tests/Db/WebhookListenerMapperTest.php b/apps/webhooks/tests/Db/WebhookListenerMapperTest.php index 4481eb6661eca..c9f6e39b31f25 100644 --- a/apps/webhooks/tests/Db/WebhookListenerMapperTest.php +++ b/apps/webhooks/tests/Db/WebhookListenerMapperTest.php @@ -44,6 +44,7 @@ protected function pruneTables() { public function testInsertListenerAndGetIt() { $listener1 = $this->mapper->addWebhookListener( + null, 'bob', 'POST', 'https://webhook.example.com/endpoint',