FallbackPasswordHasherTest.php 3.1 KB

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