DigestAuthenticateTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /**
  3. * DigestAuthenticateTest 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('DigestAuthenticate', 'Controller/Component/Auth');
  19. App::uses('AppModel', 'Model');
  20. App::uses('CakeRequest', 'Network');
  21. App::uses('CakeResponse', 'Network');
  22. require_once CAKE . 'Test' . DS . 'Case' . DS . 'Model' . DS . 'models.php';
  23. /**
  24. * Test case for DigestAuthentication
  25. *
  26. * @package Cake.Test.Case.Controller.Component.Auth
  27. */
  28. class DigestAuthenticateTest extends CakeTestCase {
  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('ComponentCollection');
  43. $this->server = $_SERVER;
  44. $this->auth = new DigestAuthenticate($this->Collection, array(
  45. 'fields' => array('username' => 'user', 'password' => 'password'),
  46. 'userModel' => 'User',
  47. 'realm' => 'localhost',
  48. 'nonce' => 123,
  49. 'opaque' => '123abc'
  50. ));
  51. $password = DigestAuthenticate::password('mariano', 'cake', 'localhost');
  52. $User = ClassRegistry::init('User');
  53. $User->updateAll(array('password' => $User->getDataSource()->value($password)));
  54. $_SERVER['REQUEST_METHOD'] = 'GET';
  55. $this->response = $this->getMock('CakeResponse');
  56. }
  57. /**
  58. * tearDown
  59. *
  60. * @return void
  61. */
  62. public function tearDown() {
  63. parent::tearDown();
  64. $_SERVER = $this->server;
  65. }
  66. /**
  67. * test applying settings in the constructor
  68. *
  69. * @return void
  70. */
  71. public function testConstructor() {
  72. $object = new DigestAuthenticate($this->Collection, array(
  73. 'userModel' => 'AuthUser',
  74. 'fields' => array('username' => 'user', 'password' => 'password'),
  75. 'nonce' => 123456
  76. ));
  77. $this->assertEquals('AuthUser', $object->settings['userModel']);
  78. $this->assertEquals(array('username' => 'user', 'password' => 'password'), $object->settings['fields']);
  79. $this->assertEquals(123456, $object->settings['nonce']);
  80. $this->assertEquals(env('SERVER_NAME'), $object->settings['realm']);
  81. }
  82. /**
  83. * test the authenticate method
  84. *
  85. * @return void
  86. */
  87. public function testAuthenticateNoData() {
  88. $request = new CakeRequest('posts/index', false);
  89. $this->response->expects($this->never())
  90. ->method('header');
  91. $this->assertFalse($this->auth->getUser($request, $this->response));
  92. }
  93. /**
  94. * test the authenticate method
  95. *
  96. * @expectedException UnauthorizedException
  97. * @expectedExceptionCode 401
  98. * @return void
  99. */
  100. public function testAuthenticateWrongUsername() {
  101. $request = new CakeRequest('posts/index', false);
  102. $request->addParams(array('pass' => array(), 'named' => array()));
  103. $_SERVER['PHP_AUTH_DIGEST'] = <<<DIGEST
  104. Digest username="incorrect_user",
  105. realm="localhost",
  106. nonce="123456",
  107. uri="/dir/index.html",
  108. qop=auth,
  109. nc=00000001,
  110. cnonce="0a4f113b",
  111. response="6629fae49393a05397450978507c4ef1",
  112. opaque="123abc"
  113. DIGEST;
  114. $this->auth->unauthenticated($request, $this->response);
  115. }
  116. /**
  117. * test that challenge headers are sent when no credentials are found.
  118. *
  119. * @return void
  120. */
  121. public function testAuthenticateChallenge() {
  122. $request = new CakeRequest('posts/index', false);
  123. $request->addParams(array('pass' => array(), 'named' => array()));
  124. try {
  125. $this->auth->unauthenticated($request, $this->response);
  126. } catch (UnauthorizedException $e) {
  127. }
  128. $this->assertNotEmpty($e);
  129. $expected = array('WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="123abc"');
  130. $this->assertEquals($expected, $e->responseHeader());
  131. }
  132. /**
  133. * test authenticate success
  134. *
  135. * @return void
  136. */
  137. public function testAuthenticateSuccess() {
  138. $request = new CakeRequest('posts/index', false);
  139. $request->addParams(array('pass' => array(), 'named' => array()));
  140. $_SERVER['PHP_AUTH_DIGEST'] = <<<DIGEST
  141. Digest username="mariano",
  142. realm="localhost",
  143. nonce="123",
  144. uri="/dir/index.html",
  145. qop=auth,
  146. nc=1,
  147. cnonce="123",
  148. response="06b257a54befa2ddfb9bfa134224aa29",
  149. opaque="123abc"
  150. DIGEST;
  151. $result = $this->auth->authenticate($request, $this->response);
  152. $expected = array(
  153. 'id' => 1,
  154. 'user' => 'mariano',
  155. 'created' => '2007-03-17 01:16:23',
  156. 'updated' => '2007-03-17 01:18:31'
  157. );
  158. $this->assertEquals($expected, $result);
  159. }
  160. /**
  161. * test scope failure.
  162. *
  163. * @expectedException UnauthorizedException
  164. * @expectedExceptionCode 401
  165. * @return void
  166. */
  167. public function testAuthenticateFailReChallenge() {
  168. $this->auth->settings['scope'] = array('user' => 'nate');
  169. $request = new CakeRequest('posts/index', false);
  170. $request->addParams(array('pass' => array(), 'named' => array()));
  171. $_SERVER['PHP_AUTH_DIGEST'] = <<<DIGEST
  172. Digest username="mariano",
  173. realm="localhost",
  174. nonce="123",
  175. uri="/dir/index.html",
  176. qop=auth,
  177. nc=1,
  178. cnonce="123",
  179. response="6629fae49393a05397450978507c4ef1",
  180. opaque="123abc"
  181. DIGEST;
  182. $this->auth->unauthenticated($request, $this->response);
  183. }
  184. /**
  185. * testParseDigestAuthData method
  186. *
  187. * @return void
  188. */
  189. public function testParseAuthData() {
  190. $digest = <<<DIGEST
  191. Digest username="Mufasa",
  192. realm="testrealm@host.com",
  193. nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
  194. uri="/dir/index.html?query=string&value=some%20value",
  195. qop=auth,
  196. nc=00000001,
  197. cnonce="0a4f113b",
  198. response="6629fae49393a05397450978507c4ef1",
  199. opaque="5ccc069c403ebaf9f0171e9517f40e41"
  200. DIGEST;
  201. $expected = array(
  202. 'username' => 'Mufasa',
  203. 'realm' => 'testrealm@host.com',
  204. 'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
  205. 'uri' => '/dir/index.html?query=string&value=some%20value',
  206. 'qop' => 'auth',
  207. 'nc' => '00000001',
  208. 'cnonce' => '0a4f113b',
  209. 'response' => '6629fae49393a05397450978507c4ef1',
  210. 'opaque' => '5ccc069c403ebaf9f0171e9517f40e41'
  211. );
  212. $result = $this->auth->parseAuthData($digest);
  213. $this->assertSame($expected, $result);
  214. $result = $this->auth->parseAuthData('');
  215. $this->assertNull($result);
  216. }
  217. /**
  218. * Test parsing a full URI. While not part of the spec some mobile clients will do it wrong.
  219. *
  220. * @return void
  221. */
  222. public function testParseAuthDataFullUri() {
  223. $digest = <<<DIGEST
  224. Digest username="admin",
  225. realm="192.168.0.2",
  226. nonce="53a7f9b83f61b",
  227. uri="http://192.168.0.2/pvcollection/sites/pull/HFD%200001.json#fragment",
  228. qop=auth,
  229. nc=00000001,
  230. cnonce="b85ff144e496e6e18d1c73020566ea3b",
  231. response="5894f5d9cd41d012bac09eeb89d2ddf2",
  232. opaque="6f65e91667cf98dd13464deaf2739fde"
  233. DIGEST;
  234. $expected = 'http://192.168.0.2/pvcollection/sites/pull/HFD%200001.json#fragment';
  235. $result = $this->auth->parseAuthData($digest);
  236. $this->assertSame($expected, $result['uri']);
  237. }
  238. /**
  239. * test parsing digest information with email addresses
  240. *
  241. * @return void
  242. */
  243. public function testParseAuthEmailAddress() {
  244. $digest = <<<DIGEST
  245. Digest username="mark@example.com",
  246. realm="testrealm@host.com",
  247. nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
  248. uri="/dir/index.html",
  249. qop=auth,
  250. nc=00000001,
  251. cnonce="0a4f113b",
  252. response="6629fae49393a05397450978507c4ef1",
  253. opaque="5ccc069c403ebaf9f0171e9517f40e41"
  254. DIGEST;
  255. $expected = array(
  256. 'username' => 'mark@example.com',
  257. 'realm' => 'testrealm@host.com',
  258. 'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
  259. 'uri' => '/dir/index.html',
  260. 'qop' => 'auth',
  261. 'nc' => '00000001',
  262. 'cnonce' => '0a4f113b',
  263. 'response' => '6629fae49393a05397450978507c4ef1',
  264. 'opaque' => '5ccc069c403ebaf9f0171e9517f40e41'
  265. );
  266. $result = $this->auth->parseAuthData($digest);
  267. $this->assertSame($expected, $result);
  268. }
  269. /**
  270. * test password hashing
  271. *
  272. * @return void
  273. */
  274. public function testPassword() {
  275. $result = DigestAuthenticate::password('mark', 'password', 'localhost');
  276. $expected = md5('mark:localhost:password');
  277. $this->assertEquals($expected, $result);
  278. }
  279. }