FallbackPasswordHasherTest.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.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. */
  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()
  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()
  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()
  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->assertTrue($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()
  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. }