1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LM\AuthAbstractor\Test; |
6
|
|
|
|
7
|
|
|
use LM\AuthAbstractor\Controller\AuthenticationKernel; |
8
|
|
|
use LM\AuthAbstractor\Configuration\IApplicationConfiguration; |
9
|
|
|
use LM\AuthAbstractor\Model\IMember; |
10
|
|
|
use LM\AuthAbstractor\Model\IAuthenticationKernel; |
11
|
|
|
use LM\AuthAbstractor\Mocker\U2fMocker; |
12
|
|
|
use LM\AuthAbstractor\Implementation\Member; |
13
|
|
|
use Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface; |
14
|
|
|
use Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage; |
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
17
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Mocks the kernel. |
21
|
|
|
* |
22
|
|
|
* It only supports one user |
23
|
|
|
* |
24
|
|
|
* @internal |
25
|
|
|
* @todo Maybe it should be moved in tests? |
26
|
|
|
*/ |
27
|
|
|
class KernelMocker |
28
|
|
|
{ |
29
|
|
|
const KEY_PWD_SETTINGS = 'pwdSettings'; |
30
|
|
|
|
31
|
|
|
const KEY_U2F_CERTIFICATES = 'u2fCertificates'; |
32
|
|
|
|
33
|
|
|
const KEY_USER_ID = 'userId'; |
34
|
|
|
|
35
|
|
|
const KEY_USER_PWD = 'userPwd'; |
36
|
|
|
|
37
|
|
|
const USER_ID = 'user'; |
38
|
|
|
|
39
|
|
|
const USER_PWD = 'pwd'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param array $options An array of options for initialising the kernel. |
43
|
|
|
* The available options are the constants of KernelMocker that begin with |
44
|
|
|
* KEY_. |
45
|
|
|
* @return IAuthenticationKernel A kernel that can be used for unit testing. |
46
|
|
|
*/ |
47
|
|
|
public function createKernel(array $options = []): IAuthenticationKernel |
48
|
|
|
{ |
49
|
|
|
if (!isset($options[self::KEY_U2F_CERTIFICATES])) { |
50
|
|
|
$options[self::KEY_U2F_CERTIFICATES] = null; |
51
|
|
|
} |
52
|
|
|
if (!isset($options[self::KEY_PWD_SETTINGS])) { |
53
|
|
|
$options[self::KEY_PWD_SETTINGS] = [ |
54
|
|
|
'min_length' => 0, |
55
|
|
|
'enforce_min_length' => false, |
56
|
|
|
'uppercase' => false, |
57
|
|
|
'special_chars' => false, |
58
|
|
|
'numbers' => false, |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
if (!isset($options[self::KEY_USER_ID])) { |
62
|
|
|
$options[self::KEY_USER_ID] = self::USER_ID; |
63
|
|
|
} |
64
|
|
|
if (!isset($options[self::KEY_USER_PWD])) { |
65
|
|
|
$options[self::KEY_USER_PWD] = self::USER_PWD; |
66
|
|
|
} |
67
|
|
|
return new AuthenticationKernel( |
68
|
|
|
new class($options[self::KEY_U2F_CERTIFICATES], $options[self::KEY_PWD_SETTINGS], $options[self::KEY_USER_ID], $options[self::KEY_USER_PWD]) implements IApplicationConfiguration { |
69
|
|
|
private $cas; |
70
|
|
|
|
71
|
|
|
private $hashedPassword; |
72
|
|
|
|
73
|
|
|
private $tokenStorage; |
74
|
|
|
|
75
|
|
|
private $username; |
76
|
|
|
|
77
|
|
|
public function __construct( |
78
|
|
|
?array $cas, |
79
|
|
|
array $pwdSettings, |
80
|
|
|
string $username, |
81
|
|
|
string $password |
82
|
|
|
) { |
83
|
|
|
$this->cas = $cas; |
84
|
|
|
$this->hashedPassword = password_hash($password, PASSWORD_DEFAULT); |
85
|
|
|
$this->pwdSettings = $pwdSettings; |
|
|
|
|
86
|
|
|
$this->tokenStorage = new SessionTokenStorage(new Session( |
87
|
|
|
new MockArraySessionStorage() |
88
|
|
|
)); |
89
|
|
|
$this->username = $username; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getAssetUri(string $assetId): string |
93
|
|
|
{ |
94
|
|
|
return 'https://example.org'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getAppId(): string |
98
|
|
|
{ |
99
|
|
|
return 'https://example.org'; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getComposerDir(): string |
103
|
|
|
{ |
104
|
|
|
return realpath(__DIR__.'/../../../../vendor'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function getCustomTwigDir(): ?string |
108
|
|
|
{ |
109
|
|
|
return null; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getLibDir(): string |
113
|
|
|
{ |
114
|
|
|
return realpath(__DIR__.'/../../../..'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getMember(string $username): IMember |
118
|
|
|
{ |
119
|
|
|
if ($this->username !== $username) { |
120
|
|
|
throw new InvalidArgumentException(); |
121
|
|
|
} |
122
|
|
|
return new Member( |
123
|
|
|
$this->hashedPassword, |
|
|
|
|
124
|
|
|
$this->username |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getU2fCertificates(): ?array |
129
|
|
|
{ |
130
|
|
|
return $this->cas; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getU2fRegistrations(string $username): array |
134
|
|
|
{ |
135
|
|
|
return (new U2fMocker($this))->getU2fRegistrationsOnly(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function getPwdSettings(): array |
139
|
|
|
{ |
140
|
|
|
return $this->pwdSettings; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getTokenStorage(): TokenStorageInterface |
144
|
|
|
{ |
145
|
|
|
return $this->tokenStorage; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function isExistingMember(string $username): bool |
149
|
|
|
{ |
150
|
|
|
return $this->username === $username; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|