DigestAuthenticateTest.php 8.0 KB

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