mcaskill/charcoal-recaptcha

Google reCAPTCHA for Charcoal.

v0.5.0 2018-05-23 15:13 UTC

This package is auto-updated.

Last update: 2024-11-22 02:40:35 UTC


README

License Latest Stable Version Code Quality Coverage Status Build Status

Google reCAPTCHA for Charcoal

A Charcoal service provider for the Google reCAPTCHA client Library.

This package can be used as a PSR-7 middleware or as an object in your service layer.

Installation

composer require mcaskill/charcoal-recaptcha

See composer.json for depenencides.

What's inside?

  • Charcoal\ReCaptcha\CaptchaServiceProvider: Pimple service provider.
  • Charcoal\ReCaptcha\CaptchaConfig: Configuring the CAPTCHA service.
  • Charcoal\ReCaptcha\CaptchaAwareTrait: Convenient trait for interfacing with the CAPTCHA service.
  • Charcoal\ReCaptcha\Captcha: Service that handles the reCAPTCHA client.
  • Charcoal\ReCaptcha\LocalizedCaptcha: Translator-aware variant of the service.

Usage

use Charcoal\ReCaptcha\Captcha;

$captcha = new Captcha([
    'config' => [
        'public_key'  => '',
        'private_key' => '',
    ]
]);

// As middleware
$app->post('/signup', '')->add($captcha);

// As standalone, with direct user input
$captcha->verify($input, $ip);

// With a PSR-7 request
$captcha->verifyRequest($request);

// Display the widget in your views
echo $captcha->display(
    [
        'data-theme' => 'dark',
        'data-type' =>  'audio',
    ],
    [
        'hl' => 'fr'
    ]
);

By default, the Captcha adapter will defer the instantiation of ReCaptcha until the first verification request.

Custom ReCaptcha Class

The ReCaptcha class be swapped using the client_class option.

use Charcoal\ReCaptcha\Captcha;
use MyApp\ MyCustomReCaptcha;

$captcha = new Captcha([
    'config' => [
        'public_key'  => '',
        'private_key' => '',
    ],
    'client_class' =>  MyCustomReCaptcha::class
]);

Custom ReCaptcha Instance

An instance of the ReCaptcha class can be assigned using the client option.

use Charcoal\ReCaptcha\Captcha;
use ReCaptcha\ReCaptcha;
use ReCaptcha\RequestMethod\CurlPost;

$client = new ReCaptcha('', new CurlPost());

$captcha = new Captcha([
    'config' => [
        'public_key'  => '',
        'private_key' => '',
    ],
    'client' => $client
]);

Service Provider

If CaptchaServiceProvider is used, the following are provided.

Parameters

Services

  • charcoal/captcha: An instance of Captcha.

Registering

Via Charcoal configuration file:

{
    "apis": {
        "google": {
            "recaptcha": {
                "public_key": "",
                "private_key": ""
            }
        },
    },
    "service_providers": {
        "charcoal/re-captcha/captcha": {}
    }
}

Via PHP:

$container->register(new Charcoal\ReCaptcha\CaptchaServiceProvider(), [
    'charcoal/captcha/config' => [
        'public_key'  => '',
        'private_key' => ''
    ]
]);

Acknowledgements

This package is inspired by:

License

  • Charcoal reCAPTCHA component is licensed under the MIT license. See LICENSE for details.
  • Charcoal framework is licensed under the MIT license. See LICENSE for details.
  • Google reCAPTCHA PHP client library is licensed under the BSD License. See the LICENSE file for details.