‪TYPO3CMS  ‪main
ReactionResolver.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\ResponseFactoryInterface;
21 use Psr\Http\Message\ResponseInterface;
22 use Psr\Http\Message\ServerRequestInterface;
23 use Psr\Http\Message\StreamFactoryInterface;
24 use Psr\Http\Server\MiddlewareInterface;
25 use Psr\Http\Server\RequestHandlerInterface;
26 use Psr\Log\LoggerInterface;
27 use Symfony\Component\Uid\Uuid;
34 
41 class ‪ReactionResolver implements MiddlewareInterface
42 {
43  public function ‪__construct(
44  private readonly LoggerInterface $logger,
45  private readonly ‪ReactionHandler $reactionHandler,
46  private readonly ‪ReactionRepository $reactionRepository,
47  private readonly ResponseFactoryInterface $responseFactory,
48  private readonly StreamFactoryInterface $streamFactory,
49  ) {}
50 
51  public function ‪process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
52  {
53  // 1. We only listen to the "reaction" endpoint
55  $routeResult = $request->getAttribute('routing');
56  if (!($routeResult instanceof ‪RouteResult) || $routeResult->getRouteName() !== 'reaction') {
57  return $handler->handle($request);
58  }
59 
60  // 2. Security check
61  $reactionIdentifier = (string)($routeResult->getArguments()['reactionIdentifier'] ?? '');
62  $secretKey = $this->‪resolveReactionSecret($request);
63  if ($secretKey === '' || !Uuid::isValid($reactionIdentifier)) {
64  return $this->‪getFailureResponse('Invalid information', $request);
65  }
66 
67  $reaction = $this->reactionRepository->getReactionRecordByIdentifier($reactionIdentifier);
68  if ($reaction === null) {
69  return $this->‪getFailureResponse('No reaction found for given identifier', $request, 404);
70  }
71 
72  if (!$reaction->isSecretValid($secretKey)) {
73  return $this->‪getFailureResponse('Invalid secret given', $request, 401);
74  }
75 
76  // 3. Handle reaction user authentication
77  $user = GeneralUtility::makeInstance(ReactionUserAuthentication::class);
78  $user->setReactionInstruction($reaction);
79  $user->start($request);
80 
81  // 4. Handle reaction
82  try {
83  return $this->reactionHandler->handleReaction($request, $reaction, $user);
84  } catch (‪ReactionNotFoundException $e) {
85  return $this->‪getFailureResponse($e->getMessage(), $request, 404);
86  }
87  }
88 
89  protected function ‪resolveReactionSecret(ServerRequestInterface $request): string
90  {
91  return $request->getHeaderLine('x-api-key');
92  }
93 
94  protected function ‪getFailureResponse(
95  string $errorMessage,
96  ServerRequestInterface $request,
97  int ‪$statusCode = 400
98  ): ResponseInterface {
99  $this->logger->warning($errorMessage, ['request' => $request]);
100 
101  return $this->responseFactory
102  ->createResponse(‪$statusCode)
103  ->withHeader('Content-Type', 'application/json')
104  ->withBody(
105  $this->streamFactory->createStream((string)json_encode(['success' => false, 'error' => $errorMessage]))
106  );
107  }
108 }
‪TYPO3\CMS\Reactions\Http\ReactionHandler
Definition: ReactionHandler.php:40
‪TYPO3\CMS\Reactions\Repository\ReactionRepository
Definition: ReactionRepository.php:36
‪TYPO3\CMS\Reactions\Http\Middleware
Definition: ReactionResolver.php:18
‪TYPO3\CMS\Backend\Routing\RouteResult
Definition: RouteResult.php:27
‪TYPO3\CMS\Reactions\Authentication\ReactionUserAuthentication
Definition: ReactionUserAuthentication.php:31
‪TYPO3\CMS\Reactions\Http\Middleware\ReactionResolver
Definition: ReactionResolver.php:42
‪TYPO3\CMS\Reactions\Exception\ReactionNotFoundException
Definition: ReactionNotFoundException.php:20
‪TYPO3\CMS\Redirects\Message\$statusCode
‪identifier readonly UriInterface readonly int $statusCode
Definition: RedirectWasHitMessage.php:34
‪TYPO3\CMS\Reactions\Http\Middleware\ReactionResolver\getFailureResponse
‪getFailureResponse(string $errorMessage, ServerRequestInterface $request, int $statusCode=400)
Definition: ReactionResolver.php:94
‪TYPO3\CMS\Reactions\Http\Middleware\ReactionResolver\process
‪process(ServerRequestInterface $request, RequestHandlerInterface $handler)
Definition: ReactionResolver.php:51
‪TYPO3\CMS\Reactions\Http\Middleware\ReactionResolver\resolveReactionSecret
‪resolveReactionSecret(ServerRequestInterface $request)
Definition: ReactionResolver.php:89
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Reactions\Http\Middleware\ReactionResolver\__construct
‪__construct(private readonly LoggerInterface $logger, private readonly ReactionHandler $reactionHandler, private readonly ReactionRepository $reactionRepository, private readonly ResponseFactoryInterface $responseFactory, private readonly StreamFactoryInterface $streamFactory,)
Definition: ReactionResolver.php:43