OauthTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Http\Client\Auth;
  16. use Cake\Core\Exception\CakeException;
  17. use Cake\Http\Client\Auth\Oauth;
  18. use Cake\Http\Client\Request;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Oauth test.
  22. */
  23. class OauthTest extends TestCase
  24. {
  25. /**
  26. * @var string
  27. */
  28. private $privateKeyString;
  29. /**
  30. * @var string
  31. */
  32. private $privateKeyStringEnc;
  33. /**
  34. * Setup
  35. *
  36. * @return void
  37. */
  38. public function setUp(): void
  39. {
  40. parent::setUp();
  41. $this->privateKeyString = file_get_contents(TEST_APP . DS . 'config' . DS . 'key.pem');
  42. $this->privateKeyStringEnc = file_get_contents(TEST_APP . DS . 'config' . DS . 'key_with_passphrase.pem');
  43. }
  44. public function testExceptionUnknownSigningMethod(): void
  45. {
  46. $this->expectException(CakeException::class);
  47. $auth = new Oauth();
  48. $creds = [
  49. 'consumerSecret' => 'it is secret',
  50. 'consumerKey' => 'a key',
  51. 'token' => 'a token value',
  52. 'tokenSecret' => 'also secret',
  53. 'method' => 'silly goose',
  54. ];
  55. $request = new Request();
  56. $auth->authentication($request, $creds);
  57. }
  58. /**
  59. * Test plain-text signing.
  60. */
  61. public function testPlainTextSigning(): void
  62. {
  63. $auth = new Oauth();
  64. $creds = [
  65. 'consumerSecret' => 'it is secret',
  66. 'consumerKey' => 'a key',
  67. 'token' => 'a token value',
  68. 'tokenSecret' => 'also secret',
  69. 'method' => 'plaintext',
  70. ];
  71. $request = new Request();
  72. $request = $auth->authentication($request, $creds);
  73. $result = $request->getHeaderLine('Authorization');
  74. $this->assertStringContainsString('OAuth', $result);
  75. $this->assertStringContainsString('oauth_version="1.0"', $result);
  76. $this->assertStringContainsString('oauth_token="a%20token%20value"', $result);
  77. $this->assertStringContainsString('oauth_consumer_key="a%20key"', $result);
  78. $this->assertStringContainsString('oauth_signature_method="PLAINTEXT"', $result);
  79. $this->assertStringContainsString('oauth_signature="it%20is%20secret%26also%20secret"', $result);
  80. $this->assertStringContainsString('oauth_timestamp=', $result);
  81. $this->assertStringContainsString('oauth_nonce=', $result);
  82. }
  83. /**
  84. * Test that baseString() normalizes the URL.
  85. */
  86. public function testBaseStringNormalizeUrl(): void
  87. {
  88. $request = new Request('HTTP://exAmple.com:80/parts/foo');
  89. $auth = new Oauth();
  90. $creds = [];
  91. $result = $auth->baseString($request, $creds);
  92. $this->assertStringContainsString('GET&', $result, 'method was missing.');
  93. $this->assertStringContainsString('http%3A%2F%2Fexample.com%2Fparts%2Ffoo', $result);
  94. }
  95. /**
  96. * Test that the query string is stripped from the normalized host.
  97. */
  98. public function testBaseStringWithQueryString(): void
  99. {
  100. $request = new Request('http://example.com/search?q=pogo&cat=2');
  101. $auth = new Oauth();
  102. $values = [
  103. 'oauth_version' => '1.0',
  104. 'oauth_nonce' => uniqid(),
  105. 'oauth_timestamp' => time(),
  106. 'oauth_signature_method' => 'HMAC-SHA1',
  107. 'oauth_token' => 'token',
  108. 'oauth_consumer_key' => 'consumer-key',
  109. ];
  110. $result = $auth->baseString($request, $values);
  111. $this->assertStringContainsString('GET&', $result, 'method was missing.');
  112. $this->assertStringContainsString(
  113. 'http%3A%2F%2Fexample.com%2Fsearch&',
  114. $result
  115. );
  116. $this->assertStringContainsString(
  117. 'cat%3D2%26oauth_consumer_key%3Dconsumer-key' .
  118. '%26oauth_nonce%3D' . $values['oauth_nonce'] .
  119. '%26oauth_signature_method%3DHMAC-SHA1' .
  120. '%26oauth_timestamp%3D' . $values['oauth_timestamp'] .
  121. '%26oauth_token%3Dtoken' .
  122. '%26oauth_version%3D1.0' .
  123. '%26q%3Dpogo',
  124. $result
  125. );
  126. }
  127. /**
  128. * Ensure that post data is sorted and encoded.
  129. *
  130. * Keys with array values have to be serialized using
  131. * a more standard HTTP approach. PHP flavoured HTTP
  132. * is not part of the Oauth spec.
  133. *
  134. * See Normalize Request Parameters (section 9.1.1)
  135. */
  136. public function testBaseStringWithPostDataNestedArrays(): void
  137. {
  138. $request = new Request(
  139. 'http://example.com/search?q=pogo',
  140. Request::METHOD_POST,
  141. [],
  142. [
  143. 'search' => [
  144. 'filters' => [
  145. 'field' => 'date',
  146. 'value' => 'one two',
  147. ],
  148. ],
  149. ]
  150. );
  151. $auth = new Oauth();
  152. $values = [
  153. 'oauth_version' => '1.0',
  154. 'oauth_nonce' => uniqid(),
  155. 'oauth_timestamp' => time(),
  156. 'oauth_signature_method' => 'HMAC-SHA1',
  157. 'oauth_token' => 'token',
  158. 'oauth_consumer_key' => 'consumer-key',
  159. ];
  160. $result = $auth->baseString($request, $values);
  161. $this->assertStringContainsString('POST&', $result, 'method was missing.');
  162. $this->assertStringContainsString(
  163. 'http%3A%2F%2Fexample.com%2Fsearch&',
  164. $result
  165. );
  166. $this->assertStringContainsString(
  167. '&oauth_consumer_key%3Dconsumer-key' .
  168. '%26oauth_nonce%3D' . $values['oauth_nonce'] .
  169. '%26oauth_signature_method%3DHMAC-SHA1' .
  170. '%26oauth_timestamp%3D' . $values['oauth_timestamp'] .
  171. '%26oauth_token%3Dtoken' .
  172. '%26oauth_version%3D1.0' .
  173. '%26q%3Dpogo' .
  174. '%26search%5Bfilters%5D%5Bfield%5D%3Ddate' .
  175. '%26search%5Bfilters%5D%5Bvalue%5D%3Done%20two',
  176. $result
  177. );
  178. }
  179. /**
  180. * Ensure that post data is sorted and encoded.
  181. *
  182. * Keys with array values have to be serialized using
  183. * a more standard HTTP approach. PHP flavoured HTTP
  184. * is not part of the Oauth spec.
  185. *
  186. * See Normalize Request Parameters (section 9.1.1)
  187. * https://wiki.oauth.net/w/page/12238556/TestCases
  188. */
  189. public function testBaseStringWithPostData(): void
  190. {
  191. $request = new Request(
  192. 'http://example.com/search?q=pogo',
  193. Request::METHOD_POST,
  194. [],
  195. [
  196. 'address' => 'post',
  197. 'zed' => 'last',
  198. 'tags' => ['oauth', 'cake'],
  199. ]
  200. );
  201. $auth = new Oauth();
  202. $values = [
  203. 'oauth_version' => '1.0',
  204. 'oauth_nonce' => uniqid(),
  205. 'oauth_timestamp' => time(),
  206. 'oauth_signature_method' => 'HMAC-SHA1',
  207. 'oauth_token' => 'token',
  208. 'oauth_consumer_key' => 'consumer-key',
  209. ];
  210. $result = $auth->baseString($request, $values);
  211. $this->assertStringContainsString('POST&', $result, 'method was missing.');
  212. $this->assertStringContainsString(
  213. 'http%3A%2F%2Fexample.com%2Fsearch&',
  214. $result
  215. );
  216. $this->assertStringContainsString(
  217. '&address%3Dpost' .
  218. '%26oauth_consumer_key%3Dconsumer-key' .
  219. '%26oauth_nonce%3D' . $values['oauth_nonce'] .
  220. '%26oauth_signature_method%3DHMAC-SHA1' .
  221. '%26oauth_timestamp%3D' . $values['oauth_timestamp'] .
  222. '%26oauth_token%3Dtoken' .
  223. '%26oauth_version%3D1.0' .
  224. '%26q%3Dpogo' .
  225. '%26tags%3Dcake' .
  226. '%26tags%3Doauth' .
  227. '%26zed%3Dlast',
  228. $result
  229. );
  230. }
  231. /**
  232. * Ensure that non-urlencoded post data is not included.
  233. *
  234. * Keys with array values have to be serialized using
  235. * a more standard HTTP approach. PHP flavoured HTTP
  236. * is not part of the Oauth spec.
  237. *
  238. * See Normalize Request Parameters (section 9.1.1)
  239. */
  240. public function testBaseStringWithXmlPostData(): void
  241. {
  242. $request = new Request(
  243. 'http://example.com/search?q=pogo',
  244. Request::METHOD_POST,
  245. [
  246. 'Content-Type' => 'application/xml',
  247. ],
  248. '<xml>stuff</xml>'
  249. );
  250. $auth = new Oauth();
  251. $values = [
  252. 'oauth_version' => '1.0',
  253. 'oauth_nonce' => uniqid(),
  254. 'oauth_timestamp' => time(),
  255. 'oauth_signature_method' => 'HMAC-SHA1',
  256. 'oauth_token' => 'token',
  257. 'oauth_consumer_key' => 'consumer-key',
  258. ];
  259. $result = $auth->baseString($request, $values);
  260. $this->assertStringContainsString('POST&', $result, 'method was missing.');
  261. $this->assertStringContainsString(
  262. 'http%3A%2F%2Fexample.com%2Fsearch&',
  263. $result
  264. );
  265. $this->assertStringContainsString(
  266. 'oauth_consumer_key%3Dconsumer-key' .
  267. '%26oauth_nonce%3D' . $values['oauth_nonce'] .
  268. '%26oauth_signature_method%3DHMAC-SHA1' .
  269. '%26oauth_timestamp%3D' . $values['oauth_timestamp'] .
  270. '%26oauth_token%3Dtoken' .
  271. '%26oauth_version%3D1.0' .
  272. '%26q%3Dpogo',
  273. $result
  274. );
  275. }
  276. /**
  277. * Test HMAC-SHA1 signing
  278. *
  279. * Hash result + parameters taken from
  280. * https://wiki.oauth.net/w/page/12238556/TestCases
  281. */
  282. public function testHmacSigning(): void
  283. {
  284. $request = new Request(
  285. 'http://photos.example.net/photos',
  286. 'GET',
  287. [],
  288. ['file' => 'vacation.jpg', 'size' => 'original']
  289. );
  290. $options = [
  291. 'consumerKey' => 'dpf43f3p2l4k3l03',
  292. 'consumerSecret' => 'kd94hf93k423kf44',
  293. 'tokenSecret' => 'pfkkdhi9sl3r4s00',
  294. 'token' => 'nnch734d00sl2jdk',
  295. 'nonce' => 'kllo9940pd9333jh',
  296. 'timestamp' => '1191242096',
  297. ];
  298. $auth = new Oauth();
  299. $request = $auth->authentication($request, $options);
  300. $result = $request->getHeaderLine('Authorization');
  301. $this->assertSignatureFormat($result);
  302. }
  303. /**
  304. * Test HMAC-SHA1 signing with a base64 consumer key
  305. */
  306. public function testHmacBase64Signing(): void
  307. {
  308. $request = new Request(
  309. 'http://photos.example.net/photos',
  310. 'GET'
  311. );
  312. $options = [
  313. 'consumerKey' => 'ZHBmNDNmM3AybDRrM2wwMw==',
  314. 'consumerSecret' => 'kd94hf93k423kf44',
  315. 'tokenSecret' => 'pfkkdhi9sl3r4s00',
  316. 'token' => 'nnch734d00sl2jdk',
  317. 'nonce' => 'kllo9940pd9333jh',
  318. 'timestamp' => '1191242096',
  319. ];
  320. $auth = new Oauth();
  321. $request = $auth->authentication($request, $options);
  322. $result = $request->getHeaderLine('Authorization');
  323. $this->assertSignatureFormat($result);
  324. }
  325. /**
  326. * Test RSA-SHA1 signing with a private key string
  327. *
  328. * Hash result + parameters taken from
  329. * https://wiki.oauth.net/w/page/12238556/TestCases
  330. */
  331. public function testRsaSigningString(): void
  332. {
  333. $request = new Request(
  334. 'http://photos.example.net/photos',
  335. 'GET',
  336. [],
  337. ['file' => 'vacaction.jpg', 'size' => 'original']
  338. );
  339. $privateKey = $this->privateKeyString;
  340. $options = [
  341. 'method' => 'RSA-SHA1',
  342. 'consumerKey' => 'dpf43f3p2l4k3l03',
  343. 'nonce' => '13917289812797014437',
  344. 'timestamp' => '1196666512',
  345. 'privateKey' => $privateKey,
  346. ];
  347. $auth = new Oauth();
  348. try {
  349. $request = $auth->authentication($request, $options);
  350. $result = $request->getHeaderLine('Authorization');
  351. $this->assertSignatureFormat($result);
  352. } catch (CakeException $e) {
  353. // Handle 22.04 + OpenSSL bug. This should be safe to remove in the future.
  354. if (str_contains($e->getMessage(), 'unexpected eof while reading')) {
  355. $this->markTestSkipped('Skipping because of OpenSSL bug.');
  356. }
  357. throw $e;
  358. }
  359. }
  360. public function testRsaSigningInvalidKey(): void
  361. {
  362. $request = new Request(
  363. 'http://photos.example.net/photos',
  364. 'GET',
  365. [],
  366. ['file' => 'vacaction.jpg', 'size' => 'original']
  367. );
  368. $options = [
  369. 'method' => 'RSA-SHA1',
  370. 'consumerKey' => 'dpf43f3p2l4k3l03',
  371. 'nonce' => '13917289812797014437',
  372. 'timestamp' => '1196666512',
  373. 'privateKey' => 'not a private key',
  374. ];
  375. $auth = new Oauth();
  376. $this->expectException(CakeException::class);
  377. $this->expectExceptionMessage('openssl error');
  378. $auth->authentication($request, $options);
  379. }
  380. /**
  381. * Test RSA-SHA1 signing with a private key file
  382. *
  383. * Hash result + parameters taken from
  384. * https://wiki.oauth.net/w/page/12238556/TestCases
  385. */
  386. public function testRsaSigningFile(): void
  387. {
  388. $request = new Request(
  389. 'http://photos.example.net/photos',
  390. 'GET',
  391. [],
  392. ['file' => 'vacaction.jpg', 'size' => 'original']
  393. );
  394. $privateKey = fopen(TEST_APP . DS . 'config' . DS . 'key.pem', 'r');
  395. $options = [
  396. 'method' => 'RSA-SHA1',
  397. 'consumerKey' => 'dpf43f3p2l4k3l03',
  398. 'nonce' => '13917289812797014437',
  399. 'timestamp' => '1196666512',
  400. 'privateKey' => $privateKey,
  401. ];
  402. $auth = new Oauth();
  403. $request = $auth->authentication($request, $options);
  404. $result = $request->getHeaderLine('Authorization');
  405. $this->assertSignatureFormat($result);
  406. }
  407. /**
  408. * Test RSA-SHA1 signing with a private key file passphrase string
  409. *
  410. * Hash result + parameters taken from
  411. * https://wiki.oauth.net/w/page/12238556/TestCases
  412. */
  413. public function testRsaSigningWithPassphraseString(): void
  414. {
  415. $request = new Request(
  416. 'http://photos.example.net/photos',
  417. 'GET',
  418. [],
  419. ['file' => 'vacaction.jpg', 'size' => 'original']
  420. );
  421. $privateKey = fopen(TEST_APP . DS . 'config' . DS . 'key_with_passphrase.pem', 'r');
  422. $passphrase = 'fancy-cakephp-passphrase';
  423. $options = [
  424. 'method' => 'RSA-SHA1',
  425. 'consumerKey' => 'dpf43f3p2l4k3l03',
  426. 'nonce' => '13917289812797014437',
  427. 'timestamp' => '1196666512',
  428. 'privateKey' => $privateKey,
  429. 'privateKeyPassphrase' => $passphrase,
  430. ];
  431. $auth = new Oauth();
  432. $request = $auth->authentication($request, $options);
  433. $result = $request->getHeaderLine('Authorization');
  434. $this->assertSignatureFormat($result);
  435. }
  436. /**
  437. * Test RSA-SHA1 signing with a private key string and passphrase string
  438. *
  439. * Hash result + parameters taken from
  440. * https://wiki.oauth.net/w/page/12238556/TestCases
  441. */
  442. public function testRsaSigningStringWithPassphraseString(): void
  443. {
  444. $request = new Request(
  445. 'http://photos.example.net/photos',
  446. 'GET',
  447. [],
  448. ['file' => 'vacaction.jpg', 'size' => 'original']
  449. );
  450. $privateKey = $this->privateKeyStringEnc;
  451. $passphrase = 'fancy-cakephp-passphrase';
  452. $options = [
  453. 'method' => 'RSA-SHA1',
  454. 'consumerKey' => 'dpf43f3p2l4k3l03',
  455. 'nonce' => '13917289812797014437',
  456. 'timestamp' => '1196666512',
  457. 'privateKey' => $privateKey,
  458. 'privateKeyPassphrase' => $passphrase,
  459. ];
  460. $auth = new Oauth();
  461. $request = $auth->authentication($request, $options);
  462. $result = $request->getHeaderLine('Authorization');
  463. $this->assertSignatureFormat($result);
  464. }
  465. /**
  466. * Test RSA-SHA1 signing with passphrase file
  467. *
  468. * Hash result + parameters taken from
  469. * https://wiki.oauth.net/w/page/12238556/TestCases
  470. */
  471. public function testRsaSigningWithPassphraseFile(): void
  472. {
  473. $this->skipIf(PHP_EOL !== "\n", 'Just the line ending "\n" is supported. You can run the test again e.g. on a linux system.');
  474. $request = new Request(
  475. 'http://photos.example.net/photos',
  476. 'GET',
  477. [],
  478. ['file' => 'vacaction.jpg', 'size' => 'original']
  479. );
  480. $privateKey = fopen(TEST_APP . DS . 'config' . DS . 'key_with_passphrase.pem', 'r');
  481. $passphrase = fopen(TEST_APP . DS . 'config' . DS . 'key_passphrase_lf', 'r');
  482. $options = [
  483. 'method' => 'RSA-SHA1',
  484. 'consumerKey' => 'dpf43f3p2l4k3l03',
  485. 'nonce' => '13917289812797014437',
  486. 'timestamp' => '1196666512',
  487. 'privateKey' => $privateKey,
  488. 'privateKeyPassphrase' => $passphrase,
  489. ];
  490. $auth = new Oauth();
  491. $request = $auth->authentication($request, $options);
  492. $result = $request->getHeaderLine('Authorization');
  493. $this->assertSignatureFormat($result);
  494. $expected = 0;
  495. $this->assertSame($expected, ftell($passphrase));
  496. }
  497. /**
  498. * Test RSA-SHA1 signing with a private key string and passphrase file
  499. *
  500. * Hash result + parameters taken from
  501. * https://wiki.oauth.net/w/page/12238556/TestCases
  502. */
  503. public function testRsaSigningStringWithPassphraseFile(): void
  504. {
  505. $this->skipIf(PHP_EOL !== "\n", 'Just the line ending "\n" is supported. You can run the test again e.g. on a linux system.');
  506. $request = new Request(
  507. 'http://photos.example.net/photos',
  508. 'GET',
  509. [],
  510. ['file' => 'vacaction.jpg', 'size' => 'original']
  511. );
  512. $privateKey = $this->privateKeyStringEnc;
  513. $passphrase = fopen(TEST_APP . DS . 'config' . DS . 'key_passphrase_lf', 'r');
  514. $options = [
  515. 'method' => 'RSA-SHA1',
  516. 'consumerKey' => 'dpf43f3p2l4k3l03',
  517. 'nonce' => '13917289812797014437',
  518. 'timestamp' => '1196666512',
  519. 'privateKey' => $privateKey,
  520. 'privateKeyPassphrase' => $passphrase,
  521. ];
  522. $auth = new Oauth();
  523. $request = $auth->authentication($request, $options);
  524. $result = $request->getHeaderLine('Authorization');
  525. $this->assertSignatureFormat($result);
  526. $expected = 0;
  527. $this->assertSame($expected, ftell($passphrase));
  528. }
  529. protected function assertSignatureFormat($result)
  530. {
  531. $this->assertMatchesRegularExpression(
  532. '/oauth_signature="[a-zA-Z0-9\/=+]+"/',
  533. urldecode($result)
  534. );
  535. }
  536. }