Skip to content

Commit

Permalink
feat: Add app_api app id to saved information about webhook
Browse files Browse the repository at this point in the history
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
  • Loading branch information
come-nc committed Jun 3, 2024
1 parent 8dd8939 commit 35fbdfa
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 3 deletions.
12 changes: 12 additions & 0 deletions apps/webhooks/lib/Controller/WebhooksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\OCSController;
use OCP\IRequest;
use OCP\ISession;
use Psr\Log\LoggerInterface;

/**
Expand All @@ -33,6 +34,7 @@ public function __construct(
private LoggerInterface $logger,
private WebhookListenerMapper $mapper,
private ?string $userId,
private ISession $session,
) {
parent::__construct($appName, $request);
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion apps/webhooks/lib/Db/WebhookListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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');
Expand Down
4 changes: 4 additions & 0 deletions apps/webhooks/lib/Db/WebhookListenerMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function getAll(): array {
}

public function addWebhookListener(
?string $appId,
string $userId,
string $httpMethod,
string $uri,
Expand All @@ -68,6 +69,7 @@ public function addWebhookListener(
) {
$webhookListener = WebhookListener::fromParams(
[
'appId' => $appId,
'userId' => $userId,
'httpMethod' => $httpMethod,
'uri' => $uri,
Expand All @@ -83,6 +85,7 @@ public function addWebhookListener(

public function updateWebhookListener(
int $id,
?string $appId,
string $userId,
string $httpMethod,
string $uri,
Expand All @@ -95,6 +98,7 @@ public function updateWebhookListener(
$webhookListener = WebhookListener::fromParams(
[
'id' => $id,
'appId' => $appId,
'userId' => $userId,
'httpMethod' => $httpMethod,
'uri' => $uri,
Expand Down
4 changes: 4 additions & 0 deletions apps/webhooks/lib/Migration/Version1000Date20240527153425.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
26 changes: 24 additions & 2 deletions apps/webhooks/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"id",
"userId",
"httpMethod",
"uri"
"uri",
"authMethod"
],
"properties": {
"id": {
Expand All @@ -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"
}
}
}
},
Expand Down Expand Up @@ -709,4 +731,4 @@
}
},
"tags": []
}
}
1 change: 1 addition & 0 deletions apps/webhooks/tests/Db/WebhookListenerMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ protected function pruneTables() {

public function testInsertListenerAndGetIt() {
$listener1 = $this->mapper->addWebhookListener(
null,
'bob',
'POST',
'https://webhook.example.com/endpoint',
Expand Down

0 comments on commit 35fbdfa

Please sign in to comment.