|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* This file is part of Aura for PHP. |
|
5
|
|
|
* |
|
6
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace Aura\Session; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* |
|
13
|
|
|
* Cross-site request forgery token tools. |
|
14
|
|
|
* |
|
15
|
|
|
* @package Aura.Session |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
|
|
class CsrfToken |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* |
|
22
|
|
|
* A cryptographically-secure random value generator. |
|
23
|
|
|
* |
|
24
|
|
|
* @var RandvalInterface |
|
25
|
|
|
* |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $randval; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* |
|
31
|
|
|
* Session segment for values in this class. |
|
32
|
|
|
* |
|
33
|
|
|
* @var SegmentInterface |
|
34
|
|
|
* |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $segment; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* |
|
40
|
|
|
* Constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param SegmentInterface $segment A segment for values in this class. |
|
43
|
|
|
* |
|
44
|
|
|
* @param RandvalInterface $randval A cryptographically-secure random |
|
45
|
|
|
* value generator. |
|
46
|
|
|
* |
|
47
|
|
|
*/ |
|
48
|
5 |
|
public function __construct(SegmentInterface $segment, RandvalInterface $randval) |
|
49
|
|
|
{ |
|
50
|
5 |
|
$this->segment = $segment; |
|
51
|
5 |
|
$this->randval = $randval; |
|
52
|
5 |
|
} |
|
53
|
5 |
|
|
|
54
|
5 |
|
/** |
|
55
|
5 |
|
* |
|
56
|
|
|
* Checks whether an incoming CSRF token value is valid. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $value The incoming token value. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $key A string key name which session value is saved. |
|
61
|
|
|
* |
|
62
|
|
|
* @return bool True if valid, false if not. |
|
63
|
|
|
* |
|
64
|
|
|
*/ |
|
65
|
|
|
public function isValid($value, $key = 'value') |
|
66
|
1 |
|
{ |
|
67
|
|
|
return hash_equals($value, $this->getValue($key)); |
|
68
|
1 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* |
|
72
|
1 |
|
* Gets the value of the outgoing CSRF token. |
|
73
|
1 |
|
* |
|
74
|
|
|
* @param string $key A string key name which session value is saved. |
|
75
|
|
|
* |
|
76
|
|
|
* @return string |
|
77
|
|
|
* |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getValue($key = 'value') |
|
80
|
|
|
{ |
|
81
|
|
|
if ($this->segment->get($key) == null ) { |
|
82
|
3 |
|
$this->regenerateValue($key); |
|
83
|
|
|
} |
|
84
|
3 |
|
|
|
85
|
|
|
return $this->segment->get($key); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* |
|
90
|
|
|
* Regenerates the value of the outgoing CSRF token. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $key A string key name which session value is saved. |
|
93
|
|
|
* |
|
94
|
5 |
|
* @return string |
|
95
|
|
|
* |
|
96
|
5 |
|
*/ |
|
97
|
5 |
|
public function regenerateValue($key = 'value') |
|
98
|
5 |
|
{ |
|
99
|
|
|
$hash = hash('sha512', $this->randval->generate()); |
|
100
|
|
|
$this->segment->set($key, $hash); |
|
101
|
|
|
|
|
102
|
|
|
return $this->segment->get($key); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Regenerates all csrf tokens |
|
108
|
|
|
* |
|
109
|
|
|
* @return void |
|
110
|
|
|
*/ |
|
111
|
|
|
public function regenerateAllKeyValues() |
|
112
|
|
|
{ |
|
113
|
|
|
$segment = $this->segment->getSegment(); |
|
114
|
|
|
|
|
115
|
|
|
if ($segment) { |
|
116
|
|
|
foreach ($segment as $key => $value) { |
|
117
|
|
|
$this->regenerateValue($key); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|