@@ -1,163 +1,163 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - namespace Palladium\Entity; |
|
| 3 | + namespace Palladium\Entity; |
|
| 4 | 4 | |
| 5 | - use Palladium\Exception\InvalidCookieToken; |
|
| 5 | + use Palladium\Exception\InvalidCookieToken; |
|
| 6 | 6 | |
| 7 | - class CookieIdentity extends Identity |
|
| 8 | - { |
|
| 7 | + class CookieIdentity extends Identity |
|
| 8 | + { |
|
| 9 | 9 | |
| 10 | - const SERIES_SIZE = 16; |
|
| 11 | - const KEY_SIZE = 32; |
|
| 10 | + const SERIES_SIZE = 16; |
|
| 11 | + const KEY_SIZE = 32; |
|
| 12 | 12 | |
| 13 | 13 | |
| 14 | - private $series; |
|
| 15 | - private $key; |
|
| 16 | - private $hash; |
|
| 14 | + private $series; |
|
| 15 | + private $key; |
|
| 16 | + private $hash; |
|
| 17 | 17 | |
| 18 | - protected $type = Identity::TYPE_COOKIE; |
|
| 18 | + protected $type = Identity::TYPE_COOKIE; |
|
| 19 | 19 | |
| 20 | 20 | |
| 21 | - public function setSeries($series) |
|
| 22 | - { |
|
| 23 | - if (empty($series)) { |
|
| 24 | - $this->series = null; |
|
| 25 | - return; |
|
| 26 | - } |
|
| 21 | + public function setSeries($series) |
|
| 22 | + { |
|
| 23 | + if (empty($series)) { |
|
| 24 | + $this->series = null; |
|
| 25 | + return; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - $this->series = (string) $series; |
|
| 29 | - } |
|
| 28 | + $this->series = (string) $series; |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * @codeCoverageIgnore |
|
| 34 | - */ |
|
| 35 | - public function getSeries() |
|
| 36 | - { |
|
| 37 | - return $this->series; |
|
| 38 | - } |
|
| 32 | + /** |
|
| 33 | + * @codeCoverageIgnore |
|
| 34 | + */ |
|
| 35 | + public function getSeries() |
|
| 36 | + { |
|
| 37 | + return $this->series; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Produces a hash from series to obscure it for storage. |
|
| 43 | - * |
|
| 44 | - * @return string |
|
| 45 | - */ |
|
| 46 | - public function getFingerprint() |
|
| 47 | - { |
|
| 48 | - return hash('sha384', $this->series); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - |
|
| 52 | - public function generateNewSeries() |
|
| 53 | - { |
|
| 54 | - $this->series = bin2hex(random_bytes(self::SERIES_SIZE)); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Assignes a new identification key and resets a the hash. |
|
| 60 | - * |
|
| 61 | - * @param string $key |
|
| 62 | - */ |
|
| 63 | - public function setKey($key) |
|
| 64 | - { |
|
| 65 | - $this->hash = null; |
|
| 66 | - |
|
| 67 | - if (empty($key)) { |
|
| 68 | - $this->key = null; |
|
| 69 | - return; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - $this->key = (string) $key; |
|
| 73 | - $this->hash = $this->makeHash($key); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * @codeCoverageIgnore |
|
| 79 | - * @return string |
|
| 80 | - */ |
|
| 81 | - public function getKey() |
|
| 82 | - { |
|
| 83 | - return $this->key; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Sets a new key and resets the hash. |
|
| 89 | - */ |
|
| 90 | - public function generateNewKey() |
|
| 91 | - { |
|
| 92 | - $key = bin2hex(random_bytes(self::KEY_SIZE)); |
|
| 93 | - $this->key = $key; |
|
| 94 | - $this->hash = $this->makeHash($key); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * @codeCoverageIgnore |
|
| 100 | - */ |
|
| 101 | - public function getHash() |
|
| 102 | - { |
|
| 103 | - return $this->hash; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - |
|
| 107 | - private function makeHash($key) |
|
| 108 | - { |
|
| 109 | - return hash('sha384', $key); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @param string $hash |
|
| 115 | - */ |
|
| 116 | - public function setHash($hash) |
|
| 117 | - { |
|
| 118 | - if (empty($hash)) { |
|
| 119 | - $this->hash = null; |
|
| 120 | - return; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - $this->hash = (string) $hash; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - |
|
| 127 | - public function matchKey($key) |
|
| 128 | - { |
|
| 129 | - return $this->makeHash($key) === $this->hash; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Retrieves the identification token in a compact form. |
|
| 135 | - * |
|
| 136 | - * @return string|null |
|
| 137 | - */ |
|
| 138 | - public function getCollapsedValue() |
|
| 139 | - { |
|
| 140 | - if (null === $this->getId()) { |
|
| 141 | - return null; |
|
| 142 | - } |
|
| 143 | - return $this->getUserId() . '|' . $this->getSeries() . '|' . $this->getKey(); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Populates the instance from the identification token. |
|
| 149 | - * |
|
| 150 | - * @param string $value |
|
| 151 | - */ |
|
| 152 | - public function setCollapsedValue($value) |
|
| 153 | - { |
|
| 154 | - if (empty($value) || substr_count($value, '|') !== 2) { |
|
| 155 | - throw new InvalidCookieToken; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - list($userId, $series, $key) = explode('|', $value); |
|
| 159 | - $this->setUserId($userId); |
|
| 160 | - $this->setSeries($series); |
|
| 161 | - $this->setKey($key); |
|
| 162 | - } |
|
| 163 | - } |
|
| 41 | + /** |
|
| 42 | + * Produces a hash from series to obscure it for storage. |
|
| 43 | + * |
|
| 44 | + * @return string |
|
| 45 | + */ |
|
| 46 | + public function getFingerprint() |
|
| 47 | + { |
|
| 48 | + return hash('sha384', $this->series); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + |
|
| 52 | + public function generateNewSeries() |
|
| 53 | + { |
|
| 54 | + $this->series = bin2hex(random_bytes(self::SERIES_SIZE)); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Assignes a new identification key and resets a the hash. |
|
| 60 | + * |
|
| 61 | + * @param string $key |
|
| 62 | + */ |
|
| 63 | + public function setKey($key) |
|
| 64 | + { |
|
| 65 | + $this->hash = null; |
|
| 66 | + |
|
| 67 | + if (empty($key)) { |
|
| 68 | + $this->key = null; |
|
| 69 | + return; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + $this->key = (string) $key; |
|
| 73 | + $this->hash = $this->makeHash($key); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * @codeCoverageIgnore |
|
| 79 | + * @return string |
|
| 80 | + */ |
|
| 81 | + public function getKey() |
|
| 82 | + { |
|
| 83 | + return $this->key; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Sets a new key and resets the hash. |
|
| 89 | + */ |
|
| 90 | + public function generateNewKey() |
|
| 91 | + { |
|
| 92 | + $key = bin2hex(random_bytes(self::KEY_SIZE)); |
|
| 93 | + $this->key = $key; |
|
| 94 | + $this->hash = $this->makeHash($key); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * @codeCoverageIgnore |
|
| 100 | + */ |
|
| 101 | + public function getHash() |
|
| 102 | + { |
|
| 103 | + return $this->hash; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + |
|
| 107 | + private function makeHash($key) |
|
| 108 | + { |
|
| 109 | + return hash('sha384', $key); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @param string $hash |
|
| 115 | + */ |
|
| 116 | + public function setHash($hash) |
|
| 117 | + { |
|
| 118 | + if (empty($hash)) { |
|
| 119 | + $this->hash = null; |
|
| 120 | + return; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + $this->hash = (string) $hash; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + |
|
| 127 | + public function matchKey($key) |
|
| 128 | + { |
|
| 129 | + return $this->makeHash($key) === $this->hash; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Retrieves the identification token in a compact form. |
|
| 135 | + * |
|
| 136 | + * @return string|null |
|
| 137 | + */ |
|
| 138 | + public function getCollapsedValue() |
|
| 139 | + { |
|
| 140 | + if (null === $this->getId()) { |
|
| 141 | + return null; |
|
| 142 | + } |
|
| 143 | + return $this->getUserId() . '|' . $this->getSeries() . '|' . $this->getKey(); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Populates the instance from the identification token. |
|
| 149 | + * |
|
| 150 | + * @param string $value |
|
| 151 | + */ |
|
| 152 | + public function setCollapsedValue($value) |
|
| 153 | + { |
|
| 154 | + if (empty($value) || substr_count($value, '|') !== 2) { |
|
| 155 | + throw new InvalidCookieToken; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + list($userId, $series, $key) = explode('|', $value); |
|
| 159 | + $this->setUserId($userId); |
|
| 160 | + $this->setSeries($series); |
|
| 161 | + $this->setKey($key); |
|
| 162 | + } |
|
| 163 | + } |
|