PasswordableBehaviorTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. <?php
  2. App::uses('ComponentCollection', 'Controller');
  3. App::uses('AuthComponent', 'Controller/Component');
  4. App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
  5. if (!function_exists('password_hash')) {
  6. require_once CakePlugin::path('Tools') . 'Lib/Bootstrap/Password.php';
  7. }
  8. class PasswordableBehaviorTest extends CakeTestCase {
  9. public $fixtures = [
  10. 'plugin.tools.tools_user', 'plugin.tools.role',
  11. ];
  12. /**
  13. * SetUp method
  14. *
  15. * @return void
  16. */
  17. public function setUp() {
  18. parent::setUp();
  19. Configure::write('Config.language', 'deu');
  20. Configure::delete('Passwordable');
  21. Configure::write('Passwordable.auth', 'AuthTest');
  22. $this->User = ClassRegistry::init('Tools.ToolsUser');
  23. if (isset($this->User->validate['pwd'])) {
  24. unset($this->User->validate['pwd']);
  25. }
  26. if (isset($this->User->validate['pwd_repeat'])) {
  27. unset($this->User->validate['pwd_repeat']);
  28. }
  29. if (isset($this->User->validate['pwd_current'])) {
  30. unset($this->User->validate['pwd_current']);
  31. }
  32. if (isset($this->User->order)) {
  33. unset($this->User->order);
  34. }
  35. $this->User->create();
  36. $data = [
  37. 'id' => '5',
  38. 'name' => 'admin',
  39. 'password' => Security::hash('somepwd', null, true),
  40. 'role_id' => '1'
  41. ];
  42. $this->User->set($data);
  43. $result = $this->User->save();
  44. $this->assertTrue((bool)$result);
  45. Router::setRequestInfo(new CakeRequest(null, false));
  46. }
  47. /**
  48. * PasswordableBehaviorTest::testObject()
  49. *
  50. * @return void
  51. */
  52. public function testObject() {
  53. $this->User->Behaviors->load('Tools.Passwordable', []);
  54. $this->assertInstanceOf('PasswordableBehavior', $this->User->Behaviors->Passwordable);
  55. $result = $this->User->Behaviors->loaded('Passwordable');
  56. $this->assertTrue($result);
  57. }
  58. /**
  59. * Make sure validation is triggered correctly
  60. *
  61. * @return void
  62. */
  63. public function testValidate() {
  64. $this->User->Behaviors->load('Tools.Passwordable', []);
  65. $this->User->create();
  66. $data = [
  67. 'pwd' => '123456',
  68. ];
  69. $this->User->set($data);
  70. $is = $this->User->save();
  71. //debug($this->User->validationErrors);
  72. $this->assertFalse($is);
  73. $this->assertEquals(['pwd_repeat'], array_keys($this->User->validationErrors));
  74. $this->User->create();
  75. $data = [
  76. 'pwd' => '1234ab',
  77. 'pwd_repeat' => '123456'
  78. ];
  79. $this->User->set($data);
  80. $is = $this->User->save();
  81. //debug($this->User->validationErrors);
  82. $this->assertFalse($is);
  83. $this->assertEquals([__d('tools', 'valErrPwdNotMatch')], $this->User->validationErrors['pwd_repeat']);
  84. $this->User->create();
  85. $data = [
  86. 'pwd' => '123456',
  87. 'pwd_repeat' => '123456'
  88. ];
  89. $this->User->set($data);
  90. //debug($this->User->validate);
  91. $is = $this->User->validates();
  92. $this->assertTrue(!empty($is));
  93. }
  94. /**
  95. * Test that confirm false does not require confirmation
  96. *
  97. * @return void
  98. */
  99. public function testValidateNoConfirm() {
  100. $this->User->Behaviors->load('Tools.Passwordable', ['confirm' => false]);
  101. $this->User->create();
  102. $data = [
  103. 'pwd' => '123456',
  104. ];
  105. $this->User->set($data);
  106. $is = $this->User->save();
  107. //debug($is);
  108. $this->assertTrue(!empty($is));
  109. }
  110. /**
  111. * Trigger validation and update process if no values are entered but are required
  112. *
  113. * @return void
  114. */
  115. public function testValidateRequired() {
  116. $this->User->Behaviors->load('Tools.Passwordable');
  117. $this->User->create();
  118. $data = [
  119. 'pwd' => '',
  120. 'pwd_repeat' => ''
  121. ];
  122. $this->User->set($data);
  123. $is = $this->User->save();
  124. $this->assertFalse($is);
  125. $this->assertEquals(['pwd', 'pwd_repeat'], array_keys($this->User->validationErrors));
  126. }
  127. /**
  128. * Validation and update process gets skipped if no values are entered
  129. *
  130. * @return void
  131. */
  132. public function testValidateNotRequired() {
  133. $this->User->Behaviors->load('Tools.Passwordable', ['require' => false]);
  134. $this->User->create();
  135. $data = [
  136. 'name' => 'foo', // we need at least one field besides the password on CREATE
  137. 'pwd' => '',
  138. 'pwd_repeat' => ''
  139. ];
  140. $this->User->set($data);
  141. $is = $this->User->save();
  142. $this->assertTrue((bool)$is);
  143. $this->assertEquals(['name', 'id'], array_keys($is[$this->User->alias]));
  144. $id = $this->User->id;
  145. $data = [
  146. 'id' => $id,
  147. 'pwd' => '',
  148. 'pwd_repeat' => ''
  149. ];
  150. $this->User->set($data);
  151. $is = $this->User->save();
  152. $this->assertTrue((bool)$is);
  153. $this->assertEquals(['id'], array_keys($is[$this->User->alias]));
  154. }
  155. /**
  156. * PasswordableBehaviorTest::testValidateEmptyWithCurrentPassword()
  157. *
  158. * @return void
  159. */
  160. public function testValidateEmptyWithCurrentPassword() {
  161. $this->User->Behaviors->load('Tools.Passwordable', ['current' => true]);
  162. $this->User->create();
  163. $data = [
  164. 'id' => '123',
  165. 'pwd' => '',
  166. 'pwd_repeat' => '',
  167. 'pwd_current' => '123456',
  168. ];
  169. $this->User->set($data);
  170. $is = $this->User->save();
  171. //debug($this->User->validationErrors);
  172. $this->assertFalse($is);
  173. $this->assertEquals(['pwd', 'pwd_repeat', 'pwd_current'], array_keys($this->User->validationErrors));
  174. $this->tearDown();
  175. $this->setUp();
  176. $this->User->Behaviors->load('Tools.Passwordable', ['require' => false, 'current' => true]);
  177. $this->User->create();
  178. $data = [
  179. 'name' => 'foo',
  180. 'pwd' => '',
  181. 'pwd_repeat' => '',
  182. 'pwd_current' => '',
  183. ];
  184. $is = $this->User->save($data);
  185. $this->assertTrue(!empty($is));
  186. }
  187. /**
  188. * PasswordableBehaviorTest::testValidateCustomRule()
  189. *
  190. * @return void
  191. */
  192. public function testValidateCustomRule() {
  193. $rules = [
  194. 'validateCustom' => [
  195. 'rule' => ['custom', '#^[a-z0-9]+$#'], // Just a test example, never use this regexp!
  196. 'message' => 'Foo Bar',
  197. 'last' => true,
  198. ],
  199. 'validateCustomExt' => [
  200. 'rule' => ['custom', '#^[a-z]+$#'], // Just a test example, never use this regexp!
  201. 'message' => 'Foo Bar Ext',
  202. 'last' => true,
  203. ]
  204. ];
  205. $this->User->Behaviors->load('Tools.Passwordable', [
  206. 'customValidation' => $rules]);
  207. $this->User->create();
  208. $data = [
  209. 'pwd' => '%123456',
  210. 'pwd_repeat' => '%123456'
  211. ];
  212. $this->User->set($data);
  213. $is = $this->User->save();
  214. $this->assertFalse($is);
  215. $result = $this->User->validationErrors;
  216. $expected = ['pwd' => ['Foo Bar']];
  217. $this->assertSame($expected, $result);
  218. $this->User->create();
  219. $data = [
  220. 'pwd' => 'abc123',
  221. 'pwd_repeat' => 'abc123'
  222. ];
  223. $this->User->set($data);
  224. $is = $this->User->save();
  225. $this->assertFalse($is);
  226. $result = $this->User->validationErrors;
  227. $expected = ['pwd' => ['Foo Bar Ext']];
  228. $this->assertSame($expected, $result);
  229. $this->User->create();
  230. $data = [
  231. 'pwd' => 'abcdef',
  232. 'pwd_repeat' => 'abcdef'
  233. ];
  234. $this->User->set($data);
  235. $is = $this->User->save();
  236. $this->assertTrue((bool)$is);
  237. }
  238. /**
  239. * Test aliases for field names
  240. */
  241. public function testDifferentFieldNames() {
  242. $this->User->Behaviors->load('Tools.Passwordable', [
  243. 'formField' => 'passw',
  244. 'formFieldRepeat' => 'passw_repeat',
  245. 'formFieldCurrent' => 'passw_current',
  246. ]);
  247. $this->User->create();
  248. $data = [
  249. 'passw' => '123456',
  250. 'passw_repeat' => '123456'
  251. ];
  252. $this->User->set($data);
  253. //debug($this->User->data);
  254. $is = $this->User->save();
  255. $this->assertTrue(!empty($is));
  256. }
  257. /**
  258. * Assert that allowSame false does not allow storing the same password as previously entered
  259. */
  260. public function testNotSame() {
  261. $this->User->Behaviors->load('Tools.Passwordable', [
  262. 'formField' => 'passw',
  263. 'formFieldRepeat' => 'passw_repeat',
  264. 'formFieldCurrent' => 'passw_current',
  265. 'allowSame' => false,
  266. 'current' => true,
  267. ]);
  268. $this->User->create();
  269. $data = [
  270. 'id' => '5',
  271. 'passw_current' => 'something',
  272. 'passw' => 'somepwd',
  273. 'passw_repeat' => 'somepwd'
  274. ];
  275. $this->User->set($data);
  276. $is = $this->User->save();
  277. //debug($this->User->validationErrors);
  278. $this->assertFalse($is);
  279. $this->User->create();
  280. $data = [
  281. 'id' => '5',
  282. 'passw_current' => 'somepwd',
  283. 'passw' => 'newpwd',
  284. 'passw_repeat' => 'newpwd'
  285. ];
  286. $this->User->set($data);
  287. $is = $this->User->save();
  288. $this->assertTrue(!empty($is));
  289. }
  290. /**
  291. * Assert that allowSame false does not allow storing the same password as previously entered
  292. */
  293. public function testNotSameWithoutCurrentField() {
  294. $this->User->Behaviors->load('Tools.Passwordable', [
  295. 'formField' => 'passw',
  296. 'formFieldRepeat' => 'passw_repeat',
  297. 'allowSame' => false,
  298. 'current' => false
  299. ]);
  300. $this->User->create();
  301. $data = [
  302. 'passw' => 'somepwd',
  303. 'passw_repeat' => 'somepwd'
  304. ];
  305. $this->User->set($data);
  306. $is = $this->User->save();
  307. $this->assertTrue((bool)$is);
  308. $id = $is[$this->User->alias]['id'];
  309. $this->User->create();
  310. $data = [
  311. 'id' => $id,
  312. 'passw' => 'somepwd',
  313. 'passw_repeat' => 'somepwd'
  314. ];
  315. $this->User->set($data);
  316. $is = $this->User->save();
  317. $this->assertFalse((bool)$is);
  318. $this->User->create();
  319. $data = [
  320. 'id' => $id,
  321. 'passw' => 'newpwd',
  322. 'passw_repeat' => 'newpwd'
  323. ];
  324. $this->User->set($data);
  325. $is = $this->User->save();
  326. $this->assertTrue((bool)$is);
  327. }
  328. /**
  329. * Assert that on edit it does not wrongly pass validation (require => false)
  330. */
  331. public function testRequireFalse() {
  332. $this->User->Behaviors->load('Tools.Passwordable', [
  333. 'formField' => 'passw',
  334. 'formFieldRepeat' => 'passw_repeat',
  335. 'require' => false
  336. ]);
  337. $this->User->create();
  338. $data = [
  339. 'passw' => 'somepwd',
  340. 'passw_repeat' => 'somepwd'
  341. ];
  342. $this->User->set($data);
  343. $is = $this->User->save();
  344. $this->assertTrue((bool)$is);
  345. $id = $is[$this->User->alias]['id'];
  346. $this->User->create();
  347. $data = [
  348. 'id' => $id,
  349. 'passw' => 'somepwd2',
  350. 'passw_repeat' => ''
  351. ];
  352. $this->User->set($data);
  353. $is = $this->User->save();
  354. $this->assertFalse((bool)$is);
  355. //debug($this->User->validationErrors);
  356. $this->User->create();
  357. $data = [
  358. 'id' => $id,
  359. 'passw' => 'somepwd2',
  360. 'passw_repeat' => 'somepwd2'
  361. ];
  362. $this->User->set($data);
  363. $is = $this->User->save();
  364. $this->assertTrue((bool)$is);
  365. }
  366. /**
  367. * Needs faking of pwd check...
  368. */
  369. public function testValidateCurrent() {
  370. $this->assertFalse($this->User->Behaviors->loaded('Passwordable'));
  371. $this->User->create();
  372. $data = [
  373. 'name' => 'xyz',
  374. 'password' => Security::hash('somepwd', null, true)];
  375. $result = $this->User->save($data);
  376. $this->assertTrue(!empty($result));
  377. $uid = (string)$this->User->id;
  378. $this->User->Behaviors->load('Tools.Passwordable', ['current' => true]);
  379. $this->User->create();
  380. $data = [
  381. 'id' => $uid,
  382. 'pwd' => '123456',
  383. 'pwd_repeat' => '12345678',
  384. //'pwd_current' => '',
  385. ];
  386. $this->User->set($data);
  387. $this->assertTrue($this->User->Behaviors->loaded('Passwordable'));
  388. $is = $this->User->save();
  389. $this->assertFalse($is);
  390. $this->User->create();
  391. $data = [
  392. 'id' => $uid,
  393. 'pwd_current' => 'somepwdx',
  394. 'pwd' => '123456',
  395. 'pwd_repeat' => '123456'
  396. ];
  397. $this->User->set($data);
  398. $is = $this->User->save();
  399. $this->assertFalse($is);
  400. $this->User->create();
  401. $data = [
  402. 'id' => $uid,
  403. 'name' => 'Yeah',
  404. 'pwd_current' => 'somepwd',
  405. 'pwd' => '123456',
  406. 'pwd_repeat' => '123456'
  407. ];
  408. $this->User->set($data);
  409. // Test whitelist setting - only "password" needs to gets auto-added
  410. $options = ['validate' => true, 'fieldList' => ['id', 'pwd', 'pwd_repeat', 'pwd_current']];
  411. $is = $this->User->save(null, $options);
  412. $this->assertTrue(!empty($is));
  413. $user = $this->User->findById($uid);
  414. // The password is updated, the name not
  415. $this->assertSame($is['ToolsUser']['password'], $user['ToolsUser']['password']);
  416. $this->assertSame('xyz', $user['ToolsUser']['name']);
  417. // Proof that we manually need to add pwd, pwd_repeat etc due to a bug in CakePHP<=2.4 allowing behaviors to only modify saving,
  418. // not validating of additional whitelist fields. Validation for those will be just skipped, no matter what the behavior tries
  419. // to set.
  420. $this->User->create();
  421. $data = [
  422. 'id' => $uid,
  423. 'name' => 'Yeah',
  424. 'pwd_current' => '123', // Obviously wrong
  425. 'pwd' => 'some', // Too short
  426. 'pwd_repeat' => 'somex' // Don't match
  427. ];
  428. $this->User->set($data);
  429. // Test whitelist setting - only "password" gets auto-added, pwd, pwd_repeat etc need to be added manually
  430. // NOTE that I had to remove the code for adding those fields from the behavior (as it was not functional)
  431. // So of course, this won't work now as expected. But feel free to try to add them in the behavior. Results will be the same.
  432. $options = ['validate' => true, 'fieldList' => ['id', 'name']];
  433. $is = $this->User->save(null, $options);
  434. if ((float)Configure::version() >= 2.5) {
  435. // Validation errors triggered - as expected
  436. $this->assertFalse($is);
  437. $this->assertSame(['pwd', 'pwd_repeat', 'pwd_current'], array_keys($this->User->validationErrors));
  438. return;
  439. }
  440. // Save is successful
  441. $this->assertTrue(!empty($is));
  442. $user = $this->User->get($uid);
  443. $this->assertSame('Yeah', $user['ToolsUser']['name']);
  444. // The password is not updated, the name is
  445. $this->assertSame($is['ToolsUser']['password'], $user['ToolsUser']['password']);
  446. $this->assertSame('Yeah', $user['ToolsUser']['name']);
  447. }
  448. /**
  449. * Test cake2.4 passwordHasher feature
  450. *
  451. * @return void
  452. */
  453. public function testPasswordHasher() {
  454. $this->skipIf((float)Configure::version() < 2.4, 'Needs 2.4 and above');
  455. $this->User->Behaviors->load('Tools.Passwordable', [
  456. 'formField' => 'pwd',
  457. 'formFieldRepeat' => 'pwd_repeat',
  458. 'allowSame' => false,
  459. 'current' => false,
  460. 'passwordHasher' => 'Complex',
  461. ]);
  462. $this->User->create();
  463. $data = [
  464. 'pwd' => 'somepwd',
  465. 'pwd_repeat' => 'somepwd'
  466. ];
  467. $this->User->set($data);
  468. $result = $this->User->save();
  469. $this->assertTrue((bool)$result);
  470. $uid = (string)$this->User->id;
  471. $this->User->Behaviors->load('Tools.Passwordable', ['current' => true]);
  472. $this->User->create();
  473. $data = [
  474. 'id' => $uid,
  475. 'pwd' => '123456',
  476. 'pwd_repeat' => '12345678',
  477. //'pwd_current' => '',
  478. ];
  479. $this->User->set($data);
  480. $this->assertTrue($this->User->Behaviors->loaded('Passwordable'));
  481. $is = $this->User->save();
  482. $this->assertFalse($is);
  483. $this->User->create();
  484. $data = [
  485. 'id' => $uid,
  486. 'pwd_current' => 'somepwdx',
  487. 'pwd' => '123456',
  488. 'pwd_repeat' => '123456'
  489. ];
  490. $this->User->set($data);
  491. $is = $this->User->save();
  492. $this->assertFalse($is);
  493. $this->User->create();
  494. $data = [
  495. 'id' => $uid,
  496. 'pwd_current' => 'somepwd',
  497. 'pwd' => '123456',
  498. 'pwd_repeat' => '123456'
  499. ];
  500. $this->User->set($data);
  501. $is = $this->User->save();
  502. $this->assertTrue(!empty($is));
  503. }
  504. /**
  505. * PasswordableBehaviorTest::testBlowfish()
  506. *
  507. * @return void
  508. */
  509. public function testBlowfish() {
  510. $this->User->Behaviors->load('Tools.Passwordable', [
  511. 'allowSame' => false,
  512. 'current' => false,
  513. 'authType' => 'Blowfish',
  514. ]);
  515. $this->User->create();
  516. $data = [
  517. 'pwd' => 'somepwd',
  518. 'pwd_repeat' => 'somepwd'
  519. ];
  520. $this->User->set($data);
  521. $result = $this->User->save();
  522. $this->assertTrue((bool)$result);
  523. $uid = (string)$this->User->id;
  524. $this->User->Behaviors->load('Tools.Passwordable', ['current' => true]);
  525. // Without the current password it will not continue
  526. $this->User->create();
  527. $data = [
  528. 'id' => $uid,
  529. 'pwd' => '123456',
  530. 'pwd_repeat' => '12345678',
  531. ];
  532. $this->User->set($data);
  533. $this->assertTrue($this->User->Behaviors->loaded('Passwordable'));
  534. $result = $this->User->save();
  535. $this->assertFalse($result);
  536. // Without the correct current password it will not continue
  537. $this->User->create();
  538. $data = [
  539. 'id' => $uid,
  540. 'pwd_current' => 'somepwdx',
  541. 'pwd' => '123456',
  542. 'pwd_repeat' => '123456'
  543. ];
  544. $this->User->set($data);
  545. $result = $this->User->save();
  546. $this->assertFalse($result);
  547. // Now it will
  548. $this->User->create();
  549. $data = [
  550. 'id' => $uid,
  551. 'pwd_current' => 'somepwd',
  552. 'pwd' => '123456',
  553. 'pwd_repeat' => '123456'
  554. ];
  555. $this->User->set($data);
  556. $result = $this->User->save();
  557. $this->assertTrue((bool)$result);
  558. }
  559. /**
  560. * Tests that passwords prior to PHP5.5 and/or password_hash() are still working
  561. * if Tools.Modern is being used.
  562. *
  563. * @return void
  564. */
  565. public function testBlowfishWithBC() {
  566. $this->skipIf(!function_exists('password_hash'), 'password_hash() is not available.');
  567. $oldHash = Security::hash('foobar', 'blowfish', false);
  568. $newHash = password_hash('foobar', PASSWORD_BCRYPT);
  569. $this->User->Behaviors->load('Tools.Passwordable', [
  570. 'allowSame' => false,
  571. 'current' => false,
  572. 'authType' => 'Blowfish',
  573. 'passwordHasher' => 'Shim.Modern'
  574. ]);
  575. $this->User->create();
  576. $data = [
  577. 'pwd' => 'somepwd',
  578. 'pwd_repeat' => 'somepwd'
  579. ];
  580. $this->User->set($data);
  581. $result = $this->User->save();
  582. $this->assertTrue((bool)$result);
  583. $uid = (string)$this->User->id;
  584. // Same pwd is not allowed
  585. $this->User->create();
  586. $data = [
  587. 'id' => $uid,
  588. 'pwd' => 'somepwd',
  589. 'pwd_repeat' => 'somepwd'
  590. ];
  591. $this->User->set($data);
  592. $result = $this->User->save();
  593. $this->assertFalse($result);
  594. $this->User->Behaviors->load('Tools.Passwordable', ['current' => true]);
  595. // Without the correct current password it will not continue
  596. $this->User->create();
  597. $data = [
  598. 'id' => $uid,
  599. 'pwd_current' => 'somepwdxyz',
  600. 'pwd' => '123456',
  601. 'pwd_repeat' => '123456'
  602. ];
  603. $this->User->set($data);
  604. $result = $this->User->save();
  605. $this->assertFalse($result);
  606. // Now it will
  607. $this->User->create();
  608. $data = [
  609. 'id' => $uid,
  610. 'pwd_current' => 'somepwd',
  611. 'pwd' => '123456',
  612. 'pwd_repeat' => '123456'
  613. ];
  614. $this->User->set($data);
  615. $result = $this->User->save();
  616. $this->assertTrue((bool)$result);
  617. // Lets set a BC password (without password_hash() method but Security class)
  618. $data = [
  619. 'id' => $uid,
  620. 'password' => $oldHash,
  621. ];
  622. $result = $this->User->save($data, ['validate' => false]);
  623. $this->assertTrue((bool)$result);
  624. // Now it will still work
  625. $this->User->create();
  626. $data = [
  627. 'id' => $uid,
  628. 'pwd_current' => 'foobar',
  629. 'pwd' => '123456',
  630. 'pwd_repeat' => '123456'
  631. ];
  632. $this->User->set($data);
  633. $result = $this->User->save();
  634. $this->assertTrue((bool)$result);
  635. // Lets set an invalid BC password (without password_hash() method but Security class)
  636. $data = [
  637. 'id' => $uid,
  638. 'password' => $oldHash . 'x',
  639. ];
  640. $result = $this->User->save($data, ['validate' => false]);
  641. $this->assertTrue((bool)$result);
  642. // Now it will still work
  643. $this->User->create();
  644. $data = [
  645. 'id' => $uid,
  646. 'pwd_current' => 'foobar',
  647. 'pwd' => '123456',
  648. 'pwd_repeat' => '123456'
  649. ];
  650. $this->User->set($data);
  651. $result = $this->User->save();
  652. $this->assertFalse($result);
  653. // Lets set a valid BC password (without password_hash() method but Security class)
  654. // But the provided pwd is incorrect
  655. $data = [
  656. 'id' => $uid,
  657. 'password' => $oldHash,
  658. ];
  659. $result = $this->User->save($data, ['validate' => false]);
  660. $this->assertTrue((bool)$result);
  661. // Now it will still work
  662. $this->User->create();
  663. $data = [
  664. 'id' => $uid,
  665. 'pwd_current' => 'foobarx',
  666. 'pwd' => '123456',
  667. 'pwd_repeat' => '123456'
  668. ];
  669. $this->User->set($data);
  670. $result = $this->User->save();
  671. $this->assertFalse($result);
  672. }
  673. /**
  674. * Tests needsPasswordRehash()
  675. *
  676. * @return void
  677. */
  678. public function testNeedsPasswordRehash() {
  679. $this->skipIf(!function_exists('password_hash'), 'password_hash() is not available.');
  680. $this->User->Behaviors->load('Tools.Passwordable', [
  681. 'allowSame' => false,
  682. 'current' => false,
  683. 'authType' => 'Blowfish',
  684. 'passwordHasher' => 'Shim.Modern'
  685. ]);
  686. $hash = password_hash('foobar', PASSWORD_BCRYPT);
  687. $result = $this->User->needsPasswordRehash($hash);
  688. $this->assertFalse($result);
  689. $hash = sha1('foobar');
  690. $result = $this->User->needsPasswordRehash($hash);
  691. $this->assertTrue($result);
  692. }
  693. /**
  694. * Tests needsPasswordRehash()
  695. *
  696. * @return void
  697. */
  698. public function testNeedsPasswordRehashWithNotSupportedHasher() {
  699. $this->User->Behaviors->load('Tools.Passwordable', [
  700. 'allowSame' => false,
  701. 'current' => false,
  702. 'authType' => 'Blowfish',
  703. ]);
  704. $hash = password_hash('foobar', PASSWORD_BCRYPT);
  705. $result = $this->User->needsPasswordRehash($hash);
  706. $this->assertFalse($result);
  707. $this->User->Behaviors->load('Tools.Passwordable', [
  708. 'allowSame' => false,
  709. 'current' => false,
  710. 'authType' => 'Blowfish',
  711. 'passwordHasher' => 'Simple'
  712. ]);
  713. $hash = password_hash('foobar', PASSWORD_BCRYPT);
  714. $result = $this->User->needsPasswordRehash($hash);
  715. $this->assertFalse($result);
  716. }
  717. /**
  718. * PasswordableBehaviorTest::testSettings()
  719. *
  720. * @return void
  721. */
  722. public function testSettings() {
  723. // Pwd min and max length
  724. $this->User->Behaviors->load('Tools.Passwordable', [
  725. 'allowSame' => false,
  726. 'current' => false,
  727. 'minLength' => 3,
  728. 'maxLength' => 6,
  729. ]);
  730. $this->User->create();
  731. $data = [
  732. 'pwd' => '123',
  733. 'pwd_repeat' => '123'
  734. ];
  735. $this->User->set($data);
  736. $result = $this->User->save();
  737. $this->assertTrue((bool)$result);
  738. $this->User->create();
  739. $data = [
  740. 'pwd' => '12345678',
  741. 'pwd_repeat' => '12345678'
  742. ];
  743. $this->User->set($data);
  744. $result = $this->User->save();
  745. $this->assertFalse($result);
  746. $expected = [
  747. 'pwd' => [__d('tools', 'valErrBetweenCharacters %s %s', 3, 6)]
  748. ];
  749. $this->assertEquals($expected, $this->User->validationErrors);
  750. }
  751. /**
  752. * Test that validate false also works.
  753. *
  754. * @return void
  755. */
  756. public function testSaveWithValidateFalse() {
  757. $this->User->Behaviors->load('Tools.Passwordable');
  758. $this->User->create();
  759. $data = [
  760. 'pwd' => '123',
  761. ];
  762. $this->User->set($data);
  763. $result = $this->User->save(null, ['validate' => false]);
  764. $this->assertTrue((bool)$result);
  765. $uid = (string)$this->User->id;
  766. $data = [
  767. 'id' => $uid,
  768. 'pwd' => '1234'
  769. ];
  770. $this->User->set($data);
  771. $result2 = $this->User->save(null, ['validate' => false]);
  772. $this->assertTrue((bool)$result2);
  773. $this->assertTrue($result['ToolsUser']['password'] !== $result2['ToolsUser']['password']);
  774. }
  775. }
  776. /**
  777. * Test component
  778. */
  779. class AuthTestComponent extends AuthComponent {
  780. }
  781. class ComplexPasswordHasher extends SimplePasswordHasher {
  782. }