BasicAuthenticateTest.php 5.5 KB

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