BasicAuthenticateTest.php 6.4 KB

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