/
TotpProviderTest.php
291 lines (244 loc) · 12.1 KB
/
TotpProviderTest.php
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Core\Tests\Functional\Authentication\Mfa\Provider;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Authentication\Mfa\MfaProviderManifest;
use TYPO3\CMS\Core\Authentication\Mfa\MfaProviderPropertyManager;
use TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry;
use TYPO3\CMS\Core\Authentication\Mfa\MfaViewType;
use TYPO3\CMS\Core\Authentication\Mfa\Provider\Totp;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Localization\LanguageServiceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
class TotpProviderTest extends FunctionalTestCase
{
private BackendUserAuthentication $user;
private MfaProviderManifest $subject;
protected function setUp(): void
{
parent::setUp();
$this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/be_users.xml');
$this->user = $this->setUpBackendUser(1);
$GLOBALS['LANG'] = $this->getContainer()->get(LanguageServiceFactory::class)->createFromUserPreferences($this->user);
$this->subject = $this->getContainer()->get(MfaProviderRegistry::class)->getProvider('totp');
}
/**
* @test
*/
public function canProcessTest(): void
{
self::assertFalse($this->subject->canProcess(new ServerRequest('https://example.com', 'POST')));
// Add necessary query parameter
self::assertTrue($this->subject->canProcess(
(new ServerRequest('https://example.com', 'POST'))
->withQueryParams(['totp' => '123456'])
));
}
/**
* @test
*/
public function isActiveTest(): void
{
// No provider entry exists
self::assertFalse($this->subject->isActive(MfaProviderPropertyManager::create($this->subject, $this->user)));
// Active state missing
$this->setupUser(['secret' => 'KRMVATZTJFZUC53FONXW2ZJB']);
self::assertFalse($this->subject->isActive(MfaProviderPropertyManager::create($this->subject, $this->user)));
// Secret missing
$this->setupUser(['active' => true]);
self::assertFalse($this->subject->isActive(MfaProviderPropertyManager::create($this->subject, $this->user)));
// Active provider
$this->setupUser(['active' => true, 'secret' => 'KRMVATZTJFZUC53FONXW2ZJB']);
self::assertTrue($this->subject->isActive(MfaProviderPropertyManager::create($this->subject, $this->user)));
}
/**
* @test
*/
public function isLockedTest(): void
{
// No provider entry exists
self::assertFalse($this->subject->isLocked(MfaProviderPropertyManager::create($this->subject, $this->user)));
// Provider is not locked
$this->setupUser(['active' => true, 'secret' => 'KRMVATZTJFZUC53FONXW2ZJB', 'attempts' => 0]);
self::assertFalse($this->subject->isLocked(MfaProviderPropertyManager::create($this->subject, $this->user)));
// Lock provider by setting attempts=3
$this->setupUser(['active' => true, 'secret' => 'KRMVATZTJFZUC53FONXW2ZJB', 'attempts' => 3]);
self::assertTrue($this->subject->isLocked(MfaProviderPropertyManager::create($this->subject, $this->user)));
}
/**
* @test
*/
public function verifyTest(): void
{
$request = (new ServerRequest('https://example.com', 'POST'));
$timestamp = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('date', 'timestamp');
$totp = GeneralUtility::makeInstance(
Totp::class,
'KRMVATZTJFZUC53FONXW2ZJB'
)->generateTotp((int)floor($timestamp / 30));
// Provider is inactive (secret missing)
$this->setupUser(['active' => true]);
self::assertFalse(
$this->subject->verify(
$request->withQueryParams(['totp' => $totp]),
MfaProviderPropertyManager::create($this->subject, $this->user)
)
);
// Provider is locked (attempts=3)
$this->setupUser(['active' => true, 'secret' => 'KRMVATZTJFZUC53FONXW2ZJB', 'attempts' => 3]);
self::assertFalse(
$this->subject->verify(
$request->withQueryParams(['totp' => $totp]),
MfaProviderPropertyManager::create($this->subject, $this->user)
)
);
// Wrong totp
$this->setupUser(['active' => true, 'secret' => 'KRMVATZTJFZUC53FONXW2ZJB', 'attempts' => 0]);
self::assertFalse(
$this->subject->verify(
$request->withQueryParams(['totp' => '123456']),
MfaProviderPropertyManager::create($this->subject, $this->user)
)
);
// Correct totp
self::assertTrue(
$this->subject->verify(
$request->withQueryParams(['totp' => $totp]),
MfaProviderPropertyManager::create($this->subject, $this->user)
)
);
}
/**
* @test
*/
public function activateTest(): void
{
$request = (new ServerRequest('https://example.com', 'POST'));
$propertyManager = MfaProviderPropertyManager::create($this->subject, $this->user);
// Wrong totp
self::assertFalse($this->subject->activate($request->withParsedBody(['totp' => '123456']), $propertyManager));
// Setup form data to activate provider
$secret = 'KRMVATZTJFZUC53FONXW2ZJB';
$timestamp = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('date', 'timestamp');
$parsedBody = [
'totp' => GeneralUtility::makeInstance(Totp::class, $secret)->generateTotp((int)floor($timestamp / 30)),
'secret' => $secret,
'checksum' => GeneralUtility::hmac($secret, 'totp-setup')
];
self::assertTrue($this->subject->activate($request->withParsedBody($parsedBody), $propertyManager));
self::assertTrue($propertyManager->getProperty('active'));
self::assertEquals('KRMVATZTJFZUC53FONXW2ZJB', $propertyManager->getProperty('secret'));
}
/**
* @test
*/
public function deactivateTest(): void
{
$request = (new ServerRequest('https://example.com', 'POST'));
// No provider entry exists
self::assertFalse($this->subject->deactivate($request, MfaProviderPropertyManager::create($this->subject, $this->user)));
// Only an active provider can be deactivated
$this->setupUser(['active' => false, 'secret' => 'KRMVATZTJFZUC53FONXW2ZJB']);
self::assertFalse($this->subject->deactivate($request, MfaProviderPropertyManager::create($this->subject, $this->user)));
// Active provider is deactivated
$this->setupUser(['active' => true, 'secret' => 'KRMVATZTJFZUC53FONXW2ZJB']);
self::assertTrue($this->subject->deactivate($request, MfaProviderPropertyManager::create($this->subject, $this->user)));
}
/**
* @test
*/
public function unlockTest(): void
{
$request = (new ServerRequest('https://example.com', 'POST'));
// No provider entry exists
self::assertFalse($this->subject->unlock($request, MfaProviderPropertyManager::create($this->subject, $this->user)));
// Provider is inactive (missing secret)
$this->setupUser(['active' => true, 'attempts' => 3]);
self::assertFalse($this->subject->unlock($request, MfaProviderPropertyManager::create($this->subject, $this->user)));
// Provider is not locked
$this->setupUser(['active' => true, 'secret' => 'KRMVATZTJFZUC53FONXW2ZJB', 'attempts' => 0]);
self::assertFalse($this->subject->unlock($request, MfaProviderPropertyManager::create($this->subject, $this->user)));
// Active and locked provider is unlocked
$this->setupUser(['active' => true, 'secret' => 'KRMVATZTJFZUC53FONXW2ZJB', 'attempts' => 3]);
self::assertTrue($this->subject->unlock($request, MfaProviderPropertyManager::create($this->subject, $this->user)));
}
/**
* @test
*/
public function updateTest(): void
{
$request = (new ServerRequest('https://example.com', 'POST'));
// No provider entry exists
self::assertFalse($this->subject->update($request, MfaProviderPropertyManager::create($this->subject, $this->user)));
// Provider is inactive (missing secret)
$this->setupUser(['active' => true, 'attempts' => 0]);
self::assertFalse($this->subject->update($request, MfaProviderPropertyManager::create($this->subject, $this->user)));
// Provider is locked (attempts=3)
$this->setupUser(['active' => true, 'attempts' => 3]);
self::assertFalse($this->subject->update($request, MfaProviderPropertyManager::create($this->subject, $this->user)));
// Active and unlocked provider is updated
$this->setupUser(['active' => true, 'secret' => 'KRMVATZTJFZUC53FONXW2ZJB', 'attempts' => 0]);
$request = $request->withParsedBody(['name' => 'some name']);
self::assertTrue($this->subject->update($request, MfaProviderPropertyManager::create($this->subject, $this->user)));
}
/**
* @test
*/
public function setupViewTest(): void
{
$request = (new ServerRequest('https://example.com', 'POST'));
$propertyManager = MfaProviderPropertyManager::create($this->subject, $this->user);
$response = $this->subject->handleRequest($request, $propertyManager, MfaViewType::SETUP)->getBody()->getContents();
self::assertMatchesRegularExpression('/<input.*id="totp"/s', $response);
self::assertMatchesRegularExpression('/<input.*id="secret"/s', $response);
self::assertMatchesRegularExpression('/<div.*id="qr-code"/s', $response);
self::assertMatchesRegularExpression('/<typo3-mfa-totp-url-info-button.*url="otpauth:\/\//s', $response);
}
/**
* @test
*/
public function editViewTest(): void
{
$request = (new ServerRequest('https://example.com', 'POST'));
$this->setupUser(['name' => 'some name', 'updated' => 1616099471, 'lastUsed' => 1616099472]);
$propertyManager = MfaProviderPropertyManager::create($this->subject, $this->user);
$response = $this->subject->handleRequest($request, $propertyManager, MfaViewType::EDIT)->getBody()->getContents();
self::assertMatchesRegularExpression('/<td>.*Name.*<td>.*some name/s', $response);
self::assertMatchesRegularExpression('/<td>.*Last updated.*<td>.*18-03-21/s', $response);
self::assertMatchesRegularExpression('/<td>.*Last used.*<td>.*18-03-21/s', $response);
}
/**
* @test
*/
public function authViewTest(): void
{
$request = (new ServerRequest('https://example.com', 'POST'));
$this->setupUser(['active' => true, 'secret' => 'KRMVATZTJFZUC53FONXW2ZJB', 'attempts' => 0]);
$propertyManager = MfaProviderPropertyManager::create($this->subject, $this->user);
$response = $this->subject->handleRequest($request, $propertyManager, MfaViewType::AUTH)->getBody()->getContents();
self::assertMatchesRegularExpression('/<input.*id="totp"/s', $response);
// Lock the provider by setting attempts=3
$this->setupUser(['active' => true, 'secret' => 'KRMVATZTJFZUC53FONXW2ZJB', 'attempts' => 3]);
$propertyManager = MfaProviderPropertyManager::create($this->subject, $this->user);
$response = $this->subject->handleRequest($request, $propertyManager, MfaViewType::AUTH)->getBody()->getContents();
self::assertStringContainsString('The maximum attempts for this provider are exceeded.', $response);
}
protected function setupUser(array $properties = []): void
{
$this->user->user['mfa'] = json_encode(['totp' => $properties]);
}
}