Revived API POST /api/v1/captcha.
Captcha validation has been adjusted.
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
|
68
app/Http/Requests/Api/V1/Captcha/CheckingRequest.php
Normal file
68
app/Http/Requests/Api/V1/Captcha/CheckingRequest.php
Normal 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'),
|
||||
);
|
||||
}
|
||||
}
|
28
app/Http/Resources/Api/V1/CaptchaVerified.php
Normal file
28
app/Http/Resources/Api/V1/CaptchaVerified.php
Normal 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()
|
||||
];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user