mirror of
https://github.com/pelican-dev/panel.git
synced 2025-06-09 11:38:58 +02:00
40 lines
1.1 KiB
PHP
40 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use App\Extensions\Captcha\CaptchaProvider;
|
|
use Closure;
|
|
use Illuminate\Foundation\Application;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use App\Events\Auth\FailedCaptcha;
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
|
|
readonly class VerifyCaptcha
|
|
{
|
|
public function __construct(private Application $app) {}
|
|
|
|
public function handle(Request $request, Closure $next, CaptchaProvider $captchaProvider): mixed
|
|
{
|
|
if ($this->app->isLocal()) {
|
|
return $next($request);
|
|
}
|
|
|
|
$schemas = $captchaProvider->getActiveSchemas();
|
|
foreach ($schemas as $schema) {
|
|
$response = $schema->validateResponse();
|
|
|
|
if ($response['success'] && $schema->verifyDomain($response['hostname'] ?? '', $request->url())) {
|
|
return $next($request);
|
|
}
|
|
|
|
event(new FailedCaptcha($request->ip(), $response['message'] ?? null));
|
|
|
|
throw new HttpException(Response::HTTP_BAD_REQUEST, "Failed to validate {$schema->getId()} captcha data.");
|
|
}
|
|
|
|
// No captcha enabled
|
|
return $next($request);
|
|
}
|
|
}
|