@@ -6,46 +6,46 @@ |
||
| 6 | 6 | |
| 7 | 7 | class ComposerStaticInitWebhookListeners |
| 8 | 8 | { |
| 9 | - public static $prefixLengthsPsr4 = array ( |
|
| 9 | + public static $prefixLengthsPsr4 = array( |
|
| 10 | 10 | 'O' => |
| 11 | - array ( |
|
| 11 | + array( |
|
| 12 | 12 | 'OCA\\WebhookListeners\\' => 21, |
| 13 | 13 | ), |
| 14 | 14 | ); |
| 15 | 15 | |
| 16 | - public static $prefixDirsPsr4 = array ( |
|
| 16 | + public static $prefixDirsPsr4 = array( |
|
| 17 | 17 | 'OCA\\WebhookListeners\\' => |
| 18 | - array ( |
|
| 19 | - 0 => __DIR__ . '/..' . '/../lib', |
|
| 18 | + array( |
|
| 19 | + 0 => __DIR__.'/..'.'/../lib', |
|
| 20 | 20 | ), |
| 21 | 21 | ); |
| 22 | 22 | |
| 23 | - public static $classMap = array ( |
|
| 24 | - 'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php', |
|
| 25 | - 'OCA\\WebhookListeners\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php', |
|
| 26 | - 'OCA\\WebhookListeners\\BackgroundJobs\\WebhookCall' => __DIR__ . '/..' . '/../lib/BackgroundJobs/WebhookCall.php', |
|
| 27 | - 'OCA\\WebhookListeners\\BackgroundJobs\\WebhookTokenCleanup' => __DIR__ . '/..' . '/../lib/BackgroundJobs/WebhookTokenCleanup.php', |
|
| 28 | - 'OCA\\WebhookListeners\\Command\\ListWebhooks' => __DIR__ . '/..' . '/../lib/Command/ListWebhooks.php', |
|
| 29 | - 'OCA\\WebhookListeners\\Controller\\WebhooksController' => __DIR__ . '/..' . '/../lib/Controller/WebhooksController.php', |
|
| 30 | - 'OCA\\WebhookListeners\\Db\\AuthMethod' => __DIR__ . '/..' . '/../lib/Db/AuthMethod.php', |
|
| 31 | - 'OCA\\WebhookListeners\\Db\\EphemeralToken' => __DIR__ . '/..' . '/../lib/Db/EphemeralToken.php', |
|
| 32 | - 'OCA\\WebhookListeners\\Db\\EphemeralTokenMapper' => __DIR__ . '/..' . '/../lib/Db/EphemeralTokenMapper.php', |
|
| 33 | - 'OCA\\WebhookListeners\\Db\\WebhookListener' => __DIR__ . '/..' . '/../lib/Db/WebhookListener.php', |
|
| 34 | - 'OCA\\WebhookListeners\\Db\\WebhookListenerMapper' => __DIR__ . '/..' . '/../lib/Db/WebhookListenerMapper.php', |
|
| 35 | - 'OCA\\WebhookListeners\\Listener\\WebhooksEventListener' => __DIR__ . '/..' . '/../lib/Listener/WebhooksEventListener.php', |
|
| 36 | - 'OCA\\WebhookListeners\\Migration\\Version1000Date20240527153425' => __DIR__ . '/..' . '/../lib/Migration/Version1000Date20240527153425.php', |
|
| 37 | - 'OCA\\WebhookListeners\\Migration\\Version1001Date20240716184935' => __DIR__ . '/..' . '/../lib/Migration/Version1001Date20240716184935.php', |
|
| 38 | - 'OCA\\WebhookListeners\\Migration\\Version1500Date20251007130000' => __DIR__ . '/..' . '/../lib/Migration/Version1500Date20251007130000.php', |
|
| 39 | - 'OCA\\WebhookListeners\\ResponseDefinitions' => __DIR__ . '/..' . '/../lib/ResponseDefinitions.php', |
|
| 40 | - 'OCA\\WebhookListeners\\Service\\PHPMongoQuery' => __DIR__ . '/..' . '/../lib/Service/PHPMongoQuery.php', |
|
| 41 | - 'OCA\\WebhookListeners\\Service\\TokenService' => __DIR__ . '/..' . '/../lib/Service/TokenService.php', |
|
| 42 | - 'OCA\\WebhookListeners\\Settings\\Admin' => __DIR__ . '/..' . '/../lib/Settings/Admin.php', |
|
| 43 | - 'OCA\\WebhookListeners\\Settings\\AdminSection' => __DIR__ . '/..' . '/../lib/Settings/AdminSection.php', |
|
| 23 | + public static $classMap = array( |
|
| 24 | + 'Composer\\InstalledVersions' => __DIR__.'/..'.'/composer/InstalledVersions.php', |
|
| 25 | + 'OCA\\WebhookListeners\\AppInfo\\Application' => __DIR__.'/..'.'/../lib/AppInfo/Application.php', |
|
| 26 | + 'OCA\\WebhookListeners\\BackgroundJobs\\WebhookCall' => __DIR__.'/..'.'/../lib/BackgroundJobs/WebhookCall.php', |
|
| 27 | + 'OCA\\WebhookListeners\\BackgroundJobs\\WebhookTokenCleanup' => __DIR__.'/..'.'/../lib/BackgroundJobs/WebhookTokenCleanup.php', |
|
| 28 | + 'OCA\\WebhookListeners\\Command\\ListWebhooks' => __DIR__.'/..'.'/../lib/Command/ListWebhooks.php', |
|
| 29 | + 'OCA\\WebhookListeners\\Controller\\WebhooksController' => __DIR__.'/..'.'/../lib/Controller/WebhooksController.php', |
|
| 30 | + 'OCA\\WebhookListeners\\Db\\AuthMethod' => __DIR__.'/..'.'/../lib/Db/AuthMethod.php', |
|
| 31 | + 'OCA\\WebhookListeners\\Db\\EphemeralToken' => __DIR__.'/..'.'/../lib/Db/EphemeralToken.php', |
|
| 32 | + 'OCA\\WebhookListeners\\Db\\EphemeralTokenMapper' => __DIR__.'/..'.'/../lib/Db/EphemeralTokenMapper.php', |
|
| 33 | + 'OCA\\WebhookListeners\\Db\\WebhookListener' => __DIR__.'/..'.'/../lib/Db/WebhookListener.php', |
|
| 34 | + 'OCA\\WebhookListeners\\Db\\WebhookListenerMapper' => __DIR__.'/..'.'/../lib/Db/WebhookListenerMapper.php', |
|
| 35 | + 'OCA\\WebhookListeners\\Listener\\WebhooksEventListener' => __DIR__.'/..'.'/../lib/Listener/WebhooksEventListener.php', |
|
| 36 | + 'OCA\\WebhookListeners\\Migration\\Version1000Date20240527153425' => __DIR__.'/..'.'/../lib/Migration/Version1000Date20240527153425.php', |
|
| 37 | + 'OCA\\WebhookListeners\\Migration\\Version1001Date20240716184935' => __DIR__.'/..'.'/../lib/Migration/Version1001Date20240716184935.php', |
|
| 38 | + 'OCA\\WebhookListeners\\Migration\\Version1500Date20251007130000' => __DIR__.'/..'.'/../lib/Migration/Version1500Date20251007130000.php', |
|
| 39 | + 'OCA\\WebhookListeners\\ResponseDefinitions' => __DIR__.'/..'.'/../lib/ResponseDefinitions.php', |
|
| 40 | + 'OCA\\WebhookListeners\\Service\\PHPMongoQuery' => __DIR__.'/..'.'/../lib/Service/PHPMongoQuery.php', |
|
| 41 | + 'OCA\\WebhookListeners\\Service\\TokenService' => __DIR__.'/..'.'/../lib/Service/TokenService.php', |
|
| 42 | + 'OCA\\WebhookListeners\\Settings\\Admin' => __DIR__.'/..'.'/../lib/Settings/Admin.php', |
|
| 43 | + 'OCA\\WebhookListeners\\Settings\\AdminSection' => __DIR__.'/..'.'/../lib/Settings/AdminSection.php', |
|
| 44 | 44 | ); |
| 45 | 45 | |
| 46 | 46 | public static function getInitializer(ClassLoader $loader) |
| 47 | 47 | { |
| 48 | - return \Closure::bind(function () use ($loader) { |
|
| 48 | + return \Closure::bind(function() use ($loader) { |
|
| 49 | 49 | $loader->prefixLengthsPsr4 = ComposerStaticInitWebhookListeners::$prefixLengthsPsr4; |
| 50 | 50 | $loader->prefixDirsPsr4 = ComposerStaticInitWebhookListeners::$prefixDirsPsr4; |
| 51 | 51 | $loader->classMap = ComposerStaticInitWebhookListeners::$classMap; |
@@ -6,24 +6,24 @@ |
||
| 6 | 6 | $baseDir = $vendorDir; |
| 7 | 7 | |
| 8 | 8 | return array( |
| 9 | - 'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php', |
|
| 10 | - 'OCA\\WebhookListeners\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php', |
|
| 11 | - 'OCA\\WebhookListeners\\BackgroundJobs\\WebhookCall' => $baseDir . '/../lib/BackgroundJobs/WebhookCall.php', |
|
| 12 | - 'OCA\\WebhookListeners\\BackgroundJobs\\WebhookTokenCleanup' => $baseDir . '/../lib/BackgroundJobs/WebhookTokenCleanup.php', |
|
| 13 | - 'OCA\\WebhookListeners\\Command\\ListWebhooks' => $baseDir . '/../lib/Command/ListWebhooks.php', |
|
| 14 | - 'OCA\\WebhookListeners\\Controller\\WebhooksController' => $baseDir . '/../lib/Controller/WebhooksController.php', |
|
| 15 | - 'OCA\\WebhookListeners\\Db\\AuthMethod' => $baseDir . '/../lib/Db/AuthMethod.php', |
|
| 16 | - 'OCA\\WebhookListeners\\Db\\EphemeralToken' => $baseDir . '/../lib/Db/EphemeralToken.php', |
|
| 17 | - 'OCA\\WebhookListeners\\Db\\EphemeralTokenMapper' => $baseDir . '/../lib/Db/EphemeralTokenMapper.php', |
|
| 18 | - 'OCA\\WebhookListeners\\Db\\WebhookListener' => $baseDir . '/../lib/Db/WebhookListener.php', |
|
| 19 | - 'OCA\\WebhookListeners\\Db\\WebhookListenerMapper' => $baseDir . '/../lib/Db/WebhookListenerMapper.php', |
|
| 20 | - 'OCA\\WebhookListeners\\Listener\\WebhooksEventListener' => $baseDir . '/../lib/Listener/WebhooksEventListener.php', |
|
| 21 | - 'OCA\\WebhookListeners\\Migration\\Version1000Date20240527153425' => $baseDir . '/../lib/Migration/Version1000Date20240527153425.php', |
|
| 22 | - 'OCA\\WebhookListeners\\Migration\\Version1001Date20240716184935' => $baseDir . '/../lib/Migration/Version1001Date20240716184935.php', |
|
| 23 | - 'OCA\\WebhookListeners\\Migration\\Version1500Date20251007130000' => $baseDir . '/../lib/Migration/Version1500Date20251007130000.php', |
|
| 24 | - 'OCA\\WebhookListeners\\ResponseDefinitions' => $baseDir . '/../lib/ResponseDefinitions.php', |
|
| 25 | - 'OCA\\WebhookListeners\\Service\\PHPMongoQuery' => $baseDir . '/../lib/Service/PHPMongoQuery.php', |
|
| 26 | - 'OCA\\WebhookListeners\\Service\\TokenService' => $baseDir . '/../lib/Service/TokenService.php', |
|
| 27 | - 'OCA\\WebhookListeners\\Settings\\Admin' => $baseDir . '/../lib/Settings/Admin.php', |
|
| 28 | - 'OCA\\WebhookListeners\\Settings\\AdminSection' => $baseDir . '/../lib/Settings/AdminSection.php', |
|
| 9 | + 'Composer\\InstalledVersions' => $vendorDir.'/composer/InstalledVersions.php', |
|
| 10 | + 'OCA\\WebhookListeners\\AppInfo\\Application' => $baseDir.'/../lib/AppInfo/Application.php', |
|
| 11 | + 'OCA\\WebhookListeners\\BackgroundJobs\\WebhookCall' => $baseDir.'/../lib/BackgroundJobs/WebhookCall.php', |
|
| 12 | + 'OCA\\WebhookListeners\\BackgroundJobs\\WebhookTokenCleanup' => $baseDir.'/../lib/BackgroundJobs/WebhookTokenCleanup.php', |
|
| 13 | + 'OCA\\WebhookListeners\\Command\\ListWebhooks' => $baseDir.'/../lib/Command/ListWebhooks.php', |
|
| 14 | + 'OCA\\WebhookListeners\\Controller\\WebhooksController' => $baseDir.'/../lib/Controller/WebhooksController.php', |
|
| 15 | + 'OCA\\WebhookListeners\\Db\\AuthMethod' => $baseDir.'/../lib/Db/AuthMethod.php', |
|
| 16 | + 'OCA\\WebhookListeners\\Db\\EphemeralToken' => $baseDir.'/../lib/Db/EphemeralToken.php', |
|
| 17 | + 'OCA\\WebhookListeners\\Db\\EphemeralTokenMapper' => $baseDir.'/../lib/Db/EphemeralTokenMapper.php', |
|
| 18 | + 'OCA\\WebhookListeners\\Db\\WebhookListener' => $baseDir.'/../lib/Db/WebhookListener.php', |
|
| 19 | + 'OCA\\WebhookListeners\\Db\\WebhookListenerMapper' => $baseDir.'/../lib/Db/WebhookListenerMapper.php', |
|
| 20 | + 'OCA\\WebhookListeners\\Listener\\WebhooksEventListener' => $baseDir.'/../lib/Listener/WebhooksEventListener.php', |
|
| 21 | + 'OCA\\WebhookListeners\\Migration\\Version1000Date20240527153425' => $baseDir.'/../lib/Migration/Version1000Date20240527153425.php', |
|
| 22 | + 'OCA\\WebhookListeners\\Migration\\Version1001Date20240716184935' => $baseDir.'/../lib/Migration/Version1001Date20240716184935.php', |
|
| 23 | + 'OCA\\WebhookListeners\\Migration\\Version1500Date20251007130000' => $baseDir.'/../lib/Migration/Version1500Date20251007130000.php', |
|
| 24 | + 'OCA\\WebhookListeners\\ResponseDefinitions' => $baseDir.'/../lib/ResponseDefinitions.php', |
|
| 25 | + 'OCA\\WebhookListeners\\Service\\PHPMongoQuery' => $baseDir.'/../lib/Service/PHPMongoQuery.php', |
|
| 26 | + 'OCA\\WebhookListeners\\Service\\TokenService' => $baseDir.'/../lib/Service/TokenService.php', |
|
| 27 | + 'OCA\\WebhookListeners\\Settings\\Admin' => $baseDir.'/../lib/Settings/Admin.php', |
|
| 28 | + 'OCA\\WebhookListeners\\Settings\\AdminSection' => $baseDir.'/../lib/Settings/AdminSection.php', |
|
| 29 | 29 | ); |
@@ -20,103 +20,103 @@ |
||
| 20 | 20 | use Psr\Log\LoggerInterface; |
| 21 | 21 | |
| 22 | 22 | class TokenService { |
| 23 | - public function __construct( |
|
| 24 | - private IProvider $tokenProvider, |
|
| 25 | - private ISecureRandom $random, |
|
| 26 | - private EphemeralTokenMapper $tokenMapper, |
|
| 27 | - private LoggerInterface $logger, |
|
| 28 | - private ITimeFactory $time, |
|
| 29 | - private IFactory $l10nFactory, |
|
| 30 | - private IUserManager $userManager, |
|
| 31 | - ) { |
|
| 32 | - } |
|
| 23 | + public function __construct( |
|
| 24 | + private IProvider $tokenProvider, |
|
| 25 | + private ISecureRandom $random, |
|
| 26 | + private EphemeralTokenMapper $tokenMapper, |
|
| 27 | + private LoggerInterface $logger, |
|
| 28 | + private ITimeFactory $time, |
|
| 29 | + private IFactory $l10nFactory, |
|
| 30 | + private IUserManager $userManager, |
|
| 31 | + ) { |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * creates an array which includes two arrays of tokens: 'user_ids' and 'user_roles' |
|
| 36 | - * The array ['user_ids' => ['jane', 'bob'], 'user_roles' => ['owner', 'trigger']] |
|
| 37 | - * as requested tokens in the registered webhook produces a result like |
|
| 38 | - * ['user_ids' => [['jane' => 'abcdtokenabcd1'], ['bob','=> 'abcdtokenabcd2']], 'user_roles' => ['owner' => ['admin' => 'abcdtokenabcd3'], 'trigger' => ['user1' => 'abcdtokenabcd4']]] |
|
| 39 | - * Created auth tokens are valid for 1 hour. |
|
| 40 | - * |
|
| 41 | - * @param WebhookListener $webhookListener |
|
| 42 | - * @param ?string $triggerUserId the user that triggered the webhook call |
|
| 43 | - * @return array{user_ids?:array<string,string>,user_roles?:array{owner?:array<string,string>,trigger?:array<string,string>}} |
|
| 44 | - */ |
|
| 45 | - public function getTokens(WebhookListener $webhookListener, ?string $triggerUserId): array { |
|
| 46 | - $tokens = [ |
|
| 47 | - 'user_ids' => [], |
|
| 48 | - 'user_roles' => [], |
|
| 49 | - ]; |
|
| 50 | - $tokenNeeded = $webhookListener->getTokenNeeded(); |
|
| 51 | - if (isset($tokenNeeded['user_ids'])) { |
|
| 52 | - foreach ($tokenNeeded['user_ids'] as $userId) { |
|
| 53 | - try { |
|
| 54 | - $tokens['user_ids'][$userId] = $this->createEphemeralToken($userId); |
|
| 55 | - } catch (\Exception $e) { |
|
| 56 | - $this->logger->error('Webhook token creation for user ' . $userId . ' failed: ' . $e->getMessage(), ['exception' => $e]); |
|
| 57 | - } |
|
| 34 | + /** |
|
| 35 | + * creates an array which includes two arrays of tokens: 'user_ids' and 'user_roles' |
|
| 36 | + * The array ['user_ids' => ['jane', 'bob'], 'user_roles' => ['owner', 'trigger']] |
|
| 37 | + * as requested tokens in the registered webhook produces a result like |
|
| 38 | + * ['user_ids' => [['jane' => 'abcdtokenabcd1'], ['bob','=> 'abcdtokenabcd2']], 'user_roles' => ['owner' => ['admin' => 'abcdtokenabcd3'], 'trigger' => ['user1' => 'abcdtokenabcd4']]] |
|
| 39 | + * Created auth tokens are valid for 1 hour. |
|
| 40 | + * |
|
| 41 | + * @param WebhookListener $webhookListener |
|
| 42 | + * @param ?string $triggerUserId the user that triggered the webhook call |
|
| 43 | + * @return array{user_ids?:array<string,string>,user_roles?:array{owner?:array<string,string>,trigger?:array<string,string>}} |
|
| 44 | + */ |
|
| 45 | + public function getTokens(WebhookListener $webhookListener, ?string $triggerUserId): array { |
|
| 46 | + $tokens = [ |
|
| 47 | + 'user_ids' => [], |
|
| 48 | + 'user_roles' => [], |
|
| 49 | + ]; |
|
| 50 | + $tokenNeeded = $webhookListener->getTokenNeeded(); |
|
| 51 | + if (isset($tokenNeeded['user_ids'])) { |
|
| 52 | + foreach ($tokenNeeded['user_ids'] as $userId) { |
|
| 53 | + try { |
|
| 54 | + $tokens['user_ids'][$userId] = $this->createEphemeralToken($userId); |
|
| 55 | + } catch (\Exception $e) { |
|
| 56 | + $this->logger->error('Webhook token creation for user ' . $userId . ' failed: ' . $e->getMessage(), ['exception' => $e]); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - } |
|
| 60 | - } |
|
| 61 | - if (isset($tokenNeeded['user_roles'])) { |
|
| 62 | - foreach ($tokenNeeded['user_roles'] as $user_role) { |
|
| 63 | - switch ($user_role) { |
|
| 64 | - case 'owner': |
|
| 65 | - // token for the person who created the flow |
|
| 66 | - $ownerId = $webhookListener->getUserId(); |
|
| 67 | - if (is_null($ownerId)) { // no owner uid available |
|
| 68 | - break; |
|
| 69 | - } |
|
| 70 | - $tokens['user_roles']['owner'] = [ |
|
| 71 | - $ownerId => $this->createEphemeralToken($ownerId) |
|
| 72 | - ]; |
|
| 73 | - break; |
|
| 74 | - case 'trigger': |
|
| 75 | - // token for the person who triggered the webhook |
|
| 76 | - if (is_null($triggerUserId)) { // no trigger uid available |
|
| 77 | - break; |
|
| 78 | - } |
|
| 79 | - $tokens['user_roles']['trigger'] = [ |
|
| 80 | - $triggerUserId => $this->createEphemeralToken($triggerUserId) |
|
| 81 | - ]; |
|
| 82 | - break; |
|
| 83 | - default: |
|
| 84 | - $this->logger->error('Webhook token creation for user role ' . $user_role . ' not defined. ', ['Not defined' => $user_role]); |
|
| 59 | + } |
|
| 60 | + } |
|
| 61 | + if (isset($tokenNeeded['user_roles'])) { |
|
| 62 | + foreach ($tokenNeeded['user_roles'] as $user_role) { |
|
| 63 | + switch ($user_role) { |
|
| 64 | + case 'owner': |
|
| 65 | + // token for the person who created the flow |
|
| 66 | + $ownerId = $webhookListener->getUserId(); |
|
| 67 | + if (is_null($ownerId)) { // no owner uid available |
|
| 68 | + break; |
|
| 69 | + } |
|
| 70 | + $tokens['user_roles']['owner'] = [ |
|
| 71 | + $ownerId => $this->createEphemeralToken($ownerId) |
|
| 72 | + ]; |
|
| 73 | + break; |
|
| 74 | + case 'trigger': |
|
| 75 | + // token for the person who triggered the webhook |
|
| 76 | + if (is_null($triggerUserId)) { // no trigger uid available |
|
| 77 | + break; |
|
| 78 | + } |
|
| 79 | + $tokens['user_roles']['trigger'] = [ |
|
| 80 | + $triggerUserId => $this->createEphemeralToken($triggerUserId) |
|
| 81 | + ]; |
|
| 82 | + break; |
|
| 83 | + default: |
|
| 84 | + $this->logger->error('Webhook token creation for user role ' . $user_role . ' not defined. ', ['Not defined' => $user_role]); |
|
| 85 | 85 | |
| 86 | - } |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - return $tokens; |
|
| 90 | - } |
|
| 91 | - private function createEphemeralToken(string $userId): string { |
|
| 92 | - $token = $this->generateRandomDeviceToken(); |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + return $tokens; |
|
| 90 | + } |
|
| 91 | + private function createEphemeralToken(string $userId): string { |
|
| 92 | + $token = $this->generateRandomDeviceToken(); |
|
| 93 | 93 | |
| 94 | - // we need the user`s language to have the token name showing up in the session list in the correct language |
|
| 95 | - $user = $this->userManager->get($userId); |
|
| 96 | - $lang = $this->l10nFactory->getUserLanguage($user); |
|
| 97 | - $l = $this->l10nFactory->get('webhook_listeners', $lang); |
|
| 98 | - $name = $l->t('Ephemeral webhook authentication'); |
|
| 99 | - $password = null; |
|
| 100 | - $deviceToken = $this->tokenProvider->generateToken( |
|
| 101 | - $token, |
|
| 102 | - $userId, |
|
| 103 | - $userId, |
|
| 104 | - $password, |
|
| 105 | - $name, |
|
| 106 | - IToken::PERMANENT_TOKEN); |
|
| 94 | + // we need the user`s language to have the token name showing up in the session list in the correct language |
|
| 95 | + $user = $this->userManager->get($userId); |
|
| 96 | + $lang = $this->l10nFactory->getUserLanguage($user); |
|
| 97 | + $l = $this->l10nFactory->get('webhook_listeners', $lang); |
|
| 98 | + $name = $l->t('Ephemeral webhook authentication'); |
|
| 99 | + $password = null; |
|
| 100 | + $deviceToken = $this->tokenProvider->generateToken( |
|
| 101 | + $token, |
|
| 102 | + $userId, |
|
| 103 | + $userId, |
|
| 104 | + $password, |
|
| 105 | + $name, |
|
| 106 | + IToken::PERMANENT_TOKEN); |
|
| 107 | 107 | |
| 108 | - $this->tokenMapper->addEphemeralToken( |
|
| 109 | - $deviceToken->getId(), |
|
| 110 | - $userId, |
|
| 111 | - $this->time->getTime()); |
|
| 112 | - return $token; |
|
| 113 | - } |
|
| 108 | + $this->tokenMapper->addEphemeralToken( |
|
| 109 | + $deviceToken->getId(), |
|
| 110 | + $userId, |
|
| 111 | + $this->time->getTime()); |
|
| 112 | + return $token; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - private function generateRandomDeviceToken(): string { |
|
| 116 | - $groups = []; |
|
| 117 | - for ($i = 0; $i < 5; $i++) { |
|
| 118 | - $groups[] = $this->random->generate(5, ISecureRandom::CHAR_HUMAN_READABLE); |
|
| 119 | - } |
|
| 120 | - return implode('-', $groups); |
|
| 121 | - } |
|
| 115 | + private function generateRandomDeviceToken(): string { |
|
| 116 | + $groups = []; |
|
| 117 | + for ($i = 0; $i < 5; $i++) { |
|
| 118 | + $groups[] = $this->random->generate(5, ISecureRandom::CHAR_HUMAN_READABLE); |
|
| 119 | + } |
|
| 120 | + return implode('-', $groups); |
|
| 121 | + } |
|
| 122 | 122 | } |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | try { |
| 54 | 54 | $tokens['user_ids'][$userId] = $this->createEphemeralToken($userId); |
| 55 | 55 | } catch (\Exception $e) { |
| 56 | - $this->logger->error('Webhook token creation for user ' . $userId . ' failed: ' . $e->getMessage(), ['exception' => $e]); |
|
| 56 | + $this->logger->error('Webhook token creation for user '.$userId.' failed: '.$e->getMessage(), ['exception' => $e]); |
|
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | } |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | ]; |
| 82 | 82 | break; |
| 83 | 83 | default: |
| 84 | - $this->logger->error('Webhook token creation for user role ' . $user_role . ' not defined. ', ['Not defined' => $user_role]); |
|
| 84 | + $this->logger->error('Webhook token creation for user role '.$user_role.' not defined. ', ['Not defined' => $user_role]); |
|
| 85 | 85 | |
| 86 | 86 | } |
| 87 | 87 | } |
@@ -35,280 +35,280 @@ |
||
| 35 | 35 | */ |
| 36 | 36 | #[OpenAPI(scope: OpenAPI::SCOPE_ADMINISTRATION)] |
| 37 | 37 | class WebhooksController extends OCSController { |
| 38 | - public function __construct( |
|
| 39 | - string $appName, |
|
| 40 | - IRequest $request, |
|
| 41 | - private LoggerInterface $logger, |
|
| 42 | - private WebhookListenerMapper $mapper, |
|
| 43 | - private ?string $userId, |
|
| 44 | - private ISession $session, |
|
| 45 | - ) { |
|
| 46 | - parent::__construct($appName, $request); |
|
| 47 | - } |
|
| 38 | + public function __construct( |
|
| 39 | + string $appName, |
|
| 40 | + IRequest $request, |
|
| 41 | + private LoggerInterface $logger, |
|
| 42 | + private WebhookListenerMapper $mapper, |
|
| 43 | + private ?string $userId, |
|
| 44 | + private ISession $session, |
|
| 45 | + ) { |
|
| 46 | + parent::__construct($appName, $request); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * List registered webhooks |
|
| 51 | - * |
|
| 52 | - * @param string|null $uri The callback URI to filter by |
|
| 53 | - * @return DataResponse<Http::STATUS_OK, list<WebhookListenersWebhookInfo>, array{}> |
|
| 54 | - * @throws OCSException Other internal error |
|
| 55 | - * |
|
| 56 | - * 200: Webhook registrations returned |
|
| 57 | - */ |
|
| 58 | - #[ApiRoute(verb: 'GET', url: '/api/v1/webhooks')] |
|
| 59 | - #[AuthorizedAdminSetting(settings:Admin::class)] |
|
| 60 | - #[AppApiAdminAccessWithoutUser] |
|
| 61 | - public function index(?string $uri = null): DataResponse { |
|
| 62 | - try { |
|
| 63 | - if ($uri !== null) { |
|
| 64 | - $webhookListeners = $this->mapper->getByUri($uri); |
|
| 65 | - } else { |
|
| 66 | - $webhookListeners = $this->mapper->getAll(); |
|
| 67 | - } |
|
| 49 | + /** |
|
| 50 | + * List registered webhooks |
|
| 51 | + * |
|
| 52 | + * @param string|null $uri The callback URI to filter by |
|
| 53 | + * @return DataResponse<Http::STATUS_OK, list<WebhookListenersWebhookInfo>, array{}> |
|
| 54 | + * @throws OCSException Other internal error |
|
| 55 | + * |
|
| 56 | + * 200: Webhook registrations returned |
|
| 57 | + */ |
|
| 58 | + #[ApiRoute(verb: 'GET', url: '/api/v1/webhooks')] |
|
| 59 | + #[AuthorizedAdminSetting(settings:Admin::class)] |
|
| 60 | + #[AppApiAdminAccessWithoutUser] |
|
| 61 | + public function index(?string $uri = null): DataResponse { |
|
| 62 | + try { |
|
| 63 | + if ($uri !== null) { |
|
| 64 | + $webhookListeners = $this->mapper->getByUri($uri); |
|
| 65 | + } else { |
|
| 66 | + $webhookListeners = $this->mapper->getAll(); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - return new DataResponse(array_values(array_map( |
|
| 70 | - fn (WebhookListener $listener): array => $listener->jsonSerialize(), |
|
| 71 | - $webhookListeners |
|
| 72 | - ))); |
|
| 73 | - } catch (\Exception $e) { |
|
| 74 | - $this->logger->error('Error when listing webhooks', ['exception' => $e]); |
|
| 75 | - throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e); |
|
| 76 | - } |
|
| 77 | - } |
|
| 69 | + return new DataResponse(array_values(array_map( |
|
| 70 | + fn (WebhookListener $listener): array => $listener->jsonSerialize(), |
|
| 71 | + $webhookListeners |
|
| 72 | + ))); |
|
| 73 | + } catch (\Exception $e) { |
|
| 74 | + $this->logger->error('Error when listing webhooks', ['exception' => $e]); |
|
| 75 | + throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e); |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * Get details on a registered webhook |
|
| 81 | - * |
|
| 82 | - * @param int $id id of the webhook |
|
| 83 | - * |
|
| 84 | - * @return DataResponse<Http::STATUS_OK, WebhookListenersWebhookInfo, array{}> |
|
| 85 | - * @throws OCSNotFoundException Webhook not found |
|
| 86 | - * @throws OCSException Other internal error |
|
| 87 | - * |
|
| 88 | - * 200: Webhook registration returned |
|
| 89 | - */ |
|
| 90 | - #[ApiRoute(verb: 'GET', url: '/api/v1/webhooks/{id}')] |
|
| 91 | - #[AuthorizedAdminSetting(settings:Admin::class)] |
|
| 92 | - #[AppApiAdminAccessWithoutUser] |
|
| 93 | - public function show(int $id): DataResponse { |
|
| 94 | - try { |
|
| 95 | - return new DataResponse($this->mapper->getById($id)->jsonSerialize()); |
|
| 96 | - } catch (DoesNotExistException $e) { |
|
| 97 | - throw new OCSNotFoundException($e->getMessage(), $e); |
|
| 98 | - } catch (\Exception $e) { |
|
| 99 | - $this->logger->error('Error when getting webhook', ['exception' => $e]); |
|
| 100 | - throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e); |
|
| 101 | - } |
|
| 102 | - } |
|
| 79 | + /** |
|
| 80 | + * Get details on a registered webhook |
|
| 81 | + * |
|
| 82 | + * @param int $id id of the webhook |
|
| 83 | + * |
|
| 84 | + * @return DataResponse<Http::STATUS_OK, WebhookListenersWebhookInfo, array{}> |
|
| 85 | + * @throws OCSNotFoundException Webhook not found |
|
| 86 | + * @throws OCSException Other internal error |
|
| 87 | + * |
|
| 88 | + * 200: Webhook registration returned |
|
| 89 | + */ |
|
| 90 | + #[ApiRoute(verb: 'GET', url: '/api/v1/webhooks/{id}')] |
|
| 91 | + #[AuthorizedAdminSetting(settings:Admin::class)] |
|
| 92 | + #[AppApiAdminAccessWithoutUser] |
|
| 93 | + public function show(int $id): DataResponse { |
|
| 94 | + try { |
|
| 95 | + return new DataResponse($this->mapper->getById($id)->jsonSerialize()); |
|
| 96 | + } catch (DoesNotExistException $e) { |
|
| 97 | + throw new OCSNotFoundException($e->getMessage(), $e); |
|
| 98 | + } catch (\Exception $e) { |
|
| 99 | + $this->logger->error('Error when getting webhook', ['exception' => $e]); |
|
| 100 | + throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e); |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * Register a new webhook |
|
| 106 | - * |
|
| 107 | - * @param string $httpMethod HTTP method to use to contact the webhook |
|
| 108 | - * @param string $uri Webhook URI endpoint |
|
| 109 | - * @param string $event Event class name to listen to |
|
| 110 | - * @param ?array<string,mixed> $eventFilter Mongo filter to apply to the serialized data to decide if firing |
|
| 111 | - * @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. |
|
| 112 | - * @param ?array<string,string> $headers Array of headers to send |
|
| 113 | - * @param "none"|"header"|null $authMethod Authentication method to use |
|
| 114 | - * @param ?array<string,mixed> $authData Array of data for authentication |
|
| 115 | - * @param ?array{user_ids?:list<string>,user_roles?:list<string>} $tokenNeeded |
|
| 116 | - * List of user ids for which to include auth tokens in the event. |
|
| 117 | - * Has two fields: "user_ids" list of user uids for which tokens are needed, "user_roles" list of roles (users not defined by their ID but by the role they have in the webhook event) for which tokens can be included. |
|
| 118 | - * Possible roles: "owner" for the user creating the webhook, "trigger" for the user triggering the webhook call. |
|
| 119 | - * Requested auth tokens are valid for 1 hour after receiving them in the event call request. |
|
| 120 | - * |
|
| 121 | - * @return DataResponse<Http::STATUS_OK, WebhookListenersWebhookInfo, array{}> |
|
| 122 | - * |
|
| 123 | - * 200: Webhook registration returned |
|
| 124 | - * |
|
| 125 | - * @throws OCSBadRequestException Bad request |
|
| 126 | - * @throws OCSForbiddenException Insufficient permissions |
|
| 127 | - * @throws OCSException Other error |
|
| 128 | - */ |
|
| 129 | - #[ApiRoute(verb: 'POST', url: '/api/v1/webhooks')] |
|
| 130 | - #[AuthorizedAdminSetting(settings:Admin::class)] |
|
| 131 | - #[AppApiAdminAccessWithoutUser] |
|
| 132 | - public function create( |
|
| 133 | - string $httpMethod, |
|
| 134 | - string $uri, |
|
| 135 | - string $event, |
|
| 136 | - ?array $eventFilter, |
|
| 137 | - ?string $userIdFilter, |
|
| 138 | - ?array $headers, |
|
| 139 | - ?string $authMethod, |
|
| 140 | - #[\SensitiveParameter] |
|
| 141 | - ?array $authData, |
|
| 142 | - ?array $tokenNeeded = null, |
|
| 143 | - ): DataResponse { |
|
| 144 | - $appId = null; |
|
| 145 | - if ($this->session->get('app_api') === true) { |
|
| 146 | - $appId = $this->request->getHeader('ex-app-id'); |
|
| 147 | - } |
|
| 148 | - try { |
|
| 149 | - $authMethod = AuthMethod::from($authMethod ?? AuthMethod::None->value); |
|
| 150 | - } catch (\ValueError $e) { |
|
| 151 | - throw new OCSBadRequestException('This auth method does not exist'); |
|
| 152 | - } |
|
| 153 | - try { |
|
| 154 | - $webhookListener = $this->mapper->addWebhookListener( |
|
| 155 | - $appId, |
|
| 156 | - $this->userId, |
|
| 157 | - $httpMethod, |
|
| 158 | - $uri, |
|
| 159 | - $event, |
|
| 160 | - $eventFilter, |
|
| 161 | - $userIdFilter, |
|
| 162 | - $headers, |
|
| 163 | - $authMethod, |
|
| 164 | - $authData, |
|
| 165 | - $tokenNeeded, |
|
| 166 | - ); |
|
| 167 | - return new DataResponse($webhookListener->jsonSerialize()); |
|
| 168 | - } catch (\UnexpectedValueException $e) { |
|
| 169 | - throw new OCSBadRequestException($e->getMessage(), $e); |
|
| 170 | - } catch (\DomainException $e) { |
|
| 171 | - throw new OCSForbiddenException($e->getMessage(), $e); |
|
| 172 | - } catch (\Exception $e) { |
|
| 173 | - $this->logger->error('Error when inserting webhook', ['exception' => $e]); |
|
| 174 | - throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e); |
|
| 175 | - } |
|
| 176 | - } |
|
| 104 | + /** |
|
| 105 | + * Register a new webhook |
|
| 106 | + * |
|
| 107 | + * @param string $httpMethod HTTP method to use to contact the webhook |
|
| 108 | + * @param string $uri Webhook URI endpoint |
|
| 109 | + * @param string $event Event class name to listen to |
|
| 110 | + * @param ?array<string,mixed> $eventFilter Mongo filter to apply to the serialized data to decide if firing |
|
| 111 | + * @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. |
|
| 112 | + * @param ?array<string,string> $headers Array of headers to send |
|
| 113 | + * @param "none"|"header"|null $authMethod Authentication method to use |
|
| 114 | + * @param ?array<string,mixed> $authData Array of data for authentication |
|
| 115 | + * @param ?array{user_ids?:list<string>,user_roles?:list<string>} $tokenNeeded |
|
| 116 | + * List of user ids for which to include auth tokens in the event. |
|
| 117 | + * Has two fields: "user_ids" list of user uids for which tokens are needed, "user_roles" list of roles (users not defined by their ID but by the role they have in the webhook event) for which tokens can be included. |
|
| 118 | + * Possible roles: "owner" for the user creating the webhook, "trigger" for the user triggering the webhook call. |
|
| 119 | + * Requested auth tokens are valid for 1 hour after receiving them in the event call request. |
|
| 120 | + * |
|
| 121 | + * @return DataResponse<Http::STATUS_OK, WebhookListenersWebhookInfo, array{}> |
|
| 122 | + * |
|
| 123 | + * 200: Webhook registration returned |
|
| 124 | + * |
|
| 125 | + * @throws OCSBadRequestException Bad request |
|
| 126 | + * @throws OCSForbiddenException Insufficient permissions |
|
| 127 | + * @throws OCSException Other error |
|
| 128 | + */ |
|
| 129 | + #[ApiRoute(verb: 'POST', url: '/api/v1/webhooks')] |
|
| 130 | + #[AuthorizedAdminSetting(settings:Admin::class)] |
|
| 131 | + #[AppApiAdminAccessWithoutUser] |
|
| 132 | + public function create( |
|
| 133 | + string $httpMethod, |
|
| 134 | + string $uri, |
|
| 135 | + string $event, |
|
| 136 | + ?array $eventFilter, |
|
| 137 | + ?string $userIdFilter, |
|
| 138 | + ?array $headers, |
|
| 139 | + ?string $authMethod, |
|
| 140 | + #[\SensitiveParameter] |
|
| 141 | + ?array $authData, |
|
| 142 | + ?array $tokenNeeded = null, |
|
| 143 | + ): DataResponse { |
|
| 144 | + $appId = null; |
|
| 145 | + if ($this->session->get('app_api') === true) { |
|
| 146 | + $appId = $this->request->getHeader('ex-app-id'); |
|
| 147 | + } |
|
| 148 | + try { |
|
| 149 | + $authMethod = AuthMethod::from($authMethod ?? AuthMethod::None->value); |
|
| 150 | + } catch (\ValueError $e) { |
|
| 151 | + throw new OCSBadRequestException('This auth method does not exist'); |
|
| 152 | + } |
|
| 153 | + try { |
|
| 154 | + $webhookListener = $this->mapper->addWebhookListener( |
|
| 155 | + $appId, |
|
| 156 | + $this->userId, |
|
| 157 | + $httpMethod, |
|
| 158 | + $uri, |
|
| 159 | + $event, |
|
| 160 | + $eventFilter, |
|
| 161 | + $userIdFilter, |
|
| 162 | + $headers, |
|
| 163 | + $authMethod, |
|
| 164 | + $authData, |
|
| 165 | + $tokenNeeded, |
|
| 166 | + ); |
|
| 167 | + return new DataResponse($webhookListener->jsonSerialize()); |
|
| 168 | + } catch (\UnexpectedValueException $e) { |
|
| 169 | + throw new OCSBadRequestException($e->getMessage(), $e); |
|
| 170 | + } catch (\DomainException $e) { |
|
| 171 | + throw new OCSForbiddenException($e->getMessage(), $e); |
|
| 172 | + } catch (\Exception $e) { |
|
| 173 | + $this->logger->error('Error when inserting webhook', ['exception' => $e]); |
|
| 174 | + throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e); |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | - /** |
|
| 179 | - * Update an existing webhook registration |
|
| 180 | - * |
|
| 181 | - * @param int $id id of the webhook |
|
| 182 | - * @param string $httpMethod HTTP method to use to contact the webhook |
|
| 183 | - * @param string $uri Webhook URI endpoint |
|
| 184 | - * @param string $event Event class name to listen to |
|
| 185 | - * @param ?array<string,mixed> $eventFilter Mongo filter to apply to the serialized data to decide if firing |
|
| 186 | - * @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. |
|
| 187 | - * @param ?array<string,string> $headers Array of headers to send |
|
| 188 | - * @param "none"|"header"|null $authMethod Authentication method to use |
|
| 189 | - * @param ?array<string,mixed> $authData Array of data for authentication |
|
| 190 | - * @param ?array{user_ids?:list<string>,user_roles?:list<string>} $tokenNeeded |
|
| 191 | - * List of user ids for which to include auth tokens in the event. |
|
| 192 | - * Has two fields: "user_ids" list of user uids for which tokens are needed, "user_roles" list of roles (users not defined by their ID but by the role they have in the webhook event) for which tokens can be included. |
|
| 193 | - * Possible roles: "owner" for the user creating the webhook, "trigger" for the user triggering the webhook call. |
|
| 194 | - * Requested auth tokens are valid for 1 hour after receiving them in the event call request. |
|
| 195 | - * |
|
| 196 | - * @return DataResponse<Http::STATUS_OK, WebhookListenersWebhookInfo, array{}> |
|
| 197 | - * |
|
| 198 | - * 200: Webhook registration returned |
|
| 199 | - * |
|
| 200 | - * @throws OCSBadRequestException Bad request |
|
| 201 | - * @throws OCSForbiddenException Insufficient permissions |
|
| 202 | - * @throws OCSException Other error |
|
| 203 | - */ |
|
| 204 | - #[ApiRoute(verb: 'POST', url: '/api/v1/webhooks/{id}')] |
|
| 205 | - #[AuthorizedAdminSetting(settings:Admin::class)] |
|
| 206 | - #[AppApiAdminAccessWithoutUser] |
|
| 207 | - public function update( |
|
| 208 | - int $id, |
|
| 209 | - string $httpMethod, |
|
| 210 | - string $uri, |
|
| 211 | - string $event, |
|
| 212 | - ?array $eventFilter, |
|
| 213 | - ?string $userIdFilter, |
|
| 214 | - ?array $headers, |
|
| 215 | - ?string $authMethod, |
|
| 216 | - #[\SensitiveParameter] |
|
| 217 | - ?array $authData, |
|
| 218 | - ?array $tokenNeeded = null, |
|
| 219 | - ): DataResponse { |
|
| 220 | - $appId = null; |
|
| 221 | - if ($this->session->get('app_api') === true) { |
|
| 222 | - $appId = $this->request->getHeader('ex-app-id'); |
|
| 223 | - } |
|
| 224 | - try { |
|
| 225 | - $authMethod = AuthMethod::from($authMethod ?? AuthMethod::None->value); |
|
| 226 | - } catch (\ValueError $e) { |
|
| 227 | - throw new OCSBadRequestException('This auth method does not exist'); |
|
| 228 | - } |
|
| 229 | - try { |
|
| 230 | - $webhookListener = $this->mapper->updateWebhookListener( |
|
| 231 | - $id, |
|
| 232 | - $appId, |
|
| 233 | - $this->userId, |
|
| 234 | - $httpMethod, |
|
| 235 | - $uri, |
|
| 236 | - $event, |
|
| 237 | - $eventFilter, |
|
| 238 | - $userIdFilter, |
|
| 239 | - $headers, |
|
| 240 | - $authMethod, |
|
| 241 | - $authData, |
|
| 242 | - $tokenNeeded, |
|
| 243 | - ); |
|
| 244 | - return new DataResponse($webhookListener->jsonSerialize()); |
|
| 245 | - } catch (\UnexpectedValueException $e) { |
|
| 246 | - throw new OCSBadRequestException($e->getMessage(), $e); |
|
| 247 | - } catch (\DomainException $e) { |
|
| 248 | - throw new OCSForbiddenException($e->getMessage(), $e); |
|
| 249 | - } catch (\Exception $e) { |
|
| 250 | - $this->logger->error('Error when updating flow with id ' . $id, ['exception' => $e]); |
|
| 251 | - throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e); |
|
| 252 | - } |
|
| 253 | - } |
|
| 178 | + /** |
|
| 179 | + * Update an existing webhook registration |
|
| 180 | + * |
|
| 181 | + * @param int $id id of the webhook |
|
| 182 | + * @param string $httpMethod HTTP method to use to contact the webhook |
|
| 183 | + * @param string $uri Webhook URI endpoint |
|
| 184 | + * @param string $event Event class name to listen to |
|
| 185 | + * @param ?array<string,mixed> $eventFilter Mongo filter to apply to the serialized data to decide if firing |
|
| 186 | + * @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. |
|
| 187 | + * @param ?array<string,string> $headers Array of headers to send |
|
| 188 | + * @param "none"|"header"|null $authMethod Authentication method to use |
|
| 189 | + * @param ?array<string,mixed> $authData Array of data for authentication |
|
| 190 | + * @param ?array{user_ids?:list<string>,user_roles?:list<string>} $tokenNeeded |
|
| 191 | + * List of user ids for which to include auth tokens in the event. |
|
| 192 | + * Has two fields: "user_ids" list of user uids for which tokens are needed, "user_roles" list of roles (users not defined by their ID but by the role they have in the webhook event) for which tokens can be included. |
|
| 193 | + * Possible roles: "owner" for the user creating the webhook, "trigger" for the user triggering the webhook call. |
|
| 194 | + * Requested auth tokens are valid for 1 hour after receiving them in the event call request. |
|
| 195 | + * |
|
| 196 | + * @return DataResponse<Http::STATUS_OK, WebhookListenersWebhookInfo, array{}> |
|
| 197 | + * |
|
| 198 | + * 200: Webhook registration returned |
|
| 199 | + * |
|
| 200 | + * @throws OCSBadRequestException Bad request |
|
| 201 | + * @throws OCSForbiddenException Insufficient permissions |
|
| 202 | + * @throws OCSException Other error |
|
| 203 | + */ |
|
| 204 | + #[ApiRoute(verb: 'POST', url: '/api/v1/webhooks/{id}')] |
|
| 205 | + #[AuthorizedAdminSetting(settings:Admin::class)] |
|
| 206 | + #[AppApiAdminAccessWithoutUser] |
|
| 207 | + public function update( |
|
| 208 | + int $id, |
|
| 209 | + string $httpMethod, |
|
| 210 | + string $uri, |
|
| 211 | + string $event, |
|
| 212 | + ?array $eventFilter, |
|
| 213 | + ?string $userIdFilter, |
|
| 214 | + ?array $headers, |
|
| 215 | + ?string $authMethod, |
|
| 216 | + #[\SensitiveParameter] |
|
| 217 | + ?array $authData, |
|
| 218 | + ?array $tokenNeeded = null, |
|
| 219 | + ): DataResponse { |
|
| 220 | + $appId = null; |
|
| 221 | + if ($this->session->get('app_api') === true) { |
|
| 222 | + $appId = $this->request->getHeader('ex-app-id'); |
|
| 223 | + } |
|
| 224 | + try { |
|
| 225 | + $authMethod = AuthMethod::from($authMethod ?? AuthMethod::None->value); |
|
| 226 | + } catch (\ValueError $e) { |
|
| 227 | + throw new OCSBadRequestException('This auth method does not exist'); |
|
| 228 | + } |
|
| 229 | + try { |
|
| 230 | + $webhookListener = $this->mapper->updateWebhookListener( |
|
| 231 | + $id, |
|
| 232 | + $appId, |
|
| 233 | + $this->userId, |
|
| 234 | + $httpMethod, |
|
| 235 | + $uri, |
|
| 236 | + $event, |
|
| 237 | + $eventFilter, |
|
| 238 | + $userIdFilter, |
|
| 239 | + $headers, |
|
| 240 | + $authMethod, |
|
| 241 | + $authData, |
|
| 242 | + $tokenNeeded, |
|
| 243 | + ); |
|
| 244 | + return new DataResponse($webhookListener->jsonSerialize()); |
|
| 245 | + } catch (\UnexpectedValueException $e) { |
|
| 246 | + throw new OCSBadRequestException($e->getMessage(), $e); |
|
| 247 | + } catch (\DomainException $e) { |
|
| 248 | + throw new OCSForbiddenException($e->getMessage(), $e); |
|
| 249 | + } catch (\Exception $e) { |
|
| 250 | + $this->logger->error('Error when updating flow with id ' . $id, ['exception' => $e]); |
|
| 251 | + throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e); |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | 254 | |
| 255 | - /** |
|
| 256 | - * Remove an existing webhook registration |
|
| 257 | - * |
|
| 258 | - * @param int $id id of the webhook |
|
| 259 | - * |
|
| 260 | - * @return DataResponse<Http::STATUS_OK, bool, array{}> |
|
| 261 | - * |
|
| 262 | - * 200: Boolean returned whether something was deleted |
|
| 263 | - * |
|
| 264 | - * @throws OCSBadRequestException Bad request |
|
| 265 | - * @throws OCSForbiddenException Insufficient permissions |
|
| 266 | - * @throws OCSException Other error |
|
| 267 | - */ |
|
| 268 | - #[ApiRoute(verb: 'DELETE', url: '/api/v1/webhooks/{id}')] |
|
| 269 | - #[AuthorizedAdminSetting(settings:Admin::class)] |
|
| 270 | - #[AppApiAdminAccessWithoutUser] |
|
| 271 | - public function destroy(int $id): DataResponse { |
|
| 272 | - try { |
|
| 273 | - $deleted = $this->mapper->deleteById($id); |
|
| 274 | - return new DataResponse($deleted); |
|
| 275 | - } catch (\UnexpectedValueException $e) { |
|
| 276 | - throw new OCSBadRequestException($e->getMessage(), $e); |
|
| 277 | - } catch (\DomainException $e) { |
|
| 278 | - throw new OCSForbiddenException($e->getMessage(), $e); |
|
| 279 | - } catch (\Exception $e) { |
|
| 280 | - $this->logger->error('Error when deleting flow with id ' . $id, ['exception' => $e]); |
|
| 281 | - throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e); |
|
| 282 | - } |
|
| 283 | - } |
|
| 255 | + /** |
|
| 256 | + * Remove an existing webhook registration |
|
| 257 | + * |
|
| 258 | + * @param int $id id of the webhook |
|
| 259 | + * |
|
| 260 | + * @return DataResponse<Http::STATUS_OK, bool, array{}> |
|
| 261 | + * |
|
| 262 | + * 200: Boolean returned whether something was deleted |
|
| 263 | + * |
|
| 264 | + * @throws OCSBadRequestException Bad request |
|
| 265 | + * @throws OCSForbiddenException Insufficient permissions |
|
| 266 | + * @throws OCSException Other error |
|
| 267 | + */ |
|
| 268 | + #[ApiRoute(verb: 'DELETE', url: '/api/v1/webhooks/{id}')] |
|
| 269 | + #[AuthorizedAdminSetting(settings:Admin::class)] |
|
| 270 | + #[AppApiAdminAccessWithoutUser] |
|
| 271 | + public function destroy(int $id): DataResponse { |
|
| 272 | + try { |
|
| 273 | + $deleted = $this->mapper->deleteById($id); |
|
| 274 | + return new DataResponse($deleted); |
|
| 275 | + } catch (\UnexpectedValueException $e) { |
|
| 276 | + throw new OCSBadRequestException($e->getMessage(), $e); |
|
| 277 | + } catch (\DomainException $e) { |
|
| 278 | + throw new OCSForbiddenException($e->getMessage(), $e); |
|
| 279 | + } catch (\Exception $e) { |
|
| 280 | + $this->logger->error('Error when deleting flow with id ' . $id, ['exception' => $e]); |
|
| 281 | + throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e); |
|
| 282 | + } |
|
| 283 | + } |
|
| 284 | 284 | |
| 285 | - /** |
|
| 286 | - * Remove all existing webhook registration mapped to an AppAPI app id |
|
| 287 | - * |
|
| 288 | - * @param string $appid id of the app, as in the ex-app-id for creation |
|
| 289 | - * |
|
| 290 | - * @return DataResponse<Http::STATUS_OK, int, array{}> |
|
| 291 | - * |
|
| 292 | - * 200: Integer number of registrations deleted |
|
| 293 | - * |
|
| 294 | - * @throws OCSBadRequestException Bad request |
|
| 295 | - * @throws OCSForbiddenException Insufficient permissions |
|
| 296 | - * @throws OCSException Other error |
|
| 297 | - */ |
|
| 298 | - #[ApiRoute(verb: 'DELETE', url: '/api/v1/webhooks/byappid/{appid}')] |
|
| 299 | - #[AuthorizedAdminSetting(settings:Admin::class)] |
|
| 300 | - #[AppApiAdminAccessWithoutUser] |
|
| 301 | - public function deleteByAppId(string $appid): DataResponse { |
|
| 302 | - try { |
|
| 303 | - $deletedCount = $this->mapper->deleteByAppId($appid); |
|
| 304 | - return new DataResponse($deletedCount); |
|
| 305 | - } catch (\UnexpectedValueException $e) { |
|
| 306 | - throw new OCSBadRequestException($e->getMessage(), $e); |
|
| 307 | - } catch (\DomainException $e) { |
|
| 308 | - throw new OCSForbiddenException($e->getMessage(), $e); |
|
| 309 | - } catch (\Exception $e) { |
|
| 310 | - $this->logger->error('Error when deleting flows for app id ' . $appid, ['exception' => $e]); |
|
| 311 | - throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e); |
|
| 312 | - } |
|
| 313 | - } |
|
| 285 | + /** |
|
| 286 | + * Remove all existing webhook registration mapped to an AppAPI app id |
|
| 287 | + * |
|
| 288 | + * @param string $appid id of the app, as in the ex-app-id for creation |
|
| 289 | + * |
|
| 290 | + * @return DataResponse<Http::STATUS_OK, int, array{}> |
|
| 291 | + * |
|
| 292 | + * 200: Integer number of registrations deleted |
|
| 293 | + * |
|
| 294 | + * @throws OCSBadRequestException Bad request |
|
| 295 | + * @throws OCSForbiddenException Insufficient permissions |
|
| 296 | + * @throws OCSException Other error |
|
| 297 | + */ |
|
| 298 | + #[ApiRoute(verb: 'DELETE', url: '/api/v1/webhooks/byappid/{appid}')] |
|
| 299 | + #[AuthorizedAdminSetting(settings:Admin::class)] |
|
| 300 | + #[AppApiAdminAccessWithoutUser] |
|
| 301 | + public function deleteByAppId(string $appid): DataResponse { |
|
| 302 | + try { |
|
| 303 | + $deletedCount = $this->mapper->deleteByAppId($appid); |
|
| 304 | + return new DataResponse($deletedCount); |
|
| 305 | + } catch (\UnexpectedValueException $e) { |
|
| 306 | + throw new OCSBadRequestException($e->getMessage(), $e); |
|
| 307 | + } catch (\DomainException $e) { |
|
| 308 | + throw new OCSForbiddenException($e->getMessage(), $e); |
|
| 309 | + } catch (\Exception $e) { |
|
| 310 | + $this->logger->error('Error when deleting flows for app id ' . $appid, ['exception' => $e]); |
|
| 311 | + throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e); |
|
| 312 | + } |
|
| 313 | + } |
|
| 314 | 314 | } |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | ?array $headers, |
| 139 | 139 | ?string $authMethod, |
| 140 | 140 | #[\SensitiveParameter] |
| 141 | - ?array $authData, |
|
| 141 | + ? array $authData, |
|
| 142 | 142 | ?array $tokenNeeded = null, |
| 143 | 143 | ): DataResponse { |
| 144 | 144 | $appId = null; |
@@ -214,7 +214,7 @@ discard block |
||
| 214 | 214 | ?array $headers, |
| 215 | 215 | ?string $authMethod, |
| 216 | 216 | #[\SensitiveParameter] |
| 217 | - ?array $authData, |
|
| 217 | + ? array $authData, |
|
| 218 | 218 | ?array $tokenNeeded = null, |
| 219 | 219 | ): DataResponse { |
| 220 | 220 | $appId = null; |
@@ -247,7 +247,7 @@ discard block |
||
| 247 | 247 | } catch (\DomainException $e) { |
| 248 | 248 | throw new OCSForbiddenException($e->getMessage(), $e); |
| 249 | 249 | } catch (\Exception $e) { |
| 250 | - $this->logger->error('Error when updating flow with id ' . $id, ['exception' => $e]); |
|
| 250 | + $this->logger->error('Error when updating flow with id '.$id, ['exception' => $e]); |
|
| 251 | 251 | throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e); |
| 252 | 252 | } |
| 253 | 253 | } |
@@ -277,7 +277,7 @@ discard block |
||
| 277 | 277 | } catch (\DomainException $e) { |
| 278 | 278 | throw new OCSForbiddenException($e->getMessage(), $e); |
| 279 | 279 | } catch (\Exception $e) { |
| 280 | - $this->logger->error('Error when deleting flow with id ' . $id, ['exception' => $e]); |
|
| 280 | + $this->logger->error('Error when deleting flow with id '.$id, ['exception' => $e]); |
|
| 281 | 281 | throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e); |
| 282 | 282 | } |
| 283 | 283 | } |
@@ -307,7 +307,7 @@ discard block |
||
| 307 | 307 | } catch (\DomainException $e) { |
| 308 | 308 | throw new OCSForbiddenException($e->getMessage(), $e); |
| 309 | 309 | } catch (\Exception $e) { |
| 310 | - $this->logger->error('Error when deleting flows for app id ' . $appid, ['exception' => $e]); |
|
| 310 | + $this->logger->error('Error when deleting flows for app id '.$appid, ['exception' => $e]); |
|
| 311 | 311 | throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e); |
| 312 | 312 | } |
| 313 | 313 | } |
@@ -23,243 +23,243 @@ |
||
| 23 | 23 | * @template-extends QBMapper<WebhookListener> |
| 24 | 24 | */ |
| 25 | 25 | class WebhookListenerMapper extends QBMapper { |
| 26 | - public const TABLE_NAME = 'webhook_listeners'; |
|
| 27 | - |
|
| 28 | - private const EVENTS_CACHE_KEY_PREFIX = 'eventsUsedInWebhooks'; |
|
| 29 | - |
|
| 30 | - private ?ICache $cache = null; |
|
| 31 | - |
|
| 32 | - public function __construct( |
|
| 33 | - IDBConnection $db, |
|
| 34 | - ICacheFactory $cacheFactory, |
|
| 35 | - ) { |
|
| 36 | - parent::__construct($db, self::TABLE_NAME, WebhookListener::class); |
|
| 37 | - if ($cacheFactory->isAvailable()) { |
|
| 38 | - $this->cache = $cacheFactory->createDistributed(); |
|
| 39 | - } |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @throws DoesNotExistException |
|
| 44 | - * @throws MultipleObjectsReturnedException |
|
| 45 | - * @throws Exception |
|
| 46 | - */ |
|
| 47 | - public function getById(int $id): WebhookListener { |
|
| 48 | - $qb = $this->db->getQueryBuilder(); |
|
| 49 | - |
|
| 50 | - $qb->select('*') |
|
| 51 | - ->from($this->getTableName()) |
|
| 52 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))); |
|
| 53 | - |
|
| 54 | - return $this->findEntity($qb); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @throws Exception |
|
| 59 | - * @return WebhookListener[] |
|
| 60 | - */ |
|
| 61 | - public function getAll(): array { |
|
| 62 | - $qb = $this->db->getQueryBuilder(); |
|
| 63 | - |
|
| 64 | - $qb->select('*') |
|
| 65 | - ->from($this->getTableName()); |
|
| 66 | - |
|
| 67 | - return $this->findEntities($qb); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @throws Exception |
|
| 72 | - */ |
|
| 73 | - public function addWebhookListener( |
|
| 74 | - ?string $appId, |
|
| 75 | - ?string $userId, |
|
| 76 | - string $httpMethod, |
|
| 77 | - string $uri, |
|
| 78 | - string $event, |
|
| 79 | - ?array $eventFilter, |
|
| 80 | - ?string $userIdFilter, |
|
| 81 | - ?array $headers, |
|
| 82 | - AuthMethod $authMethod, |
|
| 83 | - #[\SensitiveParameter] |
|
| 84 | - ?array $authData, |
|
| 85 | - ?array $tokenNeeded = [], |
|
| 86 | - ): WebhookListener { |
|
| 87 | - /* Remove any superfluous antislash */ |
|
| 88 | - $event = ltrim($event, '\\'); |
|
| 89 | - if (!class_exists($event) || !is_a($event, IWebhookCompatibleEvent::class, true)) { |
|
| 90 | - throw new \UnexpectedValueException("$event is not an event class compatible with webhooks"); |
|
| 91 | - } |
|
| 92 | - $webhookListener = WebhookListener::fromParams( |
|
| 93 | - [ |
|
| 94 | - 'appId' => $appId, |
|
| 95 | - 'userId' => $userId, |
|
| 96 | - 'httpMethod' => $httpMethod, |
|
| 97 | - 'uri' => $uri, |
|
| 98 | - 'event' => $event, |
|
| 99 | - 'eventFilter' => $eventFilter ?? [], |
|
| 100 | - 'userIdFilter' => $userIdFilter ?? '', |
|
| 101 | - 'headers' => $headers, |
|
| 102 | - 'authMethod' => $authMethod->value, |
|
| 103 | - 'tokenNeeded' => $tokenNeeded ?? [], |
|
| 104 | - ] |
|
| 105 | - ); |
|
| 106 | - $webhookListener->setAuthDataClear($authData); |
|
| 107 | - $this->cache?->remove($this->buildCacheKey($userIdFilter)); |
|
| 108 | - return $this->insert($webhookListener); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @throws Exception |
|
| 113 | - */ |
|
| 114 | - public function updateWebhookListener( |
|
| 115 | - int $id, |
|
| 116 | - ?string $appId, |
|
| 117 | - ?string $userId, |
|
| 118 | - string $httpMethod, |
|
| 119 | - string $uri, |
|
| 120 | - string $event, |
|
| 121 | - ?array $eventFilter, |
|
| 122 | - ?string $userIdFilter, |
|
| 123 | - ?array $headers, |
|
| 124 | - AuthMethod $authMethod, |
|
| 125 | - #[\SensitiveParameter] |
|
| 126 | - ?array $authData, |
|
| 127 | - ?array $tokenNeeded = [], |
|
| 128 | - ): WebhookListener { |
|
| 129 | - /* Remove any superfluous antislash */ |
|
| 130 | - $event = ltrim($event, '\\'); |
|
| 131 | - if (!class_exists($event) || !is_a($event, IWebhookCompatibleEvent::class, true)) { |
|
| 132 | - throw new \UnexpectedValueException("$event is not an event class compatible with webhooks"); |
|
| 133 | - } |
|
| 134 | - $webhookListener = WebhookListener::fromParams( |
|
| 135 | - [ |
|
| 136 | - 'id' => $id, |
|
| 137 | - 'appId' => $appId, |
|
| 138 | - 'userId' => $userId, |
|
| 139 | - 'httpMethod' => $httpMethod, |
|
| 140 | - 'uri' => $uri, |
|
| 141 | - 'event' => $event, |
|
| 142 | - 'eventFilter' => $eventFilter ?? [], |
|
| 143 | - 'userIdFilter' => $userIdFilter ?? '', |
|
| 144 | - 'headers' => $headers, |
|
| 145 | - 'authMethod' => $authMethod->value, |
|
| 146 | - 'tokenNeeded' => $tokenNeeded ?? [], |
|
| 147 | - ] |
|
| 148 | - ); |
|
| 149 | - $webhookListener->setAuthDataClear($authData); |
|
| 150 | - $this->cache?->remove($this->buildCacheKey($userIdFilter)); |
|
| 151 | - return $this->update($webhookListener); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * @throws Exception |
|
| 156 | - */ |
|
| 157 | - public function deleteById(int $id): bool { |
|
| 158 | - $qb = $this->db->getQueryBuilder(); |
|
| 159 | - |
|
| 160 | - $qb->delete($this->getTableName()) |
|
| 161 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))); |
|
| 162 | - |
|
| 163 | - return ($qb->executeStatement() > 0); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Delete all registrations made by the given appId |
|
| 168 | - * |
|
| 169 | - * @throws Exception |
|
| 170 | - * @return int number of registration deleted |
|
| 171 | - */ |
|
| 172 | - public function deleteByAppId(string $appId): int { |
|
| 173 | - $qb = $this->db->getQueryBuilder(); |
|
| 174 | - |
|
| 175 | - $qb->delete($this->getTableName()) |
|
| 176 | - ->where($qb->expr()->eq('app_id', $qb->createNamedParameter($appId, IQueryBuilder::PARAM_STR))); |
|
| 177 | - |
|
| 178 | - return $qb->executeStatement(); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * @throws Exception |
|
| 183 | - * @return list<string> |
|
| 184 | - */ |
|
| 185 | - private function getAllConfiguredEventsFromDatabase(string $userId): array { |
|
| 186 | - $qb = $this->db->getQueryBuilder(); |
|
| 187 | - |
|
| 188 | - $qb->selectDistinct('event') |
|
| 189 | - ->from($this->getTableName()) |
|
| 190 | - ->where($qb->expr()->emptyString('user_id_filter')); |
|
| 191 | - |
|
| 192 | - if ($userId !== '') { |
|
| 193 | - $qb->orWhere($qb->expr()->eq('user_id_filter', $qb->createNamedParameter($userId))); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - $result = $qb->executeQuery(); |
|
| 197 | - |
|
| 198 | - $configuredEvents = []; |
|
| 199 | - |
|
| 200 | - while (($event = $result->fetchOne()) !== false) { |
|
| 201 | - $configuredEvents[] = $event; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - return $configuredEvents; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * List all events with at least one webhook configured, with cache |
|
| 209 | - * @throws Exception |
|
| 210 | - * @return list<string> |
|
| 211 | - */ |
|
| 212 | - public function getAllConfiguredEvents(?string $userId = null): array { |
|
| 213 | - $cacheKey = $this->buildCacheKey($userId); |
|
| 214 | - $events = $this->cache?->get($cacheKey); |
|
| 215 | - if ($events !== null) { |
|
| 216 | - return json_decode($events); |
|
| 217 | - } |
|
| 218 | - $events = $this->getAllConfiguredEventsFromDatabase($userId ?? ''); |
|
| 219 | - // cache for 5 minutes |
|
| 220 | - $this->cache?->set($cacheKey, json_encode($events), 300); |
|
| 221 | - return $events; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * @throws Exception |
|
| 226 | - */ |
|
| 227 | - public function getByEvent(string $event, ?string $userId = null): array { |
|
| 228 | - $qb = $this->db->getQueryBuilder(); |
|
| 229 | - |
|
| 230 | - $qb->select('*') |
|
| 231 | - ->from($this->getTableName()) |
|
| 232 | - ->where($qb->expr()->eq('event', $qb->createNamedParameter($event, IQueryBuilder::PARAM_STR))); |
|
| 233 | - |
|
| 234 | - |
|
| 235 | - if ($userId === '' || $userId === null) { |
|
| 236 | - $qb->andWhere($qb->expr()->emptyString('user_id_filter')); |
|
| 237 | - } else { |
|
| 238 | - $qb->andWhere( |
|
| 239 | - $qb->expr()->orX( |
|
| 240 | - $qb->expr()->emptyString('user_id_filter'), |
|
| 241 | - $qb->expr()->eq('user_id_filter', $qb->createNamedParameter($userId)), |
|
| 242 | - ) |
|
| 243 | - ); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - return $this->findEntities($qb); |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * @throws Exception |
|
| 251 | - */ |
|
| 252 | - public function getByUri(string $uri): array { |
|
| 253 | - $qb = $this->db->getQueryBuilder(); |
|
| 254 | - |
|
| 255 | - $qb->select('*') |
|
| 256 | - ->from($this->getTableName()) |
|
| 257 | - ->where($qb->expr()->eq('uri', $qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR))); |
|
| 258 | - |
|
| 259 | - return $this->findEntities($qb); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - private function buildCacheKey(?string $userIdFilter): string { |
|
| 263 | - return self::EVENTS_CACHE_KEY_PREFIX . '_' . ($userIdFilter ?? ''); |
|
| 264 | - } |
|
| 26 | + public const TABLE_NAME = 'webhook_listeners'; |
|
| 27 | + |
|
| 28 | + private const EVENTS_CACHE_KEY_PREFIX = 'eventsUsedInWebhooks'; |
|
| 29 | + |
|
| 30 | + private ?ICache $cache = null; |
|
| 31 | + |
|
| 32 | + public function __construct( |
|
| 33 | + IDBConnection $db, |
|
| 34 | + ICacheFactory $cacheFactory, |
|
| 35 | + ) { |
|
| 36 | + parent::__construct($db, self::TABLE_NAME, WebhookListener::class); |
|
| 37 | + if ($cacheFactory->isAvailable()) { |
|
| 38 | + $this->cache = $cacheFactory->createDistributed(); |
|
| 39 | + } |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @throws DoesNotExistException |
|
| 44 | + * @throws MultipleObjectsReturnedException |
|
| 45 | + * @throws Exception |
|
| 46 | + */ |
|
| 47 | + public function getById(int $id): WebhookListener { |
|
| 48 | + $qb = $this->db->getQueryBuilder(); |
|
| 49 | + |
|
| 50 | + $qb->select('*') |
|
| 51 | + ->from($this->getTableName()) |
|
| 52 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))); |
|
| 53 | + |
|
| 54 | + return $this->findEntity($qb); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @throws Exception |
|
| 59 | + * @return WebhookListener[] |
|
| 60 | + */ |
|
| 61 | + public function getAll(): array { |
|
| 62 | + $qb = $this->db->getQueryBuilder(); |
|
| 63 | + |
|
| 64 | + $qb->select('*') |
|
| 65 | + ->from($this->getTableName()); |
|
| 66 | + |
|
| 67 | + return $this->findEntities($qb); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @throws Exception |
|
| 72 | + */ |
|
| 73 | + public function addWebhookListener( |
|
| 74 | + ?string $appId, |
|
| 75 | + ?string $userId, |
|
| 76 | + string $httpMethod, |
|
| 77 | + string $uri, |
|
| 78 | + string $event, |
|
| 79 | + ?array $eventFilter, |
|
| 80 | + ?string $userIdFilter, |
|
| 81 | + ?array $headers, |
|
| 82 | + AuthMethod $authMethod, |
|
| 83 | + #[\SensitiveParameter] |
|
| 84 | + ?array $authData, |
|
| 85 | + ?array $tokenNeeded = [], |
|
| 86 | + ): WebhookListener { |
|
| 87 | + /* Remove any superfluous antislash */ |
|
| 88 | + $event = ltrim($event, '\\'); |
|
| 89 | + if (!class_exists($event) || !is_a($event, IWebhookCompatibleEvent::class, true)) { |
|
| 90 | + throw new \UnexpectedValueException("$event is not an event class compatible with webhooks"); |
|
| 91 | + } |
|
| 92 | + $webhookListener = WebhookListener::fromParams( |
|
| 93 | + [ |
|
| 94 | + 'appId' => $appId, |
|
| 95 | + 'userId' => $userId, |
|
| 96 | + 'httpMethod' => $httpMethod, |
|
| 97 | + 'uri' => $uri, |
|
| 98 | + 'event' => $event, |
|
| 99 | + 'eventFilter' => $eventFilter ?? [], |
|
| 100 | + 'userIdFilter' => $userIdFilter ?? '', |
|
| 101 | + 'headers' => $headers, |
|
| 102 | + 'authMethod' => $authMethod->value, |
|
| 103 | + 'tokenNeeded' => $tokenNeeded ?? [], |
|
| 104 | + ] |
|
| 105 | + ); |
|
| 106 | + $webhookListener->setAuthDataClear($authData); |
|
| 107 | + $this->cache?->remove($this->buildCacheKey($userIdFilter)); |
|
| 108 | + return $this->insert($webhookListener); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @throws Exception |
|
| 113 | + */ |
|
| 114 | + public function updateWebhookListener( |
|
| 115 | + int $id, |
|
| 116 | + ?string $appId, |
|
| 117 | + ?string $userId, |
|
| 118 | + string $httpMethod, |
|
| 119 | + string $uri, |
|
| 120 | + string $event, |
|
| 121 | + ?array $eventFilter, |
|
| 122 | + ?string $userIdFilter, |
|
| 123 | + ?array $headers, |
|
| 124 | + AuthMethod $authMethod, |
|
| 125 | + #[\SensitiveParameter] |
|
| 126 | + ?array $authData, |
|
| 127 | + ?array $tokenNeeded = [], |
|
| 128 | + ): WebhookListener { |
|
| 129 | + /* Remove any superfluous antislash */ |
|
| 130 | + $event = ltrim($event, '\\'); |
|
| 131 | + if (!class_exists($event) || !is_a($event, IWebhookCompatibleEvent::class, true)) { |
|
| 132 | + throw new \UnexpectedValueException("$event is not an event class compatible with webhooks"); |
|
| 133 | + } |
|
| 134 | + $webhookListener = WebhookListener::fromParams( |
|
| 135 | + [ |
|
| 136 | + 'id' => $id, |
|
| 137 | + 'appId' => $appId, |
|
| 138 | + 'userId' => $userId, |
|
| 139 | + 'httpMethod' => $httpMethod, |
|
| 140 | + 'uri' => $uri, |
|
| 141 | + 'event' => $event, |
|
| 142 | + 'eventFilter' => $eventFilter ?? [], |
|
| 143 | + 'userIdFilter' => $userIdFilter ?? '', |
|
| 144 | + 'headers' => $headers, |
|
| 145 | + 'authMethod' => $authMethod->value, |
|
| 146 | + 'tokenNeeded' => $tokenNeeded ?? [], |
|
| 147 | + ] |
|
| 148 | + ); |
|
| 149 | + $webhookListener->setAuthDataClear($authData); |
|
| 150 | + $this->cache?->remove($this->buildCacheKey($userIdFilter)); |
|
| 151 | + return $this->update($webhookListener); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * @throws Exception |
|
| 156 | + */ |
|
| 157 | + public function deleteById(int $id): bool { |
|
| 158 | + $qb = $this->db->getQueryBuilder(); |
|
| 159 | + |
|
| 160 | + $qb->delete($this->getTableName()) |
|
| 161 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))); |
|
| 162 | + |
|
| 163 | + return ($qb->executeStatement() > 0); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Delete all registrations made by the given appId |
|
| 168 | + * |
|
| 169 | + * @throws Exception |
|
| 170 | + * @return int number of registration deleted |
|
| 171 | + */ |
|
| 172 | + public function deleteByAppId(string $appId): int { |
|
| 173 | + $qb = $this->db->getQueryBuilder(); |
|
| 174 | + |
|
| 175 | + $qb->delete($this->getTableName()) |
|
| 176 | + ->where($qb->expr()->eq('app_id', $qb->createNamedParameter($appId, IQueryBuilder::PARAM_STR))); |
|
| 177 | + |
|
| 178 | + return $qb->executeStatement(); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * @throws Exception |
|
| 183 | + * @return list<string> |
|
| 184 | + */ |
|
| 185 | + private function getAllConfiguredEventsFromDatabase(string $userId): array { |
|
| 186 | + $qb = $this->db->getQueryBuilder(); |
|
| 187 | + |
|
| 188 | + $qb->selectDistinct('event') |
|
| 189 | + ->from($this->getTableName()) |
|
| 190 | + ->where($qb->expr()->emptyString('user_id_filter')); |
|
| 191 | + |
|
| 192 | + if ($userId !== '') { |
|
| 193 | + $qb->orWhere($qb->expr()->eq('user_id_filter', $qb->createNamedParameter($userId))); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + $result = $qb->executeQuery(); |
|
| 197 | + |
|
| 198 | + $configuredEvents = []; |
|
| 199 | + |
|
| 200 | + while (($event = $result->fetchOne()) !== false) { |
|
| 201 | + $configuredEvents[] = $event; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + return $configuredEvents; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * List all events with at least one webhook configured, with cache |
|
| 209 | + * @throws Exception |
|
| 210 | + * @return list<string> |
|
| 211 | + */ |
|
| 212 | + public function getAllConfiguredEvents(?string $userId = null): array { |
|
| 213 | + $cacheKey = $this->buildCacheKey($userId); |
|
| 214 | + $events = $this->cache?->get($cacheKey); |
|
| 215 | + if ($events !== null) { |
|
| 216 | + return json_decode($events); |
|
| 217 | + } |
|
| 218 | + $events = $this->getAllConfiguredEventsFromDatabase($userId ?? ''); |
|
| 219 | + // cache for 5 minutes |
|
| 220 | + $this->cache?->set($cacheKey, json_encode($events), 300); |
|
| 221 | + return $events; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * @throws Exception |
|
| 226 | + */ |
|
| 227 | + public function getByEvent(string $event, ?string $userId = null): array { |
|
| 228 | + $qb = $this->db->getQueryBuilder(); |
|
| 229 | + |
|
| 230 | + $qb->select('*') |
|
| 231 | + ->from($this->getTableName()) |
|
| 232 | + ->where($qb->expr()->eq('event', $qb->createNamedParameter($event, IQueryBuilder::PARAM_STR))); |
|
| 233 | + |
|
| 234 | + |
|
| 235 | + if ($userId === '' || $userId === null) { |
|
| 236 | + $qb->andWhere($qb->expr()->emptyString('user_id_filter')); |
|
| 237 | + } else { |
|
| 238 | + $qb->andWhere( |
|
| 239 | + $qb->expr()->orX( |
|
| 240 | + $qb->expr()->emptyString('user_id_filter'), |
|
| 241 | + $qb->expr()->eq('user_id_filter', $qb->createNamedParameter($userId)), |
|
| 242 | + ) |
|
| 243 | + ); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + return $this->findEntities($qb); |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * @throws Exception |
|
| 251 | + */ |
|
| 252 | + public function getByUri(string $uri): array { |
|
| 253 | + $qb = $this->db->getQueryBuilder(); |
|
| 254 | + |
|
| 255 | + $qb->select('*') |
|
| 256 | + ->from($this->getTableName()) |
|
| 257 | + ->where($qb->expr()->eq('uri', $qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR))); |
|
| 258 | + |
|
| 259 | + return $this->findEntities($qb); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + private function buildCacheKey(?string $userIdFilter): string { |
|
| 263 | + return self::EVENTS_CACHE_KEY_PREFIX . '_' . ($userIdFilter ?? ''); |
|
| 264 | + } |
|
| 265 | 265 | } |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | ?array $headers, |
| 82 | 82 | AuthMethod $authMethod, |
| 83 | 83 | #[\SensitiveParameter] |
| 84 | - ?array $authData, |
|
| 84 | + ? array $authData, |
|
| 85 | 85 | ?array $tokenNeeded = [], |
| 86 | 86 | ): WebhookListener { |
| 87 | 87 | /* Remove any superfluous antislash */ |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | ?array $headers, |
| 124 | 124 | AuthMethod $authMethod, |
| 125 | 125 | #[\SensitiveParameter] |
| 126 | - ?array $authData, |
|
| 126 | + ? array $authData, |
|
| 127 | 127 | ?array $tokenNeeded = [], |
| 128 | 128 | ): WebhookListener { |
| 129 | 129 | /* Remove any superfluous antislash */ |
@@ -260,6 +260,6 @@ discard block |
||
| 260 | 260 | } |
| 261 | 261 | |
| 262 | 262 | private function buildCacheKey(?string $userIdFilter): string { |
| 263 | - return self::EVENTS_CACHE_KEY_PREFIX . '_' . ($userIdFilter ?? ''); |
|
| 263 | + return self::EVENTS_CACHE_KEY_PREFIX.'_'.($userIdFilter ?? ''); |
|
| 264 | 264 | } |
| 265 | 265 | } |
@@ -24,98 +24,98 @@ |
||
| 24 | 24 | */ |
| 25 | 25 | |
| 26 | 26 | class EphemeralTokenMapper extends QBMapper { |
| 27 | - public const TABLE_NAME = 'webhook_tokens'; |
|
| 28 | - public const TOKEN_LIFETIME = 1 * 1 * 60; // one hour in seconds |
|
| 29 | - |
|
| 30 | - public function __construct( |
|
| 31 | - IDBConnection $db, |
|
| 32 | - private LoggerInterface $logger, |
|
| 33 | - private ITimeFactory $time, |
|
| 34 | - private PublicKeyTokenMapper $tokenMapper, |
|
| 35 | - ) { |
|
| 36 | - parent::__construct($db, self::TABLE_NAME, EphemeralToken::class); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @throws DoesNotExistException |
|
| 41 | - * @throws MultipleObjectsReturnedException |
|
| 42 | - * @throws Exception |
|
| 43 | - */ |
|
| 44 | - public function getById(int $id): EphemeralToken { |
|
| 45 | - $qb = $this->db->getQueryBuilder(); |
|
| 46 | - |
|
| 47 | - $qb->select('*') |
|
| 48 | - ->from($this->getTableName()) |
|
| 49 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))); |
|
| 50 | - |
|
| 51 | - return $this->findEntity($qb); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @throws Exception |
|
| 56 | - * @return EphemeralToken[] |
|
| 57 | - */ |
|
| 58 | - public function getAll(): array { |
|
| 59 | - $qb = $this->db->getQueryBuilder(); |
|
| 60 | - |
|
| 61 | - $qb->select('*') |
|
| 62 | - ->from($this->getTableName()); |
|
| 63 | - |
|
| 64 | - return $this->findEntities($qb); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @param int $olderThan |
|
| 70 | - * @return EphemeralToken[] |
|
| 71 | - * @throws Exception |
|
| 72 | - */ |
|
| 73 | - public function getOlderThan($olderThan): array { |
|
| 74 | - $qb = $this->db->getQueryBuilder(); |
|
| 75 | - |
|
| 76 | - $qb->select('*') |
|
| 77 | - ->from($this->getTableName()) |
|
| 78 | - ->where($qb->expr()->lt('created_at', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT))); |
|
| 79 | - |
|
| 80 | - return $this->findEntities($qb); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * @throws Exception |
|
| 85 | - */ |
|
| 86 | - public function addEphemeralToken( |
|
| 87 | - int $tokenId, |
|
| 88 | - ?string $userId, |
|
| 89 | - int $createdAt, |
|
| 90 | - ): EphemeralToken { |
|
| 91 | - $tempToken = EphemeralToken::fromParams( |
|
| 92 | - [ |
|
| 93 | - 'tokenId' => $tokenId, |
|
| 94 | - 'userId' => $userId, |
|
| 95 | - 'createdAt' => $createdAt, |
|
| 96 | - ] |
|
| 97 | - ); |
|
| 98 | - return $this->insert($tempToken); |
|
| 99 | - } |
|
| 100 | - public function invalidateOldTokens(int $token_lifetime = self::TOKEN_LIFETIME) { |
|
| 101 | - $olderThan = $this->time->getTime() - $token_lifetime; |
|
| 102 | - try { |
|
| 103 | - $tokensToDelete = $this->getOlderThan($olderThan); |
|
| 104 | - } catch (Exception $e) { |
|
| 105 | - $this->logger->error('Webhook token deletion failed: ' . $e->getMessage(), ['exception' => $e]); |
|
| 106 | - return; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - |
|
| 110 | - $this->logger->debug('Invalidating ephemeral webhook tokens older than ' . date('c', $olderThan), ['app' => 'webhook_listeners']); |
|
| 111 | - foreach ($tokensToDelete as $token) { |
|
| 112 | - try { |
|
| 113 | - $this->tokenMapper->delete($this->tokenMapper->getTokenById($token->getTokenId())); // delete token itself |
|
| 114 | - $this->delete($token); // delete db row in webhook_tokens |
|
| 115 | - } catch (Exception $e) { |
|
| 116 | - $this->logger->error('Webhook token deletion failed: ' . $e->getMessage(), ['exception' => $e]); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - } |
|
| 120 | - } |
|
| 27 | + public const TABLE_NAME = 'webhook_tokens'; |
|
| 28 | + public const TOKEN_LIFETIME = 1 * 1 * 60; // one hour in seconds |
|
| 29 | + |
|
| 30 | + public function __construct( |
|
| 31 | + IDBConnection $db, |
|
| 32 | + private LoggerInterface $logger, |
|
| 33 | + private ITimeFactory $time, |
|
| 34 | + private PublicKeyTokenMapper $tokenMapper, |
|
| 35 | + ) { |
|
| 36 | + parent::__construct($db, self::TABLE_NAME, EphemeralToken::class); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @throws DoesNotExistException |
|
| 41 | + * @throws MultipleObjectsReturnedException |
|
| 42 | + * @throws Exception |
|
| 43 | + */ |
|
| 44 | + public function getById(int $id): EphemeralToken { |
|
| 45 | + $qb = $this->db->getQueryBuilder(); |
|
| 46 | + |
|
| 47 | + $qb->select('*') |
|
| 48 | + ->from($this->getTableName()) |
|
| 49 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))); |
|
| 50 | + |
|
| 51 | + return $this->findEntity($qb); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @throws Exception |
|
| 56 | + * @return EphemeralToken[] |
|
| 57 | + */ |
|
| 58 | + public function getAll(): array { |
|
| 59 | + $qb = $this->db->getQueryBuilder(); |
|
| 60 | + |
|
| 61 | + $qb->select('*') |
|
| 62 | + ->from($this->getTableName()); |
|
| 63 | + |
|
| 64 | + return $this->findEntities($qb); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @param int $olderThan |
|
| 70 | + * @return EphemeralToken[] |
|
| 71 | + * @throws Exception |
|
| 72 | + */ |
|
| 73 | + public function getOlderThan($olderThan): array { |
|
| 74 | + $qb = $this->db->getQueryBuilder(); |
|
| 75 | + |
|
| 76 | + $qb->select('*') |
|
| 77 | + ->from($this->getTableName()) |
|
| 78 | + ->where($qb->expr()->lt('created_at', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT))); |
|
| 79 | + |
|
| 80 | + return $this->findEntities($qb); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * @throws Exception |
|
| 85 | + */ |
|
| 86 | + public function addEphemeralToken( |
|
| 87 | + int $tokenId, |
|
| 88 | + ?string $userId, |
|
| 89 | + int $createdAt, |
|
| 90 | + ): EphemeralToken { |
|
| 91 | + $tempToken = EphemeralToken::fromParams( |
|
| 92 | + [ |
|
| 93 | + 'tokenId' => $tokenId, |
|
| 94 | + 'userId' => $userId, |
|
| 95 | + 'createdAt' => $createdAt, |
|
| 96 | + ] |
|
| 97 | + ); |
|
| 98 | + return $this->insert($tempToken); |
|
| 99 | + } |
|
| 100 | + public function invalidateOldTokens(int $token_lifetime = self::TOKEN_LIFETIME) { |
|
| 101 | + $olderThan = $this->time->getTime() - $token_lifetime; |
|
| 102 | + try { |
|
| 103 | + $tokensToDelete = $this->getOlderThan($olderThan); |
|
| 104 | + } catch (Exception $e) { |
|
| 105 | + $this->logger->error('Webhook token deletion failed: ' . $e->getMessage(), ['exception' => $e]); |
|
| 106 | + return; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + |
|
| 110 | + $this->logger->debug('Invalidating ephemeral webhook tokens older than ' . date('c', $olderThan), ['app' => 'webhook_listeners']); |
|
| 111 | + foreach ($tokensToDelete as $token) { |
|
| 112 | + try { |
|
| 113 | + $this->tokenMapper->delete($this->tokenMapper->getTokenById($token->getTokenId())); // delete token itself |
|
| 114 | + $this->delete($token); // delete db row in webhook_tokens |
|
| 115 | + } catch (Exception $e) { |
|
| 116 | + $this->logger->error('Webhook token deletion failed: ' . $e->getMessage(), ['exception' => $e]); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + } |
|
| 120 | + } |
|
| 121 | 121 | } |
@@ -102,18 +102,18 @@ |
||
| 102 | 102 | try { |
| 103 | 103 | $tokensToDelete = $this->getOlderThan($olderThan); |
| 104 | 104 | } catch (Exception $e) { |
| 105 | - $this->logger->error('Webhook token deletion failed: ' . $e->getMessage(), ['exception' => $e]); |
|
| 105 | + $this->logger->error('Webhook token deletion failed: '.$e->getMessage(), ['exception' => $e]); |
|
| 106 | 106 | return; |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | |
| 110 | - $this->logger->debug('Invalidating ephemeral webhook tokens older than ' . date('c', $olderThan), ['app' => 'webhook_listeners']); |
|
| 110 | + $this->logger->debug('Invalidating ephemeral webhook tokens older than '.date('c', $olderThan), ['app' => 'webhook_listeners']); |
|
| 111 | 111 | foreach ($tokensToDelete as $token) { |
| 112 | 112 | try { |
| 113 | 113 | $this->tokenMapper->delete($this->tokenMapper->getTokenById($token->getTokenId())); // delete token itself |
| 114 | 114 | $this->delete($token); // delete db row in webhook_tokens |
| 115 | 115 | } catch (Exception $e) { |
| 116 | - $this->logger->error('Webhook token deletion failed: ' . $e->getMessage(), ['exception' => $e]); |
|
| 116 | + $this->logger->error('Webhook token deletion failed: '.$e->getMessage(), ['exception' => $e]); |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | } |
@@ -27,131 +27,131 @@ |
||
| 27 | 27 | * @psalm-suppress PropertyNotSetInConstructor |
| 28 | 28 | */ |
| 29 | 29 | class WebhookListener extends Entity implements \JsonSerializable { |
| 30 | - /** |
|
| 31 | - * @var ?string id of the app_api application who added the webhook listener |
|
| 32 | - */ |
|
| 33 | - protected $appId = null; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @var ?string id of the user who added the webhook listener |
|
| 37 | - * @psalm-suppress PropertyNotSetInConstructor |
|
| 38 | - */ |
|
| 39 | - protected $userId = null; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var string |
|
| 43 | - * @psalm-suppress PropertyNotSetInConstructor |
|
| 44 | - */ |
|
| 45 | - protected $httpMethod; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @var string |
|
| 49 | - * @psalm-suppress PropertyNotSetInConstructor |
|
| 50 | - */ |
|
| 51 | - protected $uri; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @var string |
|
| 55 | - * @psalm-suppress PropertyNotSetInConstructor |
|
| 56 | - */ |
|
| 57 | - protected $event; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @var array |
|
| 61 | - * @psalm-suppress PropertyNotSetInConstructor |
|
| 62 | - */ |
|
| 63 | - protected $eventFilter; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @var ?string |
|
| 67 | - * If not empty, id of the user that needs to be connected for the webhook to trigger |
|
| 68 | - * @psalm-suppress PropertyNotSetInConstructor |
|
| 69 | - */ |
|
| 70 | - protected $userIdFilter; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * @var ?array |
|
| 74 | - */ |
|
| 75 | - protected $headers = null; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * @var string |
|
| 79 | - * @psalm-suppress PropertyNotSetInConstructor |
|
| 80 | - */ |
|
| 81 | - protected $authMethod; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * @var ?string |
|
| 85 | - */ |
|
| 86 | - protected $authData = null; |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @var array |
|
| 90 | - * @psalm-suppress PropertyNotSetInConstructor |
|
| 91 | - */ |
|
| 92 | - protected $tokenNeeded; |
|
| 93 | - |
|
| 94 | - private ICrypto $crypto; |
|
| 95 | - |
|
| 96 | - |
|
| 97 | - public function __construct( |
|
| 98 | - ?ICrypto $crypto = null, |
|
| 99 | - ) { |
|
| 100 | - if ($crypto === null) { |
|
| 101 | - $crypto = Server::get(ICrypto::class); |
|
| 102 | - } |
|
| 103 | - $this->crypto = $crypto; |
|
| 104 | - $this->addType('appId', 'string'); |
|
| 105 | - $this->addType('userId', 'string'); |
|
| 106 | - $this->addType('httpMethod', 'string'); |
|
| 107 | - $this->addType('uri', 'string'); |
|
| 108 | - $this->addType('event', 'string'); |
|
| 109 | - $this->addType('eventFilter', 'json'); |
|
| 110 | - $this->addType('userIdFilter', 'string'); |
|
| 111 | - $this->addType('headers', 'json'); |
|
| 112 | - $this->addType('authMethod', 'string'); |
|
| 113 | - $this->addType('authData', 'string'); |
|
| 114 | - $this->addType('tokenNeeded', 'json'); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - public function getAuthMethodEnum(): AuthMethod { |
|
| 118 | - return AuthMethod::from($this->getAuthMethod()); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - public function getAuthDataClear(): array { |
|
| 122 | - $authData = $this->getAuthData(); |
|
| 123 | - if ($authData === null) { |
|
| 124 | - return []; |
|
| 125 | - } |
|
| 126 | - return json_decode($this->crypto->decrypt($authData), associative:true, flags:JSON_THROW_ON_ERROR); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - public function setAuthDataClear( |
|
| 130 | - #[\SensitiveParameter] |
|
| 131 | - ?array $data, |
|
| 132 | - ): void { |
|
| 133 | - if ($data === null) { |
|
| 134 | - if ($this->getAuthMethodEnum() === AuthMethod::Header) { |
|
| 135 | - throw new \UnexpectedValueException('Header auth method needs an associative array of headers as auth data'); |
|
| 136 | - } |
|
| 137 | - $this->setAuthData(null); |
|
| 138 | - return; |
|
| 139 | - } |
|
| 140 | - $this->setAuthData($this->crypto->encrypt(json_encode($data))); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - public function jsonSerialize(): array { |
|
| 144 | - $fields = array_keys($this->getFieldTypes()); |
|
| 145 | - return array_combine( |
|
| 146 | - $fields, |
|
| 147 | - array_map( |
|
| 148 | - fn ($field) => $this->getter($field), |
|
| 149 | - $fields |
|
| 150 | - ) |
|
| 151 | - ); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - public function getAppId(): ?string { |
|
| 155 | - return $this->appId; |
|
| 156 | - } |
|
| 30 | + /** |
|
| 31 | + * @var ?string id of the app_api application who added the webhook listener |
|
| 32 | + */ |
|
| 33 | + protected $appId = null; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @var ?string id of the user who added the webhook listener |
|
| 37 | + * @psalm-suppress PropertyNotSetInConstructor |
|
| 38 | + */ |
|
| 39 | + protected $userId = null; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var string |
|
| 43 | + * @psalm-suppress PropertyNotSetInConstructor |
|
| 44 | + */ |
|
| 45 | + protected $httpMethod; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @var string |
|
| 49 | + * @psalm-suppress PropertyNotSetInConstructor |
|
| 50 | + */ |
|
| 51 | + protected $uri; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @var string |
|
| 55 | + * @psalm-suppress PropertyNotSetInConstructor |
|
| 56 | + */ |
|
| 57 | + protected $event; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @var array |
|
| 61 | + * @psalm-suppress PropertyNotSetInConstructor |
|
| 62 | + */ |
|
| 63 | + protected $eventFilter; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @var ?string |
|
| 67 | + * If not empty, id of the user that needs to be connected for the webhook to trigger |
|
| 68 | + * @psalm-suppress PropertyNotSetInConstructor |
|
| 69 | + */ |
|
| 70 | + protected $userIdFilter; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * @var ?array |
|
| 74 | + */ |
|
| 75 | + protected $headers = null; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * @var string |
|
| 79 | + * @psalm-suppress PropertyNotSetInConstructor |
|
| 80 | + */ |
|
| 81 | + protected $authMethod; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * @var ?string |
|
| 85 | + */ |
|
| 86 | + protected $authData = null; |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @var array |
|
| 90 | + * @psalm-suppress PropertyNotSetInConstructor |
|
| 91 | + */ |
|
| 92 | + protected $tokenNeeded; |
|
| 93 | + |
|
| 94 | + private ICrypto $crypto; |
|
| 95 | + |
|
| 96 | + |
|
| 97 | + public function __construct( |
|
| 98 | + ?ICrypto $crypto = null, |
|
| 99 | + ) { |
|
| 100 | + if ($crypto === null) { |
|
| 101 | + $crypto = Server::get(ICrypto::class); |
|
| 102 | + } |
|
| 103 | + $this->crypto = $crypto; |
|
| 104 | + $this->addType('appId', 'string'); |
|
| 105 | + $this->addType('userId', 'string'); |
|
| 106 | + $this->addType('httpMethod', 'string'); |
|
| 107 | + $this->addType('uri', 'string'); |
|
| 108 | + $this->addType('event', 'string'); |
|
| 109 | + $this->addType('eventFilter', 'json'); |
|
| 110 | + $this->addType('userIdFilter', 'string'); |
|
| 111 | + $this->addType('headers', 'json'); |
|
| 112 | + $this->addType('authMethod', 'string'); |
|
| 113 | + $this->addType('authData', 'string'); |
|
| 114 | + $this->addType('tokenNeeded', 'json'); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + public function getAuthMethodEnum(): AuthMethod { |
|
| 118 | + return AuthMethod::from($this->getAuthMethod()); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + public function getAuthDataClear(): array { |
|
| 122 | + $authData = $this->getAuthData(); |
|
| 123 | + if ($authData === null) { |
|
| 124 | + return []; |
|
| 125 | + } |
|
| 126 | + return json_decode($this->crypto->decrypt($authData), associative:true, flags:JSON_THROW_ON_ERROR); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + public function setAuthDataClear( |
|
| 130 | + #[\SensitiveParameter] |
|
| 131 | + ?array $data, |
|
| 132 | + ): void { |
|
| 133 | + if ($data === null) { |
|
| 134 | + if ($this->getAuthMethodEnum() === AuthMethod::Header) { |
|
| 135 | + throw new \UnexpectedValueException('Header auth method needs an associative array of headers as auth data'); |
|
| 136 | + } |
|
| 137 | + $this->setAuthData(null); |
|
| 138 | + return; |
|
| 139 | + } |
|
| 140 | + $this->setAuthData($this->crypto->encrypt(json_encode($data))); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + public function jsonSerialize(): array { |
|
| 144 | + $fields = array_keys($this->getFieldTypes()); |
|
| 145 | + return array_combine( |
|
| 146 | + $fields, |
|
| 147 | + array_map( |
|
| 148 | + fn ($field) => $this->getter($field), |
|
| 149 | + $fields |
|
| 150 | + ) |
|
| 151 | + ); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + public function getAppId(): ?string { |
|
| 155 | + return $this->appId; |
|
| 156 | + } |
|
| 157 | 157 | } |
@@ -18,37 +18,37 @@ |
||
| 18 | 18 | * @psalm-suppress PropertyNotSetInConstructor |
| 19 | 19 | */ |
| 20 | 20 | class EphemeralToken extends Entity implements \JsonSerializable { |
| 21 | - /** |
|
| 22 | - * @var int id of the token in the oc_authtoken db table |
|
| 23 | - */ |
|
| 24 | - protected $tokenId; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var ?string id of the user wich the token belongs to |
|
| 28 | - * @psalm-suppress PropertyNotSetInConstructor |
|
| 29 | - */ |
|
| 30 | - protected $userId = null; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @var int token creation timestamp |
|
| 34 | - * @psalm-suppress PropertyNotSetInConstructor |
|
| 35 | - */ |
|
| 36 | - protected $createdAt; |
|
| 37 | - |
|
| 38 | - public function __construct() { |
|
| 39 | - $this->addType('tokenId', 'integer'); |
|
| 40 | - $this->addType('userId', 'string'); |
|
| 41 | - $this->addType('createdAt', 'integer'); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - public function jsonSerialize(): array { |
|
| 45 | - $fields = array_keys($this->getFieldTypes()); |
|
| 46 | - return array_combine( |
|
| 47 | - $fields, |
|
| 48 | - array_map( |
|
| 49 | - fn ($field) => $this->getter($field), |
|
| 50 | - $fields |
|
| 51 | - ) |
|
| 52 | - ); |
|
| 53 | - } |
|
| 21 | + /** |
|
| 22 | + * @var int id of the token in the oc_authtoken db table |
|
| 23 | + */ |
|
| 24 | + protected $tokenId; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var ?string id of the user wich the token belongs to |
|
| 28 | + * @psalm-suppress PropertyNotSetInConstructor |
|
| 29 | + */ |
|
| 30 | + protected $userId = null; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @var int token creation timestamp |
|
| 34 | + * @psalm-suppress PropertyNotSetInConstructor |
|
| 35 | + */ |
|
| 36 | + protected $createdAt; |
|
| 37 | + |
|
| 38 | + public function __construct() { |
|
| 39 | + $this->addType('tokenId', 'integer'); |
|
| 40 | + $this->addType('userId', 'string'); |
|
| 41 | + $this->addType('createdAt', 'integer'); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + public function jsonSerialize(): array { |
|
| 45 | + $fields = array_keys($this->getFieldTypes()); |
|
| 46 | + return array_combine( |
|
| 47 | + $fields, |
|
| 48 | + array_map( |
|
| 49 | + fn ($field) => $this->getter($field), |
|
| 50 | + $fields |
|
| 51 | + ) |
|
| 52 | + ); |
|
| 53 | + } |
|
| 54 | 54 | } |
@@ -25,76 +25,76 @@ |
||
| 25 | 25 | use RuntimeException; |
| 26 | 26 | |
| 27 | 27 | class WebhookCall extends QueuedJob { |
| 28 | - public function __construct( |
|
| 29 | - private IClientService $clientService, |
|
| 30 | - private ICertificateManager $certificateManager, |
|
| 31 | - private WebhookListenerMapper $mapper, |
|
| 32 | - private LoggerInterface $logger, |
|
| 33 | - private IAppManager $appManager, |
|
| 34 | - private TokenService $tokenService, |
|
| 35 | - ITimeFactory $timeFactory, |
|
| 36 | - ) { |
|
| 37 | - parent::__construct($timeFactory); |
|
| 38 | - } |
|
| 28 | + public function __construct( |
|
| 29 | + private IClientService $clientService, |
|
| 30 | + private ICertificateManager $certificateManager, |
|
| 31 | + private WebhookListenerMapper $mapper, |
|
| 32 | + private LoggerInterface $logger, |
|
| 33 | + private IAppManager $appManager, |
|
| 34 | + private TokenService $tokenService, |
|
| 35 | + ITimeFactory $timeFactory, |
|
| 36 | + ) { |
|
| 37 | + parent::__construct($timeFactory); |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @param array $argument |
|
| 42 | - */ |
|
| 43 | - protected function run($argument): void { |
|
| 44 | - [$data, $webhookId] = $argument; |
|
| 45 | - $webhookListener = $this->mapper->getById($webhookId); |
|
| 46 | - $client = $this->clientService->newClient(); |
|
| 40 | + /** |
|
| 41 | + * @param array $argument |
|
| 42 | + */ |
|
| 43 | + protected function run($argument): void { |
|
| 44 | + [$data, $webhookId] = $argument; |
|
| 45 | + $webhookListener = $this->mapper->getById($webhookId); |
|
| 46 | + $client = $this->clientService->newClient(); |
|
| 47 | 47 | |
| 48 | - // adding Ephemeral auth tokens to the call |
|
| 49 | - $data['tokens'] = $this->tokenService->getTokens($webhookListener, $data['user']['uid'] ?? null); |
|
| 50 | - $options = [ |
|
| 51 | - 'verify' => $this->certificateManager->getAbsoluteBundlePath(), |
|
| 52 | - 'headers' => $webhookListener->getHeaders() ?? [], |
|
| 53 | - 'body' => json_encode($data), |
|
| 54 | - ]; |
|
| 55 | - try { |
|
| 56 | - switch ($webhookListener->getAuthMethodEnum()) { |
|
| 57 | - case AuthMethod::None: |
|
| 58 | - break; |
|
| 59 | - case AuthMethod::Header: |
|
| 60 | - $authHeaders = $webhookListener->getAuthDataClear(); |
|
| 61 | - $options['headers'] = array_merge($options['headers'], $authHeaders); |
|
| 62 | - break; |
|
| 63 | - } |
|
| 64 | - $webhookUri = $webhookListener->getUri(); |
|
| 65 | - $exAppId = $webhookListener->getAppId(); |
|
| 66 | - if ($exAppId !== null && str_starts_with($webhookUri, '/')) { |
|
| 67 | - // ExApp is awaiting a direct request to itself using AppAPI |
|
| 68 | - if (!$this->appManager->isEnabledForAnyone('app_api')) { |
|
| 69 | - throw new RuntimeException('AppAPI is disabled or not installed.'); |
|
| 70 | - } |
|
| 71 | - try { |
|
| 72 | - $appApiFunctions = Server::get(PublicFunctions::class); |
|
| 73 | - } catch (ContainerExceptionInterface|NotFoundExceptionInterface) { |
|
| 74 | - throw new RuntimeException('Could not get AppAPI public functions.'); |
|
| 75 | - } |
|
| 76 | - $exApp = $appApiFunctions->getExApp($exAppId); |
|
| 77 | - if ($exApp === null) { |
|
| 78 | - throw new RuntimeException('ExApp ' . $exAppId . ' is missing.'); |
|
| 79 | - } elseif (!$exApp['enabled']) { |
|
| 80 | - throw new RuntimeException('ExApp ' . $exAppId . ' is disabled.'); |
|
| 81 | - } |
|
| 82 | - $userId = ($data['user'] ?? [])['uid'] ?? null; |
|
| 83 | - $response = $appApiFunctions->exAppRequest($exAppId, $webhookUri, $userId, $webhookListener->getHttpMethod(), [], $options); |
|
| 84 | - if (is_array($response) && isset($response['error'])) { |
|
| 85 | - throw new RuntimeException(sprintf('Error during request to ExApp(%s): %s', $exAppId, $response['error'])); |
|
| 86 | - } |
|
| 87 | - } else { |
|
| 88 | - $response = $client->request($webhookListener->getHttpMethod(), $webhookUri, $options); |
|
| 89 | - } |
|
| 90 | - $statusCode = $response->getStatusCode(); |
|
| 91 | - if ($statusCode >= 200 && $statusCode < 300) { |
|
| 92 | - $this->logger->debug('Webhook returned status code ' . $statusCode, ['body' => $response->getBody()]); |
|
| 93 | - } else { |
|
| 94 | - $this->logger->warning('Webhook(' . $webhookId . ') returned unexpected status code ' . $statusCode, ['body' => $response->getBody()]); |
|
| 95 | - } |
|
| 96 | - } catch (\Exception $e) { |
|
| 97 | - $this->logger->error('Webhook(' . $webhookId . ') call failed: ' . $e->getMessage(), ['exception' => $e]); |
|
| 98 | - } |
|
| 99 | - } |
|
| 48 | + // adding Ephemeral auth tokens to the call |
|
| 49 | + $data['tokens'] = $this->tokenService->getTokens($webhookListener, $data['user']['uid'] ?? null); |
|
| 50 | + $options = [ |
|
| 51 | + 'verify' => $this->certificateManager->getAbsoluteBundlePath(), |
|
| 52 | + 'headers' => $webhookListener->getHeaders() ?? [], |
|
| 53 | + 'body' => json_encode($data), |
|
| 54 | + ]; |
|
| 55 | + try { |
|
| 56 | + switch ($webhookListener->getAuthMethodEnum()) { |
|
| 57 | + case AuthMethod::None: |
|
| 58 | + break; |
|
| 59 | + case AuthMethod::Header: |
|
| 60 | + $authHeaders = $webhookListener->getAuthDataClear(); |
|
| 61 | + $options['headers'] = array_merge($options['headers'], $authHeaders); |
|
| 62 | + break; |
|
| 63 | + } |
|
| 64 | + $webhookUri = $webhookListener->getUri(); |
|
| 65 | + $exAppId = $webhookListener->getAppId(); |
|
| 66 | + if ($exAppId !== null && str_starts_with($webhookUri, '/')) { |
|
| 67 | + // ExApp is awaiting a direct request to itself using AppAPI |
|
| 68 | + if (!$this->appManager->isEnabledForAnyone('app_api')) { |
|
| 69 | + throw new RuntimeException('AppAPI is disabled or not installed.'); |
|
| 70 | + } |
|
| 71 | + try { |
|
| 72 | + $appApiFunctions = Server::get(PublicFunctions::class); |
|
| 73 | + } catch (ContainerExceptionInterface|NotFoundExceptionInterface) { |
|
| 74 | + throw new RuntimeException('Could not get AppAPI public functions.'); |
|
| 75 | + } |
|
| 76 | + $exApp = $appApiFunctions->getExApp($exAppId); |
|
| 77 | + if ($exApp === null) { |
|
| 78 | + throw new RuntimeException('ExApp ' . $exAppId . ' is missing.'); |
|
| 79 | + } elseif (!$exApp['enabled']) { |
|
| 80 | + throw new RuntimeException('ExApp ' . $exAppId . ' is disabled.'); |
|
| 81 | + } |
|
| 82 | + $userId = ($data['user'] ?? [])['uid'] ?? null; |
|
| 83 | + $response = $appApiFunctions->exAppRequest($exAppId, $webhookUri, $userId, $webhookListener->getHttpMethod(), [], $options); |
|
| 84 | + if (is_array($response) && isset($response['error'])) { |
|
| 85 | + throw new RuntimeException(sprintf('Error during request to ExApp(%s): %s', $exAppId, $response['error'])); |
|
| 86 | + } |
|
| 87 | + } else { |
|
| 88 | + $response = $client->request($webhookListener->getHttpMethod(), $webhookUri, $options); |
|
| 89 | + } |
|
| 90 | + $statusCode = $response->getStatusCode(); |
|
| 91 | + if ($statusCode >= 200 && $statusCode < 300) { |
|
| 92 | + $this->logger->debug('Webhook returned status code ' . $statusCode, ['body' => $response->getBody()]); |
|
| 93 | + } else { |
|
| 94 | + $this->logger->warning('Webhook(' . $webhookId . ') returned unexpected status code ' . $statusCode, ['body' => $response->getBody()]); |
|
| 95 | + } |
|
| 96 | + } catch (\Exception $e) { |
|
| 97 | + $this->logger->error('Webhook(' . $webhookId . ') call failed: ' . $e->getMessage(), ['exception' => $e]); |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | 100 | } |