FallbackPasswordHasherTest.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /**
  22. * Test case for FallbackPasswordHasher
  23. */
  24. class FallbackPasswordHasherTest extends TestCase
  25. {
  26. /**
  27. * Tests that only the first hasher is user for hashing a password
  28. *
  29. * @return void
  30. */
  31. public function testHash(): void
  32. {
  33. $hasher = new FallbackPasswordHasher(['hashers' => ['Weak', 'Default']]);
  34. $weak = new WeakPasswordHasher();
  35. $this->assertSame($weak->hash('foo'), $hasher->hash('foo'));
  36. $simple = new DefaultPasswordHasher();
  37. $hasher = new FallbackPasswordHasher(['hashers' => ['Weak', 'Default']]);
  38. $this->assertSame($weak->hash('foo'), $hasher->hash('foo'));
  39. }
  40. /**
  41. * Tests that the check method will check with configured hashers until a match
  42. * is found
  43. *
  44. * @return void
  45. */
  46. public function testCheck(): void
  47. {
  48. $hasher = new FallbackPasswordHasher(['hashers' => ['Weak', 'Default']]);
  49. $weak = new WeakPasswordHasher();
  50. $simple = new DefaultPasswordHasher();
  51. $hash = $simple->hash('foo');
  52. $otherHash = $weak->hash('foo');
  53. $this->assertTrue($hasher->check('foo', $hash));
  54. $this->assertTrue($hasher->check('foo', $otherHash));
  55. }
  56. /**
  57. * Tests that the check method will work with configured hashers including different
  58. * configs per hasher.
  59. *
  60. * @return void
  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. * @return void
  77. */
  78. public function testNeedsRehash(): void
  79. {
  80. $hasher = new FallbackPasswordHasher(['hashers' => ['Default', 'Weak']]);
  81. $weak = new WeakPasswordHasher();
  82. $otherHash = $weak->hash('foo');
  83. $this->assertTrue($hasher->needsRehash($otherHash));
  84. $simple = new DefaultPasswordHasher();
  85. $hash = $simple->hash('foo');
  86. $this->assertFalse($hasher->needsRehash($hash));
  87. }
  88. }