DigestAuthenticationTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * DigestAuthenticationTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Network.Http
  16. * @since CakePHP(tm) v 2.0.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('HttpSocket', 'Network/Http');
  20. App::uses('DigestAuthentication', 'Network/Http');
  21. class DigestHttpSocket extends HttpSocket {
  22. /**
  23. * nextHeader attribute
  24. *
  25. * @var string
  26. */
  27. public $nextHeader = '';
  28. /**
  29. * request method
  30. *
  31. * @param mixed $request
  32. * @return void
  33. */
  34. public function request($request = array()) {
  35. if ($request === false) {
  36. if (isset($this->response['header']['WWW-Authenticate'])) {
  37. unset($this->response['header']['WWW-Authenticate']);
  38. }
  39. return;
  40. }
  41. $this->response['header']['WWW-Authenticate'] = $this->nextHeader;
  42. }
  43. }
  44. /**
  45. * DigestAuthenticationTest class
  46. *
  47. * @package Cake.Test.Case.Network.Http
  48. */
  49. class DigestAuthenticationTest extends CakeTestCase {
  50. /**
  51. * Socket property
  52. *
  53. * @var mixed null
  54. */
  55. public $HttpSocket = null;
  56. /**
  57. * This function sets up a HttpSocket instance we are going to use for testing
  58. *
  59. * @return void
  60. */
  61. public function setUp() {
  62. $this->HttpSocket = new DigestHttpSocket();
  63. $this->HttpSocket->request['method'] = 'GET';
  64. $this->HttpSocket->request['uri']['path'] = '/';
  65. }
  66. /**
  67. * We use this function to clean up after the test case was executed
  68. *
  69. * @return void
  70. */
  71. public function tearDown() {
  72. unset($this->HttpSocket);
  73. }
  74. /**
  75. * testBasic method
  76. *
  77. * @return void
  78. */
  79. public function testBasic() {
  80. $this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
  81. $this->assertFalse(isset($this->HttpSocket->request['header']['Authorization']));
  82. $auth = array('user' => 'admin', 'pass' => '1234');
  83. DigestAuthentication::authentication($this->HttpSocket, $auth);
  84. $this->assertTrue(isset($this->HttpSocket->request['header']['Authorization']));
  85. $this->assertEquals('The batcave', $auth['realm']);
  86. $this->assertEquals('4cded326c6c51', $auth['nonce']);
  87. }
  88. /**
  89. * testQop method
  90. *
  91. * @return void
  92. */
  93. public function testQop() {
  94. $this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
  95. $auth = array('user' => 'admin', 'pass' => '1234');
  96. DigestAuthentication::authentication($this->HttpSocket, $auth);
  97. $expected = 'Digest username="admin", realm="The batcave", nonce="4cded326c6c51", uri="/", response="da7e2a46b471d77f70a9bb3698c8902b"';
  98. $this->assertEquals($expected, $this->HttpSocket->request['header']['Authorization']);
  99. $this->assertFalse(isset($auth['qop']));
  100. $this->assertFalse(isset($auth['nc']));
  101. $this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51",qop="auth"';
  102. $auth = array('user' => 'admin', 'pass' => '1234');
  103. DigestAuthentication::authentication($this->HttpSocket, $auth);
  104. $expected = '@Digest username="admin", realm="The batcave", nonce="4cded326c6c51", uri="/", response="[a-z0-9]{32}", qop="auth", nc=00000001, cnonce="[a-z0-9]+"@';
  105. $this->assertRegExp($expected, $this->HttpSocket->request['header']['Authorization']);
  106. $this->assertEquals('auth', $auth['qop']);
  107. $this->assertEquals(2, $auth['nc']);
  108. }
  109. /**
  110. * testOpaque method
  111. *
  112. * @return void
  113. */
  114. public function testOpaque() {
  115. $this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
  116. $auth = array('user' => 'admin', 'pass' => '1234');
  117. DigestAuthentication::authentication($this->HttpSocket, $auth);
  118. $this->assertFalse(strpos($this->HttpSocket->request['header']['Authorization'], 'opaque="d8ea7aa61a1693024c4cc3a516f49b3c"'));
  119. $this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51",opaque="d8ea7aa61a1693024c4cc3a516f49b3c"';
  120. $auth = array('user' => 'admin', 'pass' => '1234');
  121. DigestAuthentication::authentication($this->HttpSocket, $auth);
  122. $this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'opaque="d8ea7aa61a1693024c4cc3a516f49b3c"') > 0);
  123. }
  124. /**
  125. * testMultipleRequest method
  126. *
  127. * @return void
  128. */
  129. public function testMultipleRequest() {
  130. $this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51",qop="auth"';
  131. $auth = array('user' => 'admin', 'pass' => '1234');
  132. DigestAuthentication::authentication($this->HttpSocket, $auth);
  133. $this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'nc=00000001') > 0);
  134. $this->assertEquals(2, $auth['nc']);
  135. DigestAuthentication::authentication($this->HttpSocket, $auth);
  136. $this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'nc=00000002') > 0);
  137. $this->assertEquals(3, $auth['nc']);
  138. $responsePos = strpos($this->HttpSocket->request['header']['Authorization'], 'response=');
  139. $response = substr($this->HttpSocket->request['header']['Authorization'], $responsePos + 10, 32);
  140. $this->HttpSocket->nextHeader = '';
  141. DigestAuthentication::authentication($this->HttpSocket, $auth);
  142. $this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'nc=00000003') > 0);
  143. $this->assertEquals(4, $auth['nc']);
  144. $responsePos = strpos($this->HttpSocket->request['header']['Authorization'], 'response=');
  145. $responseB = substr($this->HttpSocket->request['header']['Authorization'], $responsePos + 10, 32);
  146. $this->assertNotEquals($response, $responseB);
  147. }
  148. /**
  149. * testPathChanged method
  150. *
  151. * @return void
  152. */
  153. public function testPathChanged() {
  154. $this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
  155. $this->HttpSocket->request['uri']['path'] = '/admin';
  156. $auth = array('user' => 'admin', 'pass' => '1234');
  157. DigestAuthentication::authentication($this->HttpSocket, $auth);
  158. $responsePos = strpos($this->HttpSocket->request['header']['Authorization'], 'response=');
  159. $response = substr($this->HttpSocket->request['header']['Authorization'], $responsePos + 10, 32);
  160. $this->assertNotEquals('da7e2a46b471d77f70a9bb3698c8902b', $response);
  161. }
  162. /**
  163. * testNoDigestResponse method
  164. *
  165. * @return void
  166. */
  167. public function testNoDigestResponse() {
  168. $this->HttpSocket->nextHeader = false;
  169. $this->HttpSocket->request['uri']['path'] = '/admin';
  170. $auth = array('user' => 'admin', 'pass' => '1234');
  171. DigestAuthentication::authentication($this->HttpSocket, $auth);
  172. $this->assertFalse(isset($this->HttpSocket->request['header']['Authorization']));
  173. }
  174. }