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