DigestAuthenticateTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /**
  3. *
  4. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  5. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @package cake.tests.cases.libs.controller.components.auth
  13. * @since CakePHP(tm) v 2.0
  14. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. App::uses('DigestAuthenticate', 'Controller/Component/Auth');
  17. App::uses('AppModel', 'Model');
  18. App::uses('CakeRequest', 'Network');
  19. App::uses('CakeResponse', 'Network');
  20. require_once CAKE_TESTS . 'Case' . DS . 'Model' . DS . 'models.php';
  21. /**
  22. * Test case for DigestAuthentication
  23. *
  24. * @package cake.test.cases.controller.components.auth
  25. */
  26. class DigestAuthenticateTest extends CakeTestCase {
  27. public $fixtures = array('core.user', 'core.auth_user');
  28. /**
  29. * setup
  30. *
  31. * @return void
  32. */
  33. function setUp() {
  34. parent::setUp();
  35. $this->Collection = $this->getMock('ComponentCollection');
  36. $this->server = $_SERVER;
  37. $this->auth = new DigestAuthenticate($this->Collection, array(
  38. 'fields' => array('username' => 'user', 'password' => 'password'),
  39. 'userModel' => 'User',
  40. 'realm' => 'localhost',
  41. 'nonce' => 123,
  42. 'opaque' => '123abc'
  43. ));
  44. $password = DigestAuthenticate::password('mariano', 'cake', 'localhost');
  45. ClassRegistry::init('User')->updateAll(array('password' => '"' . $password . '"'));
  46. $_SERVER['REQUEST_METHOD'] = 'GET';
  47. $this->response = $this->getMock('CakeResponse');
  48. }
  49. /**
  50. * teardown
  51. *
  52. * @return void
  53. */
  54. function tearDown() {
  55. parent::tearDown();
  56. $_SERVER = $this->server;
  57. }
  58. /**
  59. * test applying settings in the constructor
  60. *
  61. * @return void
  62. */
  63. function testConstructor() {
  64. $object = new DigestAuthenticate($this->Collection, array(
  65. 'userModel' => 'AuthUser',
  66. 'fields' => array('username' => 'user', 'password' => 'password'),
  67. 'nonce' => 123456
  68. ));
  69. $this->assertEquals('AuthUser', $object->settings['userModel']);
  70. $this->assertEquals(array('username' => 'user', 'password' => 'password'), $object->settings['fields']);
  71. $this->assertEquals(123456, $object->settings['nonce']);
  72. $this->assertEquals(env('SERVER_NAME'), $object->settings['realm']);
  73. }
  74. /**
  75. * test the authenticate method
  76. *
  77. * @return void
  78. */
  79. function testAuthenticateNoData() {
  80. $request = new CakeRequest('posts/index', false);
  81. $this->response->expects($this->once())
  82. ->method('header')
  83. ->with('WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="123abc"');
  84. $this->assertFalse($this->auth->authenticate($request, $this->response));
  85. }
  86. /**
  87. * test the authenticate method
  88. *
  89. * @return void
  90. */
  91. function testAuthenticateWrongUsername() {
  92. $request = new CakeRequest('posts/index', false);
  93. $request->addParams(array('pass' => array(), 'named' => array()));
  94. $_SERVER['PHP_AUTH_DIGEST'] = <<<DIGEST
  95. Digest username="incorrect_user",
  96. realm="localhost",
  97. nonce="123456",
  98. uri="/dir/index.html",
  99. qop=auth,
  100. nc=00000001,
  101. cnonce="0a4f113b",
  102. response="6629fae49393a05397450978507c4ef1",
  103. opaque="123abc"
  104. DIGEST;
  105. $this->response->expects($this->at(0))
  106. ->method('header')
  107. ->with('WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="123abc"');
  108. $this->response->expects($this->at(1))
  109. ->method('statusCode')
  110. ->with(401);
  111. $this->response->expects($this->at(2))
  112. ->method('send');
  113. $this->assertFalse($this->auth->authenticate($request, $this->response));
  114. }
  115. /**
  116. * test that challenge headers are sent when no credentials are found.
  117. *
  118. * @return void
  119. */
  120. function testAuthenticateChallenge() {
  121. $request = new CakeRequest('posts/index', false);
  122. $request->addParams(array('pass' => array(), 'named' => array()));
  123. $this->response->expects($this->at(0))
  124. ->method('header')
  125. ->with('WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="123abc"');
  126. $this->response->expects($this->at(1))
  127. ->method('statusCode')
  128. ->with(401);
  129. $this->response->expects($this->at(2))
  130. ->method('send');
  131. $result = $this->auth->authenticate($request, $this->response);
  132. $this->assertFalse($result);
  133. }
  134. /**
  135. * test authenticate sucesss
  136. *
  137. * @return void
  138. */
  139. function testAuthenticateSuccess() {
  140. $request = new CakeRequest('posts/index', false);
  141. $request->addParams(array('pass' => array(), 'named' => array()));
  142. $_SERVER['PHP_AUTH_DIGEST'] = <<<DIGEST
  143. Digest username="mariano",
  144. realm="localhost",
  145. nonce="123",
  146. uri="/dir/index.html",
  147. qop=auth,
  148. nc=1,
  149. cnonce="123",
  150. response="06b257a54befa2ddfb9bfa134224aa29",
  151. opaque="123abc"
  152. DIGEST;
  153. $result = $this->auth->authenticate($request, $this->response);
  154. $expected = array(
  155. 'id' => 1,
  156. 'user' => 'mariano',
  157. 'created' => '2007-03-17 01:16:23',
  158. 'updated' => '2007-03-17 01:18:31'
  159. );
  160. $this->assertEquals($expected, $result);
  161. }
  162. /**
  163. * test scope failure.
  164. *
  165. * @return void
  166. */
  167. 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->response->expects($this->at(0))
  183. ->method('header')
  184. ->with('WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="123abc"');
  185. $this->response->expects($this->at(1))
  186. ->method('statusCode')
  187. ->with(401);
  188. $this->response->expects($this->at(2))
  189. ->method('send');
  190. $this->assertFalse($this->auth->authenticate($request, $this->response));
  191. }
  192. /**
  193. * testParseDigestAuthData method
  194. *
  195. * @access public
  196. * @return void
  197. */
  198. function testParseAuthData() {
  199. $digest = <<<DIGEST
  200. Digest username="Mufasa",
  201. realm="testrealm@host.com",
  202. nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
  203. uri="/dir/index.html",
  204. qop=auth,
  205. nc=00000001,
  206. cnonce="0a4f113b",
  207. response="6629fae49393a05397450978507c4ef1",
  208. opaque="5ccc069c403ebaf9f0171e9517f40e41"
  209. DIGEST;
  210. $expected = array(
  211. 'username' => 'Mufasa',
  212. 'realm' => 'testrealm@host.com',
  213. 'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
  214. 'uri' => '/dir/index.html',
  215. 'qop' => 'auth',
  216. 'nc' => '00000001',
  217. 'cnonce' => '0a4f113b',
  218. 'response' => '6629fae49393a05397450978507c4ef1',
  219. 'opaque' => '5ccc069c403ebaf9f0171e9517f40e41'
  220. );
  221. $result = $this->auth->parseAuthData($digest);
  222. $this->assertSame($expected, $result);
  223. $result = $this->auth->parseAuthData('');
  224. $this->assertNull($result);
  225. }
  226. /**
  227. * test parsing digest information with email addresses
  228. *
  229. * @return void
  230. */
  231. function testParseAuthEmailAddress() {
  232. $digest = <<<DIGEST
  233. Digest username="mark@example.com",
  234. realm="testrealm@host.com",
  235. nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
  236. uri="/dir/index.html",
  237. qop=auth,
  238. nc=00000001,
  239. cnonce="0a4f113b",
  240. response="6629fae49393a05397450978507c4ef1",
  241. opaque="5ccc069c403ebaf9f0171e9517f40e41"
  242. DIGEST;
  243. $expected = array(
  244. 'username' => 'mark@example.com',
  245. 'realm' => 'testrealm@host.com',
  246. 'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
  247. 'uri' => '/dir/index.html',
  248. 'qop' => 'auth',
  249. 'nc' => '00000001',
  250. 'cnonce' => '0a4f113b',
  251. 'response' => '6629fae49393a05397450978507c4ef1',
  252. 'opaque' => '5ccc069c403ebaf9f0171e9517f40e41'
  253. );
  254. $result = $this->auth->parseAuthData($digest);
  255. $this->assertIdentical($expected, $result);
  256. }
  257. /**
  258. * test password hashing
  259. *
  260. * @return void
  261. */
  262. function testPassword() {
  263. $result = DigestAuthenticate::password('mark', 'password', 'localhost');
  264. $expected = md5('mark:localhost:password');
  265. $this->assertEquals($expected, $result);
  266. }
  267. }