BasicAuthenticateTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 2.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Auth;
  17. use Cake\Auth\BasicAuthenticate;
  18. use Cake\Controller\ComponentRegistry;
  19. use Cake\Http\Exception\UnauthorizedException;
  20. use Cake\Http\Response;
  21. use Cake\Http\ServerRequest;
  22. use Cake\I18n\Time;
  23. use Cake\TestSuite\TestCase;
  24. /**
  25. * Test case for BasicAuthentication
  26. */
  27. class BasicAuthenticateTest extends TestCase
  28. {
  29. /**
  30. * Fixtures
  31. *
  32. * @var array
  33. */
  34. protected $fixtures = ['core.AuthUsers', 'core.Users'];
  35. /**
  36. * @var \Cake\Controller\ComponentRegistry
  37. */
  38. protected $collection;
  39. /**
  40. * @var \Cake\Auth\BasicAuthenticate
  41. */
  42. protected $auth;
  43. /**
  44. * setup
  45. *
  46. * @return void
  47. */
  48. public function setUp(): void
  49. {
  50. parent::setUp();
  51. $this->collection = new ComponentRegistry();
  52. $this->auth = new BasicAuthenticate($this->collection, [
  53. 'userModel' => 'Users',
  54. 'realm' => 'localhost',
  55. ]);
  56. $password = password_hash('password', PASSWORD_BCRYPT);
  57. $User = $this->getTableLocator()->get('Users');
  58. $User->updateAll(['password' => $password], []);
  59. }
  60. /**
  61. * test applying settings in the constructor
  62. *
  63. * @return void
  64. */
  65. public function testConstructor(): void
  66. {
  67. $object = new BasicAuthenticate($this->collection, [
  68. 'userModel' => 'AuthUser',
  69. 'fields' => ['username' => 'user', 'password' => 'password'],
  70. ]);
  71. $this->assertSame('AuthUser', $object->getConfig('userModel'));
  72. $this->assertEquals(['username' => 'user', 'password' => 'password'], $object->getConfig('fields'));
  73. }
  74. /**
  75. * test the authenticate method
  76. *
  77. * @return void
  78. */
  79. public function testAuthenticateNoData(): void
  80. {
  81. $request = new ServerRequest(['url' => 'posts/index']);
  82. $this->assertFalse($this->auth->getUser($request));
  83. }
  84. /**
  85. * test the authenticate method
  86. *
  87. * @return void
  88. */
  89. public function testAuthenticateNoUsername(): void
  90. {
  91. $request = new ServerRequest([
  92. 'url' => 'posts/index',
  93. 'environment' => ['PHP_AUTH_PW' => 'foobar'],
  94. ]);
  95. $this->assertFalse($this->auth->authenticate($request, new Response()));
  96. }
  97. /**
  98. * test the authenticate method
  99. *
  100. * @return void
  101. */
  102. public function testAuthenticateNoPassword(): void
  103. {
  104. $request = new ServerRequest([
  105. 'url' => 'posts/index',
  106. 'environment' => ['PHP_AUTH_USER' => 'mariano'],
  107. ]);
  108. $this->assertFalse($this->auth->authenticate($request, new Response()));
  109. }
  110. /**
  111. * test the authenticate method
  112. *
  113. * @return void
  114. */
  115. public function testAuthenticateInjection(): void
  116. {
  117. $request = new ServerRequest([
  118. 'url' => 'posts/index',
  119. 'environment' => [
  120. 'PHP_AUTH_USER' => '> 1',
  121. 'PHP_AUTH_PW' => "' OR 1 = 1",
  122. ],
  123. ]);
  124. $this->assertFalse($this->auth->getUser($request));
  125. $this->assertFalse($this->auth->authenticate($request, new Response()));
  126. }
  127. /**
  128. * Test that username of 0 works.
  129. *
  130. * @return void
  131. */
  132. public function testAuthenticateUsernameZero(): void
  133. {
  134. $User = $this->getTableLocator()->get('Users');
  135. $User->updateAll(['username' => '0'], ['username' => 'mariano']);
  136. $request = new ServerRequest([
  137. 'url' => 'posts/index',
  138. 'data' => [
  139. 'User' => [
  140. 'user' => '0',
  141. 'password' => 'password',
  142. ],
  143. ],
  144. ]);
  145. $_SERVER['PHP_AUTH_USER'] = '0';
  146. $_SERVER['PHP_AUTH_PW'] = 'password';
  147. $expected = [
  148. 'id' => 1,
  149. 'username' => '0',
  150. 'created' => new Time('2007-03-17 01:16:23'),
  151. 'updated' => new Time('2007-03-17 01:18:31'),
  152. ];
  153. $this->assertEquals($expected, $this->auth->authenticate($request, new Response()));
  154. }
  155. /**
  156. * test that challenge headers are sent when no credentials are found.
  157. *
  158. * @return void
  159. */
  160. public function testAuthenticateChallenge(): void
  161. {
  162. $request = new ServerRequest(['url' => 'posts/index']);
  163. try {
  164. $this->auth->unauthenticated($request, new Response());
  165. } catch (UnauthorizedException $e) {
  166. }
  167. $this->assertNotEmpty($e);
  168. $expected = ['WWW-Authenticate' => 'Basic realm="localhost"'];
  169. $this->assertEquals($expected, $e->responseHeader());
  170. }
  171. /**
  172. * test authenticate success
  173. *
  174. * @return void
  175. */
  176. public function testAuthenticateSuccess(): void
  177. {
  178. $request = new ServerRequest([
  179. 'url' => 'posts/index',
  180. 'environment' => [
  181. 'PHP_AUTH_USER' => 'mariano',
  182. 'PHP_AUTH_PW' => 'password',
  183. ],
  184. ]);
  185. $result = $this->auth->authenticate($request, new Response());
  186. $expected = [
  187. 'id' => 1,
  188. 'username' => 'mariano',
  189. 'created' => new Time('2007-03-17 01:16:23'),
  190. 'updated' => new Time('2007-03-17 01:18:31'),
  191. ];
  192. $this->assertEquals($expected, $result);
  193. }
  194. }