|
1
|
|
|
<?php |
|
2
|
|
|
namespace ScienceHook\Security\CSRF\Dependency\SessionHandler; |
|
3
|
|
|
|
|
4
|
|
|
use ScienceHook\Security\CSRF\Exception as Issues; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class SessionHandler |
|
8
|
|
|
* |
|
9
|
|
|
* @package ScienceHook\Security\Dependency\SessionHandler |
|
10
|
|
|
* @author Kshitij Kumar |
|
11
|
|
|
*/ |
|
12
|
|
|
class SessionHandler |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Checks whether SESSION is active or not |
|
16
|
|
|
* |
|
17
|
|
|
* @return object |
|
18
|
|
|
* @throws Issues\SessionNotActiveException |
|
19
|
|
|
*/ |
|
20
|
|
|
private function &checkSessionStatus(): object |
|
21
|
|
|
{ |
|
22
|
|
|
// If SESSION is not active throw Exception |
|
23
|
|
|
if (session_status() !== PHP_SESSION_ACTIVE) { |
|
24
|
|
|
throw new Issues\SessionNotActiveException( |
|
25
|
|
|
'Session is not active. Please run session_start() |
|
26
|
|
|
on top of your file.' |
|
27
|
|
|
); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
return $this; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Checks if SESSION value is set |
|
35
|
|
|
* |
|
36
|
|
|
* @param mixed $session_variable Session value to be verified |
|
37
|
|
|
* @return bool |
|
38
|
|
|
* @throws Issues\SessionValueNotSet |
|
39
|
|
|
*/ |
|
40
|
|
|
private function isSessionValueSet($session_variable): bool |
|
41
|
|
|
{ |
|
42
|
|
|
if (!isset($session_variable)) { |
|
43
|
|
|
throw new Issues\SessionValueNotSet( |
|
44
|
|
|
'Token value is empty. Please refresh your page |
|
45
|
|
|
and try submitting form again.' |
|
46
|
|
|
); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return true; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Sets SESSION value |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $form_name Form identifier e.g. name or id |
|
56
|
|
|
* @param string $token_prefix Token Prefix |
|
57
|
|
|
* @param string $time_token_prefix Time Token Prefix |
|
58
|
|
|
* @param int $byte_length Byte length for random_bytes |
|
59
|
|
|
* @throws Issues\SessionNotActiveException |
|
60
|
|
|
* @throws \Exception |
|
61
|
|
|
* @throws \TypeError |
|
62
|
|
|
* @throws \Error |
|
63
|
|
|
*/ |
|
64
|
|
|
public function setSessionValue( |
|
65
|
|
|
string $form_name, |
|
66
|
|
|
string $token_prefix, |
|
67
|
|
|
string $time_token_prefix, |
|
68
|
|
|
int $byte_length |
|
69
|
|
|
) { |
|
70
|
|
|
$this->checkSessionStatus(); |
|
71
|
|
|
|
|
72
|
|
|
// random_bytes can throw Exceptions or Errors |
|
73
|
|
|
$_SESSION[$token_prefix . $form_name] = bin2hex(random_bytes($byte_length)); |
|
74
|
|
|
|
|
75
|
|
|
// Set current time with the token |
|
76
|
|
|
$_SESSION[$time_token_prefix . $form_name] = time(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Returns session value |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $form_name |
|
83
|
|
|
* @param string $prefix |
|
84
|
|
|
* @return mixed |
|
85
|
|
|
* @throws Issues\SessionNotActiveException |
|
86
|
|
|
* @throws Issues\SessionValueNotSet |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getSessionValue(string $form_name, string $prefix) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->checkSessionStatus()->isSessionValueSet($_SESSION[$prefix . $form_name]); |
|
91
|
|
|
|
|
92
|
|
|
return $_SESSION[$prefix . $form_name]; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Unsets both token SESSIONs for the Form |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $form_name |
|
99
|
|
|
* @param string $token_prefix |
|
100
|
|
|
* @param string $time_token_prefix |
|
101
|
|
|
* @throws Issues\SessionNotActiveException |
|
102
|
|
|
* @throws Issues\SessionValueNotSet |
|
103
|
|
|
*/ |
|
104
|
|
|
public function unsetSession( |
|
105
|
|
|
string $form_name, |
|
106
|
|
|
string $token_prefix, |
|
107
|
|
|
string $time_token_prefix |
|
108
|
|
|
) { |
|
109
|
|
|
// Unsets SESSION if tokens are found |
|
110
|
|
|
if ($this->checkSessionStatus()->isSessionValueSet($_SESSION[$token_prefix . |
|
111
|
|
|
$form_name])) { |
|
112
|
|
|
unset($_SESSION[$token_prefix . $form_name]); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if ($this->checkSessionStatus()->isSessionValueSet($_SESSION[$time_token_prefix |
|
116
|
|
|
. $form_name])) { |
|
117
|
|
|
unset($_SESSION[$time_token_prefix . $form_name]); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|