|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ubiquity\security\csrf; |
|
3
|
|
|
|
|
4
|
|
|
use Ubiquity\security\csrf\storages\TokenStorageInterface; |
|
5
|
|
|
use Ubiquity\security\csrf\storages\SessionTokenStorage; |
|
6
|
|
|
use Ubiquity\security\csrf\generators\Md5Selector; |
|
7
|
|
|
use Ubiquity\security\csrf\generators\GeneratorInterface; |
|
8
|
|
|
use Ubiquity\security\csrf\generators\RandomValidator; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Ubiquity\security\csrf$CsrfManager |
|
12
|
|
|
* This class is part of Ubiquity |
|
13
|
|
|
* |
|
14
|
|
|
* @author jc |
|
15
|
|
|
* @version 1.0.0 |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
|
|
class CsrfManager { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* |
|
22
|
|
|
* @var GeneratorInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private static $selector; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* |
|
28
|
|
|
* @var GeneratorInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private static $validator; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* |
|
34
|
|
|
* @var TokenStorageInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private static $storage; |
|
37
|
|
|
|
|
38
|
|
|
public static function start(TokenStorageInterface $storage = null, GeneratorInterface $selector = null, GeneratorInterface $validator = null) { |
|
39
|
|
|
self::$selector = $selector ?? new Md5Selector(); |
|
40
|
|
|
self::$validator = $validator ?? new RandomValidator(); |
|
41
|
|
|
self::$storage = $storage ?? new SessionTokenStorage(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Generates or retrieve and return a token. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $name |
|
48
|
|
|
* @return \Ubiquity\security\csrf\UToken |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function getToken($name) { |
|
51
|
|
|
$id = self::$selector->generate($name); |
|
52
|
|
|
if (self::$storage->exists($id)) { |
|
53
|
|
|
$value = self::$storage->get($id); |
|
54
|
|
|
} else { |
|
55
|
|
|
$value = self::$validator->generate(); |
|
56
|
|
|
self::$storage->set($id, $value); |
|
57
|
|
|
} |
|
58
|
|
|
return new UToken($id, $value); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Remove an existing token |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $name |
|
65
|
|
|
* @return ?string |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function removeToken(string $name): ?string { |
|
68
|
|
|
return self::$storage->remove(self::$selector->generate($name)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Returns whether the given CSRF token is valid, given his id. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $id |
|
75
|
|
|
* @param string $value |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function isValid(string $id, string $value): bool { |
|
79
|
|
|
if (! self::$storage->exists($id)) { |
|
80
|
|
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return hash_equals(self::$storage->get($id), $value); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns whether the given CSRF token is valid, given his name. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $name |
|
90
|
|
|
* @param string $value |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
|
|
public static function isValidByName(string $name, string $value): bool { |
|
94
|
|
|
return self::isValid(self::$selector->generate($name), $value); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Return a selector corresponding to a name, using the active selector. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $name |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
public static function getSelector(string $name): string { |
|
104
|
|
|
return self::$selector->generate($name); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Generates a token value using the active validator. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $value |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
|
public static function generateValue(?string $value = null): string { |
|
114
|
|
|
return self::$validator->generate($value); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public static function getValidatorClass(): string { |
|
118
|
|
|
return \get_class(self::$validator); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public static function getSelectorClass(): string { |
|
122
|
|
|
return \get_class(self::$selector); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public static function getStorageClass(): string { |
|
126
|
|
|
return \get_class(self::$storage); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public static function isStarted(): bool { |
|
130
|
|
|
return isset(self::$storage); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
|