sunrise / recaptcha
Google reCAPTCHA client.
v1.1.0
2025-02-20 21:24 UTC
Requires
- php: >=8.2
- psr/http-client: ^1.0
- psr/http-factory: ^1.0 || ^2.0
- psr/http-server-middleware: ^1.0
- sunrise/coder: ^1.1
- sunrise/hydrator: ^3.15
- sunrise/translator: ^1.0
Requires (Dev)
- php-di/php-di: ^7.0
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5
- squizlabs/php_codesniffer: ^3.11
- sunrise/http-router: ^3.0@RC
- symfony/validator: ^7.2
- vimeo/psalm: ^6.6
This package is auto-updated.
Last update: 2025-02-20 21:25:52 UTC
README
Installation
composer require sunrise/recaptcha
How to use
Quick Start
composer require sunrise/http-client-curl sunrise/http-message
use Sunrise\Coder\CodecManager; use Sunrise\Coder\Codec\UrlEncodedCodec; use Sunrise\Http\Client\Curl\Client; use Sunrise\Http\Message\RequestFactory; use Sunrise\Http\Message\ResponseFactory; use Sunrise\Hydrator\Hydrator; use Sunrise\Recaptcha\Dto\RecaptchaVerificationRequest; use Sunrise\Recaptcha\RecaptchaVerificationClient; use Sunrise\Recaptcha\RecaptchaVerificationConfiguration; // https://developers.google.com/recaptcha/docs/faq $privateKey = '6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe'; $client = new RecaptchaVerificationClient( verificationConfiguration: new RecaptchaVerificationConfiguration(privateKey: $privateKey), httpRequestFactory: new RequestFactory(), httpClient: new Client(new ResponseFactory()), codecManager: new CodecManager([new UrlEncodedCodec()]), hydrator: new Hydrator(), ); $userToken = ''; $clientResponse = $client->sendRequest(new RecaptchaVerificationRequest(userToken: $userToken)); if ($clientResponse->success) { // Challenge passed } else { // Challenge failed }
Integration with Sunrise Router
use Sunrise\Http\Router\Annotation\Consumes; use Sunrise\Http\Router\Annotation\Middleware; use Sunrise\Http\Router\Annotation\PostApiRoute; use Sunrise\Http\Router\Annotation\RequestBody; use Sunrise\Http\Router\MediaType; use Sunrise\Recaptcha\Integration\Router\Middleware\RecaptchaChallengeMiddleware; final class AuthController { #[PostApiRoute('api.login', '/api/login')] #[Consumes(MediaType::JSON)] #[Middleware(RecaptchaChallengeMiddleware::class)] public function login(#[RequestBody] LoginRequest $loginRequest): void { } }
Integration with Symfony Validator
use SensitiveParameter; use Sunrise\Recaptcha\Integration\Validator\Constraint\RecaptchaChallenge; use Symfony\Component\Validator\Constraints as Assert; final readonly class UserLoginRequest { public function __construct( #[Assert\NotBlank] #[Assert\Email] #[SensitiveParameter] public string $email, #[Assert\NotBlank] #[SensitiveParameter] public string $password, #[Assert\NotBlank(message: 'Prove that you are not a robot.')] #[RecaptchaChallenge] #[SensitiveParameter] public string $recaptcha, ) { } }
PHP-DI definitions
use DI\ContainerBuilder; use Sunrise\Recaptcha\RecaptchaVerificationClientInterface; $containerBuilder = new ContainerBuilder(); $containerBuilder->addDefinition(__DIR__ . '/../vendor/sunrise/recaptcha/resources/definitions/recaptcha_verification.php'); // If Sunrise Router is used $containerBuilder->addDefinitions(__DIR__ . '/../vendor/sunrise/recaptcha/resources/definitions/integration/router/middleware/recaptcha_challenge_middleware.php'); // If Symfony Validator is used $containerBuilder->addDefinition(__DIR__ . '/../vendor/sunrise/recaptcha/resources/definitions/integration/validator/constraint/recaptcha_challenge_validator.php'); $container = $containerBuilder->build(); // See above for usage examples. $recaptchaClient = $container->get(RecaptchaVerificationClientInterface::class);
Tests
composer test