OauthTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. /**
  24. * @expectedException \Cake\Core\Exception\Exception
  25. */
  26. public function testExceptionUnknownSigningMethod()
  27. {
  28. $auth = new Oauth();
  29. $creds = [
  30. 'consumerSecret' => 'it is secret',
  31. 'consumerKey' => 'a key',
  32. 'token' => 'a token value',
  33. 'tokenSecret' => 'also secret',
  34. 'method' => 'silly goose',
  35. ];
  36. $request = new Request();
  37. $auth->authentication($request, $creds);
  38. }
  39. /**
  40. * Test plain-text signing.
  41. *
  42. * @return void
  43. */
  44. public function testPlainTextSigning()
  45. {
  46. $auth = new Oauth();
  47. $creds = [
  48. 'consumerSecret' => 'it is secret',
  49. 'consumerKey' => 'a key',
  50. 'token' => 'a token value',
  51. 'tokenSecret' => 'also secret',
  52. 'method' => 'plaintext',
  53. ];
  54. $request = new Request();
  55. $auth->authentication($request, $creds);
  56. $result = $request->header('Authorization');
  57. $this->assertContains('OAuth', $result);
  58. $this->assertContains('oauth_version="1.0"', $result);
  59. $this->assertContains('oauth_token="a%20token%20value"', $result);
  60. $this->assertContains('oauth_consumer_key="a%20key"', $result);
  61. $this->assertContains('oauth_signature_method="PLAINTEXT"', $result);
  62. $this->assertContains('oauth_signature="it%20is%20secret%26also%20secret"', $result);
  63. $this->assertContains('oauth_timestamp=', $result);
  64. $this->assertContains('oauth_nonce=', $result);
  65. }
  66. /**
  67. * Test that baseString() normalizes the URL.
  68. *
  69. * @return void
  70. */
  71. public function testBaseStringNormalizeUrl()
  72. {
  73. $request = new Request();
  74. $request->url('HTTP://exAmple.com:80/parts/foo');
  75. $auth = new Oauth();
  76. $creds = [];
  77. $result = $auth->baseString($request, $creds);
  78. $this->assertContains('GET&', $result, 'method was missing.');
  79. $this->assertContains('http%3A%2F%2Fexample.com%2Fparts%2Ffoo', $result);
  80. }
  81. /**
  82. * Test that the query string is stripped from the normalized host.
  83. *
  84. * @return void
  85. */
  86. public function testBaseStringWithQueryString()
  87. {
  88. $request = new Request();
  89. $request->url('http://example.com/search?q=pogo&cat=2');
  90. $auth = new Oauth();
  91. $values = [
  92. 'oauth_version' => '1.0',
  93. 'oauth_nonce' => uniqid(),
  94. 'oauth_timestamp' => time(),
  95. 'oauth_signature_method' => 'HMAC-SHA1',
  96. 'oauth_token' => 'token',
  97. 'oauth_consumer_key' => 'consumer-key',
  98. ];
  99. $result = $auth->baseString($request, $values);
  100. $this->assertContains('GET&', $result, 'method was missing.');
  101. $this->assertContains(
  102. 'http%3A%2F%2Fexample.com%2Fsearch&',
  103. $result
  104. );
  105. $this->assertContains(
  106. 'cat%3D2%26oauth_consumer_key%3Dconsumer-key' .
  107. '%26oauth_nonce%3D' . $values['oauth_nonce'] .
  108. '%26oauth_signature_method%3DHMAC-SHA1' .
  109. '%26oauth_timestamp%3D' . $values['oauth_timestamp'] .
  110. '%26oauth_token%3Dtoken' .
  111. '%26oauth_version%3D1.0' .
  112. '%26q%3Dpogo',
  113. $result
  114. );
  115. }
  116. /**
  117. * Ensure that post data is sorted and encoded.
  118. *
  119. * Keys with array values have to be serialized using
  120. * a more standard HTTP approach. PHP flavoured HTTP
  121. * is not part of the Oauth spec.
  122. *
  123. * See Normalize Request Parameters (section 9.1.1)
  124. * http://wiki.oauth.net/w/page/12238556/TestCases
  125. *
  126. * @return void
  127. */
  128. public function testBaseStringWithPostData()
  129. {
  130. $request = new Request();
  131. $request->url('http://example.com/search?q=pogo')
  132. ->method(Request::METHOD_POST)
  133. ->body([
  134. 'address' => 'post',
  135. 'tags' => ['oauth', 'cake'],
  136. 'zed' => 'last'
  137. ]);
  138. $auth = new Oauth();
  139. $values = [
  140. 'oauth_version' => '1.0',
  141. 'oauth_nonce' => uniqid(),
  142. 'oauth_timestamp' => time(),
  143. 'oauth_signature_method' => 'HMAC-SHA1',
  144. 'oauth_token' => 'token',
  145. 'oauth_consumer_key' => 'consumer-key',
  146. ];
  147. $result = $auth->baseString($request, $values);
  148. $this->assertContains('POST&', $result, 'method was missing.');
  149. $this->assertContains(
  150. 'http%3A%2F%2Fexample.com%2Fsearch&',
  151. $result
  152. );
  153. $this->assertContains(
  154. '&address%3Dpost' .
  155. '%26oauth_consumer_key%3Dconsumer-key' .
  156. '%26oauth_nonce%3D' . $values['oauth_nonce'] .
  157. '%26oauth_signature_method%3DHMAC-SHA1' .
  158. '%26oauth_timestamp%3D' . $values['oauth_timestamp'] .
  159. '%26oauth_token%3Dtoken' .
  160. '%26oauth_version%3D1.0' .
  161. '%26q%3Dpogo' .
  162. '%26tags%3Dcake' .
  163. '%26tags%3Doauth' .
  164. '%26zed%3Dlast',
  165. $result
  166. );
  167. }
  168. /**
  169. * Test HMAC-SHA1 signing
  170. *
  171. * Hash result + parameters taken from
  172. * http://wiki.oauth.net/w/page/12238556/TestCases
  173. *
  174. * @return void
  175. */
  176. public function testHmacSigning()
  177. {
  178. $request = new Request();
  179. $request->url('http://photos.example.net/photos')
  180. ->body([
  181. 'file' => 'vacation.jpg',
  182. 'size' => 'original'
  183. ]);
  184. $options = [
  185. 'consumerKey' => 'dpf43f3p2l4k3l03',
  186. 'consumerSecret' => 'kd94hf93k423kf44',
  187. 'tokenSecret' => 'pfkkdhi9sl3r4s00',
  188. 'token' => 'nnch734d00sl2jdk',
  189. 'nonce' => 'kllo9940pd9333jh',
  190. 'timestamp' => '1191242096'
  191. ];
  192. $auth = new Oauth();
  193. $auth->authentication($request, $options);
  194. $result = $request->header('Authorization');
  195. $expected = 'tR3+Ty81lMeYAr/Fid0kMTYa/WM=';
  196. $this->assertContains(
  197. 'oauth_signature="' . $expected . '"',
  198. urldecode($result)
  199. );
  200. }
  201. /**
  202. * Test RSA-SHA1 signing
  203. *
  204. * Hash result + parameters taken from
  205. * http://wiki.oauth.net/w/page/12238556/TestCases
  206. *
  207. * @return void
  208. */
  209. public function testRsaSigning() {
  210. $request = new Request();
  211. $request->url('http://photos.example.net/photos')
  212. ->body([
  213. 'file' => 'vacaction.jpg',
  214. 'size' => 'original'
  215. ]);
  216. $private_key_path = TEST_APP . DS . 'config' . DS . 'key.pem';
  217. $options = [
  218. 'method' => 'RSA-SHA1',
  219. 'consumerKey' => 'dpf43f3p2l4k3l03',
  220. 'nonce' => '13917289812797014437',
  221. 'timestamp' => '1196666512',
  222. 'private_key_file' => $private_key_path,
  223. ];
  224. $auth = new Oauth();
  225. $auth->authentication($request, $options);
  226. $result = $request->header('Authorization');
  227. $expected = 'jvTp/wX1TYtByB1m+Pbyo0lnCOLIsyGCH7wke8AUs3BpnwZJtAuEJkvQL2/9n4s5wUmUl4aCI4BwpraNx4RtEXMe5qg5T1LVTGliMRpKasKsW//e+RinhejgCuzoH26dyF8iY2ZZ/5D1ilgeijhV/vBka5twt399mXwaYdCwFYE=';
  228. $this->assertContains(
  229. 'oauth_signature="' . $expected . '"',
  230. urldecode($result)
  231. );
  232. }
  233. /**
  234. * Test RSA-SHA1 signing with passphrase string
  235. *
  236. * Hash result + parameters taken from
  237. * http://wiki.oauth.net/w/page/12238556/TestCases
  238. *
  239. * @return void
  240. */
  241. public function testRsaSigningWithPassphraseString() {
  242. $request = new Request();
  243. $request->url('http://photos.example.net/photos')
  244. ->body([
  245. 'file' => 'vacaction.jpg',
  246. 'size' => 'original'
  247. ]);
  248. $private_key_path = TEST_APP . DS . 'config' . DS . 'key_with_passphrase.pem';
  249. $passphrase = 'fancy-cakephp-passphrase';
  250. $options = [
  251. 'method' => 'RSA-SHA1',
  252. 'consumerKey' => 'dpf43f3p2l4k3l03',
  253. 'nonce' => '13917289812797014437',
  254. 'timestamp' => '1196666512',
  255. 'private_key_file' => $private_key_path,
  256. 'private_key_passphrase' => $passphrase,
  257. ];
  258. $auth = new Oauth();
  259. $auth->authentication($request, $options);
  260. $result = $request->header('Authorization');
  261. $expected = 'jvTp/wX1TYtByB1m+Pbyo0lnCOLIsyGCH7wke8AUs3BpnwZJtAuEJkvQL2/9n4s5wUmUl4aCI4BwpraNx4RtEXMe5qg5T1LVTGliMRpKasKsW//e+RinhejgCuzoH26dyF8iY2ZZ/5D1ilgeijhV/vBka5twt399mXwaYdCwFYE=';
  262. $this->assertContains(
  263. 'oauth_signature="' . $expected . '"',
  264. urldecode($result)
  265. );
  266. }
  267. /**
  268. * Test RSA-SHA1 signing with passphrase file
  269. *
  270. * Hash result + parameters taken from
  271. * http://wiki.oauth.net/w/page/12238556/TestCases
  272. *
  273. * @return void
  274. */
  275. public function testRsaSigningWithPassphraseFile() {
  276. $request = new Request();
  277. $request->url('http://photos.example.net/photos')
  278. ->body([
  279. 'file' => 'vacaction.jpg',
  280. 'size' => 'original'
  281. ]);
  282. $private_key_path = TEST_APP . DS . 'config' . DS . 'key_with_passphrase.pem';
  283. if(PHP_EOL == "\n") $passphrase_path = TEST_APP . DS . 'config' . DS . 'key_passphrase_lf';
  284. else if(PHP_EOL == "\r\n") $passphrase_path = TEST_APP . DS . 'config' . DS . 'key_passphrase_crlf';
  285. else if(PHP_EOL == "\r") $passphrase_path = TEST_APP . DS . 'config' . DS . 'key_passphrase_cr';
  286. else { $this->markTestSkipped('The file for the key passphrase could not be loaded as PHP_EOL could not be recognized.'); return; }
  287. $passphrase = fopen($passphrase_path, 'r');
  288. $options = [
  289. 'method' => 'RSA-SHA1',
  290. 'consumerKey' => 'dpf43f3p2l4k3l03',
  291. 'nonce' => '13917289812797014437',
  292. 'timestamp' => '1196666512',
  293. 'private_key_file' => $private_key_path,
  294. 'private_key_passphrase' => $passphrase,
  295. ];
  296. $auth = new Oauth();
  297. $auth->authentication($request, $options);
  298. $result = $request->header('Authorization');
  299. $expected = 'jvTp/wX1TYtByB1m+Pbyo0lnCOLIsyGCH7wke8AUs3BpnwZJtAuEJkvQL2/9n4s5wUmUl4aCI4BwpraNx4RtEXMe5qg5T1LVTGliMRpKasKsW//e+RinhejgCuzoH26dyF8iY2ZZ/5D1ilgeijhV/vBka5twt399mXwaYdCwFYE=';
  300. $this->assertContains(
  301. 'oauth_signature="' . $expected . '"',
  302. urldecode($result)
  303. );
  304. $expected = 0;
  305. $this->assertEquals($expected, ftell($passphrase));
  306. }
  307. }