BasicAuthenticateTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * BasicAuthenticateTest file
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package Cake.Test.Case.Controller.Component.Auth
  15. * @since CakePHP(tm) v 2.0
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('AuthComponent', 'Controller/Component');
  19. App::uses('BasicAuthenticate', 'Controller/Component/Auth');
  20. App::uses('AppModel', 'Model');
  21. App::uses('CakeRequest', 'Network');
  22. App::uses('CakeResponse', 'Network');
  23. require_once CAKE . 'Test' . DS . 'Case' . DS . 'Model' . DS . 'models.php';
  24. /**
  25. * Test case for BasicAuthentication
  26. *
  27. * @package Cake.Test.Case.Controller.Component.Auth
  28. */
  29. class BasicAuthenticateTest extends CakeTestCase {
  30. /**
  31. * Fixtures
  32. *
  33. * @var array
  34. */
  35. public $fixtures = array('core.user', 'core.auth_user');
  36. /**
  37. * setup
  38. *
  39. * @return void
  40. */
  41. public function setUp() {
  42. parent::setUp();
  43. $this->Collection = $this->getMock('ComponentCollection');
  44. $this->auth = new BasicAuthenticate($this->Collection, array(
  45. 'fields' => array('username' => 'user', 'password' => 'password'),
  46. 'userModel' => 'User',
  47. 'realm' => 'localhost',
  48. 'recursive' => 0
  49. ));
  50. $password = Security::hash('password', null, true);
  51. $User = ClassRegistry::init('User');
  52. $User->updateAll(array('password' => $User->getDataSource()->value($password)));
  53. $this->response = $this->getMock('CakeResponse');
  54. }
  55. /**
  56. * test applying settings in the constructor
  57. *
  58. * @return void
  59. */
  60. public function testConstructor() {
  61. $object = new BasicAuthenticate($this->Collection, array(
  62. 'userModel' => 'AuthUser',
  63. 'fields' => array('username' => 'user', 'password' => 'password')
  64. ));
  65. $this->assertEquals('AuthUser', $object->settings['userModel']);
  66. $this->assertEquals(array('username' => 'user', 'password' => 'password'), $object->settings['fields']);
  67. $this->assertEquals(env('SERVER_NAME'), $object->settings['realm']);
  68. }
  69. /**
  70. * test the authenticate method
  71. *
  72. * @return void
  73. */
  74. public function testAuthenticateNoData() {
  75. $request = new CakeRequest('posts/index', false);
  76. $this->response->expects($this->never())
  77. ->method('header');
  78. $this->assertFalse($this->auth->getUser($request));
  79. }
  80. /**
  81. * test the authenticate method
  82. *
  83. * @return void
  84. */
  85. public function testAuthenticateNoUsername() {
  86. $request = new CakeRequest('posts/index', false);
  87. $_SERVER['PHP_AUTH_PW'] = 'foobar';
  88. $this->assertFalse($this->auth->authenticate($request, $this->response));
  89. }
  90. /**
  91. * test the authenticate method
  92. *
  93. * @return void
  94. */
  95. public function testAuthenticateNoPassword() {
  96. $request = new CakeRequest('posts/index', false);
  97. $_SERVER['PHP_AUTH_USER'] = 'mariano';
  98. $_SERVER['PHP_AUTH_PW'] = null;
  99. $this->assertFalse($this->auth->authenticate($request, $this->response));
  100. }
  101. /**
  102. * test the authenticate method
  103. *
  104. * @return void
  105. */
  106. public function testAuthenticateInjection() {
  107. $request = new CakeRequest('posts/index', false);
  108. $request->addParams(array('pass' => array(), 'named' => array()));
  109. $_SERVER['PHP_AUTH_USER'] = '> 1';
  110. $_SERVER['PHP_AUTH_PW'] = "' OR 1 = 1";
  111. $this->assertFalse($this->auth->getUser($request));
  112. $this->assertFalse($this->auth->authenticate($request, $this->response));
  113. }
  114. /**
  115. * test that challenge headers are sent when no credentials are found.
  116. *
  117. * @return void
  118. */
  119. public function testAuthenticateChallenge() {
  120. $request = new CakeRequest('posts/index', false);
  121. $request->addParams(array('pass' => array(), 'named' => array()));
  122. try {
  123. $this->auth->unauthenticated($request, $this->response);
  124. } catch (UnauthorizedException $e) {
  125. }
  126. $this->assertNotEmpty($e);
  127. $expected = array('WWW-Authenticate: Basic realm="localhost"');
  128. $this->assertEquals($expected, $e->responseHeader());
  129. }
  130. /**
  131. * test authenticate success
  132. *
  133. * @return void
  134. */
  135. public function testAuthenticateSuccess() {
  136. $request = new CakeRequest('posts/index', false);
  137. $request->addParams(array('pass' => array(), 'named' => array()));
  138. $_SERVER['PHP_AUTH_USER'] = 'mariano';
  139. $_SERVER['PHP_AUTH_PW'] = 'password';
  140. $result = $this->auth->authenticate($request, $this->response);
  141. $expected = array(
  142. 'id' => 1,
  143. 'user' => 'mariano',
  144. 'created' => '2007-03-17 01:16:23',
  145. 'updated' => '2007-03-17 01:18:31'
  146. );
  147. $this->assertEquals($expected, $result);
  148. }
  149. /**
  150. * test scope failure.
  151. *
  152. * @expectedException UnauthorizedException
  153. * @expectedExceptionCode 401
  154. * @return void
  155. */
  156. public function testAuthenticateFailReChallenge() {
  157. $this->auth->settings['scope'] = array('user' => 'nate');
  158. $request = new CakeRequest('posts/index', false);
  159. $request->addParams(array('pass' => array(), 'named' => array()));
  160. $_SERVER['PHP_AUTH_USER'] = 'mariano';
  161. $_SERVER['PHP_AUTH_PW'] = 'password';
  162. $this->auth->unauthenticated($request, $this->response);
  163. }
  164. /**
  165. * testAuthenticateWithBlowfish
  166. *
  167. * @return void
  168. */
  169. public function testAuthenticateWithBlowfish() {
  170. $hash = Security::hash('password', 'blowfish');
  171. $this->skipIf(strpos($hash, '$2a$') === false, 'Skipping blowfish tests as hashing is not working');
  172. $request = new CakeRequest('posts/index', false);
  173. $request->addParams(array('pass' => array(), 'named' => array()));
  174. $_SERVER['PHP_AUTH_USER'] = 'mariano';
  175. $_SERVER['PHP_AUTH_PW'] = 'password';
  176. $User = ClassRegistry::init('User');
  177. $User->updateAll(
  178. array('password' => $User->getDataSource()->value($hash)),
  179. array('User.user' => 'mariano')
  180. );
  181. $this->auth->settings['passwordHasher'] = 'Blowfish';
  182. $result = $this->auth->authenticate($request, $this->response);
  183. $expected = array(
  184. 'id' => 1,
  185. 'user' => 'mariano',
  186. 'created' => '2007-03-17 01:16:23',
  187. 'updated' => '2007-03-17 01:18:31'
  188. );
  189. $this->assertEquals($expected, $result);
  190. }
  191. }