DigestAuthenticateTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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\TableRegistry;
  23. use Cake\TestSuite\TestCase;
  24. /**
  25. * Test case for DigestAuthentication
  26. *
  27. */
  28. class DigestAuthenticateTest extends TestCase
  29. {
  30. /**
  31. * Fixtures
  32. *
  33. * @var array
  34. */
  35. public $fixtures = ['core.auth_users', 'core.users'];
  36. /**
  37. * setup
  38. *
  39. * @return void
  40. */
  41. public function setUp()
  42. {
  43. parent::setUp();
  44. $this->Collection = $this->getMock('Cake\Controller\ComponentRegistry');
  45. $this->auth = new DigestAuthenticate($this->Collection, [
  46. 'realm' => 'localhost',
  47. 'nonce' => 123,
  48. 'opaque' => '123abc'
  49. ]);
  50. $password = DigestAuthenticate::password('mariano', 'cake', 'localhost');
  51. $User = TableRegistry::get('Users');
  52. $User->updateAll(['password' => $password], []);
  53. $this->response = $this->getMock('Cake\Network\Response');
  54. }
  55. /**
  56. * test applying settings in the constructor
  57. *
  58. * @return void
  59. */
  60. public function testConstructor()
  61. {
  62. $object = new DigestAuthenticate($this->Collection, [
  63. 'userModel' => 'AuthUser',
  64. 'fields' => ['username' => 'user', 'password' => 'pass'],
  65. 'nonce' => 123456
  66. ]);
  67. $this->assertEquals('AuthUser', $object->config('userModel'));
  68. $this->assertEquals(['username' => 'user', 'password' => 'pass'], $object->config('fields'));
  69. $this->assertEquals(123456, $object->config('nonce'));
  70. $this->assertEquals(env('SERVER_NAME'), $object->config('realm'));
  71. }
  72. /**
  73. * test the authenticate method
  74. *
  75. * @return void
  76. */
  77. public function testAuthenticateNoData()
  78. {
  79. $request = new Request('posts/index');
  80. $this->response->expects($this->never())
  81. ->method('header');
  82. $this->assertFalse($this->auth->getUser($request, $this->response));
  83. }
  84. /**
  85. * test the authenticate method
  86. *
  87. * @expectedException \Cake\Network\Exception\UnauthorizedException
  88. * @expectedExceptionCode 401
  89. * @return void
  90. */
  91. public function testAuthenticateWrongUsername()
  92. {
  93. $request = new Request('posts/index');
  94. $request->addParams(['pass' => []]);
  95. $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. $request->env('PHP_AUTH_DIGEST', $digest);
  107. $this->auth->unauthenticated($request, $this->response);
  108. }
  109. /**
  110. * test that challenge headers are sent when no credentials are found.
  111. *
  112. * @return void
  113. */
  114. public function testAuthenticateChallenge()
  115. {
  116. $request = new Request([
  117. 'url' => 'posts/index',
  118. 'environment' => ['REQUEST_METHOD' => 'GET']
  119. ]);
  120. $request->addParams(['pass' => []]);
  121. try {
  122. $this->auth->unauthenticated($request, $this->response);
  123. } catch (UnauthorizedException $e) {
  124. }
  125. $this->assertNotEmpty($e);
  126. $expected = ['WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="123abc"'];
  127. $this->assertEquals($expected, $e->responseHeader());
  128. }
  129. /**
  130. * test authenticate success
  131. *
  132. * @return void
  133. */
  134. public function testAuthenticateSuccess()
  135. {
  136. $request = new Request([
  137. 'url' => 'posts/index',
  138. 'environment' => ['REQUEST_METHOD' => 'GET']
  139. ]);
  140. $request->addParams(['pass' => []]);
  141. $digest = <<<DIGEST
  142. Digest username="mariano",
  143. realm="localhost",
  144. nonce="123",
  145. uri="/dir/index.html",
  146. qop=auth,
  147. nc=1,
  148. cnonce="123",
  149. response="06b257a54befa2ddfb9bfa134224aa29",
  150. opaque="123abc"
  151. DIGEST;
  152. $request->env('PHP_AUTH_DIGEST', $digest);
  153. $result = $this->auth->authenticate($request, $this->response);
  154. $expected = [
  155. 'id' => 1,
  156. 'username' => 'mariano',
  157. 'created' => new Time('2007-03-17 01:16:23'),
  158. 'updated' => new Time('2007-03-17 01:18:31')
  159. ];
  160. $this->assertEquals($expected, $result);
  161. }
  162. /**
  163. * test authenticate success
  164. *
  165. * @return void
  166. */
  167. public function testAuthenticateSuccessSimulatedRequestMethod()
  168. {
  169. $request = new Request([
  170. 'url' => 'posts/index',
  171. 'post' => ['_method' => 'PUT'],
  172. 'environment' => ['REQUEST_METHOD' => 'GET']
  173. ]);
  174. $request->addParams(['pass' => []]);
  175. $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="06b257a54befa2ddfb9bfa134224aa29",
  184. opaque="123abc"
  185. DIGEST;
  186. $request->env('PHP_AUTH_DIGEST', $digest);
  187. $result = $this->auth->authenticate($request, $this->response);
  188. $expected = [
  189. 'id' => 1,
  190. 'username' => 'mariano',
  191. 'created' => new Time('2007-03-17 01:16:23'),
  192. 'updated' => new Time('2007-03-17 01:18:31')
  193. ];
  194. $this->assertEquals($expected, $result);
  195. }
  196. /**
  197. * test scope failure.
  198. *
  199. * @expectedException \Cake\Network\Exception\UnauthorizedException
  200. * @expectedExceptionCode 401
  201. * @return void
  202. */
  203. public function testAuthenticateFailReChallenge()
  204. {
  205. $this->auth->config('scope.username', 'nate');
  206. $request = new Request([
  207. 'url' => 'posts/index',
  208. 'environment' => ['REQUEST_METHOD' => 'GET']
  209. ]);
  210. $request->addParams(['pass' => []]);
  211. $digest = <<<DIGEST
  212. Digest username="mariano",
  213. realm="localhost",
  214. nonce="123",
  215. uri="/dir/index.html",
  216. qop=auth,
  217. nc=1,
  218. cnonce="123",
  219. response="6629fae49393a05397450978507c4ef1",
  220. opaque="123abc"
  221. DIGEST;
  222. $request->env('PHP_AUTH_DIGEST', $digest);
  223. $this->auth->unauthenticated($request, $this->response);
  224. }
  225. /**
  226. * testLoginHeaders method
  227. *
  228. * @return void
  229. */
  230. public function testLoginHeaders()
  231. {
  232. $request = new Request([
  233. 'environment' => ['SERVER_NAME' => 'localhost']
  234. ]);
  235. $this->auth = new DigestAuthenticate($this->Collection, [
  236. 'realm' => 'localhost',
  237. 'nonce' => '123'
  238. ]);
  239. $expected = 'WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="421aa90e079fa326b6494f812ad13e79"';
  240. $result = $this->auth->loginHeaders($request);
  241. $this->assertEquals($expected, $result);
  242. }
  243. /**
  244. * testParseDigestAuthData method
  245. *
  246. * @return void
  247. */
  248. public function testParseAuthData()
  249. {
  250. $digest = <<<DIGEST
  251. Digest username="Mufasa",
  252. realm="testrealm@host.com",
  253. nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
  254. uri="/dir/index.html?query=string&value=some%20value",
  255. qop=auth,
  256. nc=00000001,
  257. cnonce="0a4f113b",
  258. response="6629fae49393a05397450978507c4ef1",
  259. opaque="5ccc069c403ebaf9f0171e9517f40e41"
  260. DIGEST;
  261. $expected = [
  262. 'username' => 'Mufasa',
  263. 'realm' => 'testrealm@host.com',
  264. 'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
  265. 'uri' => '/dir/index.html?query=string&value=some%20value',
  266. 'qop' => 'auth',
  267. 'nc' => '00000001',
  268. 'cnonce' => '0a4f113b',
  269. 'response' => '6629fae49393a05397450978507c4ef1',
  270. 'opaque' => '5ccc069c403ebaf9f0171e9517f40e41'
  271. ];
  272. $result = $this->auth->parseAuthData($digest);
  273. $this->assertSame($expected, $result);
  274. $result = $this->auth->parseAuthData('');
  275. $this->assertNull($result);
  276. }
  277. /**
  278. * Test parsing a full URI. While not part of the spec some mobile clients will do it wrong.
  279. *
  280. * @return void
  281. */
  282. public function testParseAuthDataFullUri()
  283. {
  284. $digest = <<<DIGEST
  285. Digest username="admin",
  286. realm="192.168.0.2",
  287. nonce="53a7f9b83f61b",
  288. uri="http://192.168.0.2/pvcollection/sites/pull/HFD%200001.json#fragment",
  289. qop=auth,
  290. nc=00000001,
  291. cnonce="b85ff144e496e6e18d1c73020566ea3b",
  292. response="5894f5d9cd41d012bac09eeb89d2ddf2",
  293. opaque="6f65e91667cf98dd13464deaf2739fde"
  294. DIGEST;
  295. $expected = 'http://192.168.0.2/pvcollection/sites/pull/HFD%200001.json#fragment';
  296. $result = $this->auth->parseAuthData($digest);
  297. $this->assertSame($expected, $result['uri']);
  298. }
  299. /**
  300. * test parsing digest information with email addresses
  301. *
  302. * @return void
  303. */
  304. public function testParseAuthEmailAddress()
  305. {
  306. $digest = <<<DIGEST
  307. Digest username="mark@example.com",
  308. realm="testrealm@host.com",
  309. nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
  310. uri="/dir/index.html",
  311. qop=auth,
  312. nc=00000001,
  313. cnonce="0a4f113b",
  314. response="6629fae49393a05397450978507c4ef1",
  315. opaque="5ccc069c403ebaf9f0171e9517f40e41"
  316. DIGEST;
  317. $expected = [
  318. 'username' => 'mark@example.com',
  319. 'realm' => 'testrealm@host.com',
  320. 'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
  321. 'uri' => '/dir/index.html',
  322. 'qop' => 'auth',
  323. 'nc' => '00000001',
  324. 'cnonce' => '0a4f113b',
  325. 'response' => '6629fae49393a05397450978507c4ef1',
  326. 'opaque' => '5ccc069c403ebaf9f0171e9517f40e41'
  327. ];
  328. $result = $this->auth->parseAuthData($digest);
  329. $this->assertSame($expected, $result);
  330. }
  331. /**
  332. * test password hashing
  333. *
  334. * @return void
  335. */
  336. public function testPassword()
  337. {
  338. $result = DigestAuthenticate::password('mark', 'password', 'localhost');
  339. $expected = md5('mark:localhost:password');
  340. $this->assertEquals($expected, $result);
  341. }
  342. }