-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from wi-wissen/master
add custom endpoints and localisation
- Loading branch information
Showing
7 changed files
with
103 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
@@ -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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.' | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.' | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,6 @@ | |
|
||
class FriendlyCaptcha | ||
{ | ||
/** | ||
* FriendlyCaptcha Verification URL | ||
*/ | ||
const VERIFICATION_API_ENDPOINT = 'https://api.friendlycaptcha.com/api/v1/siteverify'; | ||
|
||
/** | ||
* FriendlyCaptcha secret | ||
* | ||
|
@@ -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; | ||
} | ||
|
||
|
@@ -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'])) { | ||
|
@@ -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; | ||
} | ||
|
||
|
@@ -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; | ||
|
@@ -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, | ||
]); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters