DigestAuthenticateTest.php 8.3 KB

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