FormAuthenticateTest.php 9.5 KB

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