Revived API /api/v1/captcha.
Now a new captcha is created to check for a bot.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Requests\Api\V1\Captcha\CaptchaRequest;
|
||||
use App\Http\Resources\Api\V1\Captcha;
|
||||
use App\Services\Api\V1\CaptchaService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
@@ -12,9 +13,11 @@ final class CaptchaController extends Controller
|
||||
private readonly CaptchaService $captchaService
|
||||
) { }
|
||||
|
||||
public function getCaptcha(): JsonResponse
|
||||
public function getCaptcha(CaptchaRequest $request): JsonResponse
|
||||
{
|
||||
$result = $this->captchaService->generate();
|
||||
$captchaPublicToken = $request->getDto();
|
||||
$expires = now()->addSeconds(config('captcha.waiting_for_captcha_verification_in_seconds'));
|
||||
$result = $this->captchaService->createKeyWithCaptcha($captchaPublicToken, $expires);
|
||||
if (!$result->isSuccess()) {
|
||||
return response()->json($result->getData())->setStatusCode($result->getCode());
|
||||
}
|
||||
|
57
app/Http/Requests/Api/V1/Captcha/CaptchaRequest.php
Normal file
57
app/Http/Requests/Api/V1/Captcha/CaptchaRequest.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Api\V1\Captcha;
|
||||
|
||||
use App\Contracts\FormRequestDto;
|
||||
use App\Dto\HttpUserData;
|
||||
use App\Dto\Request\Api\V1\Captcha\CaptchaPublicToken;
|
||||
use App\Models\CaptchaToken;
|
||||
use App\Repositories\CaptchaTokenRepository;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
final class CaptchaRequest extends FormRequest implements FormRequestDto
|
||||
{
|
||||
private readonly CaptchaToken $captchaToken;
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(CaptchaTokenRepository $captchaTokenRepository): bool
|
||||
{
|
||||
if (!$this->hasHeader('public-token')) {
|
||||
return false;
|
||||
}
|
||||
$captchaToken = $captchaTokenRepository->getCaptchaTokenByPublicToken($this->header('public-token'));
|
||||
if (is_null($captchaToken)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->captchaToken = $captchaToken;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
|
||||
];
|
||||
}
|
||||
|
||||
public function getDto(): CaptchaPublicToken
|
||||
{
|
||||
$httpUserData = new HttpUserData(
|
||||
$this->getClientIp(),
|
||||
$this->userAgent(),
|
||||
$this->header('referer')
|
||||
);
|
||||
|
||||
return new CaptchaPublicToken(
|
||||
$this->captchaToken,
|
||||
$httpUserData
|
||||
);
|
||||
}
|
||||
}
|
@@ -23,9 +23,17 @@ final class Captcha extends JsonResource
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'image_base64' => $this->resource->getImageBase64(),
|
||||
'image_text_base64' => $this->resource->getImageTextBase64(),
|
||||
'captcha_key' => $this->resource->getKey()
|
||||
'image_head' => [
|
||||
'base64' => $this->resource->getImageHead()->getImageBase64(),
|
||||
'width' => $this->resource->getImageHead()->getWidth(),
|
||||
'height' => $this->resource->getImageHead()->getHeight(),
|
||||
],
|
||||
'image_body' => [
|
||||
'base64' => $this->resource->getImageBody()->getImageBase64(),
|
||||
'width' => $this->resource->getImageBody()->getWidth(),
|
||||
'height' => $this->resource->getImageBody()->getHeight(),
|
||||
],
|
||||
'captcha_key' => $this->resource->getKey()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user