|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/** |
| 3 | + * @link https://github.com/monarc-project for the canonical source repository |
| 4 | + * @copyright Copyright (c) 2016-2025 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3 |
| 5 | + * @license MONARC is licensed under GNU Affero General Public License version 3 |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Monarc\FrontOffice\Service; |
| 9 | + |
| 10 | +use Laminas\Captcha\Image as CaptchaImage; |
| 11 | +use Laminas\Session\Container; |
| 12 | +use Monarc\Core\Entity\ActionHistorySuperClass; |
| 13 | +use Monarc\Core\Service\ConfigService; |
| 14 | + |
| 15 | +final class CaptchaService |
| 16 | +{ |
| 17 | + private const DEFAULT_FAILED_LOGIN_ATTEMPTS = 3; |
| 18 | + |
| 19 | + private array $params; |
| 20 | + |
| 21 | + public function __construct(private ActionHistoryService $actionHistoryService, ConfigService $config) |
| 22 | + { |
| 23 | + $this->params = $config->getCaptchaConfig(); |
| 24 | + } |
| 25 | + |
| 26 | + public function isActivated(): bool |
| 27 | + { |
| 28 | + $isEnabled = (bool)($this->params['enabled'] ?? false); |
| 29 | + if (!$isEnabled) { |
| 30 | + return false; |
| 31 | + } |
| 32 | + |
| 33 | + $failedLoginAttemptsLimit = (int)($this->params['failedLoginAttempts'] ?? self::DEFAULT_FAILED_LOGIN_ATTEMPTS); |
| 34 | + if ($failedLoginAttemptsLimit > 0) { |
| 35 | + /* Login attempt number validation from the logs. */ |
| 36 | + $lastLoginsHistory = $this->actionHistoryService->getActionsHistoryByAction( |
| 37 | + ActionHistorySuperClass::ACTION_LOGIN_ATTEMPT, |
| 38 | + $failedLoginAttemptsLimit |
| 39 | + ); |
| 40 | + if (\count($lastLoginsHistory) < $failedLoginAttemptsLimit) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + foreach ($lastLoginsHistory as $lastLoginHistory) { |
| 44 | + if ($lastLoginHistory->getStatus() === ActionHistorySuperClass::STATUS_SUCCESS) { |
| 45 | + return false; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return true; |
| 51 | + } |
| 52 | + |
| 53 | + public function generate(): array |
| 54 | + { |
| 55 | + $captcha = new CaptchaImage($this->params['params']); |
| 56 | + |
| 57 | + $captchaId = $captcha->generate(); |
| 58 | + |
| 59 | + /* Store the captcha ID for validation. */ |
| 60 | + $session = new Container('captcha'); |
| 61 | + $session->offsetSet('captchaId', $captchaId); |
| 62 | + |
| 63 | + return [ |
| 64 | + 'captchaId' => $captchaId, |
| 65 | + 'captchaUrl' => $captcha->getImgUrl() . $captcha->getId() . $captcha->getSuffix(), |
| 66 | + ]; |
| 67 | + } |
| 68 | + |
| 69 | + public function isValid(string $captchaId, string $inputText): bool |
| 70 | + { |
| 71 | + $captcha = new CaptchaImage($this->params['params']); |
| 72 | + |
| 73 | + $session = new Container('captcha'); |
| 74 | + $storedCaptchaId = $session->offsetGet('captchaId'); |
| 75 | + if ($captchaId !== $storedCaptchaId) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + return $captcha->isValid(['input' => $inputText, 'id' => $storedCaptchaId]); |
| 80 | + } |
| 81 | +} |
0 commit comments