Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1 from DarkGhostHunter/master
Browse files Browse the repository at this point in the history
Fixes Transparent Middleware on local development
  • Loading branch information
DarkGhostHunter authored Apr 9, 2019
2 parents f4863c0 + 009cbf5 commit a49c6bd
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 26 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ CAPTCHAVEL_MODE=auto

#### `auto`

The `auto` option leverages the frontend work to you. Just add the `data-recaptcha="true"` attribute to the forms where you want to check for reCAPTCHA.
The `auto` option leverages the frontend work from you. Just add the `data-recaptcha="true"` attribute to the forms where you want to check for reCAPTCHA.

```blade
<form action="/login" method="post" data-recaptcha="true">
Expand All @@ -237,7 +237,9 @@ The `auto` option leverages the frontend work to you. Just add the `data-recaptc
</form>
```

Captchavel will inject the Google reCAPTCHA v3 as a deferred script in the head before `<head>` tag, in every response (except JSON, AJAX or anything non-HTML), so it can have more analytics about how users interact with your site.
Captchavel will inject the Google reCAPTCHA v3 as a deferred script before `<head>` tag, in every response (except JSON, AJAX or anything non-HTML), so it can have more analytics about how users interact with your site.

To override the script that gets injected, take a look in the [editing the script view](#editing-the-script-view) section.

#### `manual`

Expand All @@ -248,8 +250,6 @@ Since the frontend won't have nothing injected, this mode it gives you freedom t
* manually include the `recaptcha-inject` middleware only in the routes you want,
* or include the `recaptcha::script` blade template in your layouts you want.

Take a look in the [editing the script view](#editing-the-script-view) section.

> The manual mode is very handy if your responses have a lot of data and want better performance, because the middleware won't look into the responses.
### Enable on Local Environment
Expand Down Expand Up @@ -298,7 +298,7 @@ Route::post('{product]/review')

The Google reCAPTCHA library underneath allows to make the request to the reCAPTCHA servers using a custom "Request Method". The `request_method` key accepts the Class you want to instance.

The default `null` value is enough for any normal application, but you're free to, for example, create your own logic or use the classes included in the [ReCaptcha package](https://github.com/google/recaptcha/tree/master/src/ReCaptcha/RequestMethod) (that this package requires). You can mimic this next example, were we will use Guzzle.
The default `null` value is enough for any normal application, but you're free to, for example, create your own logic or use the classes included in the [ReCaptcha package](https://github.com/google/recaptcha/tree/master/src/ReCaptcha/RequestMethod) (that this package requires).

```php
<?php
Expand All @@ -311,6 +311,8 @@ return [
];
```

You can mimic this next example were we will use Guzzle.

#### Example implementation

First, we will create our `GuzzleRequestMethod` with the `submit()` method as required. This method will return the reCAPTCHA response from the external server using the Guzzle Client.
Expand Down
2 changes: 1 addition & 1 deletion src/CaptchavelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected function bootMiddleware()
protected function shouldEnableMiddleware()
{
return $this->app->environment('production')
|| $this->app->environment('local') && $this->app->make('config')->get('captchavel.enable_local');
|| ($this->app->environment('local') && $this->app->make('config')->get('captchavel.enable_local'));
}

/**
Expand Down
18 changes: 11 additions & 7 deletions src/Http/Middleware/InjectRecaptchaScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class InjectRecaptchaScript
/**
* InjectRecaptchaScript constructor.
*
* @param \Illuminate\Contracts\Config\Repository $config
* @param \Illuminate\Contracts\View\Factory $view
* @param \Illuminate\Contracts\Config\Repository $config
* @param \Illuminate\Contracts\View\Factory $view
*/
public function __construct(Config $config, View $view)
{
Expand All @@ -39,8 +39,8 @@ public function __construct(Config $config, View $view)
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
* @throws \Throwable
* @throws \\Symfony\Component\HttpKernel\Exception\HttpException
Expand All @@ -65,21 +65,25 @@ public function handle($request, Closure $next)
*/
protected function isHtml(Request $request, $response)
{
return $response instanceof Response && $request->acceptsHtml() && !$request->ajax() && !$request->pjax();
return $response instanceof Response
&& $request->acceptsHtml()
&& ! $request->ajax()
&& ! $request->pjax()
&& ! $response->exception;
}

/**
* Injects the front-end Scripts
*
* @param \Illuminate\Http\Response $response
* @param \Illuminate\Http\Response $response
* @return \Illuminate\Http\Response
*/
protected function injectScript(Response $response)
{
// To inject the script automatically, we will do it before the ending
// head tag. If it's not found, the response may not be valid HTML,
// so we will bail out returning the original untouched content.
if (!$endHeadPosition = stripos($content = $response->content(), '</head>')) {
if (! $endHeadPosition = stripos($content = $response->content(), '</head>')) {
return $response;
};

Expand Down
20 changes: 8 additions & 12 deletions tests/Middleware/InjectRecaptchaScriptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,7 @@

namespace DarkGhostHunter\Captchavel\Tests;

use DarkGhostHunter\Captchavel\Exceptions\FailedRecaptchaException;
use DarkGhostHunter\Captchavel\Exceptions\InvalidCaptchavelMiddlewareMethod;
use DarkGhostHunter\Captchavel\Exceptions\InvalidRecaptchaException;
use DarkGhostHunter\Captchavel\Http\Middleware\CheckRecaptcha;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Orchestra\Testbench\TestCase;
use ReCaptcha\ReCaptcha;
use ReCaptcha\RequestMethod;
use ReCaptcha\RequestParameters;
use ReCaptcha\Response;

class InjectRecaptchaScriptTest extends TestCase
{
Expand Down Expand Up @@ -109,4 +97,12 @@ public function testDoesntInjectsOnAjax()
->assertDontSee('api.js?render=test-key&onload=captchavelCallback');
}

public function testDoesntInjectsOnException()
{
$response = $this->get('route-doesnt-exists-will-trigger-exception');

$response->assertDontSee('Start Captchavel Script')
->assertDontSee('api.js?render=test-key&onload=captchavelCallback');
}

}
18 changes: 17 additions & 1 deletion tests/ServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,26 @@ public function testDoesntRegisterInjectMiddlewareOnNonAuto()
);
}

public function testRegisterMiddlewareOnLocalTrue()
{
$this->app['env'] = 'local';
$this->app['config']->set('captchavel.enable_local', true);

/** @var CaptchavelServiceProvider $provider */
$provider = $this->app->make(CaptchavelServiceProvider::class, ['app' => $this->app]);

$provider->boot();

/** @var \Illuminate\Routing\Router $router */
$router = $this->app->make('router');

$this->assertEquals(CheckRecaptcha::class, $router->getMiddleware()['recaptcha']);
}

public function testPublishesConfigFile()
{
$this->artisan('vendor:publish', [
'--provider' => 'DarkGhostHunter\Captchavel\CaptchavelServiceProvider'
'--provider' => CaptchavelServiceProvider::class
]);

$this->assertFileExists(config_path('captchavel.php'));
Expand Down

0 comments on commit a49c6bd

Please sign in to comment.