1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Faulancer\Security; |
4
|
|
|
|
5
|
|
|
use Faulancer\Exception\ServiceNotFoundException; |
6
|
|
|
use Faulancer\Service\SessionManagerService; |
7
|
|
|
use Faulancer\ServiceLocator\ServiceInterface; |
8
|
|
|
use Faulancer\ServiceLocator\ServiceLocator; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Csrf |
12
|
|
|
* |
13
|
|
|
* @package Faulancer\Security |
14
|
|
|
* @author Florian Knapp <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Csrf |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Generates a token and save it to session |
20
|
|
|
* |
21
|
|
|
* @param string $identifier |
22
|
|
|
* |
23
|
|
|
* @return string |
24
|
|
|
* |
25
|
|
|
* @throws ServiceNotFoundException |
26
|
|
|
*/ |
27
|
|
|
public static function getToken(string $identifier = '') :string |
28
|
|
|
{ |
29
|
|
|
$token = self::_getSessionManager()->get('csrf' . $identifier); |
|
|
|
|
30
|
|
|
|
31
|
|
|
if (!self::_getSessionManager()->has('csrf' . $identifier)) { |
|
|
|
|
32
|
|
|
$token = bin2hex(openssl_random_pseudo_bytes(16)); |
33
|
|
|
self::saveToSession($token, $identifier); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $token; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Check if token is valid |
41
|
|
|
* |
42
|
|
|
* @param string $token |
43
|
|
|
* @param string $identifier |
44
|
|
|
* |
45
|
|
|
* @return bool |
46
|
|
|
* |
47
|
|
|
* @throws ServiceNotFoundException |
48
|
|
|
*/ |
49
|
|
|
public static function isValid(string $token, string $identifier = '') :bool |
50
|
|
|
{ |
51
|
|
|
$sessionToken = self::_getSessionManager()->get('csrf' . $identifier); |
|
|
|
|
52
|
|
|
$isValid = $token === $sessionToken; |
53
|
|
|
|
54
|
|
|
if ($isValid) { |
55
|
|
|
self::_getSessionManager()->delete('csrf' . $identifier); |
|
|
|
|
56
|
|
|
return true; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Saves token into session |
64
|
|
|
* |
65
|
|
|
* @param string $token |
66
|
|
|
* @param string $identifier |
67
|
|
|
* |
68
|
|
|
* @return void |
69
|
|
|
* |
70
|
|
|
* @throws ServiceNotFoundException |
71
|
|
|
*/ |
72
|
|
|
private static function saveToSession(string $token, string $identifier = '') |
73
|
|
|
{ |
74
|
|
|
self::_getSessionManager()->set('csrf' . $identifier, $token); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return SessionManagerService|ServiceInterface |
79
|
|
|
* |
80
|
|
|
* @throws ServiceNotFoundException |
81
|
|
|
*/ |
82
|
|
|
private static function _getSessionManager() |
83
|
|
|
{ |
84
|
|
|
return ServiceLocator::instance()->get(SessionManagerService::class); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: