OauthTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Network\Http\Auth;
  15. use Cake\Network\Http\Auth\Oauth;
  16. use Cake\Network\Http\Request;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * Oauth test.
  20. */
  21. class OauthTest extends TestCase {
  22. /**
  23. * @expectedException \Cake\Core\Exception\Exception
  24. */
  25. public function testExceptionUnknownSigningMethod() {
  26. $auth = new Oauth();
  27. $creds = [
  28. 'consumerSecret' => 'it is secret',
  29. 'consumerKey' => 'a key',
  30. 'token' => 'a token value',
  31. 'tokenSecret' => 'also secret',
  32. 'method' => 'silly goose',
  33. ];
  34. $request = new Request();
  35. $auth->authentication($request, $creds);
  36. }
  37. /**
  38. * Test plain-text signing.
  39. *
  40. * @return void
  41. */
  42. public function testPlainTextSigning() {
  43. $auth = new Oauth();
  44. $creds = [
  45. 'consumerSecret' => 'it is secret',
  46. 'consumerKey' => 'a key',
  47. 'token' => 'a token value',
  48. 'tokenSecret' => 'also secret',
  49. 'method' => 'plaintext',
  50. ];
  51. $request = new Request();
  52. $auth->authentication($request, $creds);
  53. $result = $request->header('Authorization');
  54. $this->assertContains('OAuth', $result);
  55. $this->assertContains('oauth_version="1.0"', $result);
  56. $this->assertContains('oauth_token="a%20token%20value"', $result);
  57. $this->assertContains('oauth_consumer_key="a%20key"', $result);
  58. $this->assertContains('oauth_signature_method="PLAINTEXT"', $result);
  59. $this->assertContains('oauth_signature="it%20is%20secret%26also%20secret"', $result);
  60. $this->assertContains('oauth_timestamp=', $result);
  61. $this->assertContains('oauth_nonce=', $result);
  62. }
  63. /**
  64. * Test that baseString() normalizes the URL.
  65. *
  66. * @return void
  67. */
  68. public function testBaseStringNormalizeUrl() {
  69. $request = new Request();
  70. $request->url('HTTP://exAmple.com:80/parts/foo');
  71. $auth = new Oauth();
  72. $creds = [];
  73. $result = $auth->baseString($request, $creds);
  74. $this->assertContains('GET&', $result, 'method was missing.');
  75. $this->assertContains('http%3A%2F%2Fexample.com%2Fparts%2Ffoo', $result);
  76. }
  77. /**
  78. * Test that the query string is stripped from the normalized host.
  79. *
  80. * @return void
  81. */
  82. public function testBaseStringWithQueryString() {
  83. $request = new Request();
  84. $request->url('http://example.com/search?q=pogo&cat=2');
  85. $auth = new Oauth();
  86. $values = [
  87. 'oauth_version' => '1.0',
  88. 'oauth_nonce' => uniqid(),
  89. 'oauth_timestamp' => time(),
  90. 'oauth_signature_method' => 'HMAC-SHA1',
  91. 'oauth_token' => 'token',
  92. 'oauth_consumer_key' => 'consumer-key',
  93. ];
  94. $result = $auth->baseString($request, $values);
  95. $this->assertContains('GET&', $result, 'method was missing.');
  96. $this->assertContains(
  97. 'http%3A%2F%2Fexample.com%2Fsearch&',
  98. $result
  99. );
  100. $this->assertContains(
  101. 'cat%3D2%26oauth_consumer_key%3Dconsumer-key' .
  102. '%26oauth_nonce%3D' . $values['oauth_nonce'] .
  103. '%26oauth_signature_method%3DHMAC-SHA1' .
  104. '%26oauth_timestamp%3D' . $values['oauth_timestamp'] .
  105. '%26oauth_token%3Dtoken' .
  106. '%26oauth_version%3D1.0' .
  107. '%26q%3Dpogo',
  108. $result
  109. );
  110. }
  111. /**
  112. * Ensure that post data is sorted and encoded.
  113. *
  114. * Keys with array values have to be serialized using
  115. * a more standard HTTP approach. PHP flavoured HTTP
  116. * is not part of the Oauth spec.
  117. *
  118. * See Normalize Request Parameters (section 9.1.1)
  119. * http://wiki.oauth.net/w/page/12238556/TestCases
  120. *
  121. * @return void
  122. */
  123. public function testBaseStringWithPostData() {
  124. $request = new Request();
  125. $request->url('http://example.com/search?q=pogo')
  126. ->method(Request::METHOD_POST)
  127. ->body([
  128. 'address' => 'post',
  129. 'tags' => ['oauth', 'cake'],
  130. 'zed' => 'last'
  131. ]);
  132. $auth = new Oauth();
  133. $values = [
  134. 'oauth_version' => '1.0',
  135. 'oauth_nonce' => uniqid(),
  136. 'oauth_timestamp' => time(),
  137. 'oauth_signature_method' => 'HMAC-SHA1',
  138. 'oauth_token' => 'token',
  139. 'oauth_consumer_key' => 'consumer-key',
  140. ];
  141. $result = $auth->baseString($request, $values);
  142. $this->assertContains('POST&', $result, 'method was missing.');
  143. $this->assertContains(
  144. 'http%3A%2F%2Fexample.com%2Fsearch&',
  145. $result
  146. );
  147. $this->assertContains(
  148. '&address%3Dpost' .
  149. '%26oauth_consumer_key%3Dconsumer-key' .
  150. '%26oauth_nonce%3D' . $values['oauth_nonce'] .
  151. '%26oauth_signature_method%3DHMAC-SHA1' .
  152. '%26oauth_timestamp%3D' . $values['oauth_timestamp'] .
  153. '%26oauth_token%3Dtoken' .
  154. '%26oauth_version%3D1.0' .
  155. '%26q%3Dpogo' .
  156. '%26tags%3Dcake' .
  157. '%26tags%3Doauth' .
  158. '%26zed%3Dlast',
  159. $result
  160. );
  161. }
  162. /**
  163. * Test HMAC-SHA1 signing
  164. *
  165. * Hash result + parameters taken from
  166. * http://wiki.oauth.net/w/page/12238556/TestCases
  167. *
  168. * @return void
  169. */
  170. public function testHmacSigning() {
  171. $request = new Request();
  172. $request->url('http://photos.example.net/photos')
  173. ->body([
  174. 'file' => 'vacation.jpg',
  175. 'size' => 'original'
  176. ]);
  177. $options = [
  178. 'consumerKey' => 'dpf43f3p2l4k3l03',
  179. 'consumerSecret' => 'kd94hf93k423kf44',
  180. 'tokenSecret' => 'pfkkdhi9sl3r4s00',
  181. 'token' => 'nnch734d00sl2jdk',
  182. 'nonce' => 'kllo9940pd9333jh',
  183. 'timestamp' => '1191242096'
  184. ];
  185. $auth = new Oauth();
  186. $auth->authentication($request, $options);
  187. $result = $request->header('Authorization');
  188. $expected = 'tR3+Ty81lMeYAr/Fid0kMTYa/WM=';
  189. $this->assertContains(
  190. 'oauth_signature="' . $expected . '"',
  191. urldecode($result)
  192. );
  193. }
  194. }