ModernPasswordHasherTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * ModernPasswordHasher file
  4. *
  5. */
  6. App::uses('ModernPasswordHasher', '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 ModernPasswordHasher
  18. *
  19. */
  20. class ModernPasswordHasherTest 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 TestModernPasswordHasherController(new CakeRequest(), new CakeResponse());
  32. $this->Controller->constructClasses();
  33. $this->Controller->startupProcess();
  34. // Modern pwd account
  35. $this->Controller->TestModernPasswordHasherUser->create();
  36. $user = array(
  37. 'username' => 'itisme',
  38. 'email' => '',
  39. 'pwd' => 'secure123456'
  40. );
  41. $res = $this->Controller->TestModernPasswordHasherUser->save($user);
  42. $this->assertTrue((bool)$res);
  43. CakeSession::delete('Auth');
  44. }
  45. /**
  46. * @return void
  47. */
  48. public function tearDown() {
  49. parent::tearDown();
  50. }
  51. /**
  52. * @return void
  53. */
  54. public function testBasics() {
  55. $this->Controller->request->data = array(
  56. 'TestModernPasswordHasherUser' => array(
  57. 'username' => 'itisme',
  58. 'password' => 'xyz'
  59. ),
  60. );
  61. $result = $this->Controller->Auth->login();
  62. $this->assertFalse($result);
  63. }
  64. /**
  65. * @return void
  66. */
  67. public function testLogin() {
  68. $this->Controller->request->data = array(
  69. 'TestModernPasswordHasherUser' => array(
  70. 'username' => 'itisme',
  71. 'password' => 'secure123456'
  72. ),
  73. );
  74. $result = $this->Controller->Auth->login();
  75. $this->assertTrue($result);
  76. // This could be done in login() action after successfully logging in.
  77. $hash = $this->Controller->TestModernPasswordHasherUser->hash('secure123456');
  78. $this->assertFalse($this->Controller->TestModernPasswordHasherUser->needsRehash($hash));
  79. }
  80. }
  81. class TestModernPasswordHasherController extends Controller {
  82. public $uses = array('Tools.TestModernPasswordHasherUser');
  83. public $components = array('Auth');
  84. /**
  85. * @return void
  86. */
  87. public function beforeFilter() {
  88. parent::beforeFilter();
  89. $this->Auth->authenticate = array(
  90. 'Form' => array(
  91. 'passwordHasher' => 'Tools.Modern',
  92. 'fields' => array(
  93. 'username' => 'username',
  94. 'password' => 'password'
  95. ),
  96. 'userModel' => 'Tools.TestModernPasswordHasherUser'
  97. )
  98. );
  99. }
  100. }
  101. class TestModernPasswordHasherUser extends Model {
  102. public $useTable = 'tools_auth_users';
  103. /**
  104. * TestModernPasswordHasherUser::beforeSave()
  105. *
  106. * @param array $options
  107. * @return bool Success
  108. */
  109. public function beforeSave($options = array()) {
  110. if (!empty($this->data[$this->alias]['pwd'])) {
  111. $this->data[$this->alias]['password'] = $this->hash($this->data[$this->alias]['pwd']);
  112. }
  113. return true;
  114. }
  115. /**
  116. * @param string $pwd
  117. * @return string Hash
  118. */
  119. public function hash($pwd) {
  120. $passwordHasher = $this->_getPasswordHasher('Tools.Modern');
  121. return $passwordHasher->hash($pwd);
  122. }
  123. /**
  124. * @param string $pwd
  125. * @return bool Success
  126. */
  127. public function needsRehash($pwd) {
  128. $passwordHasher = $this->_getPasswordHasher('Tools.Modern');
  129. return $passwordHasher->needsRehash($pwd);
  130. }
  131. /**
  132. * @param mixed $hasher Name or options array.
  133. * @return PasswordHasher
  134. */
  135. protected function _getPasswordHasher($hasher) {
  136. $class = $hasher;
  137. $config = [];
  138. if (is_array($hasher)) {
  139. $class = $hasher['className'];
  140. unset($hasher['className']);
  141. $config = $hasher;
  142. }
  143. list($plugin, $class) = pluginSplit($class, true);
  144. $className = $class . 'PasswordHasher';
  145. App::uses($className, $plugin . 'Controller/Component/Auth');
  146. if (!class_exists($className)) {
  147. throw new CakeException(sprintf('Password hasher class "%s" was not found.', $class));
  148. }
  149. if (!is_subclass_of($className, 'AbstractPasswordHasher')) {
  150. throw new CakeException('Password hasher must extend AbstractPasswordHasher class.');
  151. }
  152. return new $className($config);
  153. }
  154. }