Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| IPLockout | |
100.00% |
11 / 11 |
|
100.00% |
3 / 3 |
3 | |
100.00% |
1 / 1 |
| logFailedAttempt | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| isAllowed | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| getLockoutStorage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2020 (original work) (update and modification) Open Assessment Technologies SA |
| 19 | * (under the project TAO-PRODUCT) |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | declare(strict_types=1); |
| 24 | |
| 25 | namespace oat\tao\model\oauth\lockout; |
| 26 | |
| 27 | use oat\oatbox\service\ConfigurableService; |
| 28 | use oat\oatbox\service\exception\InvalidService; |
| 29 | use oat\oatbox\service\exception\InvalidServiceManagerException; |
| 30 | use oat\tao\model\oauth\lockout\storage\LockoutStorageInterface; |
| 31 | |
| 32 | /** |
| 33 | * Lock based on IP |
| 34 | * @package oat\tao\model\oauth\lockout |
| 35 | */ |
| 36 | class IPLockout extends ConfigurableService implements LockoutInterface |
| 37 | { |
| 38 | /** Storage to store failed attempts */ |
| 39 | public const OPTION_LOCKOUT_STORAGE = 'storage'; |
| 40 | /** something that will return ip */ |
| 41 | public const OPTION_IP_FACTORY = 'IPFactory'; |
| 42 | /** Maximum failed attempt count */ |
| 43 | public const OPTION_THRESHOLD = 'threshold'; |
| 44 | /** Block time after last failed attempt when threshold reached */ |
| 45 | public const OPTION_TIMEOUT = 'timeout'; |
| 46 | /** |
| 47 | * Flags to get client ip. |
| 48 | * Default flags\order : HTTP_CLIENT_IP, HTTP_X_FORWARDED_FOR, HTTP_X_FORWARDED_FOR, REMOTE_ADDR |
| 49 | */ |
| 50 | public const OPTION_SERVER_IP_FLAGS = 'SERVER_IP_FLAGS'; |
| 51 | |
| 52 | /** |
| 53 | * @throws InvalidService |
| 54 | * @throws InvalidServiceManagerException |
| 55 | */ |
| 56 | public function logFailedAttempt(): void |
| 57 | { |
| 58 | $this->getLockoutStorage()->store( |
| 59 | $this->getOption(self::OPTION_IP_FACTORY)->create(), |
| 60 | $this->getOption(self::OPTION_TIMEOUT) |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @return bool |
| 66 | * @throws InvalidService |
| 67 | * @throws InvalidServiceManagerException |
| 68 | */ |
| 69 | public function isAllowed(): bool |
| 70 | { |
| 71 | $failedAttempts = $this->getLockoutStorage() |
| 72 | ->getFailedAttempts( |
| 73 | $this->getOption(self::OPTION_IP_FACTORY)->create(), |
| 74 | $this->getOption(self::OPTION_TIMEOUT) |
| 75 | ); |
| 76 | return $failedAttempts < $this->getOption(self::OPTION_THRESHOLD); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @return LockoutStorageInterface |
| 81 | * @throws InvalidService |
| 82 | * @throws InvalidServiceManagerException |
| 83 | */ |
| 84 | protected function getLockoutStorage(): LockoutStorageInterface |
| 85 | { |
| 86 | return $this->getSubService(self::OPTION_LOCKOUT_STORAGE); |
| 87 | } |
| 88 | } |