Skip to content

Commit

Permalink
Merge pull request #1 from wi-wissen/master
Browse files Browse the repository at this point in the history
add custom endpoints and localisation
  • Loading branch information
ossycodes authored Jan 7, 2023
2 parents f41e5e4 + e7f997e commit b2c6294
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 25 deletions.
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ composer require ossycodes/friendlycaptcha

### Configuration

Add `FRIENDLY_CAPTCHA_SECRET` and `FRIENDLY_CAPTCHA_SITEKEY` in **.env** file :
Add `FRIENDLY_CAPTCHA_SECRET`, `FRIENDLY_CAPTCHA_SITEKEY` and optional `FRIENDLY_CAPTCHA_PUZZLE_ENDPOINT`, `FRIENDLY_CAPTCHA_VERIFY_ENDPOINT` in **.env** file :

```
FRIENDLY_CAPTCHA_SECRET=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
FRIENDLY_CAPTCHA_SITEKEY=XXXXXXXXXXXXXXXX
FRIENDLY_CAPTCHA_PUZZLE_ENDPOINT=https://api.friendlycaptcha.com/api/v1/puzzle #optional
FRIENDLY_CAPTCHA_VERIFY_ENDPOINT=https://api.friendlycaptcha.com/api/v1/siteverify #optional
```

