-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add friendly captcha form type [up-port] * p11 adjustments
- Loading branch information
Showing
24 changed files
with
433 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
form_builder: | ||
types: | ||
friendly_captcha: | ||
class: FormBuilderBundle\Form\Type\FriendlyCaptchaType | ||
backend: | ||
form_type_group: security_fields | ||
label: 'form_builder_type.friendly_captcha' | ||
icon_class: 'form_builder_icon_friendly_captcha' | ||
constraints: false | ||
fields: | ||
optional.email_label: ~ | ||
options.help_text: ~ | ||
options.data: ~ | ||
options.value: ~ | ||
options.start: | ||
display_group_id: attributes | ||
type: select | ||
label: 'form_builder_type_field.friendly_captcha.start' | ||
config: | ||
options: | ||
- ['Focus', 'focus'] | ||
- ['Auto', 'auto'] | ||
- ['None', 'none'] | ||
options.darkmode: | ||
display_group_id: attributes | ||
type: checkbox | ||
label: 'form_builder_type_field.friendly_captcha.darkmode' | ||
config: | ||
default_value: null | ||
options.callback: | ||
display_group_id: attributes | ||
type: textfield | ||
label: 'form_builder_type_field.friendly_captcha.callback' | ||
config: | ||
default_value: null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace FormBuilderBundle\Form\Type; | ||
|
||
use FormBuilderBundle\Configuration\Configuration; | ||
use FormBuilderBundle\Validator\Constraints\FriendlyCaptcha; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\HiddenType; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\Form\FormView; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
final class FriendlyCaptchaType extends AbstractType | ||
{ | ||
protected Configuration $configuration; | ||
protected RequestStack $requestStack; | ||
|
||
public function __construct(RequestStack $requestStack, Configuration $configuration) | ||
{ | ||
$this->requestStack = $requestStack; | ||
$this->configuration = $configuration; | ||
} | ||
|
||
public function buildView(FormView $view, FormInterface $form, array $options): void | ||
{ | ||
$config = $this->configuration->getConfig('spam_protection'); | ||
$friendlyCaptchaConfig = $config['friendly_captcha']; | ||
|
||
$locale = $options['lang'] ?? null; | ||
if ($locale === null) { | ||
$locale = $this->requestStack->getCurrentRequest()?->getLocale() ?? 'en'; | ||
} | ||
|
||
$friendlyCaptchaDataAttributes = array_filter([ | ||
'sitekey' => $friendlyCaptchaConfig['site_key'], | ||
'lang' => str_contains($locale, '_') ? explode('_', $locale)[0] : $locale, | ||
'start' => $options['start'] ?? 'focus', | ||
'callback' => $options['callback'] ?? null, | ||
'puzzle-endpoint' => $friendlyCaptchaConfig['eu_only'] === true | ||
? $friendlyCaptchaConfig['puzzle']['eu_endpoint'] | ||
: $friendlyCaptchaConfig['puzzle']['global_endpoint'], | ||
]); | ||
|
||
$view->vars['friendly_captcha_attributes'] = $friendlyCaptchaDataAttributes; | ||
$view->vars['darkmode'] = $options['darkmode'] ?? false; | ||
} | ||
|
||
public function getParent(): string | ||
{ | ||
return HiddenType::class; | ||
} | ||
|
||
public function getBlockPrefix(): string | ||
{ | ||
return 'form_builder_friendly_captcha_type'; | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$resolver->setDefaults([ | ||
'lang' => null, | ||
'start' => 'focus', | ||
'callback' => null, | ||
'mapped' => false, | ||
'darkmode' => false, | ||
'constraints' => [new FriendlyCaptcha()], | ||
]); | ||
|
||
$resolver->setAllowedValues('start', ['auto', 'focus', 'none']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FormBuilderBundle\Migrations; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\Migrations\AbstractMigration; | ||
use FormBuilderBundle\Tool\Install; | ||
use Symfony\Component\DependencyInjection\ContainerAwareInterface; | ||
use Symfony\Component\DependencyInjection\ContainerAwareTrait; | ||
|
||
final class Version20240628143429 extends AbstractMigration implements ContainerAwareInterface | ||
{ | ||
use ContainerAwareTrait; | ||
|
||
public function getDescription(): string | ||
{ | ||
return ''; | ||
} | ||
|
||
public function up(Schema $schema): void | ||
{ | ||
$installer = $this->container->get(Install::class); | ||
$installer->updateTranslations(); | ||
} | ||
|
||
public function down(Schema $schema): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace FormBuilderBundle\Tool\FriendlyCaptcha; | ||
|
||
class Response | ||
{ | ||
private bool $success; | ||
private ?string $details; | ||
private ?array $errors; | ||
|
||
public static function fromJson(string $json): Response | ||
{ | ||
$responseData = json_decode($json, true, 512, JSON_THROW_ON_ERROR); | ||
|
||
if (!$responseData) { | ||
return new self(false, 'invalid-json', null); | ||
} | ||
|
||
$success = $responseData['success'] ?? false; | ||
$details = $responseData['details'] ?? null; | ||
$errors = $responseData['errors'] ?? null; | ||
|
||
return new self($success, $details, $errors); | ||
} | ||
|
||
public function __construct( | ||
bool $success, | ||
?string $details, | ||
?array $errors | ||
) { | ||
$this->success = $success; | ||
$this->details = $details; | ||
$this->errors = $errors; | ||
} | ||
|
||
public function isSuccess(): bool | ||
{ | ||
return $this->success; | ||
} | ||
|
||
public function getDetails(): ?string | ||
{ | ||
return $this->details; | ||
} | ||
|
||
public function getErrors(): ?array | ||
{ | ||
return $this->errors; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return [ | ||
'success' => $this->isSuccess(), | ||
'details' => $this->getDetails(), | ||
'errors' => $this->getErrors(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace FormBuilderBundle\Tool; | ||
|
||
use FormBuilderBundle\Configuration\Configuration; | ||
use FormBuilderBundle\Tool\FriendlyCaptcha\Response; | ||
use GuzzleHttp\Client; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
class FriendlyCaptchaProcessor implements FriendlyCaptchaProcessorInterface | ||
{ | ||
|
||
public function __construct( | ||
protected Configuration $configuration, | ||
protected RequestStack $requestStack | ||
) { | ||
} | ||
|
||
public function verify(mixed $value): Response | ||
{ | ||
$client = new Client(); | ||
$config = $this->configuration->getConfig('spam_protection'); | ||
$friendlyCaptchaConfig = $config['friendly_captcha']; | ||
|
||
$verificationEndpoint = $friendlyCaptchaConfig['eu_only'] === true | ||
? $friendlyCaptchaConfig['verification']['eu_endpoint'] | ||
: $friendlyCaptchaConfig['verification']['global_endpoint']; | ||
|
||
$response = $client->post( | ||
$verificationEndpoint, | ||
[ | ||
'form_params' => [ | ||
'secret' => $friendlyCaptchaConfig['secret_key'], | ||
'sitekey' => $friendlyCaptchaConfig['site_key'], | ||
'solution' => $value, | ||
], | ||
] | ||
); | ||
|
||
return Response::fromJson($response->getBody()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace FormBuilderBundle\Tool; | ||
|
||
use FormBuilderBundle\Tool\FriendlyCaptcha\Response; | ||
|
||
interface FriendlyCaptchaProcessorInterface | ||
{ | ||
public function verify(mixed $value): Response; | ||
} |
Oops, something went wrong.