feat(altcha): add altcha validation layer to login
Some checks are pending
Cadoles/hydra-sql/pipeline/pr-develop Build started...
Some checks are pending
Cadoles/hydra-sql/pipeline/pr-develop Build started...
This commit is contained in:
52
src/Altcha/AltchaValidator.php
Normal file
52
src/Altcha/AltchaValidator.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Altcha;
|
||||
|
||||
use Symfony\Component\Form\FormError;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class AltchaValidator
|
||||
{
|
||||
public function __construct(
|
||||
private readonly string $altchaHost,
|
||||
private readonly string $altchaBaseUrl,
|
||||
private readonly HttpClientInterface $httpClient,
|
||||
private readonly TranslatorInterface $translator
|
||||
) {
|
||||
}
|
||||
|
||||
public function validate(FormEvent $formEvent): void
|
||||
{
|
||||
$form = $formEvent->getForm();
|
||||
$data = $form->getData();
|
||||
|
||||
$response = $this->httpClient->request(
|
||||
'POST',
|
||||
$this->altchaHost.$this->altchaBaseUrl.'/verify',
|
||||
[
|
||||
'body' => json_encode($data),
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json',
|
||||
],
|
||||
],
|
||||
);
|
||||
|
||||
if (Response::HTTP_OK !== $response->getStatusCode()) {
|
||||
$form->addError(new FormError($this->translator->trans('altcha.validator.server_validation_error', [], 'form')));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$content = $response->getContent();
|
||||
$parsedResponse = json_decode($content);
|
||||
|
||||
if (true !== $parsedResponse->success) {
|
||||
$form->addError(new FormError($this->translator->trans('altcha.validator.server_validation_error', [], 'form')));
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user