Search by

codepulse2020 / laravel-captcha

haosheng

A Laravel package providing unified captcha verification with support for reCAPTCHA, Turnstile, and hCaptcha

Package info

github.com/codepulse2020/laravel-captcha

pkg:composer/codepulse2020/laravel-captcha

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v2.0.0 2026-11-30 03:07 UTC

This package is auto-updated.

Last update: 2026-11-30 03:10:48 UTC


README

A Laravel package that provides a unified API for integrating multiple captcha services, including Google reCAPTCHA (v2 & v3), Cloudflare Turnstile, and hCaptcha. Easily switch between providers with minimal code changes.

  • PHP 8.2+, Laravel 10 / 11 / 12 / 13 (Laravel 13 requires PHP 8.3+)

Installation

You can install the package via composer:

composer require codepulse2020/laravel-captcha

You can publish the config file with:

php artisan vendor:publish --tag="captcha-config"

This is the contents of the published config file:

return [

    /*
    |--------------------------------------------------------------------------
    | 啟用驗證
    |--------------------------------------------------------------------------
    |
    | 是否啟用驗證碼驗證。停用後,所有驅動的驗證一律視為通過,適合用於
    | 本機開發或自動化測試。留空或無法辨識的值會視為啟用。
    |
    */

    'enabled' => env('CAPTCHA_ENABLED', true),

    /*
    |--------------------------------------------------------------------------
    | 預設驅動
    |--------------------------------------------------------------------------
    |
    | 未指定驅動時所使用的驗證碼服務,對應下方「驗證碼服務」中的設定。
    |
    | 支援:"recaptcha"、"turnstile"、"hcaptcha"
    |
    */

    'default' => env('CAPTCHA_DRIVER', 'recaptcha'),

    /*
    |--------------------------------------------------------------------------
    | 允許的網域
    |--------------------------------------------------------------------------
    |
    | 限制 token 必須由這些網域產生,例如 ['example.com'],需完全相符、
    | 不含通訊協定與連接埠。空陣列表示不檢查,由各服務後台的網域設定把關。
    |
    */

    'hostnames' => [],

    /*
    |--------------------------------------------------------------------------
    | 驗證碼服務
    |--------------------------------------------------------------------------
    |
    | 各驗證碼服務的金鑰與選項,只需填寫實際使用的服務。secret_key 為
    | 必填;site_key 供前端使用,後端驗證不需要。
    |
    | reCAPTCHA 的 version 可為 "v2" 或 "v3",其他值會丟出例外;score 為
    | v3 的最低通過分數,介於 0.0 至 1.0 之間,低於此分數即視為驗證失敗。
    |
    */

    'providers' => [

        'recaptcha' => [
            'version' => env('RECAPTCHA_VERSION', 'v2'),
            'site_key' => env('RECAPTCHA_SITE_KEY'),
            'secret_key' => env('RECAPTCHA_SECRET_KEY'),
            'score' => env('RECAPTCHA_SCORE', 0.5),
        ],

        'turnstile' => [
            'site_key' => env('TURNSTILE_SITE_KEY'),
            'secret_key' => env('TURNSTILE_SECRET_KEY'),
        ],

        'hcaptcha' => [
            'site_key' => env('HCAPTCHA_SITE_KEY'),
            'secret_key' => env('HCAPTCHA_SECRET_KEY'),
        ],

    ],

];

Configuration

Add the following environment variables to your .env file based on the captcha provider you want to use:

# Global settings
CAPTCHA_ENABLED=true
CAPTCHA_DRIVER=recaptcha

# Google reCAPTCHA
RECAPTCHA_VERSION=v2
RECAPTCHA_SCORE=0.5
RECAPTCHA_SITE_KEY=your-site-key
RECAPTCHA_SECRET_KEY=your-secret-key

# Cloudflare Turnstile
TURNSTILE_SITE_KEY=your-site-key
TURNSTILE_SECRET_KEY=your-secret-key

# hCaptcha
HCAPTCHA_SITE_KEY=your-site-key
HCAPTCHA_SECRET_KEY=your-secret-key

Usage

Using the Facade

use CodePulse2020\Captcha\Facades\Captcha;

// Verify using the default driver
$result = Captcha::verify($request->input('captcha_token'), $request->ip());

// Verify using a specific driver
$result = Captcha::driver('turnstile')->verify($token, $ip);

// Also require the action (reCAPTCHA v3 and Turnstile only)
$result = Captcha::verify($token, $ip, 'login');

// Check if captcha is enabled
if (Captcha::isEnabled()) {
    // ...
}

Using the Validation Rule

use CodePulse2020\Captcha\Rules\CaptchaRule;

$request->validate([
    'captcha_token' => [new CaptchaRule],
]);

// With a specific driver
$request->validate([
    'captcha_token' => [new CaptchaRule('turnstile')],
]);

The rule also runs when the field is missing or empty, so required is not needed. Leave it out if you want requests without a token to pass while captcha is disabled.

// Also require the action (reCAPTCHA v3 and Turnstile only)
$request->validate([
    'captcha_token' => [new CaptchaRule(action: 'login')],
]);

Other drivers, including reCAPTCHA v2, throw an InvalidArgumentException when an action is given.

Custom Drivers

The manager is rebuilt for every request or job, so register custom drivers with afterResolving in a service provider's register() method. Calling Captcha::extend() directly in boot() is lost after the first reset under Octane or queue workers.

use CodePulse2020\Captcha\CaptchaManager;

$this->app->afterResolving(CaptchaManager::class, function (CaptchaManager $captcha) {
    $captcha->extend('custom', fn ($app) => new CustomProvider);
});

The driver must implement CaptchaProviderInterface.

Failures

Connection errors, unexpected responses and configuration errors (such as an invalid secret key) fail verification and are logged as warnings. Tokens and secret keys are never logged.

Supported Drivers

Driver Provider Versions
recaptcha Google reCAPTCHA v2, v3
turnstile Cloudflare Turnstile -
hcaptcha hCaptcha -

Migrating from haosheng0211/laravel-captcha

Remove the old package before installing this one; both register the Captcha alias and the captcha config.

composer remove haosheng0211/laravel-captcha
composer require codepulse2020/laravel-captcha

Then replace the MrJin\Captcha namespace with CodePulse2020\Captcha. Existing config keys, environment variables and translation keys still work. See the CHANGELOG for behaviour changes.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.