BasicAuthenticateTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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\FrozenTime;
  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<string>
  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. public function setUp(): void
  47. {
  48. parent::setUp();
  49. $this->collection = new ComponentRegistry();
  50. $this->auth = new BasicAuthenticate($this->collection, [
  51. 'userModel' => 'Users',
  52. 'realm' => 'localhost',
  53. ]);
  54. $password = password_hash('password', PASSWORD_BCRYPT);
  55. $User = $this->getTableLocator()->get('Users');
  56. $User->updateAll(['password' => $password], []);
  57. }
  58. /**
  59. * test applying settings in the constructor
  60. */
  61. public function testConstructor(): void
  62. {
  63. $object = new BasicAuthenticate($this->collection, [
  64. 'userModel' => 'AuthUser',
  65. 'fields' => ['username' => 'user', 'password' => 'password'],
  66. ]);
  67. $this->assertSame('AuthUser', $object->getConfig('userModel'));
  68. $this->assertEquals(['username' => 'user', 'password' => 'password'], $object->getConfig('fields'));
  69. }
  70. /**
  71. * test the authenticate method
  72. */
  73. public function testAuthenticateNoData(): void
  74. {
  75. $request = new ServerRequest(['url' => 'posts/index']);
  76. $this->assertFalse($this->auth->getUser($request));
  77. }
  78. /**
  79. * test the authenticate method
  80. */
  81. public function testAuthenticateNoUsername(): void
  82. {
  83. $request = new ServerRequest([
  84. 'url' => 'posts/index',
  85. 'environment' => ['PHP_AUTH_PW' => 'foobar'],
  86. ]);
  87. $this->assertFalse($this->auth->authenticate($request, new Response()));
  88. }
  89. /**
  90. * test the authenticate method
  91. */
  92. public function testAuthenticateNoPassword(): void
  93. {
  94. $request = new ServerRequest([
  95. 'url' => 'posts/index',
  96. 'environment' => ['PHP_AUTH_USER' => 'mariano'],
  97. ]);
  98. $this->assertFalse($this->auth->authenticate($request, new Response()));
  99. }
  100. /**
  101. * test the authenticate method
  102. */
  103. public function testAuthenticateInjection(): void
  104. {
  105. $request = new ServerRequest([
  106. 'url' => 'posts/index',
  107. 'environment' => [
  108. 'PHP_AUTH_USER' => '> 1',
  109. 'PHP_AUTH_PW' => "' OR 1 = 1",
  110. ],
  111. ]);
  112. $this->assertFalse($this->auth->getUser($request));
  113. $this->assertFalse($this->auth->authenticate($request, new Response()));
  114. }
  115. /**
  116. * Test that username of 0 works.
  117. */
  118. public function testAuthenticateUsernameZero(): void
  119. {
  120. $User = $this->getTableLocator()->get('Users');
  121. $User->updateAll(['username' => '0'], ['username' => 'mariano']);
  122. $request = new ServerRequest([
  123. 'url' => 'posts/index',
  124. 'data' => [
  125. 'User' => [
  126. 'user' => '0',
  127. 'password' => 'password',
  128. ],
  129. ],
  130. ]);
  131. $_SERVER['PHP_AUTH_USER'] = '0';
  132. $_SERVER['PHP_AUTH_PW'] = 'password';
  133. $expected = [
  134. 'id' => 1,
  135. 'username' => '0',
  136. 'created' => new FrozenTime('2007-03-17 01:16:23'),
  137. 'updated' => new FrozenTime('2007-03-17 01:18:31'),
  138. ];
  139. $this->assertEquals($expected, $this->auth->authenticate($request, new Response()));
  140. }
  141. /**
  142. * test that challenge headers are sent when no credentials are found.
  143. */
  144. public function testAuthenticateChallenge(): void
  145. {
  146. $request = new ServerRequest(['url' => 'posts/index']);
  147. $e = null;
  148. try {
  149. $this->auth->unauthenticated($request, new Response());
  150. } catch (UnauthorizedException $e) {
  151. }
  152. $this->assertNotEmpty($e);
  153. $expected = ['WWW-Authenticate' => 'Basic realm="localhost"'];
  154. $this->assertEquals($expected, $e->getHeaders());
  155. }
  156. /**
  157. * test authenticate success
  158. */
  159. public function testAuthenticateSuccess(): void
  160. {
  161. $request = new ServerRequest([
  162. 'url' => 'posts/index',
  163. 'environment' => [
  164. 'PHP_AUTH_USER' => 'mariano',
  165. 'PHP_AUTH_PW' => 'password',
  166. ],
  167. ]);
  168. $result = $this->auth->authenticate($request, new Response());
  169. $expected = [
  170. 'id' => 1,
  171. 'username' => 'mariano',
  172. 'created' => new FrozenTime('2007-03-17 01:16:23'),
  173. 'updated' => new FrozenTime('2007-03-17 01:18:31'),
  174. ];
  175. $this->assertEquals($expected, $result);
  176. }
  177. }