ChallengeProvider.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\State\Provider;
  4. use AltchaOrg\Altcha\Altcha;
  5. use AltchaOrg\Altcha\Challenge;
  6. use AltchaOrg\Altcha\ChallengeOptions;
  7. use ApiPlatform\Metadata\GetCollection;
  8. use ApiPlatform\State\ProviderInterface;
  9. use ApiPlatform\Metadata\Operation;
  10. use Symfony\Component\HttpFoundation\Response;
  11. class ChallengeProvider implements ProviderInterface
  12. {
  13. public function __construct(
  14. private readonly string $hmacKey
  15. ) {}
  16. /**
  17. * @param mixed[] $uriVariables
  18. * @param mixed[] $context
  19. */
  20. public function provide(Operation $operation, array $uriVariables = [], array $context = []): Challenge
  21. {
  22. if ($operation instanceof GetCollection) {
  23. throw new \RuntimeException('not supported', Response::HTTP_METHOD_NOT_ALLOWED);
  24. }
  25. $options = new ChallengeOptions([
  26. 'hmacKey' => $this->hmacKey,
  27. 'maxNumber' => 100000,
  28. 'expires' => (new \DateTime())->modify('+15 minute')
  29. ]);
  30. return Altcha::createChallenge($options);
  31. }
  32. }