PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Test/Case/Controller/Component/Auth/DigestAuthenticateTest.php

https://github.com/gustavor/lore
PHP | 303 lines | 193 code | 36 blank | 74 comment | 0 complexity | 78b21e93db58ed3246fc3e8ff95ade17 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  5. * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @package Cake.Test.Case.Controller.Component.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 . 'Test' . DS . 'Case' . DS . 'Model' . DS . 'models.php';
  21. /**
  22. * Test case for DigestAuthentication
  23. *
  24. * @package Cake.Test.Case.Controller.Component.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. public 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. $User = ClassRegistry::init('User');
  46. $User->updateAll(array('password' => $User->getDataSource()->value($password)));
  47. $_SERVER['REQUEST_METHOD'] = 'GET';
  48. $this->response = $this->getMock('CakeResponse');
  49. }
  50. /**
  51. * teardown
  52. *
  53. * @return void
  54. */
  55. public function tearDown() {
  56. parent::tearDown();
  57. $_SERVER = $this->server;
  58. }
  59. /**
  60. * test applying settings in the constructor
  61. *
  62. * @return void
  63. */
  64. public function testConstructor() {
  65. $object = new DigestAuthenticate($this->Collection, array(
  66. 'userModel' => 'AuthUser',
  67. 'fields' => array('username' => 'user', 'password' => 'password'),
  68. 'nonce' => 123456
  69. ));
  70. $this->assertEquals('AuthUser', $object->settings['userModel']);
  71. $this->assertEquals(array('username' => 'user', 'password' => 'password'), $object->settings['fields']);
  72. $this->assertEquals(123456, $object->settings['nonce']);
  73. $this->assertEquals(env('SERVER_NAME'), $object->settings['realm']);
  74. }
  75. /**
  76. * test the authenticate method
  77. *
  78. * @return void
  79. */
  80. public function testAuthenticateNoData() {
  81. $request = new CakeRequest('posts/index', false);
  82. $this->response->expects($this->once())
  83. ->method('header')
  84. ->with('WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="123abc"');
  85. $this->assertFalse($this->auth->authenticate($request, $this->response));
  86. }
  87. /**
  88. * test the authenticate method
  89. *
  90. * @return void
  91. */
  92. public function testAuthenticateWrongUsername() {
  93. $request = new CakeRequest('posts/index', false);
  94. $request->addParams(array('pass' => array(), 'named' => array()));
  95. $_SERVER['PHP_AUTH_DIGEST'] = <<<DIGEST
  96. Digest username="incorrect_user",
  97. realm="localhost",
  98. nonce="123456",
  99. uri="/dir/index.html",
  100. qop=auth,
  101. nc=00000001,
  102. cnonce="0a4f113b",
  103. response="6629fae49393a05397450978507c4ef1",
  104. opaque="123abc"
  105. DIGEST;
  106. $this->response->expects($this->at(0))
  107. ->method('header')
  108. ->with('WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="123abc"');
  109. $this->response->expects($this->at(1))
  110. ->method('statusCode')
  111. ->with(401);
  112. $this->response->expects($this->at(2))
  113. ->method('send');
  114. $this->assertFalse($this->auth->authenticate($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. $this->response->expects($this->at(0))
  125. ->method('header')
  126. ->with('WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="123abc"');
  127. $this->response->expects($this->at(1))
  128. ->method('statusCode')
  129. ->with(401);
  130. $this->response->expects($this->at(2))
  131. ->method('send');
  132. $result = $this->auth->authenticate($request, $this->response);
  133. $this->assertFalse($result);
  134. }
  135. /**
  136. * test authenticate sucesss
  137. *
  138. * @return void
  139. */
  140. public function testAuthenticateSuccess() {
  141. $request = new CakeRequest('posts/index', false);
  142. $request->addParams(array('pass' => array(), 'named' => array()));
  143. $_SERVER['PHP_AUTH_DIGEST'] = <<<DIGEST
  144. Digest username="mariano",
  145. realm="localhost",
  146. nonce="123",
  147. uri="/dir/index.html",
  148. qop=auth,
  149. nc=1,
  150. cnonce="123",
  151. response="06b257a54befa2ddfb9bfa134224aa29",
  152. opaque="123abc"
  153. DIGEST;
  154. $result = $this->auth->authenticate($request, $this->response);
  155. $expected = array(
  156. 'id' => 1,
  157. 'user' => 'mariano',
  158. 'created' => '2007-03-17 01:16:23',
  159. 'updated' => '2007-03-17 01:18:31'
  160. );
  161. $this->assertEquals($expected, $result);
  162. }
  163. /**
  164. * test scope failure.
  165. *
  166. * @return void
  167. */
  168. public function testAuthenticateFailReChallenge() {
  169. $this->auth->settings['scope'] = array('user' => 'nate');
  170. $request = new CakeRequest('posts/index', false);
  171. $request->addParams(array('pass' => array(), 'named' => array()));
  172. $_SERVER['PHP_AUTH_DIGEST'] = <<<DIGEST
  173. Digest username="mariano",
  174. realm="localhost",
  175. nonce="123",
  176. uri="/dir/index.html",
  177. qop=auth,
  178. nc=1,
  179. cnonce="123",
  180. response="6629fae49393a05397450978507c4ef1",
  181. opaque="123abc"
  182. DIGEST;
  183. $this->response->expects($this->at(0))
  184. ->method('header')
  185. ->with('WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="123abc"');
  186. $this->response->expects($this->at(1))
  187. ->method('statusCode')
  188. ->with(401);
  189. $this->response->expects($this->at(2))
  190. ->method('send');
  191. $this->assertFalse($this->auth->authenticate($request, $this->response));
  192. }
  193. /**
  194. * testParseDigestAuthData method
  195. *
  196. * @return void
  197. */
  198. public 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. public 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. public function testPassword() {
  263. $result = DigestAuthenticate::password('mark', 'password', 'localhost');
  264. $expected = md5('mark:localhost:password');
  265. $this->assertEquals($expected, $result);
  266. }
  267. }