BasicAuthenticateTest.php 6.3 KB

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