Git Product home page Git Product logo

laravel-recaptchav3's Introduction

Laravel Recaptcha V3

Build Status Total Downloads Latest Stable Version License

Laravel package for Google's Recaptcha V3. This is a lightweight package which focuses on the backend validation of Recaptcha V3 captchas.

Installation

To get started, use Composer to add the package to your project's dependencies:

composer require josiasmontag/laravel-recaptchav3

Add RECAPTCHAV3_SITEKEY and RECAPTCHAV3_SECRET to your .env file. (You can get them here)

RECAPTCHAV3_SITEKEY=sitekey
RECAPTCHAV3_SECRET=secret

Optionally, you can publish the config file:

php artisan vendor:publish --provider="Lunaweb\RecaptchaV3\Providers\RecaptchaV3ServiceProvider"

Usage

Init Recaptcha Javascript

Recaptcha v3 works best when it is loaded on every page to get the most context about interactions. Therefore, add to your header or footer template:

{!! RecaptchaV3::initJs() !!}

Forms

RecaptchaV3::field($action, $name='g-recaptcha-response') creates an invisible input field that gets filled with a Recaptcha token on load.

<form method="post" action="/register">
    {!! RecaptchaV3::field('register') !!}
    <input type="submit" value="Register"></input>
</form>

Validation

Add the recaptchav3 validator to the rules array. The rule accepts two parameters: The action name and the minimum required score (defaults to 0.5).

$validate = Validator::make(Input::all(), [
	'g-recaptcha-response' => 'required|recaptchav3:register,0.5'
]);

Getting the score

Alternatively, you can get the score and take variable action:

// Import the facade class
use Lunaweb\RecaptchaV3\Facades\RecaptchaV3;
//  RecaptchaV3::verify($token, $action)
$score = RecaptchaV3::verify($request->get('g-recaptcha-response'), 'register')
if($score > 0.7) {
    // go
} elseif($score > 0.3) {
    // require additional email verification
} else {
    return abort(400, 'You are most likely a bot');
}

Custom validation error message

Add the following values to the custom array in the validation language file:

'custom' => [
    'g-recaptcha-response' => [
        'recaptchav3' => 'Captcha error message',
    ],
],

Hiding the ReCAPTCHA Badge

Add to your CSS file:

.grecaptcha-badge { visibility: hidden !important; }

Localization

By default, the package follows the default application locale, which is defined in config/app.php. If you want to change this behavior, you can specify what locale to use by adding a new environment variable :

RECAPTCHAV3_LOCALE=ar

Testing

To make your forms testable, you can mock the RecaptchaV3 facade:

RecaptchaV3::shouldReceive('verify')
    ->once()
    ->andReturn(1.0);

laravel-recaptchav3's People

Contributors

bondif avatar joeri-kixx avatar josiasmontag avatar laravel-shift avatar tderick avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

laravel-recaptchav3's Issues

Enhancement : Hide badge

First of all, thanks for sharing this !

A good enhancement would be to hide the badge, and optionnally, show it when the users starts filling a protected form.

Feature request

Hi,

sometimes the page on my website loads slow, so some people push the button to submit to early, so they dont got an google token and the post fail, is it an option to make an callback function so, when it is loaded an callback will be fired, so i can enable the button?

Error with Ajax

Every time a request is sent using ajax, and there is an error, just let it do it once, you have to reload the page to be able to use it again, because it returns the catpcha error. It should have a function to refresh the captcha.
I made a login where it returns if the person has active, double authentication, so if it returns yes, it should ask for the code on the same page, but it doesn't stop because if it sends the code at once, it should refresh.

reCAPTCHA will timeout

If you are on the page for too long, when you submit the form, there will be a timeout and the reCAPTCHA will not validate. Perhaps, adding a built in function that only sets the token when the form is submitted would solve the issue?

Bug : validateRecaptchav3 does not exist

Here is an error I get when submitting a form

