FormAuthenticateTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 2.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\FormAuthenticate;
  17. use Cake\Cache\Cache;
  18. use Cake\Core\App;
  19. use Cake\Core\Configure;
  20. use Cake\Core\Plugin;
  21. use Cake\I18n\Time;
  22. use Cake\Network\Request;
  23. use Cake\ORM\Entity;
  24. use Cake\ORM\TableRegistry;
  25. use Cake\TestSuite\TestCase;
  26. use Cake\Utility\Security;
  27. /**
  28. * Test case for FormAuthentication
  29. *
  30. */
  31. class FormAuthenticateTest extends TestCase
  32. {
  33. /**
  34. * Fixtures
  35. *
  36. * @var array
  37. */
  38. public $fixtures = ['core.users', 'core.auth_users'];
  39. /**
  40. * setup
  41. *
  42. * @return void
  43. */
  44. public function setUp()
  45. {
  46. parent::setUp();
  47. $this->Collection = $this->getMock('Cake\Controller\ComponentRegistry');
  48. $this->auth = new FormAuthenticate($this->Collection, [
  49. 'userModel' => 'Users'
  50. ]);
  51. $password = password_hash('password', PASSWORD_DEFAULT);
  52. TableRegistry::clear();
  53. $Users = TableRegistry::get('Users');
  54. $Users->updateAll(['password' => $password], []);
  55. $this->response = $this->getMock('Cake\Network\Response');
  56. }
  57. /**
  58. * test applying settings in the constructor
  59. *
  60. * @return void
  61. */
  62. public function testConstructor()
  63. {
  64. $object = new FormAuthenticate($this->Collection, [
  65. 'userModel' => 'AuthUsers',
  66. 'fields' => ['username' => 'user', 'password' => 'password']
  67. ]);
  68. $this->assertEquals('AuthUsers', $object->config('userModel'));
  69. $this->assertEquals(['username' => 'user', 'password' => 'password'], $object->config('fields'));
  70. }
  71. /**
  72. * test the authenticate method
  73. *
  74. * @return void
  75. */
  76. public function testAuthenticateNoData()
  77. {
  78. $request = new Request('posts/index');
  79. $request->data = [];
  80. $this->assertFalse($this->auth->authenticate($request, $this->response));
  81. }
  82. /**
  83. * test the authenticate method
  84. *
  85. * @return void
  86. */
  87. public function testAuthenticateNoUsername()
  88. {
  89. $request = new Request('posts/index');
  90. $request->data = ['password' => 'foobar'];
  91. $this->assertFalse($this->auth->authenticate($request, $this->response));
  92. }
  93. /**
  94. * test the authenticate method
  95. *
  96. * @return void
  97. */
  98. public function testAuthenticateNoPassword()
  99. {
  100. $request = new Request('posts/index');
  101. $request->data = ['username' => 'mariano'];
  102. $this->assertFalse($this->auth->authenticate($request, $this->response));
  103. }
  104. /**
  105. * test authenticate password is false method
  106. *
  107. * @return void
  108. */
  109. public function testAuthenticatePasswordIsFalse()
  110. {
  111. $request = new Request('posts/index', false);
  112. $request->data = [
  113. 'username' => 'mariano',
  114. 'password' => null
  115. ];
  116. $this->assertFalse($this->auth->authenticate($request, $this->response));
  117. }
  118. /**
  119. * Test for password as empty string with _checkFields() call skipped
  120. * Refs https://github.com/cakephp/cakephp/pull/2441
  121. *
  122. * @return void
  123. */
  124. public function testAuthenticatePasswordIsEmptyString()
  125. {
  126. $request = new Request('posts/index', false);
  127. $request->data = [
  128. 'username' => 'mariano',
  129. 'password' => ''
  130. ];
  131. $this->auth = $this->getMock(
  132. 'Cake\Auth\FormAuthenticate',
  133. ['_checkFields'],
  134. [
  135. $this->Collection,
  136. [
  137. 'userModel' => 'Users'
  138. ]
  139. ]
  140. );
  141. // Simulate that check for ensuring password is not empty is missing.
  142. $this->auth->expects($this->once())
  143. ->method('_checkFields')
  144. ->will($this->returnValue(true));
  145. $this->assertFalse($this->auth->authenticate($request, $this->response));
  146. }
  147. /**
  148. * test authenticate field is not string
  149. *
  150. * @return void
  151. */
  152. public function testAuthenticateFieldsAreNotString()
  153. {
  154. $request = new Request('posts/index', false);
  155. $request->data = [
  156. 'username' => ['mariano', 'phpnut'],
  157. 'password' => 'my password'
  158. ];
  159. $this->assertFalse($this->auth->authenticate($request, $this->response));
  160. $request->data = [
  161. 'username' => 'mariano',
  162. 'password' => ['password1', 'password2']
  163. ];
  164. $this->assertFalse($this->auth->authenticate($request, $this->response));
  165. }
  166. /**
  167. * test the authenticate method
  168. *
  169. * @return void
  170. */
  171. public function testAuthenticateInjection()
  172. {
  173. $request = new Request('posts/index');
  174. $request->data = [
  175. 'username' => '> 1',
  176. 'password' => "' OR 1 = 1"
  177. ];
  178. $this->assertFalse($this->auth->authenticate($request, $this->response));
  179. }
  180. /**
  181. * test authenticate success
  182. *
  183. * @return void
  184. */
  185. public function testAuthenticateSuccess()
  186. {
  187. $request = new Request('posts/index');
  188. $request->data = [
  189. 'username' => 'mariano',
  190. 'password' => 'password'
  191. ];
  192. $result = $this->auth->authenticate($request, $this->response);
  193. $expected = [
  194. 'id' => 1,
  195. 'username' => 'mariano',
  196. 'created' => new Time('2007-03-17 01:16:23'),
  197. 'updated' => new Time('2007-03-17 01:18:31')
  198. ];
  199. $this->assertEquals($expected, $result);
  200. }
  201. /**
  202. * Test that authenticate() includes virtual fields.
  203. *
  204. * @return void
  205. */
  206. public function testAuthenticateIncludesVirtualFields()
  207. {
  208. $users = TableRegistry::get('Users');
  209. $users->entityClass('TestApp\Model\Entity\VirtualUser');
  210. $request = new Request('posts/index');
  211. $request->data = [
  212. 'username' => 'mariano',
  213. 'password' => 'password'
  214. ];
  215. $result = $this->auth->authenticate($request, $this->response);
  216. $expected = [
  217. 'id' => 1,
  218. 'username' => 'mariano',
  219. 'bonus' => 'bonus',
  220. 'created' => new Time('2007-03-17 01:16:23'),
  221. 'updated' => new Time('2007-03-17 01:18:31')
  222. ];
  223. $this->assertEquals($expected, $result);
  224. }
  225. /**
  226. * test scope failure.
  227. *
  228. * @return void
  229. */
  230. public function testAuthenticateScopeFail()
  231. {
  232. $this->auth->config('scope', ['Users.id' => 2]);
  233. $request = new Request('posts/index');
  234. $request->data = [
  235. 'username' => 'mariano',
  236. 'password' => 'password'
  237. ];
  238. $this->assertFalse($this->auth->authenticate($request, $this->response));
  239. }
  240. /**
  241. * test a model in a plugin.
  242. *
  243. * @return void
  244. */
  245. public function testPluginModel()
  246. {
  247. Cache::delete('object_map', '_cake_core_');
  248. Plugin::load('TestPlugin');
  249. $PluginModel = TableRegistry::get('TestPlugin.AuthUsers');
  250. $user['id'] = 1;
  251. $user['username'] = 'gwoo';
  252. $user['password'] = password_hash(Security::salt() . 'cake', PASSWORD_BCRYPT);
  253. $PluginModel->save(new Entity($user));
  254. $this->auth->config('userModel', 'TestPlugin.AuthUsers');
  255. $request = new Request('posts/index');
  256. $request->data = [
  257. 'username' => 'gwoo',
  258. 'password' => 'cake'
  259. ];
  260. $result = $this->auth->authenticate($request, $this->response);
  261. $expected = [
  262. 'id' => 1,
  263. 'username' => 'gwoo',
  264. 'created' => new Time('2007-03-17 01:16:23'),
  265. 'updated' => new Time('2007-03-17 01:18:31')
  266. ];
  267. $this->assertEquals($expected, $result);
  268. Plugin::unload();
  269. }
  270. /**
  271. * test password hasher settings
  272. *
  273. * @return void
  274. */
  275. public function testPasswordHasherSettings()
  276. {
  277. $this->auth->config('passwordHasher', [
  278. 'className' => 'Default',
  279. 'hashType' => PASSWORD_BCRYPT
  280. ]);
  281. $passwordHasher = $this->auth->passwordHasher();
  282. $result = $passwordHasher->config();
  283. $this->assertEquals(PASSWORD_BCRYPT, $result['hashType']);
  284. $hash = password_hash('mypass', PASSWORD_BCRYPT);
  285. $User = TableRegistry::get('Users');
  286. $User->updateAll(
  287. ['password' => $hash],
  288. ['username' => 'mariano']
  289. );
  290. $request = new Request('posts/index');
  291. $request->data = [
  292. 'username' => 'mariano',
  293. 'password' => 'mypass'
  294. ];
  295. $result = $this->auth->authenticate($request, $this->response);
  296. $expected = [
  297. 'id' => 1,
  298. 'username' => 'mariano',
  299. 'created' => new Time('2007-03-17 01:16:23'),
  300. 'updated' => new Time('2007-03-17 01:18:31')
  301. ];
  302. $this->assertEquals($expected, $result);
  303. $this->auth = new FormAuthenticate($this->Collection, [
  304. 'fields' => ['username' => 'username', 'password' => 'password'],
  305. 'userModel' => 'Users'
  306. ]);
  307. $this->auth->config('passwordHasher', [
  308. 'className' => 'Default'
  309. ]);
  310. $this->assertEquals($expected, $this->auth->authenticate($request, $this->response));
  311. $User->updateAll(
  312. ['password' => '$2y$10$/G9GBQDZhWUM4w/WLes3b.XBZSK1hGohs5dMi0vh/oen0l0a7DUyK'],
  313. ['username' => 'mariano']
  314. );
  315. $this->assertFalse($this->auth->authenticate($request, $this->response));
  316. }
  317. /**
  318. * Tests that using default means password don't need to be rehashed
  319. *
  320. * @return void
  321. */
  322. public function testAuthenticateNoRehash()
  323. {
  324. $request = new Request('posts/index');
  325. $request->data = [
  326. 'username' => 'mariano',
  327. 'password' => 'password'
  328. ];
  329. $result = $this->auth->authenticate($request, $this->response);
  330. $this->assertNotEmpty($result);
  331. $this->assertFalse($this->auth->needsPasswordRehash());
  332. }
  333. /**
  334. * Tests that not using the Default password hasher means that the password
  335. * needs to be rehashed
  336. *
  337. * @return void
  338. */
  339. public function testAuthenticateRehash()
  340. {
  341. $this->auth = new FormAuthenticate($this->Collection, [
  342. 'userModel' => 'Users',
  343. 'passwordHasher' => 'Weak'
  344. ]);
  345. $password = $this->auth->passwordHasher()->hash('password');
  346. TableRegistry::get('Users')->updateAll(['password' => $password], []);
  347. $request = new Request('posts/index');
  348. $request->data = [
  349. 'username' => 'mariano',
  350. 'password' => 'password'
  351. ];
  352. $result = $this->auth->authenticate($request, $this->response);
  353. $this->assertNotEmpty($result);
  354. $this->assertTrue($this->auth->needsPasswordRehash());
  355. }
  356. }