‪TYPO3CMS  10.4
PasswordHashFactoryTest.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 
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
31 class ‪PasswordHashFactoryTest extends UnitTestCase
32 {
37  {
38  $this->expectException(\InvalidArgumentException::class);
39  $this->expectExceptionCode(1533948312);
40  (new ‪PasswordHashFactory())->get('ThisIsNotAValidHash', 'foo');
41  }
42 
47  {
48  $this->expectException(\LogicException::class);
49  $this->expectExceptionCode(1533949053);
50  ‪$GLOBALS['TYPO3_CONF_VARS']['FE']['passwordHashing']['className'] = '';
51  (new ‪PasswordHashFactory())->get('ThisIsNotAValidHash', 'FE');
52  }
53 
58  {
59  $this->expectException(\LogicException::class);
60  $this->expectExceptionCode(1533949053);
61  ‪$GLOBALS['TYPO3_CONF_VARS']['FE']['passwordHashing']['options'] = '';
62  (new ‪PasswordHashFactory())->get('ThisIsNotAValidHash', 'FE');
63  }
64 
69  {
70  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['availablePasswordHashAlgorithms'] = [ \stdClass::class ];
71  $this->expectException(\LogicException::class);
72  $this->expectExceptionCode(1533818569);
73  (new ‪PasswordHashFactory())->get('ThisIsNotAValidHash', 'BE');
74  }
75 
80  {
81  $this->expectException(InvalidPasswordHashException::class);
82  $this->expectExceptionCode(1533818591);
83  (new ‪PasswordHashFactory())->get('ThisIsNotAValidHash', 'BE');
84  }
85 
90  {
91  $phpassProphecy = $this->prophesize(PhpassPasswordHash::class);
92  GeneralUtility::addInstance(PhpassPasswordHash::class, $phpassProphecy->reveal());
93  $phpassProphecy->isAvailable()->shouldBeCalled()->willReturn(false);
94  $this->expectException(InvalidPasswordHashException::class);
95  $this->expectExceptionCode(1533818591);
96  (new ‪PasswordHashFactory())->get('$P$C7u7E10SBEie/Jbdz0jDtUcWhzgOPF.', 'BE');
97  }
98 
103  {
104  $phpassProphecy = $this->prophesize(PhpassPasswordHash::class);
105  GeneralUtility::addInstance(PhpassPasswordHash::class, $phpassProphecy->reveal());
106  $phpassProphecy->isAvailable()->shouldBeCalled()->willReturn(true);
107  $hash = '$P$C7u7E10SBEie/Jbdz0jDtUcWhzgOPF.';
108  $phpassProphecy->isValidSaltedPW($hash)->shouldBeCalled()->willReturn(false);
109  $this->expectException(InvalidPasswordHashException::class);
110  $this->expectExceptionCode(1533818591);
111  (new ‪PasswordHashFactory())->get($hash, 'BE');
112  }
113 
118  {
119  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['availablePasswordHashAlgorithms'] = [ TestPasswordHash::class ];
120  ‪$GLOBALS['TYPO3_CONF_VARS']['FE']['passwordHashing'] = [
121  'className' => TestPasswordHash::class,
122  'options' => [
123  'foo' => 'bar'
124  ],
125  ];
126  $this->expectException(\RuntimeException::class);
127  $this->expectExceptionCode(1533950385);
128  (new ‪PasswordHashFactory())->get('someHash', 'FE');
129  }
130 
135  {
136  $phpassProphecy = $this->prophesize(PhpassPasswordHash::class);
137  $phpassRevelation = $phpassProphecy->reveal();
138  GeneralUtility::addInstance(PhpassPasswordHash::class, $phpassRevelation);
139  $phpassProphecy->isAvailable()->shouldBeCalled()->willReturn(true);
140  $hash = '$P$C7u7E10SBEie/Jbdz0jDtUcWhzgOPF.';
141  $phpassProphecy->isValidSaltedPW($hash)->shouldBeCalled()->willReturn(true);
142  self::assertSame($phpassRevelation, (new ‪PasswordHashFactory())->get($hash, 'BE'));
143  }
144 
149  {
150  $this->expectException(\InvalidArgumentException::class);
151  $this->expectExceptionCode(1533820041);
152  (new ‪PasswordHashFactory())->getDefaultHashInstance('foo');
153  }
154 
159  {
160  $this->expectException(\LogicException::class);
161  $this->expectExceptionCode(1533950622);
162  ‪$GLOBALS['TYPO3_CONF_VARS']['FE']['passwordHashing']['className'] = '';
163  (new ‪PasswordHashFactory())->getDefaultHashInstance('FE');
164  }
165 
170  {
171  $this->expectException(\LogicException::class);
172  $this->expectExceptionCode(1533950622);
173  ‪$GLOBALS['TYPO3_CONF_VARS']['FE']['passwordHashing']['options'] = '';
174  (new ‪PasswordHashFactory())->getDefaultHashInstance('FE');
175  }
176 
181  {
182  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['saltedpasswords']['FE']['saltedPWHashingMethod'] = Argon2iPasswordHash::class;
183  $hashInstance = (new ‪PasswordHashFactory())->getDefaultHashInstance('FE');
184  self::assertInstanceOf(Argon2iPasswordHash::class, $hashInstance);
185  }
186 
191  {
192  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['saltedpasswords']['BE']['saltedPWHashingMethod'] = Argon2iPasswordHash::class;
193  $hashInstance = (new ‪PasswordHashFactory())->getDefaultHashInstance('BE');
194  self::assertInstanceOf(Argon2iPasswordHash::class, $hashInstance);
195  }
196 
201  {
202  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['passwordHashing']['className'] = \stdClass::class;
203  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['availablePasswordHashAlgorithms'] = [ \stdClass::class ];
204  $this->expectException(\LogicException::class);
205  $this->expectExceptionCode(1533820281);
206  (new ‪PasswordHashFactory())->getDefaultHashInstance('BE');
207  }
208 
213  {
214  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['passwordHashing']['className'] = \stdClass::class;
215  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['availablePasswordHashAlgorithms'] = [ Argon2iPasswordHash::class ];
216  $this->expectException(InvalidPasswordHashException::class);
217  $this->expectExceptionCode(1533820194);
218  (new ‪PasswordHashFactory())->getDefaultHashInstance('BE');
219  }
220 
225  {
226  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['saltedpasswords']['BE']['saltedPWHashingMethod'] = Argon2iPasswordHash::class;
227  $argonProphecy = $this->prophesize(Argon2iPasswordHash::class);
228  GeneralUtility::addInstance(Argon2iPasswordHash::class, $argonProphecy->reveal());
229  $argonProphecy->isAvailable()->shouldBeCalled()->willReturn(false);
230  $this->expectException(InvalidPasswordHashException::class);
231  $this->expectExceptionCode(1533822084);
232  (new ‪PasswordHashFactory())->getDefaultHashInstance('BE');
233  }
234 
239  {
240  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['availablePasswordHashAlgorithms'] = [ TestPasswordHash::class ];
241  ‪$GLOBALS['TYPO3_CONF_VARS']['FE']['passwordHashing'] = [
242  'className' => TestPasswordHash::class,
243  'options' => [
244  'foo' => 'bar'
245  ],
246  ];
247  $this->expectException(\RuntimeException::class);
248  $this->expectExceptionCode(1533950385);
249  (new ‪PasswordHashFactory())->getDefaultHashInstance('FE');
250  }
251 
256  {
257  $methods = [
258  'foo',
259  'bar'
260  ];
261  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['availablePasswordHashAlgorithms'] = $methods;
262  self::assertSame($methods, ‪PasswordHashFactory::getRegisteredSaltedHashingMethods());
263  }
264 
269  {
270  $this->expectException(\RuntimeException::class);
271  $this->expectExceptionCode(1533948733);
272  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['availablePasswordHashAlgorithms'] = [];
274  }
275 }
‪TYPO3\CMS\Core\Crypto\PasswordHashing\PasswordHashFactory
Definition: PasswordHashFactory.php:27
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\PasswordHashFactoryTest\getThrowsExceptionIfModeIsNotBeOrFe
‪getThrowsExceptionIfModeIsNotBeOrFe()
Definition: PasswordHashFactoryTest.php:36
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\PasswordHashFactoryTest\getDefaultHashReturnsInstanceOfConfiguredDefaultFeMethod
‪getDefaultHashReturnsInstanceOfConfiguredDefaultFeMethod()
Definition: PasswordHashFactoryTest.php:180
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\PasswordHashFactoryTest\getReturnsInstanceOfHashClassThatHandlesHash
‪getReturnsInstanceOfHashClassThatHandlesHash()
Definition: PasswordHashFactoryTest.php:134
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\PasswordHashFactoryTest\getThrowsExceptionWithBrokenClassNameModeConfiguration
‪getThrowsExceptionWithBrokenClassNameModeConfiguration()
Definition: PasswordHashFactoryTest.php:46
‪TYPO3\CMS\Core\Crypto\PasswordHashing\InvalidPasswordHashException
Definition: InvalidPasswordHashException.php:26
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\PasswordHashFactoryTest\getThrowsExceptionIfClassThatHandlesAHashSaysNoToHash
‪getThrowsExceptionIfClassThatHandlesAHashSaysNoToHash()
Definition: PasswordHashFactoryTest.php:102
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\PasswordHashFactoryTest\getDefaultHashReturnsInstanceOfConfiguredDefaultBeMethod
‪getDefaultHashReturnsInstanceOfConfiguredDefaultBeMethod()
Definition: PasswordHashFactoryTest.php:190
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\PasswordHashFactoryTest\getDefaultHashThrowsExceptionIfDefaultHashMethodIsNotRegistered
‪getDefaultHashThrowsExceptionIfDefaultHashMethodIsNotRegistered()
Definition: PasswordHashFactoryTest.php:212
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\PasswordHashFactoryTest\getThrowsExceptionIfARegisteredHashDoesNotImplementSaltInterface
‪getThrowsExceptionIfARegisteredHashDoesNotImplementSaltInterface()
Definition: PasswordHashFactoryTest.php:68
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\PasswordHashFactoryTest\getRegisteredSaltedHashingMethodsReturnsRegisteredMethods
‪getRegisteredSaltedHashingMethodsReturnsRegisteredMethods()
Definition: PasswordHashFactoryTest.php:255
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\PasswordHashFactoryTest\getThrowsExceptionIfNoClassIsFoundThatHandlesGivenHash
‪getThrowsExceptionIfNoClassIsFoundThatHandlesGivenHash()
Definition: PasswordHashFactoryTest.php:79
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\PasswordHashFactoryTest\getDefaultHashInstanceThrowsExceptionIfModeIsNotBeOrFe
‪getDefaultHashInstanceThrowsExceptionIfModeIsNotBeOrFe()
Definition: PasswordHashFactoryTest.php:148
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\PasswordHashFactoryTest\getDefaultHashInstanceThrowsExceptionWithBrokenClassNameModeConfiguration
‪getDefaultHashInstanceThrowsExceptionWithBrokenClassNameModeConfiguration()
Definition: PasswordHashFactoryTest.php:158
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\PasswordHashFactoryTest\getThrowsExceptionWithBrokenOptionsModeConfiguration
‪getThrowsExceptionWithBrokenOptionsModeConfiguration()
Definition: PasswordHashFactoryTest.php:57
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\PasswordHashFactoryTest\getDefaultHashInstanceThrowsExceptionWithBrokenOptionsModeConfiguration
‪getDefaultHashInstanceThrowsExceptionWithBrokenOptionsModeConfiguration()
Definition: PasswordHashFactoryTest.php:169
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\PasswordHashFactoryTest\getHandsConfiguredOptionsToHashClassIfMethodIsConfiguredDefaultForMode
‪getHandsConfiguredOptionsToHashClassIfMethodIsConfiguredDefaultForMode()
Definition: PasswordHashFactoryTest.php:117
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\PasswordHashFactoryTest\getDefaultHashThrowsExceptionIfDefaultHashMethodDoesNotImplementSaltInterface
‪getDefaultHashThrowsExceptionIfDefaultHashMethodDoesNotImplementSaltInterface()
Definition: PasswordHashFactoryTest.php:200
‪TYPO3\CMS\Core\Crypto\PasswordHashing\PasswordHashFactory\getRegisteredSaltedHashingMethods
‪static array getRegisteredSaltedHashingMethods()
Definition: PasswordHashFactory.php:139
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\PasswordHashFactoryTest\getRegisteredSaltedHashingMethodsThrowsExceptionIfNoMethodIsConfigured
‪getRegisteredSaltedHashingMethodsThrowsExceptionIfNoMethodIsConfigured()
Definition: PasswordHashFactoryTest.php:268
‪TYPO3\CMS\Core\Crypto\PasswordHashing\PhpassPasswordHash
Definition: PhpassPasswordHash.php:35
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\PasswordHashFactoryTest\getDefaultHashThrowsExceptionIfDefaultHashMethodIsNotAvailable
‪getDefaultHashThrowsExceptionIfDefaultHashMethodIsNotAvailable()
Definition: PasswordHashFactoryTest.php:224
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Fixtures\TestPasswordHash
Definition: TestPasswordHash.php:24
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\PasswordHashFactoryTest\getThrowsExceptionIfClassThatHandlesAHashIsNotAvailable
‪getThrowsExceptionIfClassThatHandlesAHashIsNotAvailable()
Definition: PasswordHashFactoryTest.php:89
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\PasswordHashFactoryTest
Definition: PasswordHashFactoryTest.php:32
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Crypto\PasswordHashing\Argon2iPasswordHash
Definition: Argon2iPasswordHash.php:31
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\PasswordHashFactoryTest\getDefaultHoshHandsConfiguredOptionsToHashClass
‪getDefaultHoshHandsConfiguredOptionsToHashClass()
Definition: PasswordHashFactoryTest.php:238
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing
Definition: Argon2idPasswordHashTest.php:18