Skip to content

Commit

Permalink
#275 / #308 Add a configuration to recaptcha V3 score
Browse files Browse the repository at this point in the history
  • Loading branch information
nenes25 committed Feb 24, 2025
1 parent 16b9e0e commit b48b6ff
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
6 changes: 4 additions & 2 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/**
* Module Captcha
* Add (re)captcha on contact and account creation forms
* © Hervé Hennes 2013-2024
* © Hervé Hennes 2013-2025
* https://github.com/nenes25/eicaptcha
* https://www.h-hennes.fr/blog/
*/
- V 2.5.1 - 2024- #288 Fix php7.0 error

- V 2.6.0 - 2025-02-24 #288 Fix php7.0 error
#275 / #308 Configure reCaptcha V3 min score
- V 2.5.0 - 2024-02-15 #229 Replace override of Authcontroller
#247 Registration check on prestashop 8.x
#221 recaptcha v3 doesn't validate in form added with elementor form builder of warehouse theme
Expand Down
18 changes: 17 additions & 1 deletion eicaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct()
$this->author = 'hhennes';
$this->name = 'eicaptcha';
$this->tab = 'front_office_features';
$this->version = '2.5.0';
$this->version = '2.6.0';
$this->need_instance = 1;

$this->bootstrap = true;
Expand Down Expand Up @@ -447,13 +447,18 @@ protected function _validateCaptcha()
}

