Revived API POST /captcha/{captcha_uuid}.

Receiving captcha information for validation.
This commit is contained in:
2023-11-26 22:42:22 +06:00
parent 10425db5e0
commit 9221e089dd
17 changed files with 290 additions and 5 deletions

View File

@@ -4,11 +4,13 @@ namespace App\Services\Api\V1;
use App\Dto\Request\Api\V1\Captcha\CaptchaPublicToken;
use App\Dto\Request\Api\V1\Captcha\CheckingDto;
use App\Dto\Request\Api\V1\Captcha\VerificationInformationDto;
use App\Enums\CaptchaLogType;
use App\Repositories\CaptchaLogRepository;
use App\Repositories\CaptchaRepository;
use App\Repositories\DataCaptchaRepository;
use App\ServiceResults\Api\V1\CaptchaService\Captcha;
use App\ServiceResults\Api\V1\CaptchaService\CaptchaVerificationInformationResult;
use App\ServiceResults\Api\V1\CaptchaService\CaptchaVerifiedResult;
use App\ServiceResults\ServiceResultError;
use App\Services\Captcha\CaptchaHandler;
@@ -17,6 +19,7 @@ use App\Services\CaptchaLog\CaptchaLogHandler;
use App\Services\Service;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
final class CaptchaService extends Service
{
@@ -90,4 +93,54 @@ final class CaptchaService extends Service
return $result;
}
public function verificationInformation(string $captchaUuid, VerificationInformationDto $verificationInformationDto, int $expiresMinutes, int $maxInfoDisplayCount = 1): ServiceResultError | CaptchaVerificationInformationResult
{
try {
$captcha = $this->captchaRepository->getCaptchaByUuid($verificationInformationDto->getCaptchaToken(), $captchaUuid);
if (is_null($captcha)) {
return $this->errNotFound(__('Captcha not found'));
}
$captchaLogs = $this->captchaLogRepository->getCaptchaLogsByTypes([CaptchaLogType::Verified, CaptchaLogType::ReadVerified], $captcha->id);
$this->captchaLogHandler->handleStore($captcha->id, CaptchaLogType::ReadVerified, $verificationInformationDto->getHttpUserData());
$captchaVerificationLog = $captchaLogs->firstWhere('type', CaptchaLogType::Verified);
if (is_null($captchaVerificationLog)) {
return new CaptchaVerificationInformationResult(
status: false,
message: __('Captcha does not pass verification'),
);
}
if (!is_null($verificationInformationDto->getUserAgent()) && Str::limit($verificationInformationDto->getUserAgent(), 255, '') !== $captchaVerificationLog->user_agent) {
return new CaptchaVerificationInformationResult(
status: false,
message: __('Captcha User Agent does not match'),
);
}
if (now()->diffInMinutes($captchaVerificationLog->created_at) > $expiresMinutes) {
return new CaptchaVerificationInformationResult(
status: false,
message: __('The time for captcha verification has passed'),
);
}
if ($captchaLogs->where('type', CaptchaLogType::ReadVerified)->count() >= $maxInfoDisplayCount) {
return new CaptchaVerificationInformationResult(
status: false,
message: __('Captcha verification status view count exceeded'),
);
}
} catch (\Throwable $e) {
report($e);
return $this->errService('Captcha service error!');
}
return new CaptchaVerificationInformationResult(
status: true,
message: __('Captcha Verified'),
);
}
}