Adding reCaptcha to password reset request form.

This commit is contained in:
Alexey Skobkin 2020-01-20 20:44:27 +03:00
parent b2695ade8e
commit 2816915c7c
No known key found for this signature in database
GPG key ID: 5D5CEF6F221278E7
10 changed files with 181 additions and 2 deletions

12
.env
View file

@ -29,3 +29,15 @@ APP_LOCAL_PATH=/var/www/magnetico-web/current
MAILER_DSN=smtp://localhost MAILER_DSN=smtp://localhost
MAILER_FROM=no-reply@magnetico-web.tld MAILER_FROM=no-reply@magnetico-web.tld
###< symfony/mailer ### ###< symfony/mailer ###
###> google/recaptcha ###
# To use Google Recaptcha, you must register a site on Recaptcha's admin panel:
# https://www.google.com/recaptcha/admin
#GOOGLE_RECAPTCHA_SITE_KEY=
#GOOGLE_RECAPTCHA_SECRET=
###< google/recaptcha ###
###> excelwebzone/recaptcha-bundle ###
EWZ_RECAPTCHA_SITE_KEY=
EWZ_RECAPTCHA_SECRET=
###< excelwebzone/recaptcha-bundle ###

View file

@ -16,6 +16,7 @@
"ext-ctype": "*", "ext-ctype": "*",
"ext-hash": "*", "ext-hash": "*",
"ext-iconv": "*", "ext-iconv": "*",
"excelwebzone/recaptcha-bundle": "^1.5",
"sensio/framework-extra-bundle": "^5.1", "sensio/framework-extra-bundle": "^5.1",
"symfony/console": "^4.1", "symfony/console": "^4.1",
"symfony/dotenv": "^4.1", "symfony/dotenv": "^4.1",

103
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "d200c2a87d96fe3ba6edb03246159fa7", "content-hash": "104b6028a5f243620b6fb0cf58be3ae8",
"packages": [ "packages": [
{ {
"name": "doctrine/annotations", "name": "doctrine/annotations",
@ -1204,6 +1204,107 @@
], ],
"time": "2020-01-05T14:11:20+00:00" "time": "2020-01-05T14:11:20+00:00"
}, },
{
"name": "excelwebzone/recaptcha-bundle",
"version": "v1.5.14",
"source": {
"type": "git",
"url": "https://github.com/excelwebzone/EWZRecaptchaBundle.git",
"reference": "f526d3036616529a7c1be81e5ddea5af5b4fd331"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/excelwebzone/EWZRecaptchaBundle/zipball/f526d3036616529a7c1be81e5ddea5af5b4fd331",
"reference": "f526d3036616529a7c1be81e5ddea5af5b4fd331",
"shasum": ""
},
"require": {
"google/recaptcha": "^1.1",
"php": ">=5.6 || ^7.0",
"symfony/form": "^2.8 || ^3.0 || ^4.0",
"symfony/framework-bundle": "^2.8 || ^3.0 || ^4.0",
"symfony/security-bundle": "^2.8 || ^3.0 || ^4.0",
"symfony/validator": "^2.8 || ^3.0 || ^4.0"
},
"require-dev": {
"phpunit/phpunit": "^5 || ^6 || ^7"
},
"type": "symfony-bundle",
"extra": {
"symfony": {
"allow-contrib": "true"
}
},
"autoload": {
"psr-4": {
"EWZ\\Bundle\\RecaptchaBundle\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Michael H. Arieli",
"email": "excelwebzone@gmail.com",
"homepage": "http://excelwebzone.com/"
}
],
"description": "This bundle provides easy reCAPTCHA form field integration",
"homepage": "https://github.com/excelwebzone/EWZRecaptchaBundle",
"keywords": [
"recaptcha"
],
"time": "2019-07-05T18:47:50+00:00"
},
{
"name": "google/recaptcha",
"version": "1.2.3",
"source": {
"type": "git",
"url": "https://github.com/google/recaptcha.git",
"reference": "98c4a6573b27e8b0990ea8789c74ea378795134c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/google/recaptcha/zipball/98c4a6573b27e8b0990ea8789c74ea378795134c",
"reference": "98c4a6573b27e8b0990ea8789c74ea378795134c",
"shasum": ""
},
"require": {
"php": ">=5.5"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.2.20|^2.15",
"php-coveralls/php-coveralls": "^2.1",
"phpunit/phpunit": "^4.8.36|^5.7.27|^6.59|^7.5.11"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.2.x-dev"
}
},
"autoload": {
"psr-4": {
"ReCaptcha\\": "src/ReCaptcha"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"description": "Client library for reCAPTCHA, a free service that protects websites from spam and abuse.",
"homepage": "https://www.google.com/recaptcha/",
"keywords": [
"Abuse",
"captcha",
"recaptcha",
"spam"
],
"time": "2019-08-16T15:48:25+00:00"
},
{ {
"name": "jdorn/sql-formatter", "name": "jdorn/sql-formatter",
"version": "v1.2.17", "version": "v1.2.17",

View file

@ -12,4 +12,5 @@ return [
Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true], Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true], Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],
Sentry\SentryBundle\SentryBundle::class => ['all' => true], Sentry\SentryBundle\SentryBundle::class => ['all' => true],
EWZ\Bundle\RecaptchaBundle\EWZRecaptchaBundle::class => ['all' => true],
]; ];

