Skip to content

Commit

Permalink
fix(webhooks): Fix a few more psalm notices
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 11, 2024
1 parent bff7d3c commit e0b9ff4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 21 deletions.
3 changes: 3 additions & 0 deletions apps/webhook_listeners/lib/BackgroundJobs/WebhookCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public function __construct(
parent::__construct($timeFactory);
}

/**
* @param array $argument
*/
protected function run($argument): void {
[$data, $webhookId] = $argument;
$webhookListener = $this->mapper->getById($webhookId);
Expand Down
15 changes: 10 additions & 5 deletions apps/webhook_listeners/lib/Controller/WebhooksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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;
Expand Down Expand Up @@ -53,7 +54,7 @@ public function __construct(
* 200: Webhook registrations returned
*/
#[ApiRoute(verb: 'GET', url: '/api/v1/webhooks')]
#[AuthorizedAdminSetting(settings:'OCA\WebhookListeners\Settings\Admin')]
#[AuthorizedAdminSetting(settings:Admin::class)]
public function index(): DataResponse {
try {
$webhookListeners = $this->mapper->getAll();
Expand Down Expand Up @@ -82,7 +83,7 @@ public function index(): DataResponse {
* 200: Webhook registration returned
*/
#[ApiRoute(verb: 'GET', url: '/api/v1/webhooks/{id}')]
#[AuthorizedAdminSetting(settings:'OCA\WebhookListeners\Settings\Admin')]
#[AuthorizedAdminSetting(settings:Admin::class)]
public function show(int $id): DataResponse {
try {
return new DataResponse($this->mapper->getById($id)->jsonSerialize());
Expand Down Expand Up @@ -114,7 +115,7 @@ public function show(int $id): DataResponse {
* @throws OCSException Other error
*/
#[ApiRoute(verb: 'POST', url: '/api/v1/webhooks')]
#[AuthorizedAdminSetting(settings:'OCA\WebhookListeners\Settings\Admin')]
#[AuthorizedAdminSetting(settings:Admin::class)]
public function create(
string $httpMethod,
string $uri,
Expand All @@ -135,6 +136,8 @@ public function create(
throw new OCSBadRequestException('This auth method does not exist');
}
try {
/* We can never reach here without a user in session */
assert(is_string($this->userId));
$webhookListener = $this->mapper->addWebhookListener(
$appId,
$this->userId,
Expand Down Expand Up @@ -178,7 +181,7 @@ public function create(
* @throws OCSException Other error
*/
#[ApiRoute(verb: 'POST', url: '/api/v1/webhooks/{id}')]
#[AuthorizedAdminSetting(settings:'OCA\WebhookListeners\Settings\Admin')]
#[AuthorizedAdminSetting(settings:Admin::class)]
public function update(
int $id,
string $httpMethod,
Expand All @@ -200,6 +203,8 @@ public function update(
throw new OCSBadRequestException('This auth method does not exist');
}
try {
/* We can never reach here without a user in session */
assert(is_string($this->userId));
$webhookListener = $this->mapper->updateWebhookListener(
$id,
$appId,
Expand Down Expand Up @@ -237,7 +242,7 @@ public function update(
* @throws OCSException Other error
*/
#[ApiRoute(verb: 'DELETE', url: '/api/v1/webhooks/{id}')]
#[AuthorizedAdminSetting(settings:'OCA\WebhookListeners\Settings\Admin')]
#[AuthorizedAdminSetting(settings:Admin::class)]
public function destroy(int $id): DataResponse {
try {
$deleted = $this->mapper->deleteById($id);
Expand Down
58 changes: 42 additions & 16 deletions apps/webhook_listeners/lib/Db/WebhookListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,60 @@
* @method ?array getHeaders()
* @method ?string getAuthData()
* @method void setAuthData(?string $data)
* @method ?string getAuthMethod()
* @method string getAuthMethod()
* @psalm-suppress PropertyNotSetInConstructor
*/
class WebhookListener extends Entity implements \JsonSerializable {
/** @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 */
/**
* @var ?string id of the app_api application who added the webhook listener
*/
protected $appId = null;

/**
* @var string id of the user who added the webhook listener
* @psalm-suppress PropertyNotSetInConstructor
*/
protected $userId;

/** @var string */
/**
* @var string
* @psalm-suppress PropertyNotSetInConstructor
*/
protected $httpMethod;

/** @var string */
/**
* @var string
* @psalm-suppress PropertyNotSetInConstructor
*/
protected $uri;

/** @var string */
/**
* @var string
* @psalm-suppress PropertyNotSetInConstructor
*/
protected $event;

/** @var array */
/**
* @var array
* @psalm-suppress PropertyNotSetInConstructor
*/
protected $eventFilter;

/** @var ?array */
protected $headers;
/**
* @var ?array
*/
protected $headers = null;

/** @var ?string */
/**
* @var string
* @psalm-suppress PropertyNotSetInConstructor
*/
protected $authMethod;

/** @var ?string */
protected $authData;
/**
* @var ?string
*/
protected $authData = null;

private ICrypto $crypto;

Expand All @@ -75,10 +100,11 @@ public function getAuthMethodEnum(): AuthMethod {
}

public function getAuthDataClear(): array {
if ($this->authData === null) {
$authData = $this->getAuthData();
if ($authData === null) {
return [];
}
return json_decode($this->crypto->decrypt($this->getAuthData()), associative:true, flags:JSON_THROW_ON_ERROR);
return json_decode($this->crypto->decrypt($authData), associative:true, flags:JSON_THROW_ON_ERROR);
}

public function setAuthDataClear(
Expand Down

0 comments on commit e0b9ff4

Please sign in to comment.