‪TYPO3CMS  ‪main
WebhookMessageHandler.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Log\LoggerInterface;
22 use Symfony\Component\Messenger\Attribute\AsMessageHandler;
27 
32 #[AsMessageHandler]
34 {
35  private string ‪$algo = 'sha256';
36 
37  public function ‪__construct(
38  private readonly ‪WebhookRepository $repository,
39  private readonly ‪RequestFactory $requestFactory,
40  private readonly LoggerInterface $logger,
41  ) {}
42 
43  public function ‪__invoke(‪WebhookMessageInterface $message): void
44  {
45  $configuredWebhooks = $this->repository
46  ->setApplyDefaultRestrictions(true)
47  ->getConfiguredWebhooksByType(get_class($message));
48  foreach ($configuredWebhooks as $webhookInstruction) {
49  $this->logger->info('Sending webhook', [
50  'webhook-identifier' => $webhookInstruction->getIdentifier(),
51  ]);
52  try {
53  $response = $this->‪sendRequest($webhookInstruction, $message);
54  $this->logger->debug('Webhook sent', [
55  'target_url' => $webhookInstruction->getTargetUrl(),
56  'response_code' => $response->getStatusCode(),
57  ]);
58  } catch (\Exception $e) {
59  $this->logger->error('Webhook sending failed', [
60  'failure_message' => $e->getMessage(),
61  ]);
62  }
63  }
64  }
65 
66  protected function ‪sendRequest(‪WebhookInstruction $webhookInstruction, ‪WebhookMessageInterface $message): ResponseInterface
67  {
68  $body = json_encode($message, JSON_THROW_ON_ERROR);
69  $headers = $this->‪buildHeaders($webhookInstruction, $body);
70 
71  $options = [
72  'headers' => $headers,
73  'body' => $body,
74  ];
75  if (!$webhookInstruction->‪verifySSL()) {
76  $options['verify'] = false;
77  }
78 
79  return $this->requestFactory->request(
80  $webhookInstruction->‪getTargetUrl(),
81  $webhookInstruction->‪getHttpMethod(),
82  $options
83  );
84  }
85 
86  private function ‪buildHash(‪WebhookInstruction $webhookInstruction, string $body): string
87  {
88  return hash_hmac($this->algo, sprintf(
89  '%s:%s',
90  $webhookInstruction->‪getIdentifier(),
91  $body
92  ), $webhookInstruction->‪getSecret());
93  }
94 
95  private function ‪buildHeaders(‪WebhookInstruction $webhookInstruction, string $body): array
96  {
97  $headers = ‪$GLOBALS['TYPO3_CONF_VARS']['HTTP']['headers'] ?? [];
98  $headers['Content-Type'] = 'application/json';
99  $headers['Webhook-Signature-Algo'] = ‪$this->algo;
100  $headers = array_merge($headers, $webhookInstruction->‪getAdditionalHeaders());
101  $headers['Webhook-Signature'] = $this->‪buildHash($webhookInstruction, $body);
102  return $headers;
103  }
104 }
‪TYPO3\CMS\Webhooks\MessageHandler\WebhookMessageHandler\sendRequest
‪sendRequest(WebhookInstruction $webhookInstruction, WebhookMessageInterface $message)
Definition: WebhookMessageHandler.php:66
‪TYPO3\CMS\Webhooks\MessageHandler
Definition: WebhookMessageHandler.php:18
‪TYPO3\CMS\Webhooks\MessageHandler\WebhookMessageHandler\__construct
‪__construct(private readonly WebhookRepository $repository, private readonly RequestFactory $requestFactory, private readonly LoggerInterface $logger,)
Definition: WebhookMessageHandler.php:37
‪TYPO3\CMS\Webhooks\Model\WebhookInstruction\getSecret
‪getSecret()
Definition: WebhookInstruction.php:83
‪TYPO3\CMS\Webhooks\MessageHandler\WebhookMessageHandler\__invoke
‪__invoke(WebhookMessageInterface $message)
Definition: WebhookMessageHandler.php:43
‪TYPO3\CMS\Webhooks\Model\WebhookInstruction\verifySSL
‪verifySSL()
Definition: WebhookInstruction.php:78
‪TYPO3\CMS\Webhooks\Model\WebhookInstruction\getHttpMethod
‪getHttpMethod()
Definition: WebhookInstruction.php:73
‪TYPO3\CMS\Core\Messaging\WebhookMessageInterface
Definition: WebhookMessageInterface.php:28
‪TYPO3\CMS\Webhooks\MessageHandler\WebhookMessageHandler\buildHeaders
‪buildHeaders(WebhookInstruction $webhookInstruction, string $body)
Definition: WebhookMessageHandler.php:95
‪TYPO3\CMS\Webhooks\Model\WebhookInstruction\getTargetUrl
‪getTargetUrl()
Definition: WebhookInstruction.php:68
‪TYPO3\CMS\Webhooks\MessageHandler\WebhookMessageHandler
Definition: WebhookMessageHandler.php:34
‪TYPO3\CMS\Core\Http\RequestFactory
Definition: RequestFactory.php:30
‪TYPO3\CMS\Webhooks\Model\WebhookInstruction
Definition: WebhookInstruction.php:28
‪TYPO3\CMS\Webhooks\MessageHandler\WebhookMessageHandler\$algo
‪string $algo
Definition: WebhookMessageHandler.php:35
‪TYPO3\CMS\Webhooks\MessageHandler\WebhookMessageHandler\buildHash
‪buildHash(WebhookInstruction $webhookInstruction, string $body)
Definition: WebhookMessageHandler.php:86
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Webhooks\Repository\WebhookRepository
Definition: WebhookRepository.php:35
‪TYPO3\CMS\Webhooks\Model\WebhookInstruction\getAdditionalHeaders
‪getAdditionalHeaders()
Definition: WebhookInstruction.php:88
‪TYPO3\CMS\Webhooks\Model\WebhookInstruction\getIdentifier
‪getIdentifier()
Definition: WebhookInstruction.php:63