View file

@ -0,0 +1,2 @@
ewz_recaptcha:
enabled: false

View file

@ -0,0 +1,4 @@
# See https://github.com/excelwebzone/EWZRecaptchaBundle for full configuration
ewz_recaptcha:
public_key: '%env(EWZ_RECAPTCHA_SITE_KEY)%'
private_key: '%env(EWZ_RECAPTCHA_SECRET)%'

View file

@ -0,0 +1,21 @@
#services:
#
# # Inject this service in your controllers/services to verify a submitted captcha.
# ReCaptcha\ReCaptcha:
# arguments:
# $secret: '%env(GOOGLE_RECAPTCHA_SECRET)%'
# $requestMethod: '@ReCaptcha\RequestMethod'
#
# # Curl is set here as default transport to communicate with Google servers.
# # If you do not have php-curl extension, you can change for a socket or a plain POST request.
# # Check out the repository for all other request methods:
# # https://github.com/google/recaptcha/tree/master/src/ReCaptcha/RequestMethod
# ReCaptcha\RequestMethod: '@ReCaptcha\RequestMethod\CurlPost'
# ReCaptcha\RequestMethod\CurlPost: null
# ReCaptcha\RequestMethod\Curl: null
#
## Uncomment this line if you want to inject the site key to all your Twig templates.
## You can also inject the "google_recaptcha_site_key" container parameter to your controllers.
##twig:
## globals:
## google_recaptcha_site_key: '%google_recaptcha_site_key%'

View file

@ -2,6 +2,7 @@
namespace App\Form\Data; namespace App\Form\Data;
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints as ReCaptcha;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
class PasswordResetRequestData class PasswordResetRequestData
@ -13,4 +14,11 @@ class PasswordResetRequestData
* @Assert\NotBlank() * @Assert\NotBlank()
*/ */
public $email; public $email;
/**
* @var string
*
* @ReCaptcha\IsTrue
*/
public $recaptcha;
} }

View file

@ -4,13 +4,17 @@ namespace App\Form;
use App\Form\Data\PasswordResetRequestData; use App\Form\Data\PasswordResetRequestData;
use Symfony\Component\Form\{AbstractType, Extension\Core\Type\EmailType, FormBuilderInterface}; use Symfony\Component\Form\{AbstractType, Extension\Core\Type\EmailType, FormBuilderInterface};
use EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
class PasswordResetRequestType extends AbstractType class PasswordResetRequestType extends AbstractType
{ {
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)
{ {
$builder->add('email', EmailType::class, ['required' => true]); $builder
->add('email', EmailType::class, ['required' => true])
->add('recaptcha', EWZRecaptchaType::class)
;
} }
public function configureOptions(OptionsResolver $resolver) public function configureOptions(OptionsResolver $resolver)

View file

@ -68,6 +68,31 @@
"egulias/email-validator": { "egulias/email-validator": {
"version": "2.1.14" "version": "2.1.14"
}, },
"excelwebzone/recaptcha-bundle": {
"version": "1.5",
"recipe": {
"repo": "github.com/symfony/recipes-contrib",
"branch": "master",
"version": "1.5",
"ref": "fd4da7bc71749db65bc83abf5d164bfa9c839cf4"
},
"files": [
"config/packages/dev/ewz_recaptcha.yaml",
"config/packages/ewz_recaptcha.yaml"
]
},
"google/recaptcha": {
"version": "1.1",
"recipe": {
"repo": "github.com/symfony/recipes-contrib",
"branch": "master",
"version": "1.1",
"ref": "d087df3e087f50da3955f2def05079380da5894b"
},
"files": [
"config/packages/google_recaptcha.yaml"
]
},
"guzzlehttp/guzzle": { "guzzlehttp/guzzle": {
"version": "6.5.2" "version": "6.5.2"
}, },