FallbackPasswordHasherTest.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Auth;
  17. use Cake\Auth\DefaultPasswordHasher;
  18. use Cake\Auth\FallbackPasswordHasher;
  19. use Cake\Auth\WeakPasswordHasher;
  20. use Cake\TestSuite\TestCase;
  21. use Cake\Utility\Security;
  22. /**
  23. * Test case for FallbackPasswordHasher
  24. */
  25. class FallbackPasswordHasherTest extends TestCase
  26. {
  27. public function setUp(): void
  28. {
  29. parent::setUp();
  30. Security::setSalt('YJfIxfs2guVoUubWDYhG93b0qyJfIxfs2guwvniR2G0FgaC9mia1390as13dla8kjasdlwerpoiASf');
  31. }
  32. /**
  33. * Tests that only the first hasher is user for hashing a password
  34. */
  35. public function testHash(): void
  36. {
  37. $hasher = new FallbackPasswordHasher(['hashers' => ['Weak', 'Default']]);
  38. $weak = new WeakPasswordHasher();
  39. $this->assertSame($weak->hash('foo'), $hasher->hash('foo'));
  40. $simple = new DefaultPasswordHasher();
  41. $hasher = new FallbackPasswordHasher(['hashers' => ['Weak', 'Default']]);
  42. $this->assertSame($weak->hash('foo'), $hasher->hash('foo'));
  43. }
  44. /**
  45. * Tests that the check method will check with configured hashers until a match
  46. * is found
  47. */
  48. public function testCheck(): void
  49. {
  50. $hasher = new FallbackPasswordHasher(['hashers' => ['Weak', 'Default']]);
  51. $weak = new WeakPasswordHasher();
  52. $simple = new DefaultPasswordHasher();
  53. $hash = $simple->hash('foo');
  54. $otherHash = $weak->hash('foo');
  55. $this->assertTrue($hasher->check('foo', $hash));
  56. $this->assertTrue($hasher->check('foo', $otherHash));
  57. }
  58. /**
  59. * Tests that the check method will work with configured hashers including different
  60. * configs per hasher.
  61. */
  62. public function testCheckWithConfigs(): void
  63. {
  64. $hasher = new FallbackPasswordHasher(['hashers' => ['Default', 'Weak' => ['hashType' => 'md5']]]);
  65. $legacy = new WeakPasswordHasher(['hashType' => 'md5']);
  66. $simple = new DefaultPasswordHasher();
  67. $hash = $simple->hash('foo');
  68. $legacyHash = $legacy->hash('foo');
  69. $this->assertNotSame($hash, $legacyHash);
  70. $this->assertTrue($hasher->check('foo', $hash));
  71. $this->assertTrue($hasher->check('foo', $legacyHash));
  72. }
  73. /**
  74. * Tests that the password only needs to be re-built according to the first hasher
  75. */
  76. public function testNeedsRehash(): void
  77. {
  78. $hasher = new FallbackPasswordHasher(['hashers' => ['Default', 'Weak']]);
  79. $weak = new WeakPasswordHasher();
  80. $otherHash = $weak->hash('foo');
  81. $this->assertTrue($hasher->needsRehash($otherHash));
  82. $simple = new DefaultPasswordHasher();
  83. $hash = $simple->hash('foo');
  84. $this->assertFalse($hasher->needsRehash($hash));
  85. }
  86. }