add all controllers logic
This commit is contained in:
@@ -4,6 +4,14 @@ namespace App\Services\Auth;
|
||||
|
||||
class RegistrationCaptchaService
|
||||
{
|
||||
private function cacheKey(): string
|
||||
{
|
||||
$ip = (string) (request()?->ip() ?? 'unknown');
|
||||
$ua = (string) (request()?->header('User-Agent') ?? '');
|
||||
$hash = substr(sha1($ua), 0, 12);
|
||||
return 'captcha:' . $ip . ':' . $hash;
|
||||
}
|
||||
|
||||
public function generate(?int $length = null): string
|
||||
{
|
||||
$length = $length ?? random_int(4, 8);
|
||||
@@ -14,14 +22,14 @@ class RegistrationCaptchaService
|
||||
$captchaText .= $characters[random_int(0, strlen($characters) - 1)];
|
||||
}
|
||||
|
||||
session()->put('captcha_answer', $captchaText);
|
||||
cache()->put($this->cacheKey(), $captchaText, now()->addMinutes(10));
|
||||
|
||||
return $captchaText;
|
||||
}
|
||||
|
||||
public function verify(string $input): bool
|
||||
{
|
||||
$expected = (string) session()->get('captcha_answer', '');
|
||||
$expected = (string) cache()->get($this->cacheKey(), '');
|
||||
if ($expected === '') {
|
||||
return false;
|
||||
}
|
||||
@@ -31,6 +39,6 @@ class RegistrationCaptchaService
|
||||
|
||||
public function clear(): void
|
||||
{
|
||||
session()->forget('captcha_answer');
|
||||
cache()->forget($this->cacheKey());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user