‪TYPO3CMS  11.5
MfaProviderPropertyManagerTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
27 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
28 
29 class ‪MfaProviderPropertyManagerTest extends FunctionalTestCase
30 {
32 
33  protected function ‪setUp(): void
34  {
35  parent::setUp();
36 
37  $this->importCSVDataSet(__DIR__ . '/../Fixtures/be_users.csv');
38 
39  $this->user = GeneralUtility::makeInstance(BackendUserAuthentication::class);
40  $this->user->enablecolumns = ['deleted' => true];
41  $this->user->setBeUserByUid(4);
42  }
43 
47  public function ‪createTest(): void
48  {
49  $propertyManager = $this->‪createPropertyManager('totp');
50 
51  self::assertEquals('totp', $propertyManager->getIdentifier());
52  self::assertEquals($this->user, $propertyManager->getUser());
53  }
54 
58  public function ‪hasProviderEntryTest(): void
59  {
60  self::assertFalse($this->‪createPropertyManager('recovery-codes')->hasProviderEntry());
61  self::assertTrue($this->‪createPropertyManager('totp')->hasProviderEntry());
62  }
63 
67  public function ‪hasPropertyTest(): void
68  {
69  $propertyManager = $this->‪createPropertyManager('totp');
70  self::assertFalse($propertyManager->hasProperty('unknown'));
71  self::assertTrue($propertyManager->hasProperty('active'));
72  }
73 
77  public function ‪getPropertyTest(): void
78  {
79  $propertyManager = $this->‪createPropertyManager('totp');
80  self::assertNull($propertyManager->getProperty('unknown'));
81  self::assertEquals('defaultValue', $propertyManager->getProperty('unknown', 'defaultValue'));
82  self::assertTrue($propertyManager->getProperty('active'));
83  self::assertEquals('KRMVATZTJFZUC53FONXW2ZJB', $propertyManager->getProperty('secret'));
84  }
85 
89  public function ‪getPropertiesTest(): void
90  {
91  $propertyManager = $this->‪createPropertyManager('recovery-codes');
92  self::assertCount(0, $propertyManager->getProperties());
93  $propertyManager = $this->‪createPropertyManager('totp');
94  self::assertCount(3, $propertyManager->getProperties());
95  self::assertEquals(
96  [
97  'active' => true,
98  'secret' => 'KRMVATZTJFZUC53FONXW2ZJB',
99  'attempts' => 2,
100  ],
101  $propertyManager->getProperties()
102  );
103  }
104 
108  public function ‪updatePropertiesTest(): void
109  {
110  $propertyManager = $this->‪createPropertyManager('totp');
111 
112  // Ensure "updated" property is not set
113  self::assertFalse($propertyManager->hasProperty('updated'));
114  self::assertEquals(2, $propertyManager->getProperty('attempts'));
115 
116  $propertyManager->updateProperties([
117  'lastUsed' => 1614012257,
118  'attempts' => 3,
119  ]);
120 
121  // "updated" property was automatically added
122  self::assertTrue($propertyManager->hasProperty('updated'));
123  self::assertEquals(1614012257, $propertyManager->getProperty('lastUsed'));
124  self::assertEquals(3, $propertyManager->getProperty('attempts'));
125  self::assertCount(5, $propertyManager->getProperties());
126 
127  // Ensure the data were also assigned to the user
128  $userMfaData = json_decode($this->user->user['mfa'], true);
129  self::assertEquals(1614012257, $userMfaData['totp']['lastUsed']);
130  self::assertEquals(3, $userMfaData['totp']['attempts']);
131  self::assertCount(5, $userMfaData['totp']);
132 
133  $propertyManager->updateProperties(['updated' => 123456789]);
134 
135  // "updated" property is properly set
136  self::assertEquals(123456789, $propertyManager->getProperty('updated'));
137 
138  // Finally ensure, the data was actually written to the database
139  $this->‪assertDatabaseValue(
140  '{"totp":{"secret":"KRMVATZTJFZUC53FONXW2ZJB","active":true,"attempts":3,"lastUsed":1614012257,"updated":123456789}}'
141  );
142  }
143 
148  {
149  $this->expectExceptionCode(1612781782);
150  $this->expectException(\InvalidArgumentException::class);
151  $this->‪createPropertyManager('totp')->createProviderEntry(['key' => 'value']);
152  }
153 
157  public function ‪createProviderEntryTest(): void
158  {
159  $timestamp = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('date', 'timestamp');
160  $propertyManager = $this->‪createPropertyManager('recovery-codes');
161 
162  // Ensure entry does not yet exist
163  self::assertFalse($propertyManager->hasProviderEntry());
164 
165  $propertyManager->createProviderEntry([
166  'active' => true,
167  'codes' => ['some-code', 'another-code'],
168  'updated' => 123456789,
169  ]);
170 
171  self::assertTrue($propertyManager->hasProviderEntry());
172  self::assertCount(4, $propertyManager->getProperties());
173  self::assertTrue($propertyManager->getProperty('active'));
174  self::assertEquals(['some-code', 'another-code'], $propertyManager->getProperty('codes'));
175  // Ensure "updated" is not overwritten
176  self::assertEquals(123456789, $propertyManager->hasProperty('updated'));
177  // Ensure "created" is automatically set
178  self::assertTrue($propertyManager->hasProperty('created'));
179  self::assertEquals($timestamp, $propertyManager->getProperty('created'));
180 
181  // Ensure the data were also assigned to the user
182  $userMfaData = json_decode($this->user->user['mfa'], true);
183  self::assertCount(4, $userMfaData['recovery-codes']);
184  self::assertTrue($userMfaData['recovery-codes']['active']);
185  self::assertEquals(['some-code', 'another-code'], $userMfaData['recovery-codes']['codes']);
186  self::assertEquals(123456789, $userMfaData['recovery-codes']['updated']);
187  self::assertTrue((bool)($userMfaData['recovery-codes']['created'] ?? false));
188  self::assertEquals($timestamp, $userMfaData['recovery-codes']['created']);
189 
190  // Finally ensure, the data was actually written to the database
191  $this->‪assertDatabaseValue(
192  '{"totp":{"secret":"KRMVATZTJFZUC53FONXW2ZJB","active":true,"attempts":2},"recovery-codes":{"active":true,"codes":["some-code","another-code"],"updated":123456789,"created":' . $timestamp . '}}'
193  );
194  }
195 
199  public function ‪deleteProviderEntryTest(): void
200  {
201  $propertyManager = $this->‪createPropertyManager('totp');
202  self::assertTrue($propertyManager->hasProviderEntry());
203  $propertyManager->deleteProviderEntry();
204  self::assertFalse($propertyManager->hasProviderEntry());
205 
206  // Ensure the data were also assigned to the user
207  $userMfaData = json_decode($this->user->user['mfa'], true);
208  self::assertFalse((bool)($userMfaData['totp'] ?? false));
209 
210  // Finally ensure, the data was actually written to the database
211  $this->‪assertDatabaseValue('[]');
212  }
213 
214  protected function ‪createPropertyManager(string $providerIdentifier): ‪MfaProviderPropertyManager
215  {
217  $this->get(MfaProviderRegistry::class)->getProvider($providerIdentifier),
218  $this->user
219  );
220  }
221 
222  protected function ‪assertDatabaseValue(string $expected): void
223  {
224  self::assertEquals($expected, BackendUtility::getRecord(...['be_users', 4, 'mfa'])['mfa'] ?? null);
225  }
226 }
‪TYPO3\CMS\Core\Tests\Functional\Authentication\Mfa\MfaProviderPropertyManagerTest\createProviderEntryTest
‪createProviderEntryTest()
Definition: MfaProviderPropertyManagerTest.php:157
‪TYPO3\CMS\Core\Tests\Functional\Authentication\Mfa\MfaProviderPropertyManagerTest\createPropertyManager
‪createPropertyManager(string $providerIdentifier)
Definition: MfaProviderPropertyManagerTest.php:214
‪TYPO3\CMS\Core\Tests\Functional\Authentication\Mfa\MfaProviderPropertyManagerTest\assertDatabaseValue
‪assertDatabaseValue(string $expected)
Definition: MfaProviderPropertyManagerTest.php:222
‪TYPO3\CMS\Core\Tests\Functional\Authentication\Mfa\MfaProviderPropertyManagerTest\deleteProviderEntryTest
‪deleteProviderEntryTest()
Definition: MfaProviderPropertyManagerTest.php:199
‪TYPO3\CMS\Core\Tests\Functional\Authentication\Mfa\MfaProviderPropertyManagerTest\getPropertiesTest
‪getPropertiesTest()
Definition: MfaProviderPropertyManagerTest.php:89
‪TYPO3\CMS\Core\Tests\Functional\Authentication\Mfa\MfaProviderPropertyManagerTest\createProviderEntryThrowsExceptionOnAlreadyExistingEntryTest
‪createProviderEntryThrowsExceptionOnAlreadyExistingEntryTest()
Definition: MfaProviderPropertyManagerTest.php:147
‪TYPO3\CMS\Core\Tests\Functional\Authentication\Mfa\MfaProviderPropertyManagerTest\$user
‪AbstractUserAuthentication $user
Definition: MfaProviderPropertyManagerTest.php:31
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Core\Tests\Functional\Authentication\Mfa\MfaProviderPropertyManagerTest
Definition: MfaProviderPropertyManagerTest.php:30
‪TYPO3\CMS\Core\Tests\Functional\Authentication\Mfa\MfaProviderPropertyManagerTest\setUp
‪setUp()
Definition: MfaProviderPropertyManagerTest.php:33
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Tests\Functional\Authentication\Mfa\MfaProviderPropertyManagerTest\createTest
‪createTest()
Definition: MfaProviderPropertyManagerTest.php:47
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderPropertyManager
Definition: MfaProviderPropertyManager.php:33
‪TYPO3\CMS\Core\Tests\Functional\Authentication\Mfa
Definition: MfaProviderPropertyManagerTest.php:18
‪TYPO3\CMS\Core\Tests\Functional\Authentication\Mfa\MfaProviderPropertyManagerTest\getPropertyTest
‪getPropertyTest()
Definition: MfaProviderPropertyManagerTest.php:77
‪TYPO3\CMS\Core\Tests\Functional\Authentication\Mfa\MfaProviderPropertyManagerTest\hasPropertyTest
‪hasPropertyTest()
Definition: MfaProviderPropertyManagerTest.php:67
‪TYPO3\CMS\Core\Tests\Functional\Authentication\Mfa\MfaProviderPropertyManagerTest\updatePropertiesTest
‪updatePropertiesTest()
Definition: MfaProviderPropertyManagerTest.php:108
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderPropertyManager\create
‪static MfaProviderPropertyManager create(MfaProviderManifestInterface $provider, AbstractUserAuthentication $user)
Definition: MfaProviderPropertyManager.php:224
‪TYPO3\CMS\Core\Tests\Functional\Authentication\Mfa\MfaProviderPropertyManagerTest\hasProviderEntryTest
‪hasProviderEntryTest()
Definition: MfaProviderPropertyManagerTest.php:58
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication
Definition: AbstractUserAuthentication.php:56
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry
Definition: MfaProviderRegistry.php:28