feat(altcha): add altcha validation layer to login
Some checks are pending
Cadoles/hydra-sql/pipeline/pr-develop Build started...

This commit is contained in:
2025-03-24 17:20:17 +01:00
parent 1cb5ae6bc3
commit 12523398f6
51 changed files with 2417 additions and 1137 deletions

View File

@ -0,0 +1,39 @@
<?php
namespace App\Altcha\Form;
use Symfony\Component\Validator\Constraints as Assert;
class AltchaModel
{
/**
* @Assert\NotBlank()
* @Assert\Regex("/^(SHA-1|SHA-256|SHA-512)$/")
*/
public string $algorithm;
/**
* @Assert\NotBlank()
*/
public string $challenge;
/**
* @Assert\NotBlank()
*/
public string $salt;
/**
* @Assert\NotBlank()
*/
public int $number;
/**
* @Assert\NotBlank()
*/
public string $signature;
/**
* @Assert\NotBlank()
*/
public int $took;
}

View File

@ -0,0 +1,81 @@
<?php
namespace App\Altcha\Form;
use App\Altcha\AltchaTransformer;
use App\Altcha\AltchaValidator;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class AltchaType extends AbstractType
{
public function __construct(
private readonly string $altchaHost,
private readonly string $altchaBaseUrl,
private readonly string $altchaDebug,
private readonly string $altchaWorkers,
private readonly string $altchaDelay,
private readonly string $altchaMockError,
private readonly AltchaValidator $altchaValidator,
private readonly HttpClientInterface $httpClient,
private readonly TranslatorInterface $translator
) {
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->addModelTransformer(new AltchaTransformer());
$builder->addEventListener(FormEvents::POST_SUBMIT, [$this->altchaValidator, 'validate']);
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
$translations = [
'label' => $this->translator->trans('altcha.widget.label', [], 'form'),
'verified' => $this->translator->trans('altcha.widget.verified', [], 'form'),
'verifying' => $this->translator->trans('altcha.widget.verifying', [], 'form'),
'waitAlert' => $this->translator->trans('altcha.widget.waitalert', [], 'form'),
'error' => $this->translator->trans('altcha.widget.error', [], 'form'),
'expired' => $this->translator->trans('altcha.widget.expired', [], 'form'),
];
$view->vars['translations'] = json_encode($translations);
$view->vars['challengeJson'] = $this->requestChallenge();
$view->vars['debug'] = $this->altchaDebug;
$view->vars['workers'] = $this->altchaWorkers;
$view->vars['delay'] = $this->altchaDelay;
$view->vars['mockError'] = $this->altchaMockError;
}
private function requestChallenge(): string
{
$resp = $this->httpClient->request('GET', $this->altchaHost.$this->altchaBaseUrl.'/request');
if (Response::HTTP_OK === $resp->getStatusCode()) {
return $resp->getContent();
}
return '';
}
public function getParent(): string
{
return TextType::class;
}
public function getName(): string
{
return $this->getBlockPrefix();
}
public function getBlockPrefix(): string
{
return 'altcha';
}
}