Skip to content

Commit

Permalink
Add docs for captcha
Browse files Browse the repository at this point in the history
  • Loading branch information
Isengo1989 committed Dec 21, 2023
1 parent 813fd3c commit a1bcc92
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions guides/plugins/plugins/storefront/add-custom-captcha.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
nav:
title: Add custom captcha
position: 80

---

# Add custom captcha

## Overview

You can add your custom captcha to the Shopware 6 core. This guide will show you how to do that.

## Prerequisites

In order to be able to start with this guide, you need to have an own plugin running. As to most guides, this guide is also built upon the [Plugin base guide](../plugin-base-guide)

## Adding custom captcha to your plugin

In order to add custom captcha to your plugin, create a new folder called `Captcha` inside the `src/Framework` directory of your plugin. This is optional, but it's a good practice to keep your plugin files organized.

Take a look at the AbstractCaptcha class. This class is the base class for all captcha types. It contains the following methods:

* `supports(string $type): bool` - This method is used to check if the captcha type is supported by the plugin.
* `isValid(string $code): bool` - This method is used to check if the captcha code is valid.
* `getName(): string` - This method is used to get the name of the captcha type.
* `shouldBreak(): bool` - This method is used to check if the captcha should break the validation.

Check failure on line 27 in guides/plugins/plugins/storefront/add-custom-captcha.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/storefront/add-custom-captcha.md#L27

You have used the passive voice repeatedly in nearby sentences. To make your writing clearer and easier to read, consider using active voice. (REP_PASSIVE_VOICE[6]) URL: https://languagetool.org/insights/post/what-is-passive-voice/ Rule: https://community.languagetool.org/rule/show/REP_PASSIVE_VOICE?lang=en-US&subId=6 Category: REPETITIONS_STYLE
Raw output
guides/plugins/plugins/storefront/add-custom-captcha.md:27:38: You have used the passive voice repeatedly in nearby sentences. To make your writing clearer and easier to read, consider using active voice. (REP_PASSIVE_VOICE[6])
 URL: https://languagetool.org/insights/post/what-is-passive-voice/ 
 Rule: https://community.languagetool.org/rule/show/REP_PASSIVE_VOICE?lang=en-US&subId=6
 Category: REPETITIONS_STYLE
* `getData(): array` - This method is used to get the data of the captcha type.
+ `getViolations(): ConstraintViolationListInterface` - This method is used to get the violations of the captcha type.


Simply extend the AbstractCaptcha class and implement the methods isValid and getName. The isValid method should return true if the captcha code is valid, false otherwise. The getName method should return the name of the captcha type.

```php

Check failure on line 34 in guides/plugins/plugins/storefront/add-custom-captcha.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/storefront/add-custom-captcha.md#L34

File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `PHP` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING
Raw output
guides/plugins/plugins/storefront/add-custom-captcha.md:34:3: File types are normally capitalized. (FILE_EXTENSIONS_CASE[1])
 Suggestions: `PHP`
 URL: https://languagetool.org/insights/post/spelling-capital-letters/ 
 Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1
 Category: CASING

<?php declare(strict_types=1);

namespace Shopware\Storefront\Framework\Captcha;

use GuzzleHttp\ClientInterface;
use Psr\Http\Client\ClientExceptionInterface;
use Shopware\Core\Framework\Log\Package;
use Symfony\Component\HttpFoundation\Request;

#[Package('storefront')]
class YourCaptcha extends AbstractCaptcha
{
final public const CAPTCHA_NAME = 'yourCaptchaName';
final public const CAPTCHA_REQUEST_PARAMETER = '_your_captcha_name';
private const YOUR_CAPTCHA_ENDPOINT = 'https://www.yourcaptcha.com/verify';

/**
* @internal
*/
public function __construct(private readonly ClientInterface $client)
{
}

/**
* {@inheritdoc}
*/
public function isValid(Request $request, array $captchaConfig): bool
{
if (!$request->get(self::CAPTCHA_REQUEST_PARAMETER)) {
return false;
}

try {
$response = $this->client->request('POST', self::GOOGLE_CAPTCHA_VERIFY_ENDPOINT, [
'form_params' => [
'response' => $request->get(self::CAPTCHA_REQUEST_PARAMETER),
'remoteip' => $request->getClientIp(),
],
]);

$responseRaw = $response->getBody()->getContents();
$response = json_decode($responseRaw, true);

return $response && (bool) $response['success'];
} catch (ClientExceptionInterface) {
return false;
}
}

/**
* {@inheritdoc}
*/
public function getName(): string
{
return self::CAPTCHA_NAME;
}
}

```

## Google reCAPTCHA v3 example

You might want to check out the example [GoogleReCaptchaV3](https://github.com/shopware/shopware/blob/trunk/src/Storefront/Framework/Captcha/GoogleReCaptchaV3.php) class from the Shopware 6 core. It's a good example of how to implement a custom captcha type.

0 comments on commit a1bcc92

Please sign in to comment.