You can obtain your site-key from [here](https://docs.friendlycaptcha.com/#/installation?id=_1-generating-a-sitekey) and secret from [here](https://apiserver-prod.friendlycaptcha.eu/dashboard/accounts/1118678876/apikeys)

## Usage

In your layout file, include the FriendlyCaptcha widget scripts using the `@friendlyCaptchaRenderWidgetScripts` Blade directive. This should be added to the `<head>` of your document.
For FriendlyCaptcha widget scripts from a CDN, add the Blade directive `@friendlyCaptchaRenderWidgetScripts` in your layout file. This should be added to the `<head>` of your document.

```blade
<html>
Expand All @@ -48,13 +50,25 @@ or if you don't want to use the Blade directive you can do this instead

You have two options on how to add the script tag either from unpkg (default) or from jsdelivr

@friendlyCaptchaRenderWidgetScripts()
`@friendlyCaptchaRenderWidgetScripts()`
or
@friendlyCaptchaRenderWidgetScripts('jsdelivr')
`@friendlyCaptchaRenderWidgetScripts('jsdelivr')`

{!! FriendlyCaptcha::renderWidgetScripts() !!}
`{!! FriendlyCaptcha::renderWidgetScripts() !!}`
or
{!! FriendlyCaptcha::renderWidgetScripts('jsdelivr') !!}
`{!! FriendlyCaptcha::renderWidgetScripts('jsdelivr') !!}`

You can also host the FriendlyCaptcha widget scripts yourself:

```
npm install --save [email protected]
```

And import it in your app:

```js
import "friendly-challenge/widget";
```


Once that's done, you can call the `renderWidget()` method in `<form>` to output the appropriate markup (friendlycaptcha widget) with your site key configured.
Expand Down
18 changes: 18 additions & 0 deletions lang/de/validation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
|--------------------------------------------------------------------------
| Validation Language Lines
|--------------------------------------------------------------------------
*/

return [
'secret_missing' => 'Sie haben vergessen, den Parameter secret (=API-Schlüssel) hinzuzufügen.',
'secret_invalid' => 'Der von Ihnen angegebene API-Schlüssel war ungültig.',
'solution_missing' => 'Sie haben vergessen, den Parameter secret (=API-Schlüssel) hinzuzufügen.',
'secret_missing' => 'Sie haben vergessen, den Lösungsparameter hinzuzufügen.',
'bad_request' => 'Mit Ihrer Anfrage ist etwas anderes nicht in Ordnung, z. B. ist Ihr Anfragekörper leer.',
'solution_invalid' => 'Die von Ihnen angegebene Lösung war ungültig (vielleicht wurde versucht, das Rätsel zu manipulieren).',
'solution_timeout_or_duplicate' => 'Das Rätsel, für das Sie die Lösung angegeben haben, ist abgelaufen oder wurde bereits verwendet.',
'unexpected' => 'Ein unerwarteter Fehler ist aufgetreten.'
];
18 changes: 18 additions & 0 deletions lang/en/validation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
|--------------------------------------------------------------------------
| Validation Language Lines
|--------------------------------------------------------------------------
*/

return [
'secret_missing' => 'You forgot to add the secret (=API key) parameter.',
'secret_invalid' => 'The API key you provided was invalid.',
'solution_missing' => 'You forgot to add the secret (=API key) parameter.',
'secret_missing' => 'You forgot to add the solution parameter.',
'bad_request' => 'Something else is wrong with your request, e.g. your request body is empty.',
'solution_invalid' => 'The solution you provided was invalid (perhaps the user tried to tamper with the puzzle).',
'solution_timeout_or_duplicate' => 'The puzzle that the solution was for has expired or has already been used.',
'unexpected' => 'An unexpected error occurred.'
];
36 changes: 24 additions & 12 deletions src/FriendlyCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@

class FriendlyCaptcha
{
/**
* FriendlyCaptcha Verification URL
*/
const VERIFICATION_API_ENDPOINT = 'https://api.friendlycaptcha.com/api/v1/siteverify';

/**
* FriendlyCaptcha secret
*
Expand Down Expand Up @@ -39,25 +34,27 @@ class FriendlyCaptcha
*/
protected $http;

public function __construct($secret, $sitekey, $options = [])
public function __construct($secret, $sitekey, $puzzle, $verify, $options = [])
{
$this->secret = $secret;
$this->sitekey = $sitekey;
$this->puzzle = $puzzle;
$this->verify = $verify;
$this->http = new Client($options);
}

public function renderWidgetScripts($option = 'unpkg')
{
if ($option == 'unpkg') {
return <<<EOF
<script type="module" src="https://unpkg.com/[email protected].8/widget.module.min.js" async defer></script>
<script nomodule src="https://unpkg.com/[email protected].8/widget.min.js" async defer></script>
<script type="module" src="https://unpkg.com/[email protected].9/widget.module.min.js" async defer></script>
<script nomodule src="https://unpkg.com/[email protected].9/widget.min.js" async defer></script>
EOF;
}

return <<<EOF
<script type="module" src="https://cdn.jsdelivr.net/npm/[email protected].8/widget.module.min.js" async defer></script>
<script nomodule src="https://cdn.jsdelivr.net/npm/[email protected].8/widget.min.js" async defer></script>
<script type="module" src="https://cdn.jsdelivr.net/npm/[email protected].9/widget.module.min.js" async defer></script>
<script nomodule src="https://cdn.jsdelivr.net/npm/[email protected].9/widget.min.js" async defer></script>
EOF;
}

Expand All @@ -76,6 +73,8 @@ public function renderWidget($attributes = [])
*/
protected function prepareAttributes(array $attributes)
{
$attributes['data-puzzle-endpoint'] = $this->puzzle;

$attributes['data-sitekey'] = $this->sitekey;

if (isset($attributes['dark-theme'])) {
Expand All @@ -86,6 +85,12 @@ protected function prepareAttributes(array $attributes)

$attributes['class'] = trim('frc-captcha');

$locale = \App::currentLocale();
if (in_array($locale, ["en", "fr", "de", "it", "nl", "pt", "es", "ca", "da", "ja", "ru", "sv", "el", "uk", "bg", "cs", "sk", "no", "fi", "lt", "lt", "pl", "et", "hr", "sr", "sl", "hu", "ro", "zh", "zh_TW", "vi"])) {
//use supported locale - https://docs.friendlycaptcha.com/#/widget_api?id=data-lang-attribute
$attributes['data-lang'] = $locale;
}

return $attributes;
}

Expand Down Expand Up @@ -145,7 +150,14 @@ public function verifyResponse($solution)
return $this;
}

$this->errors = $verifyResponse['errors'];
if (isset($verifyResponse['errors'])) {
$this->errors = $verifyResponse['errors'];
}

if (isset($verifyResponse['error'])) {
$this->errors = [$verifyResponse['error']];
}

$this->isSuccess = false;

return $this;
Expand All @@ -161,7 +173,7 @@ public function verifyResponse($solution)
*/
protected function sendRequestVerify(array $data = [])
{
$response = $this->http->request('POST', static::VERIFICATION_API_ENDPOINT, [
$response = $this->http->request('POST', $this->verify, [
'form_params' => $data,
]);

Expand Down
14 changes: 14 additions & 0 deletions src/FriendlyCaptchaServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public function boot()
$this->bootBladeDirectives();

$this->bootMacro();

$this->bootLang();
}

/**
Expand Down Expand Up @@ -74,6 +76,16 @@ public function bootMacro()
});
}

/**
* boot lang
*/
public function bootLang()
{
Rule::macro('friendlycaptcha', function () {
$this->loadTranslationsFrom(__DIR__.'/../lang', 'friendlycaptcha');
});
}

/**
* Register the application services.
*/
Expand All @@ -87,6 +99,8 @@ public function register()
return new FriendlyCaptcha(
$app['config']['friendlycaptcha.secret'],
$app['config']['friendlycaptcha.sitekey'],
$app['config']['friendlycaptcha.puzzle_endpoint'],
$app['config']['friendlycaptcha.verify_endpoint'],
$app['config']['friendlycaptcha.options']
);
});
Expand Down
14 changes: 7 additions & 7 deletions src/Rules/FriendlyCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,25 @@ protected function mapErrorCodeToMessage(string $code): string
{
switch ($code) {
case "secret_missing":
return "You forgot to add the secret (=API key) parameter.";
return __('validation.secret_missing');
break;
case "secret_invalid":
return "The API key you provided was invalid.";
return __('validation.secret_invalid');
break;
case "solution_missing":
return "You forgot to add the solution parameter.";
return __('validation.solution_missing');
break;
case "bad_request":
return "Something else is wrong with your request, e.g. your request body is empty.";
return __('validation.bad_request');
break;
case "solution_invalid":
return "The solution you provided was invalid (perhaps the user tried to tamper with the puzzle).";
return __('validation.solution_invalid');
break;
case "solution_timeout_or_duplicate":
return "The puzzle that the solution was for has expired or has already been used.";
return __('validation.solution_timeout_or_duplicate');
break;
default:
return "An unexpected error occurred.";
return __('validation.unexpected');
}
}
}
2 changes: 2 additions & 0 deletions src/config/friendlycaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
return [
'secret' => env('FRIENDLY_CAPTCHA_SECRET'),
'sitekey' => env('FRIENDLY_CAPTCHA_SITEKEY'),
'puzzle_endpoint' => env('FRIENDLY_CAPTCHA_PUZZLE_ENDPOINT', 'https://api.friendlycaptcha.com/api/v1/puzzle'),
'verify_endpoint' => env('FRIENDLY_CAPTCHA_VERIFY_ENDPOINT', 'https://api.friendlycaptcha.com/api/v1/siteverify'),
'options' => [
'timeout' => 30,
'http_errors' => false,
Expand Down

0 comments on commit b2c6294

Please sign in to comment.