$context = Context::getContext();
$captchaVersion = Configuration::get('CAPTCHA_VERSION');
$captchaV3MinScore = Configuration::get('CAPTCHA_V3_MINIMAL_SCORE');
//Fix issue if allow_url_open is set to 0
if (function_exists('ini_get') && !ini_get('allow_url_fopen')) {
$recaptchaMethod = new \ReCaptcha\RequestMethod\CurlPost();
} else {
$recaptchaMethod = null;
}
$captcha = new ReCaptcha(Configuration::get('CAPTCHA_PRIVATE_KEY'), $recaptchaMethod);
if ($captchaVersion == 3) {
$captcha->setScoreThreshold($captchaV3MinScore);
}
$result = $captcha->verify(
Tools::getValue('g-recaptcha-response'),
Tools::getRemoteAddr()
Expand All @@ -463,6 +468,17 @@ protected function _validateCaptcha()
$errorMessage = $this->l('Please validate the captcha field before submitting your request');
$this->debugger->log($errorMessage);
$this->debugger->log(sprintf($this->l('Recaptcha response %s'), print_r($result->getErrorCodes(), true)));
if ($captchaVersion == 3) {
if ($result->getScore() < $captchaV3MinScore) {
$errorMessageV3 =
sprintf(
$this->l('Your request has been blocked by the captcha system, due to a low score of %s , required score is %s'),
$result->getScore(),
$captchaV3MinScore
);
$this->debugger->log($errorMessageV3);
}
}
$context->controller->errors[] = $errorMessage;

return false;
Expand Down
14 changes: 14 additions & 0 deletions src/ConfigForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ public function renderForm()
],
'tab' => 'general',
],
[
'type' => 'text',
'label' => $this->l('Captcha V3 mininum score'),
'hint' => sprintf(
$this->l('The minimum score required to validate the captcha is a note between 0 and 1. Default is 0.5 , you can read more about it here: %s'),
'<a href="https://developers.google.com/recaptcha/docs/v3?#interpreting_the_score" target="_blank">https://developers.google.com/recaptcha/docs/v3?#interpreting_the_score</a>'
),
'name' => 'CAPTCHA_V3_MINIMAL_SCORE',
'required' => true,
'empty_message' => $this->l('Please fill the captcha v3 minimal score.'),
'tab' => 'general',
],
[
'type' => 'text',
'label' => $this->l('Captcha public key (Site key)'),
Expand Down Expand Up @@ -352,6 +364,7 @@ public function postProcess()
{
if (Tools::isSubmit('SubmitCaptchaConfiguration')) {
Configuration::updateValue('CAPTCHA_VERSION', Tools::getValue('CAPTCHA_VERSION'));
Configuration::updateValue('CAPTCHA_V3_MINIMAL_SCORE', Tools::getValue('CAPTCHA_V3_MINIMAL_SCORE'));
Configuration::updateValue('CAPTCHA_PUBLIC_KEY', Tools::getValue('CAPTCHA_PUBLIC_KEY'));
Configuration::updateValue('CAPTCHA_PRIVATE_KEY', Tools::getValue('CAPTCHA_PRIVATE_KEY'));
Configuration::updateValue('CAPTCHA_ENABLE_LOGGED_CUSTOMERS', Tools::getValue('CAPTCHA_ENABLE_LOGGED_CUSTOMERS'));
Expand All @@ -377,6 +390,7 @@ public function getConfigFieldsValues()
{
return [
'CAPTCHA_VERSION' => Tools::getValue('CAPTCHA_VERSION', Configuration::get('CAPTCHA_VERSION')),
'CAPTCHA_V3_MINIMAL_SCORE' => Tools::getValue('CAPTCHA_V3_MINIMAL_SCORE', Configuration::get('CAPTCHA_V3_MINIMAL_SCORE')),
'CAPTCHA_PRIVATE_KEY' => Tools::getValue('CAPTCHA_PRIVATE_KEY', Configuration::get('CAPTCHA_PRIVATE_KEY')),
'CAPTCHA_PUBLIC_KEY' => Tools::getValue('CAPTCHA_PUBLIC_KEY', Configuration::get('CAPTCHA_PUBLIC_KEY')),
'CAPTCHA_ENABLE_LOGGED_CUSTOMERS' => Tools::getValue('CAPTCHA_ENABLE_LOGGED_CUSTOMERS', Configuration::get('CAPTCHA_ENABLE_LOGGED_CUSTOMERS')),
Expand Down
4 changes: 4 additions & 0 deletions translations/fr.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
$_MODULE['<{eicaptcha}prestashop>eicaptcha_793b58515cefe26f6a3c5ab782460a69'] = 'Le module Eicatpcha doit être configuré';
$_MODULE['<{eicaptcha}prestashop>eicaptcha_632ee8e447c5c09ca7577f9281cbb999'] = 'Merci de valider le captcha';
$_MODULE['<{eicaptcha}prestashop>eicaptcha_9ed81dbbae814f9a81cd15927ebd4795'] = 'Réponse recaptcha %s';
$_MODULE['<{eicaptcha}prestashop>eicaptcha_5113573bdec931c88c64ccdd4e297026'] = 'Votre requête a été bloquée par le captcha v3 avec un score de %s, score requis de %s';
$_MODULE['<{eicaptcha}prestashop>eicaptcha_e8a0559d823da2ac04358b8bf1a1dbd1'] = 'Captcha soumis avec succès';
$_MODULE['<{eicaptcha}prestashop>debugger_01447f1863a21f8171b5894cc3e535ac'] = 'Ce module nécessite composer pour fonctionner, merci de vous rendre dans le dossier %s, et de lancer la commande \"composer install\" ou de télécharger la dernière release sur github';
$_MODULE['<{eicaptcha}prestashop>debugger_35d70791f9c315b2bd646de3464b1686'] = 'Le module n\'est pas compatible avec votre version de Prestashop';
Expand Down Expand Up @@ -38,6 +39,9 @@
$_MODULE['<{eicaptcha}prestashop>configform_c25456954fac1e3669128249766ef488'] = 'Version de Recaptcha';
$_MODULE['<{eicaptcha}prestashop>configform_ec765f1adc3b4253f2d3b131a4a8618f'] = 'V2';
$_MODULE['<{eicaptcha}prestashop>configform_d5b50b8cf96bcc8aba90f306f5e6189c'] = 'V3';
$_MODULE['<{eicaptcha}prestashop>configform_0575fd3dd9df1c1eb623d805420c4ae1'] = 'Score minimum pour le captcha V3';
$_MODULE['<{eicaptcha}prestashop>configform_37071d2ea59963609c35f1104d8b9e35'] = 'La note minimum pour valider le captcha est une note entre 0 et 1. Par défaut la valeur de de 0.5. Vous pouvez trouver des informations sur le sujet ici : %s';
$_MODULE['<{eicaptcha}prestashop>configform_9901fa09c200314688f085cc5e358fdd'] = 'Merci de saisir le score minimum pour le captcha v3';
$_MODULE['<{eicaptcha}prestashop>configform_2d6759f60f165404e274683029df8e5b'] = 'Clé de site';
$_MODULE['<{eicaptcha}prestashop>configform_de7e39ae12eb7d5e08c84ff8739ee5fc'] = 'Merci de saisir la clé de site';
$_MODULE['<{eicaptcha}prestashop>configform_532719b9b22c0444d8c65bdc6b80fa1e'] = 'Clé secrète';
Expand Down
33 changes: 33 additions & 0 deletions upgrade/upgrade-2.6.0.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file docs/licenses/LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author Hervé HENNES <[email protected]> and contributors / https://github.com/nenes25/eicaptcha
* @copyright since 2013 Hervé HENNES
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License ("AFL") v. 3.0
*/
if (!defined('_PS_VERSION_')) {
exit;
}

/**
* Upgrade module 2_6_0 Add a new configuration to allow to define the recaptcha v3 minimal score
*
* @param Module $module
*
* @return bool
*/
function upgrade_module_2_6_0($module)
{
Configuration::updateValue('CAPTCHA_V3_MINIMAL_SCORE', '0.5');

return true;
}

0 comments on commit b48b6ff

Please sign in to comment.