Method Illuminate\Validation\Validator::validateRecaptchav3 does not exist

After some debugging, it seems the extend('recaptchav3' of the provider is not called before the validation.

The only solution was to move the extend code to my AppServiceProvider::boot...

Note that I add the provider to my config/app.php :

\Lunaweb\RecaptchaV3\Providers\RecaptchaV3ServiceProvider::class,

and that I ran a config:clear and a cache:clear.
If I add a die in the RecaptchaV3ServiceProvider, it got executed when I load a page, but not when I post a form (the error above occurs before it can).

I'm new to laravel, so it's probably a mistake of mine, right ?

verify method returns false

use Lunaweb\RecaptchaV3\Facades\RecaptchaV3;
// RecaptchaV3::verify($token, $action)
$score = RecaptchaV3::verify($request->get('g-recaptcha-response'), 'register')
if($score > 0.7) {
// go
} elseif($score > 0.3) {
// require additional email verification
} else {
return abort(400, 'You are most likely a bot');
}

above code always returns false

Sometimes no score is submitted

Sometimes i see forms being submitted with no score that are not spam.
At the moment i receive my "failed" forms and add the score to check it.
$recaptcha_score = RecaptchaV3::verify($request->get('g-recaptcha-response'), 'contact_general');
Resulting in $recaptcha_score being empty (with Validator using 'g-recaptcha-response' => 'required').

Invalid action name

No matter what i do i always get:
Invalid action name, may only include "A-Za-z/_". Do not include user-specific information. And the form cannot be submitted.

It's rendered like this in the html

  grecaptcha.ready(function() {
      grecaptcha.execute('6LdC4jklAAAAAP7JF31jlY3AjXFEGtCxJxHZRjGf', {action: 'handle-form'}).then(function(token) {
         document.getElementById('g-recaptcha-response-64234447bb0d9').value = token;
      });
  });

Any idea what's going wrong?

Content Security Policy Nonce

My server has Content Security Policy enabled, i want to initialize js code with a nonce to allow recaptcha script execution.

Google docs recommend use nonce: FAQ

Can be added a function called initJsNonce() or related?

    /**
     * @param string $nonce
     * @return string
     */
    public function initJsNonce(string $nonce): string
    {
        return '<script src="' . $this->origin . '/api.js?render=' . $this->sitekey . '" nonce="'.$nonce.'"></script>';
    }

As workaround how can add/override RecaptchaV3 class to add this?

I'm not familiarized with Laravel Container to do that, if anyone can help me I would appreciate.

Score returns null recently

Recently, after working fine for some time, the score returns null or simply the validation fails.

Ran some tests:
Locally score is detected fine, but validation fails anyway.
on Production, score is null and validation fails anyway.

Anyone else experiencing this?

Thanks.

Field g-recaptcha-response is required

I've installed the package following documentation.
The recaptcha logo appears at the bottom of the page, but when I'm going to submit the form, it shows me

Field g-recaptcha-response is required

Also in the console appears this:

contact:280 Uncaught ReferenceError: grecaptcha is not defined
    at contact:280

Proxy Support

How to use and set up with proxy needed? It's not have proxy set up option?

Add suport for Guzzle 7

Hi, please add support for the latest version of guzzlehttp/guzzle 7.0.1. I have Laravel 7 and PHP 7.3.21
I have this error in composer.

Using version ^0.5.0 for josiasmontag/laravel-recaptchav3
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

Problem 1
- Installation request for josiasmontag/laravel-recaptchav3 ^0.5.0 -> satisfiable by josiasmontag/laravel-recaptchav3[0.5.0].
- Conclusion: remove guzzlehttp/guzzle 7.0.1
- Conclusion: don't install guzzlehttp/guzzle 7.0.1
- josiasmontag/laravel-recaptchav3 0.5.0 requires guzzlehttp/guzzle ^6.2 -> satisfiable by guzzlehttp/guzzle[6.2.0, 6.2.1, 6.2.2, 6.2.3, 6.3.0, 6.3.1, 6.3.2, 6.3.3, 6.4.0, 6.4.1, 6.5.0, 6.5.1, 6.5.2, 6.5.3, 6.5.4, 6.5.5].
- Can only install one of: guzzlehttp/guzzle[6.2.0, 7.0.1].
- Can only install one of: guzzlehttp/guzzle[6.2.1, 7.0.1].
- Can only install one of: guzzlehttp/guzzle[6.2.2, 7.0.1].
- Can only install one of: guzzlehttp/guzzle[6.2.3, 7.0.1].
- Can only install one of: guzzlehttp/guzzle[6.3.0, 7.0.1].
- Can only install one of: guzzlehttp/guzzle[6.3.1, 7.0.1].
- Can only install one of: guzzlehttp/guzzle[6.3.2, 7.0.1].
- Can only install one of: guzzlehttp/guzzle[6.3.3, 7.0.1].
- Can only install one of: guzzlehttp/guzzle[6.4.0, 7.0.1].
- Can only install one of: guzzlehttp/guzzle[6.4.1, 7.0.1].
- Can only install one of: guzzlehttp/guzzle[6.5.0, 7.0.1].
- Can only install one of: guzzlehttp/guzzle[6.5.1, 7.0.1].
- Can only install one of: guzzlehttp/guzzle[6.5.2, 7.0.1].
- Can only install one of: guzzlehttp/guzzle[6.5.3, 7.0.1].
- Can only install one of: guzzlehttp/guzzle[6.5.4, 7.0.1].
- Can only install one of: guzzlehttp/guzzle[6.5.5, 7.0.1].
- Installation request for guzzlehttp/guzzle (locked at 7.0.1) -> satisfiable by guzzlehttp/guzzle[7.0.1].

Installation failed, reverting ./composer.json to its original content.

make RECAPTCHA_ORIGIN configurable

For Chinese visitors/sites, they don't have abilities to access www.google.com for GFW reasons, but they can access recaptcha's domain www.recaptcha.net

eg

so, can you make captcha origin configurable?

const VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';

return '<script src="https://www.google.com/recaptcha/api.js?render=' . $this->sitekey . '"></script>';

Defer the loading recaptcha__en.js

Hello,
I really like this package. I had no problems setting it up and using it. But lately, the download of the recaptcha__en.js script from www.gstatic.com is the slowest resource that affects my site speed the most. Is it possible to defer the loading of this script?
Best Regards.

Localization of the widget

Hi,

As I noticed, there is no support for localization, in order to show the widget in different languages depending on the application and the user's needs.

Thanks

Add error message

Is it possible to add an error message if the score is lower then required?
with use of the validator?

Class 'App\Http\Controllers\Validator' not found

I'm new, and I don't know how to import this class to use Validator and RecaptchaV3.
Can you help me please.

Error:
Symfony\Component\Debug\Exception\FatalThrowableError
Class 'App\Http\Controllers\Validator' not found

Config file publishing

Hello, I'm starting with this and the config file publishing is not working.

The documentation says:

php artisan vendor:publish --provider="Lunaweb\RecaptchaV3\RecaptchaV3ServiceProvider"

but I only was able to publish it doing:

php artisan vendor:publish --provider="Lunaweb\RecaptchaV3\Providers\RecaptchaV3ServiceProvider"

Maybe the namespace is wrong or just the docs.

error message

hi,

how do i change the error message? it outputs this validation.recaptchav3
sorry, new to laravel.

Update dependency

Hi Josias,
I pareciate your time to develop this package.
But now I'm trying to update guzzle on my own way and composer response:

josiasmontag/laravel-recaptchav3 0.3.0 requires guzzlehttp/guzzle ^6.2 -> found guzzlehttp/guzzle[6.2.0, ..., 6.5.x-dev] but it conflicts with your root composer.json require (7.0)

It is possible for you update guzzle?
Thx

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.