Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| TokenStoreSession | |
0.00% |
0 / 21 |
|
0.00% |
0 / 7 |
210 | |
0.00% |
0 / 1 |
| getSession | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getToken | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| setToken | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| hasToken | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| removeToken | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| clear | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| getAll | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| 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) 2017 (original work) Open Assessment Technologies SA ; |
| 19 | */ |
| 20 | |
| 21 | namespace oat\tao\model\security\xsrf; |
| 22 | |
| 23 | use oat\oatbox\Configurable; |
| 24 | use PHPSession; |
| 25 | |
| 26 | /** |
| 27 | * TokenStore into the PHP session |
| 28 | * |
| 29 | * @author Bertrand Chevrier <bertrand@taotesting.com> |
| 30 | */ |
| 31 | class TokenStoreSession extends Configurable implements TokenStore |
| 32 | { |
| 33 | private const TOKEN_NAMESPACE = 'CSRF_TOKEN_'; |
| 34 | |
| 35 | /** |
| 36 | * @var PHPSession |
| 37 | */ |
| 38 | private $session; |
| 39 | |
| 40 | /** |
| 41 | * @return PHPSession |
| 42 | */ |
| 43 | private function getSession() |
| 44 | { |
| 45 | if ($this->session === null) { |
| 46 | $this->session = PHPSession::singleton(); |
| 47 | } |
| 48 | return $this->session; |
| 49 | } |
| 50 | |
| 51 | public function getToken(string $tokenId): ?Token |
| 52 | { |
| 53 | return $this->hasToken($tokenId) |
| 54 | ? $this->getSession()->getAttribute(self::TOKEN_NAMESPACE . $tokenId) |
| 55 | : null; |
| 56 | } |
| 57 | |
| 58 | public function setToken(string $tokenId, Token $token): void |
| 59 | { |
| 60 | $this->getSession()->setAttribute(self::TOKEN_NAMESPACE . $tokenId, $token); |
| 61 | } |
| 62 | |
| 63 | public function hasToken(string $tokenId): bool |
| 64 | { |
| 65 | return $this->getSession()->hasAttribute(self::TOKEN_NAMESPACE . $tokenId); |
| 66 | } |
| 67 | |
| 68 | public function removeToken(string $tokenId): bool |
| 69 | { |
| 70 | $removed = false; |
| 71 | if ($this->hasToken($tokenId)) { |
| 72 | $this->getSession()->removeAttribute(self::TOKEN_NAMESPACE . $tokenId); |
| 73 | $removed = true; |
| 74 | } |
| 75 | |
| 76 | return $removed; |
| 77 | } |
| 78 | |
| 79 | public function clear(): void |
| 80 | { |
| 81 | foreach ($this->getSession()->getAttributeNames() as $key) { |
| 82 | if (strpos($key, self::TOKEN_NAMESPACE) === 0) { |
| 83 | $this->getSession()->removeAttribute($key); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | public function getAll(): array |
| 89 | { |
| 90 | $tokens = []; |
| 91 | foreach ($this->getSession()->getAttributeNames() as $sessionAttributeKey) { |
| 92 | if (strpos($sessionAttributeKey, self::TOKEN_NAMESPACE) === 0) { |
| 93 | $tokens[] = $this->getSession()->getAttribute($sessionAttributeKey); |
| 94 | } |
| 95 | } |
| 96 | return $tokens; |
| 97 | } |
| 98 | } |