|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Palladium\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Palladium\Exception\InvalidCookieToken; |
|
6
|
|
|
|
|
7
|
|
|
class CookieIdentity extends Identity |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
const SERIES_SIZE = 16; |
|
11
|
|
|
const KEY_SIZE = 32; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
private $series; |
|
15
|
|
|
private $key; |
|
16
|
|
|
private $hash; |
|
17
|
|
|
|
|
18
|
|
|
protected $type = Identity::TYPE_COOKIE; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
5 |
|
public function setSeries($series) |
|
22
|
|
|
{ |
|
23
|
5 |
|
if (empty($series)) { |
|
24
|
2 |
|
$this->series = null; |
|
25
|
2 |
|
return; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
3 |
|
$this->series = (string) $series; |
|
29
|
3 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @codeCoverageIgnore |
|
34
|
|
|
*/ |
|
35
|
|
|
public function getSeries() |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->series; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Produces a hash from series to obscure it for storage. |
|
43
|
|
|
* |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
1 |
|
public function getFingerprint() |
|
47
|
|
|
{ |
|
48
|
1 |
|
return hash('sha384', $this->series); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
1 |
|
public function generateNewSeries() |
|
53
|
|
|
{ |
|
54
|
1 |
|
$this->series = bin2hex(random_bytes(self::SERIES_SIZE)); |
|
55
|
1 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Assignes a new identification key and resets a the hash. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $key |
|
62
|
|
|
*/ |
|
63
|
5 |
|
public function setKey($key) |
|
64
|
|
|
{ |
|
65
|
5 |
|
$this->hash = null; |
|
66
|
|
|
|
|
67
|
5 |
|
if (empty($key)) { |
|
68
|
2 |
|
$this->key = null; |
|
69
|
2 |
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
3 |
|
$this->key = (string) $key; |
|
73
|
3 |
|
$this->hash = $this->makeHash($key); |
|
74
|
3 |
|
} |
|
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
|
1 |
|
public function generateNewKey() |
|
91
|
|
|
{ |
|
92
|
1 |
|
$key = bin2hex(random_bytes(self::KEY_SIZE)); |
|
93
|
1 |
|
$this->key = $key; |
|
94
|
1 |
|
$this->hash = $this->makeHash($key); |
|
95
|
1 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @codeCoverageIgnore |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getHash() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->hash; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
4 |
|
private function makeHash($key) |
|
108
|
|
|
{ |
|
109
|
4 |
|
return hash('sha384', $key); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param string $hash |
|
115
|
|
|
*/ |
|
116
|
5 |
|
public function setHash($hash) |
|
117
|
|
|
{ |
|
118
|
5 |
|
if (empty($hash)) { |
|
119
|
2 |
|
$this->hash = null; |
|
120
|
2 |
|
return; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
3 |
|
$this->hash = (string) $hash; |
|
124
|
3 |
|
} |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
1 |
|
public function matchKey($key) |
|
128
|
|
|
{ |
|
129
|
1 |
|
return $this->makeHash($key) === $this->hash; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|