Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 53 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| RdsLockoutStorage | |
0.00% |
0 / 53 |
|
0.00% |
0 / 6 |
110 | |
0.00% |
0 / 1 |
| store | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
12 | |||
| getAddressInfo | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| getFailedAttempts | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| resetIp | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| getSchema | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getQueryBuilder | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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 | declare(strict_types=1); |
| 23 | |
| 24 | namespace oat\tao\model\oauth\lockout\storage; |
| 25 | |
| 26 | use common_persistence_SqlPersistence as Persistence; |
| 27 | use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
| 28 | use Doctrine\DBAL\Query\QueryBuilder; |
| 29 | use Doctrine\DBAL\Schema\Schema; |
| 30 | |
| 31 | /** |
| 32 | * Class RdsLockoutStorage |
| 33 | * |
| 34 | * @author Ivan Klimchuk <ivan@taotesting.com> |
| 35 | * @package oat\tao\model\oauth\lockout\storage |
| 36 | * |
| 37 | * @method Persistence getPersistence() |
| 38 | */ |
| 39 | class RdsLockoutStorage extends LockoutStorageAbstract |
| 40 | { |
| 41 | public const TABLE_NAME = 'oauth_lti_failures'; |
| 42 | |
| 43 | public const FIELD_ID = 'id'; |
| 44 | public const FIELD_ADDRESS = 'address'; |
| 45 | public const FIELD_EXPIRE_AT = 'expire_at'; |
| 46 | public const FIELD_ATTEMPTS = 'attempts'; |
| 47 | |
| 48 | /** |
| 49 | * @inheritDoc |
| 50 | */ |
| 51 | public function store(string $ip, int $ttl = 0): void |
| 52 | { |
| 53 | $id = ip2long($ip); |
| 54 | $expireAt = time() + $ttl; |
| 55 | $addressInfo = $this->getAddressInfo($id); |
| 56 | if (!$addressInfo) { |
| 57 | try { |
| 58 | $this->getPersistence()->insert( |
| 59 | self::TABLE_NAME, |
| 60 | [ |
| 61 | self::FIELD_ID => $id, |
| 62 | self::FIELD_ADDRESS => $ip, |
| 63 | self::FIELD_ATTEMPTS => 1, // first failed attempt |
| 64 | self::FIELD_EXPIRE_AT => $expireAt |
| 65 | ] |
| 66 | ); |
| 67 | |
| 68 | return; |
| 69 | } catch (UniqueConstraintViolationException $exception) { |
| 70 | $addressInfo = $this->getAddressInfo($id); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | $attempts = $addressInfo[self::FIELD_ATTEMPTS] + 1; |
| 75 | |
| 76 | $data = [ |
| 77 | 'conditions' => [self::FIELD_ID => $id], |
| 78 | 'updateValues' => [ |
| 79 | self::FIELD_EXPIRE_AT => $expireAt, |
| 80 | self::FIELD_ATTEMPTS => $attempts |
| 81 | ] |
| 82 | ]; |
| 83 | |
| 84 | $this->getPersistence()->updateMultiple(self::TABLE_NAME, [$data]); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @param int $id |
| 89 | * |
| 90 | * @return mixed |
| 91 | */ |
| 92 | protected function getAddressInfo(int $id) |
| 93 | { |
| 94 | $queryBuilder = $this->getQueryBuilder() |
| 95 | ->select('*') |
| 96 | ->from(self::TABLE_NAME) |
| 97 | ->where(sprintf('%s = ?', self::FIELD_ID)); |
| 98 | |
| 99 | $entries = $this->getPersistence()->query($queryBuilder->getSQL(), [$id])->fetchAll(); |
| 100 | |
| 101 | return reset($entries); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @param string $ip |
| 106 | * |
| 107 | * @param int $timeout |
| 108 | * @return int |
| 109 | */ |
| 110 | public function getFailedAttempts(string $ip, int $timeout): int |
| 111 | { |
| 112 | $attempts = 0; |
| 113 | $queryBuilder = $this->getQueryBuilder() |
| 114 | ->select('*') |
| 115 | ->from(self::TABLE_NAME) |
| 116 | ->where(sprintf('%s = ?', self::FIELD_ID)) |
| 117 | ->andWhere(sprintf('%s > ?', self::FIELD_EXPIRE_AT)); |
| 118 | |
| 119 | $found = $this->getPersistence() |
| 120 | ->query($queryBuilder->getSQL(), [ip2long($ip), time()]) |
| 121 | ->fetchAll(); |
| 122 | |
| 123 | if (count($found)) { |
| 124 | $found = reset($found); |
| 125 | if (time() > $found[self::FIELD_EXPIRE_AT]) { |
| 126 | $this->resetIp($ip); |
| 127 | } else { |
| 128 | $attempts = $found[self::FIELD_ATTEMPTS]; |
| 129 | } |
| 130 | } |
| 131 | return $attempts; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @param string $ip |
| 136 | * |
| 137 | * @return bool |
| 138 | */ |
| 139 | public function resetIp(string $ip): bool |
| 140 | { |
| 141 | $queryBuilder = $this->getQueryBuilder() |
| 142 | ->delete(self::TABLE_NAME) |
| 143 | ->where(sprintf('%s = ?', self::FIELD_ID)); |
| 144 | |
| 145 | return $this->getPersistence() |
| 146 | ->query($queryBuilder->getSQL(), [ip2long($ip)])->execute(); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @param Schema $schema |
| 151 | * |
| 152 | * @return mixed |
| 153 | */ |
| 154 | public function getSchema(Schema $schema) |
| 155 | { |
| 156 | return $this->getServiceLocator()->get(RdsLockoutSchema::class)->getSchema($schema); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @return QueryBuilder |
| 161 | */ |
| 162 | protected function getQueryBuilder(): QueryBuilder |
| 163 | { |
| 164 | return $this->getPersistence()->getPlatForm()->getQueryBuilder(); |
| 165 | } |
| 166 | } |