Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
16 / 16 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| Token | |
100.00% |
16 / 16 |
|
100.00% |
7 / 7 |
10 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| setValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setCreatedAt | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isExpired | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| getCreatedAt | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| jsonSerialize | |
100.00% |
4 / 4 |
|
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) 2023 (original work) Open Assessment Technologies SA ; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\security\xsrf; |
| 24 | |
| 25 | use JsonSerializable; |
| 26 | use oat\tao\model\security\TokenGenerator; |
| 27 | |
| 28 | /** |
| 29 | * Class that provides the Token model |
| 30 | * |
| 31 | * @author Martijn Swinkels <m.swinkels@taotesting.com> |
| 32 | */ |
| 33 | class Token implements JsonSerializable |
| 34 | { |
| 35 | use TokenGenerator; |
| 36 | |
| 37 | public const TOKEN_KEY = 'token'; |
| 38 | public const TIMESTAMP_KEY = 'ts'; |
| 39 | |
| 40 | /** |
| 41 | * @var string |
| 42 | */ |
| 43 | private $token; |
| 44 | |
| 45 | /** |
| 46 | * @var float |
| 47 | */ |
| 48 | private $tokenTimeStamp; |
| 49 | |
| 50 | /** |
| 51 | * Token constructor. |
| 52 | * @param array $data |
| 53 | * @throws \common_Exception |
| 54 | */ |
| 55 | public function __construct($data = []) |
| 56 | { |
| 57 | if (empty($data)) { |
| 58 | $this->token = $this->generate(); |
| 59 | $this->tokenTimeStamp = microtime(true); |
| 60 | } elseif (isset($data[self::TOKEN_KEY], $data[self::TIMESTAMP_KEY])) { |
| 61 | $this->setValue($data[self::TOKEN_KEY]); |
| 62 | $this->setCreatedAt($data[self::TIMESTAMP_KEY]); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Set the value of the token. |
| 68 | * |
| 69 | * @param string $token |
| 70 | */ |
| 71 | public function setValue($token) |
| 72 | { |
| 73 | $this->token = $token; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Set the microtime at which the token was created. |
| 78 | * @param float $timestamp |
| 79 | */ |
| 80 | public function setCreatedAt($timestamp) |
| 81 | { |
| 82 | $this->tokenTimeStamp = $timestamp; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Get the value of the token. |
| 87 | * |
| 88 | * @return string |
| 89 | */ |
| 90 | public function getValue() |
| 91 | { |
| 92 | return $this->token; |
| 93 | } |
| 94 | |
| 95 | public function isExpired(int $timeLimit): bool |
| 96 | { |
| 97 | $actualTime = microtime(true); |
| 98 | |
| 99 | return $timeLimit > 0 && ($this->getCreatedAt() + $timeLimit) < $actualTime; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get the microtime at which the token was created. |
| 104 | * |
| 105 | * @return float |
| 106 | */ |
| 107 | public function getCreatedAt() |
| 108 | { |
| 109 | return $this->tokenTimeStamp; |
| 110 | } |
| 111 | |
| 112 | public function jsonSerialize(): array |
| 113 | { |
| 114 | return [ |
| 115 | self::TOKEN_KEY => $this->getValue(), |
| 116 | self::TIMESTAMP_KEY => $this->getCreatedAt(), |
| 117 | ]; |
| 118 | } |
| 119 | } |