Revived API POST /captcha/{captcha_uuid}.
Receiving captcha information for validation.
This commit is contained in:
		@@ -4,7 +4,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\Requests\Api\V1\Captcha\VerificationInformationRequest;
 | 
			
		||||
use App\Http\Resources\Api\V1\Captcha;
 | 
			
		||||
use App\Http\Resources\Api\V1\CaptchaVerificationInformation;
 | 
			
		||||
use App\Http\Resources\Api\V1\CaptchaVerified;
 | 
			
		||||
use App\Services\Api\V1\CaptchaService;
 | 
			
		||||
use Illuminate\Http\JsonResponse;
 | 
			
		||||
@@ -38,4 +40,18 @@ final class CaptchaController extends Controller
 | 
			
		||||
 | 
			
		||||
        return response()->json(new CaptchaVerified($result));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function verificationInformation(string $captchaUuid, VerificationInformationRequest $request): JsonResponse
 | 
			
		||||
    {
 | 
			
		||||
        $params = $request->getDto();
 | 
			
		||||
        $expiresMinutes = config('captcha.verification_data_view_limit_in_minutes');
 | 
			
		||||
        $maxInfoDisplayCount = config('captcha.max_info_display_count');
 | 
			
		||||
        $result = $this->captchaService->verificationInformation($captchaUuid, $params, $expiresMinutes, $maxInfoDisplayCount);
 | 
			
		||||
 | 
			
		||||
        if (!$result->isSuccess()) {
 | 
			
		||||
            return response()->json($result->getData())->setStatusCode($result->getCode());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return response()->json(new CaptchaVerificationInformation($result));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,58 @@
 | 
			
		||||
<?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\VerificationInformationDto;
 | 
			
		||||
use App\Models\CaptchaToken;
 | 
			
		||||
use App\Repositories\CaptchaTokenRepository;
 | 
			
		||||
use Illuminate\Foundation\Http\FormRequest;
 | 
			
		||||
 | 
			
		||||
final class VerificationInformationRequest 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('private-token')) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        $captchaToken = $captchaTokenRepository->getCaptchaTokenByPrivateToken($this->header('private-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 [
 | 
			
		||||
            'user_agent' => ['nullable', 'string'],
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getDto(): VerificationInformationDto
 | 
			
		||||
    {
 | 
			
		||||
        $httpUserData = new HttpUserData(
 | 
			
		||||
            $this->getClientIp(),
 | 
			
		||||
            $this->userAgent(),
 | 
			
		||||
            $this->header('referer')
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        return new VerificationInformationDto(
 | 
			
		||||
            captchaToken: $this->captchaToken,
 | 
			
		||||
            httpUserData: $httpUserData,
 | 
			
		||||
            userAgent: $this->input('user_agent', null),
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										30
									
								
								app/Http/Resources/Api/V1/CaptchaVerificationInformation.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								app/Http/Resources/Api/V1/CaptchaVerificationInformation.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
<?php declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace App\Http\Resources\Api\V1;
 | 
			
		||||
 | 
			
		||||
use Illuminate\Http\Request;
 | 
			
		||||
use Illuminate\Http\Resources\Json\JsonResource;
 | 
			
		||||
 | 
			
		||||
final class CaptchaVerificationInformation extends JsonResource
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * @var \App\ServiceResults\Api\V1\CaptchaService\CaptchaVerificationInformationResult
 | 
			
		||||
     */
 | 
			
		||||
    public $resource;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Transform the resource into an array.
 | 
			
		||||
     *
 | 
			
		||||
     * @param  Request $request
 | 
			
		||||
     * @return array
 | 
			
		||||
     */
 | 
			
		||||
    public function toArray(Request $request): array
 | 
			
		||||
    {
 | 
			
		||||
        $info = [];
 | 
			
		||||
 | 
			
		||||
        return [
 | 
			
		||||
            'status' => $this->resource->isStatus(),
 | 
			
		||||
            'message' => $this->resource->getMessage(),
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user