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

Commit

Permalink
Reworked middleware.
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkGhostHunter committed Apr 9, 2021
1 parent 8515a30 commit 61f5ce9
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 132 deletions.
4 changes: 3 additions & 1 deletion src/Captchavel.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ public function __construct(Factory $http, Repository $config)
*/
public function getChallenge(string $challenge, string $ip, string $version): ReCaptchaResponse
{
$response = $this->send($challenge, $ip, $this->useCredentials($version))->setVersion($version)->setAsResolved();
$response = $this->send($challenge, $ip, $this->useCredentials($version))
->setVersion($version)
->setAsResolved();

Container::getInstance()->instance(ReCaptchaResponse::class, $response);

Expand Down
6 changes: 3 additions & 3 deletions src/CaptchavelFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ class CaptchavelFake extends Captchavel
/**
* Resolves a reCAPTCHA challenge.
*
* @param string $challenge
* @param string|null $challenge
* @param string $ip
* @param string|null $version
* @param string $version
*
* @return \DarkGhostHunter\Captchavel\Http\ReCaptchaResponse
*/
public function getChallenge(string $challenge, string $ip, string $version = null): ReCaptchaResponse
public function getChallenge(?string $challenge = null, string $ip, string $version): ReCaptchaResponse
{
return (new ReCaptchaResponse(
[
Expand Down
1 change: 0 additions & 1 deletion src/Facades/Captchavel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ protected static function getFacadeAccessor(): string
* Returns a new Captchavel service to fake responses.
*
* @return \DarkGhostHunter\Captchavel\CaptchavelFake
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public static function fake(): CaptchavelFake
{
Expand Down
26 changes: 26 additions & 0 deletions src/Http/Middleware/ChecksCaptchavelStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace DarkGhostHunter\Captchavel\Http\Middleware;

trait ChecksCaptchavelStatus
{
/**
* Determines if the reCAPTCHA verification should be enabled.
*
* @return bool
*/
protected function isEnabled(): bool
{
return $this->config->get('captchavel.enable');
}

/**
* Check if the reCAPTCHA response should be faked on-demand.
*
* @return bool
*/
protected function isFake(): bool
{
return $this->config->get('captchavel.fake');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,12 @@
namespace DarkGhostHunter\Captchavel\Http\Middleware;

use DarkGhostHunter\Captchavel\Captchavel;
use DarkGhostHunter\Captchavel\CaptchavelFake;
use DarkGhostHunter\Captchavel\Http\ReCaptchaResponse;
use Illuminate\Config\Repository;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

abstract class BaseReCaptchaMiddleware
trait ValidatesRequestAndResponse
{
/**
* Captchavel connector.
*
* @var \DarkGhostHunter\Captchavel\Captchavel|\DarkGhostHunter\Captchavel\CaptchavelFake
*/
protected Captchavel $captchavel;

/**
* Application Config repository.
*
* @var \Illuminate\Config\Repository
*/
protected Repository $config;

/**
* BaseReCaptchaMiddleware constructor.
*
* @param \DarkGhostHunter\Captchavel\Captchavel $captchavel
* @param \Illuminate\Config\Repository $config
*/
public function __construct(Captchavel $captchavel, Repository $config)
{
$this->config = $config;
$this->captchavel = $captchavel;
}

/**
* Determines if the reCAPTCHA verification should be enabled.
*
* @return bool
*/
protected function isEnabled(): bool
{
return $this->config->get('captchavel.enable');
}

/**
* Check if the reCAPTCHA response should be faked on-demand.
*
* @return bool
*/
protected function isFake(): bool
{
return $this->config->get('captchavel.fake');
}

/**
* Check if the reCAPTCHA response must be real.
*
* @return bool
*/
protected function isReal(): bool
{
return !$this->isFake();
}

/**
* Validate if this Request has the reCAPTCHA challenge string.
*
Expand All @@ -86,34 +28,18 @@ protected function validateRequest(Request $request, string $input): void
}

/**
* Retrieves the Captchavel response from reCAPTCHA servers.
* Creates a new Validation Exception instance.
*
* @param \Illuminate\Http\Request $request
* @param string $input
* @param string $version
* @param string $message
*
* @return \DarkGhostHunter\Captchavel\Http\ReCaptchaResponse
* @return \Illuminate\Validation\ValidationException
*/
protected function retrieveChallenge(Request $request, string $input, string $version): ReCaptchaResponse
protected function validationException(string $input, string $message): ValidationException
{
return $this->captchavel->getChallenge($request->input($input), $request->ip(), $version);
return ValidationException::withMessages([$input => trans($message)])->redirectTo(back()->getTargetUrl());
}

/**
* Fakes a score reCAPTCHA response.
*
* @param \Illuminate\Http\Request $request
*
* @return void
*/
protected function fakeResponseScore(Request $request): void
{
if ($this->captchavel instanceof CaptchavelFake && null === $this->captchavel->score) {
$request->filled('is_robot')
? $this->captchavel->fakeRobots()
: $this->captchavel->fakeHumans();
}
}

/**
* Validate the Hostname and APK name from the response.
Expand Down Expand Up @@ -157,17 +83,4 @@ protected function validateResponse(
);
}
}

/**
* Creates a new Validation Exception instance.
*
* @param string $input
* @param string $message
*
* @return \Illuminate\Validation\ValidationException
*/
protected function validationException(string $input, string $message): ValidationException
{
return ValidationException::withMessages([$input => trans($message)])->redirectTo(back()->getTargetUrl());
}
}
39 changes: 36 additions & 3 deletions src/Http/Middleware/VerifyReCaptchaV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,40 @@

use Closure;
use DarkGhostHunter\Captchavel\Captchavel;
use Illuminate\Config\Repository;
use Illuminate\Http\Request;

class VerifyReCaptchaV2 extends BaseReCaptchaMiddleware
class VerifyReCaptchaV2
{
use ChecksCaptchavelStatus;
use ValidatesRequestAndResponse;

/**
* Captchavel connector.
*
* @var \DarkGhostHunter\Captchavel\Captchavel|\DarkGhostHunter\Captchavel\CaptchavelFake
*/
protected Captchavel $captchavel;

/**
* Application Config repository.
*
* @var \Illuminate\Config\Repository
*/
protected Repository $config;

/**
* BaseReCaptchaMiddleware constructor.
*
* @param \DarkGhostHunter\Captchavel\Captchavel $captchavel
* @param \Illuminate\Config\Repository $config
*/
public function __construct(Captchavel $captchavel, Repository $config)
{
$this->config = $config;
$this->captchavel = $captchavel;
}

/**
* Handle the incoming request.
*
Expand All @@ -21,9 +51,12 @@ class VerifyReCaptchaV2 extends BaseReCaptchaMiddleware
*/
public function handle(Request $request, Closure $next, string $version, string $input = Captchavel::INPUT)
{
if ($this->isEnabled() && $this->isReal()) {
if ($this->isEnabled()) {
$this->validateRequest($request, $input);
$this->validateResponse($this->retrieveChallenge($request, $input, $version), $input);
$this->validateResponse(
$this->captchavel->getChallenge($request->input($input), $request->ip(), $version),
$input
);
}

return $next($request);
Expand Down
73 changes: 49 additions & 24 deletions src/Http/Middleware/VerifyReCaptchaV3.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,41 @@
use DarkGhostHunter\Captchavel\CaptchavelFake;
use DarkGhostHunter\Captchavel\Facades\Captchavel as CaptchavelFacade;
use DarkGhostHunter\Captchavel\Http\ReCaptchaResponse;
use Illuminate\Config\Repository;
use Illuminate\Container\Container;
use Illuminate\Http\Request;

class VerifyReCaptchaV3 extends BaseReCaptchaMiddleware
class VerifyReCaptchaV3
{
use ChecksCaptchavelStatus;
use ValidatesRequestAndResponse;

/**
* Captchavel connector.
*
* @var \DarkGhostHunter\Captchavel\Captchavel|\DarkGhostHunter\Captchavel\CaptchavelFake
*/
protected Captchavel $captchavel;

/**
* Application Config repository.
*
* @var \Illuminate\Config\Repository
*/
protected Repository $config;

/**
* BaseReCaptchaMiddleware constructor.
*
* @param \DarkGhostHunter\Captchavel\Captchavel $captchavel
* @param \Illuminate\Config\Repository $config
*/
public function __construct(Captchavel $captchavel, Repository $config)
{
$this->config = $config;
$this->captchavel = $captchavel;
}

/**
* Handle the incoming request.
*
Expand All @@ -32,59 +62,54 @@ public function handle(Request $request,
)
{
if ($this->isEnabled()) {
if ($this->isReal()) {
$this->validateRequest($request, $input);
} else {
$this->ensureFakeCaptchavel();
if ($this->isFake()) {
$this->fakeResponseScore($request);
$this->prepareRequestForFaking($request, $input);
} else {
$this->validateRequest($request, $input);
}

$this->processChallenge($request, $threshold, $action, $input);
$this->processChallenge($request, $input, $threshold, $action);
}

return $next($request);
}

/**
* Ensure we're using Captchavel Fake.
* Fakes a score reCAPTCHA response.
*
* @param \Illuminate\Http\Request $request
*
* @return void
*/
protected function ensureFakeCaptchavel(): void
protected function fakeResponseScore(Request $request): void
{
if (! $this->captchavel instanceof CaptchavelFake) {
$this->captchavel = CaptchavelFacade::fake();
}
}

/**
* Prepare the Request to with a fake challenge input.
*
* @param \Illuminate\Http\Request $request
* @param string $input
*/
protected function prepareRequestForFaking(Request $request, string $input)
{
if ($request->missing($input)) {
$request->merge([$input => 'fake_challenge_input']);
// If the Captchavel has set an score to fake, use it, otherwise go default.
if ($this->captchavel->score === null) {
$request->filled('is_robot') ? $this->captchavel->fakeRobots() : $this->captchavel->fakeHumans();
}
}

/**
* Process the response from reCAPTCHA servers.
*
* @param \Illuminate\Http\Request $request
* @param string $input
* @param null|string $threshold
* @param null|string $action
* @param string $input
*
* @throws \Illuminate\Validation\ValidationException
*/
protected function processChallenge(Request $request, ?string $threshold, ?string $action, string $input)
protected function processChallenge(Request $request, string $input, ?string $threshold, ?string $action)
{
$response = $this->retrieveChallenge($request, $input, Captchavel::SCORE)
->setThreshold($this->normalizeThreshold($threshold));
$response = $this->captchavel->getChallenge(
$request->input($input),
$request->ip(),
Captchavel::SCORE
)->setThreshold($this->normalizeThreshold($threshold));

$this->validateResponse($response, $input, $this->normalizeAction($action));

Expand Down
Loading

0 comments on commit 61f5ce9

Please sign in to comment.