ChallengeProcessor.php 979 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\State\Processor;
  4. use AltchaOrg\Altcha\Altcha;
  5. use ApiPlatform\Metadata\Operation;
  6. use ApiPlatform\Metadata\Post;
  7. use ApiPlatform\State\ProcessorInterface;
  8. use App\ApiResource\Challenge;
  9. use Symfony\Component\HttpFoundation\Response;
  10. class ChallengeProcessor implements ProcessorInterface
  11. {
  12. public function __construct(
  13. private readonly string $hmacKey
  14. ) {}
  15. public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): Challenge
  16. {
  17. if (!$operation instanceof Post) {
  18. throw new \RuntimeException('not supported', Response::HTTP_METHOD_NOT_ALLOWED);
  19. }
  20. /** @var Challenge $challenge */
  21. $challenge = $data;
  22. $valid = Altcha::verifySolution(
  23. $challenge->getPayload(),
  24. $this->hmacKey,
  25. true
  26. );
  27. $challenge->setVerified($valid);
  28. return $challenge;
  29. }
  30. }