FallbackPasswordHasherTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * FallbackPasswordHasher file
  4. *
  5. */
  6. App::uses('FallbackPasswordHasher', 'Tools.Controller/Component/Auth');
  7. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  8. App::uses('Controller', 'Controller');
  9. App::uses('CakeRequest', 'Network');
  10. App::uses('CakeResponse', 'Network');
  11. App::uses('Model', 'Model');
  12. App::uses('CakeSession', 'Model/Datasource');
  13. if (!defined('PASSWORD_BCRYPT')) {
  14. require CakePlugin::path('Tools') . 'Lib/Bootstrap/Password.php';
  15. }
  16. /**
  17. * Test case for FallbackPasswordHasher
  18. *
  19. */
  20. class FallbackPasswordHasherTest extends MyCakeTestCase {
  21. public $fixtures = ['plugin.tools.tools_auth_user'];
  22. public $Controller;
  23. public $request;
  24. /**
  25. * Setup
  26. *
  27. * @return void
  28. */
  29. public function setUp() {
  30. parent::setUp();
  31. $this->Controller = new TestFallbackPasswordHasherController(new CakeRequest(), new CakeResponse());
  32. $this->Controller->constructClasses();
  33. $this->Controller->startupProcess();
  34. // Modern pwd account
  35. $this->Controller->TestFallbackPasswordHasherUser->create();
  36. $user = array(
  37. 'username' => 'itisme',
  38. 'email' => '',
  39. 'pwd' => 'secure123456'
  40. );
  41. $res = $this->Controller->TestFallbackPasswordHasherUser->save($user);
  42. $this->assertTrue((bool)$res);
  43. // Old pwd account
  44. $this->Controller->TestFallbackPasswordHasherUser->create();
  45. $user = array(
  46. 'username' => 'itwasme',
  47. 'email' => '',
  48. 'password' => Security::hash('123456', null, true)
  49. );
  50. $res = $this->Controller->TestFallbackPasswordHasherUser->save($user);
  51. $this->assertTrue((bool)$res);
  52. CakeSession::delete('Auth');
  53. //var_dump($this->Controller->TestFallbackPasswordHasherUser->find('all'));
  54. }
  55. public function tearDown() {
  56. parent::tearDown();
  57. }
  58. /**
  59. * @return void
  60. */
  61. public function testBasics() {
  62. $this->Controller->request->data = array(
  63. 'TestFallbackPasswordHasherUser' => array(
  64. 'username' => 'itisme',
  65. 'password' => 'xyz'
  66. ),
  67. );
  68. $result = $this->Controller->Auth->login();
  69. $this->assertFalse($result);
  70. }
  71. /**
  72. * @return void
  73. */
  74. public function testLogin() {
  75. $this->Controller->request->data = array(
  76. 'TestFallbackPasswordHasherUser' => array(
  77. 'username' => 'itisme',
  78. 'password' => 'secure123456'
  79. ),
  80. );
  81. $result = $this->Controller->Auth->login();
  82. $this->assertTrue($result);
  83. // This could be done in login() action after successfully logging in.
  84. $hash = $this->Controller->TestFallbackPasswordHasherUser->hash('secure123456');
  85. $this->assertFalse($this->Controller->TestFallbackPasswordHasherUser->needsRehash($hash));
  86. }
  87. /**
  88. * @return void
  89. */
  90. public function testLoginOld() {
  91. $this->Controller->request->data = array(
  92. 'TestFallbackPasswordHasherUser' => array(
  93. 'username' => 'itwasme',
  94. 'password' => '123456'
  95. ),
  96. );
  97. $result = $this->Controller->Auth->login();
  98. $this->assertTrue($result);
  99. // This could be done in login() action after successfully logging in.
  100. $hash = Security::hash('123456', null, true);
  101. $this->assertTrue($this->Controller->TestFallbackPasswordHasherUser->needsRehash($hash));
  102. }
  103. }
  104. class TestFallbackPasswordHasherController extends Controller {
  105. public $uses = array('Tools.TestFallbackPasswordHasherUser');
  106. public $components = array('Auth');
  107. public function beforeFilter() {
  108. parent::beforeFilter();
  109. $options = array(
  110. 'className' => 'Tools.Fallback',
  111. 'hashers' => array(
  112. 'Tools.Modern', 'Simple'
  113. //'Tools.Modern' => array('userModel' => 'Tools.TestFallbackPasswordHasherUser'), 'Simple' => array('userModel' => 'Tools.TestFallbackPasswordHasherUser')
  114. )
  115. );
  116. $this->Auth->authenticate = array(
  117. 'Form' => array(
  118. 'passwordHasher' => $options,
  119. 'fields' => array(
  120. 'username' => 'username',
  121. 'password' => 'password'
  122. ),
  123. 'userModel' => 'Tools.TestFallbackPasswordHasherUser'
  124. )
  125. );
  126. }
  127. }
  128. class TestFallbackPasswordHasherUser extends Model {
  129. public $useTable = 'tools_auth_users';
  130. /**
  131. * TestFallbackPasswordHasherUser::beforeSave()
  132. *
  133. * @param array $options
  134. * @return bool Success
  135. */
  136. public function beforeSave($options = array()) {
  137. if (!empty($this->data[$this->alias]['pwd'])) {
  138. $this->data[$this->alias]['password'] = $this->hash($this->data[$this->alias]['pwd']);
  139. }
  140. return true;
  141. }
  142. /**
  143. * @param string $pwd
  144. * @return string Hash
  145. */
  146. public function hash($pwd) {
  147. $options = array(
  148. 'className' => 'Tools.Fallback',
  149. 'hashers' => array(
  150. 'Tools.Modern', 'Simple'
  151. )
  152. );
  153. $passwordHasher = $this->_getPasswordHasher($options);
  154. return $passwordHasher->hash($pwd);
  155. }
  156. /**
  157. * @param string $pwd
  158. * @return bool Success
  159. */
  160. public function needsRehash($pwd) {
  161. $options = array(
  162. 'className' => 'Tools.Fallback',
  163. 'hashers' => array(
  164. 'Tools.Modern', 'Simple'
  165. )
  166. );
  167. $passwordHasher = $this->_getPasswordHasher($options);
  168. return $passwordHasher->needsRehash($pwd);
  169. }
  170. /**
  171. * PasswordableBehavior::_getPasswordHasher()
  172. *
  173. * @param mixed $hasher Name or options array.
  174. * @return PasswordHasher
  175. */
  176. protected function _getPasswordHasher($hasher) {
  177. $class = $hasher;
  178. $config = [];
  179. if (is_array($hasher)) {
  180. $class = $hasher['className'];
  181. unset($hasher['className']);
  182. $config = $hasher;
  183. }
  184. list($plugin, $class) = pluginSplit($class, true);
  185. $className = $class . 'PasswordHasher';
  186. App::uses($className, $plugin . 'Controller/Component/Auth');
  187. if (!class_exists($className)) {
  188. throw new CakeException(sprintf('Password hasher class "%s" was not found.', $class));
  189. }
  190. if (!is_subclass_of($className, 'AbstractPasswordHasher')) {
  191. throw new CakeException('Password hasher must extend AbstractPasswordHasher class.');
  192. }
  193. return new $className($config);
  194. }
  195. }