FallbackPasswordHasher.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\Auth;
  16. use Cake\Auth\AbstractPasswordHasher;
  17. /**
  18. * A password hasher that can use multiple different hashes where only
  19. * one is the preferred one. This is useful when trying to migrate an
  20. * existing database of users from one password type to another.
  21. *
  22. */
  23. class FallbackPasswordHasher extends AbstractPasswordHasher {
  24. /**
  25. * Default config for this object.
  26. *
  27. * @var array
  28. */
  29. protected $_defaultConfig = [
  30. 'hashers' => []
  31. ];
  32. /**
  33. * Holds the list of password hasher objects that will be used
  34. *
  35. * @var array
  36. */
  37. protected $_hashers = [];
  38. /**
  39. * Constructor
  40. *
  41. * @param array $config configuration options for this object. Requires the
  42. * `hashers` key to be present in the array with a list of other hashers to be
  43. * used
  44. */
  45. public function __construct(array $config = array()) {
  46. parent::__construct($config);
  47. foreach ($this->_config['hashers'] as $hasher) {
  48. $this->_hashers[] = PasswordHasherFactory::build($hasher);
  49. }
  50. }
  51. /**
  52. * Generates password hash.
  53. *
  54. * Uses the first password hasher in the list to generate the hash
  55. *
  56. * @param string $password Plain text password to hash.
  57. * @return string Password hash
  58. */
  59. public function hash($password) {
  60. return $this->_hashers[0]->hash($password);
  61. }
  62. /**
  63. * Verifies that the provided password corresponds to its hashed version
  64. *
  65. * This will iterate over all configured hashers until one of them returns
  66. * true.
  67. *
  68. * @param string $password Plain text password to hash.
  69. * @param string $hashedPassword Existing hashed password.
  70. * @return bool True if hashes match else false.
  71. */
  72. public function check($password, $hashedPassword) {
  73. foreach ($this->_hashers as $hasher) {
  74. if ($hasher->check($password, $hashedPassword)) {
  75. return true;
  76. }
  77. }
  78. return false;
  79. }
  80. /**
  81. * Returns true if the password need to be rehashed, with the first hasher present
  82. * in the list of hashers
  83. *
  84. * @param string $password The password to verify
  85. * @return bool
  86. */
  87. public function needsRehash($password) {
  88. return $this->_hashers[0]->needsRehash($password);
  89. }
  90. }