Revived API POST /api/v1/captcha.

Captcha validation has been adjusted.
This commit is contained in:
2023-11-26 15:09:42 +06:00
parent 18899c81f2
commit 520a3ba068
21 changed files with 365 additions and 12 deletions

View File

@@ -3,7 +3,9 @@
namespace App\Http\Controllers\Api\V1;
use App\Http\Requests\Api\V1\Captcha\CaptchaRequest;
use App\Http\Requests\Api\V1\Captcha\CheckingRequest;
use App\Http\Resources\Api\V1\Captcha;
use App\Http\Resources\Api\V1\CaptchaVerified;
use App\Services\Api\V1\CaptchaService;
use Illuminate\Http\JsonResponse;
@@ -24,4 +26,16 @@ final class CaptchaController extends Controller
return response()->json(new Captcha($result));
}
public function checking(CheckingRequest $request): JsonResponse
{
$maxCountError = config('captcha.validate_max_count_errors');
$params = $request->getDto();
$result = $this->captchaService->checking($params, $maxCountError);
if (!$result->isSuccess()) {
return response()->json($result->getData())->setStatusCode($result->getCode());
}
return response()->json(new CaptchaVerified($result));
}
}

View File

@@ -0,0 +1,68 @@
<?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\Dto\Request\Api\V1\Captcha\CheckingDto;
use App\Models\CaptchaToken;
use App\Repositories\CaptchaTokenRepository;
use Illuminate\Foundation\Http\FormRequest;
final class CheckingRequest 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 [
'captcha_key' => ['required', 'string', 'max:75'],
'verification' => ['required', 'array'],
'verification.*' => ['required', 'array', 'size:2'],
'verification.*.x' => ['required', 'numeric', 'min:0'],
'verification.*.y' => ['required', 'numeric', 'min:0'],
];
}
public function getDto(): CheckingDto
{
$httpUserData = new HttpUserData(
$this->getClientIp(),
$this->userAgent(),
$this->header('referer')
);
$captchaPublicToken = new CaptchaPublicToken(
$this->captchaToken,
$httpUserData
);
return new CheckingDto(
captchaPublicToken: $captchaPublicToken,
captchaKey: $this->input('captcha_key'),
coordinators: $this->input('verification'),
);
}
}

View File

@@ -0,0 +1,28 @@
<?php declare(strict_types=1);
namespace App\Http\Resources\Api\V1;
use App\ServiceResults\Api\V1\CaptchaService\CaptchaVerifiedResult;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
final class CaptchaVerified extends JsonResource
{
/**
* @var CaptchaVerifiedResult
*/
public $resource;
/**
* Transform the resource into an array.
*
* @param Request $request
* @return array
*/
public function toArray(Request $request): array
{
return [
'captcha_key' => $this->resource->getKey()
];
}
}