OauthTest.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Test\Unit\Oauth;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class OauthTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /** @var \Magento\Integration\Model\Oauth\ConsumerFactory */
  13. private $_consumerFactory;
  14. /** @var \Magento\Integration\Model\Oauth\NonceFactory */
  15. private $_nonceFactory;
  16. /** @var \Magento\Integration\Model\Oauth\TokenFactory */
  17. private $_tokenFactory;
  18. /** @var \Magento\Integration\Model\Oauth\Consumer */
  19. private $_consumerMock;
  20. /** @var \Magento\Integration\Model\Oauth\Token */
  21. private $_tokenMock;
  22. /** @var \Magento\Framework\Oauth\Helper\Oauth */
  23. private $_oauthHelperMock;
  24. /** @var \Magento\Framework\Oauth\Oauth */
  25. private $_oauth;
  26. /** @var \Zend_Oauth_Http_Utility */
  27. private $_httpUtilityMock;
  28. /** @var \Magento\Framework\Stdlib\DateTime\DateTime */
  29. private $_dateMock;
  30. /**
  31. * @var \Psr\Log\LoggerInterface
  32. */
  33. private $_loggerMock;
  34. private $_oauthToken;
  35. private $_oauthSecret;
  36. private $_oauthVerifier;
  37. const CONSUMER_ID = 1;
  38. const REQUEST_URL = 'http://magento.ll';
  39. protected function setUp()
  40. {
  41. $this->_consumerFactory = $this->getMockBuilder(\Magento\Integration\Model\Oauth\ConsumerFactory::class)
  42. ->disableOriginalConstructor()
  43. ->setMethods(['create'])
  44. ->getMock();
  45. $this->_consumerMock = $this->getMockBuilder(\Magento\Integration\Model\Oauth\Consumer::class)
  46. ->disableOriginalConstructor()->setMethods(
  47. [
  48. 'getCreatedAt',
  49. 'loadByKey',
  50. 'load',
  51. 'getId',
  52. 'getSecret',
  53. 'getCallbackUrl',
  54. 'save',
  55. 'getData',
  56. 'isValidForTokenExchange',
  57. '__wakeup',
  58. ]
  59. )
  60. ->getMock();
  61. $this->_consumerFactory->expects($this->any())
  62. ->method('create')
  63. ->will($this->returnValue($this->_consumerMock));
  64. $this->_nonceFactory = $this->getMockBuilder(\Magento\Integration\Model\Oauth\NonceFactory::class)
  65. ->disableOriginalConstructor()
  66. ->setMethods(['create'])
  67. ->getMock();
  68. $this->_tokenFactory = $this->getMockBuilder(
  69. \Magento\Integration\Model\Oauth\TokenFactory::class
  70. )->disableOriginalConstructor()->setMethods(['create'])->getMock();
  71. $this->_tokenMock = $this->getMockBuilder(\Magento\Integration\Model\Oauth\Token::class)
  72. ->disableOriginalConstructor()
  73. ->setMethods(
  74. [
  75. 'getId',
  76. 'load',
  77. 'getType',
  78. 'createRequestToken',
  79. 'getToken',
  80. 'getSecret',
  81. 'createVerifierToken',
  82. 'getVerifier',
  83. 'getConsumerId',
  84. 'convertToAccess',
  85. 'getRevoked',
  86. 'getResource',
  87. 'loadByConsumerIdAndUserType',
  88. '__wakeup',
  89. ]
  90. )
  91. ->getMock();
  92. $this->_tokenFactory->expects($this->any())->method('create')->will($this->returnValue($this->_tokenMock));
  93. $this->_oauthHelperMock = $this->getMockBuilder(\Magento\Framework\Oauth\Helper\Oauth::class)
  94. ->setConstructorArgs([new \Magento\Framework\Math\Random()])
  95. ->getMock();
  96. $this->_httpUtilityMock = $this->getMockBuilder(\Zend_Oauth_Http_Utility::class)
  97. ->setMethods(['sign'])
  98. ->getMock();
  99. $this->_dateMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\DateTime::class)
  100. ->disableOriginalConstructor()
  101. ->getMock();
  102. $this->_loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
  103. ->disableOriginalConstructor()
  104. ->getMock();
  105. $nonceGenerator = new \Magento\Integration\Model\Oauth\Nonce\Generator(
  106. $this->_oauthHelperMock,
  107. $this->_nonceFactory,
  108. $this->_dateMock
  109. );
  110. $tokenProvider = new \Magento\Integration\Model\Oauth\Token\Provider(
  111. $this->_consumerFactory,
  112. $this->_tokenFactory,
  113. $this->_loggerMock
  114. );
  115. $this->_oauth = new \Magento\Framework\Oauth\Oauth(
  116. $this->_oauthHelperMock,
  117. $nonceGenerator,
  118. $tokenProvider,
  119. $this->_httpUtilityMock
  120. );
  121. $this->_oauthToken = $this->_generateRandomString(\Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN);
  122. $this->_oauthSecret = $this->_generateRandomString(\Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN_SECRET);
  123. $this->_oauthVerifier = $this->_generateRandomString(
  124. \Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN_VERIFIER
  125. );
  126. }
  127. public function tearDown()
  128. {
  129. unset($this->_consumerFactory);
  130. unset($this->_nonceFactory);
  131. unset($this->_tokenFactory);
  132. unset($this->_oauthHelperMock);
  133. unset($this->_httpUtilityMock);
  134. unset($this->_dateMock);
  135. unset($this->_oauth);
  136. }
  137. /**
  138. * @param array $amendments
  139. * @return array
  140. */
  141. protected function _getRequestTokenParams($amendments = [])
  142. {
  143. $requiredParams = [
  144. 'oauth_version' => '1.0',
  145. 'oauth_consumer_key' => $this->_generateRandomString(
  146. \Magento\Framework\Oauth\Helper\Oauth::LENGTH_CONSUMER_KEY
  147. ),
  148. 'oauth_nonce' => '',
  149. 'oauth_timestamp' => time(),
  150. 'oauth_signature_method' => \Magento\Framework\Oauth\OauthInterface::SIGNATURE_SHA1,
  151. 'oauth_signature' => 'invalid_signature',
  152. ];
  153. return array_merge($requiredParams, $amendments);
  154. }
  155. /**
  156. * \Magento\Framework\Oauth\OauthInterface::ERR_VERSION_REJECTED
  157. *
  158. * @expectedException \Magento\Framework\Oauth\OauthInputException
  159. */
  160. public function testGetRequestTokenVersionRejected()
  161. {
  162. $this->_oauth->getRequestToken(
  163. $this->_getRequestTokenParams(['oauth_version' => '2.0']),
  164. self::REQUEST_URL
  165. );
  166. }
  167. /**
  168. * \Magento\Framework\Oauth\OauthInterface::ERR_CONSUMER_KEY_REJECTED
  169. *
  170. * @expectedException \Magento\Framework\Oauth\Exception
  171. */
  172. public function testGetRequestTokenConsumerKeyRejected()
  173. {
  174. $this->_oauth->getRequestToken(
  175. $this->_getRequestTokenParams(['oauth_consumer_key' => 'wrong_key_length']),
  176. self::REQUEST_URL
  177. );
  178. }
  179. /**
  180. * \Magento\Framework\Oauth\OauthInterface::ERR_CONSUMER_KEY_REJECTED
  181. *
  182. * @expectedException \Magento\Framework\Oauth\Exception
  183. */
  184. public function testGetRequestTokenConsumerKeyNotFound()
  185. {
  186. $this->_consumerMock->expects(
  187. $this->once()
  188. )->method(
  189. 'loadByKey'
  190. )->will(
  191. $this->returnValue(new \Magento\Framework\DataObject())
  192. );
  193. $this->_oauth->getRequestToken($this->_getRequestTokenParams(), self::REQUEST_URL);
  194. }
  195. /**
  196. * \Magento\Framework\Oauth\OauthInterface::ERR_CONSUMER_KEY_INVALID
  197. *
  198. * @expectedException \Magento\Framework\Oauth\Exception
  199. */
  200. public function testGetRequestTokenOutdatedConsumerKey()
  201. {
  202. $this->_setupConsumer();
  203. $this->_setupNonce();
  204. $this->_consumerMock
  205. ->expects($this->any())
  206. ->method('isValidForTokenExchange')
  207. ->will($this->returnValue(false));
  208. $this->_oauth->getRequestToken($this->_getRequestTokenParams(), self::REQUEST_URL);
  209. }
  210. /**
  211. * @param bool $isLoadable
  212. */
  213. protected function _setupConsumer($isLoadable = true)
  214. {
  215. $this->_consumerMock->expects($this->any())->method('loadByKey')->will($this->returnSelf());
  216. $this->_consumerMock->expects(
  217. $this->any()
  218. )->method(
  219. 'getCreatedAt'
  220. )->will(
  221. $this->returnValue(date('c', strtotime('-1 day')))
  222. );
  223. if ($isLoadable) {
  224. $this->_consumerMock->expects($this->any())->method('load')->will($this->returnSelf());
  225. } else {
  226. $this->_consumerMock->expects(
  227. $this->any()
  228. )->method(
  229. 'load'
  230. )->will(
  231. $this->returnValue(new \Magento\Framework\DataObject())
  232. );
  233. }
  234. $this->_consumerMock->expects($this->any())->method('getId')->will($this->returnValue(1));
  235. $this->_consumerMock->expects($this->any())->method('getSecret')->will($this->returnValue('consumer_secret'));
  236. $this->_consumerMock->expects(
  237. $this->any()
  238. )->method(
  239. 'getCallbackUrl'
  240. )->will(
  241. $this->returnValue('callback_url')
  242. );
  243. }
  244. protected function _makeValidExpirationPeriod()
  245. {
  246. $this->_consumerMock
  247. ->expects($this->any())
  248. ->method('isValidForTokenExchange')
  249. ->will($this->returnValue(true));
  250. }
  251. /**
  252. * \Magento\Framework\Oauth\OauthInterface::ERR_TIMESTAMP_REFUSED
  253. *
  254. * @expectedException \Magento\Framework\Oauth\Exception
  255. * @dataProvider dataProviderForGetRequestTokenNonceTimestampRefusedTest
  256. */
  257. public function testGetRequestTokenOauthTimestampRefused($timestamp)
  258. {
  259. $this->_setupConsumer();
  260. $this->_makeValidExpirationPeriod();
  261. $this->_oauth->getRequestToken(
  262. $this->_getRequestTokenParams(['oauth_timestamp' => $timestamp]),
  263. self::REQUEST_URL
  264. );
  265. }
  266. /**
  267. * @return array
  268. */
  269. public function dataProviderForGetRequestTokenNonceTimestampRefusedTest()
  270. {
  271. return [
  272. [0],
  273. //Adding one day deviation
  274. [time() + \Magento\Integration\Model\Oauth\Nonce\Generator::TIME_DEVIATION + 86400]
  275. ];
  276. }
  277. /**
  278. * @param bool $isUsed
  279. * @param int $timestamp
  280. */
  281. protected function _setupNonce($isUsed = false, $timestamp = 0)
  282. {
  283. $nonceMock = $this->getMockBuilder(
  284. \Magento\Integration\Model\Oauth\Nonce::class
  285. )->disableOriginalConstructor()->setMethods(
  286. [
  287. 'loadByCompositeKey',
  288. 'getNonce',
  289. 'getTimestamp',
  290. 'setNonce',
  291. 'setConsumerId',
  292. 'setTimestamp',
  293. 'save',
  294. '__wakeup',
  295. ]
  296. )->getMock();
  297. $nonceMock->expects($this->any())->method('getNonce')->will($this->returnValue($isUsed));
  298. $nonceMock->expects($this->any())->method('loadByCompositeKey')->will($this->returnSelf());
  299. $nonceMock->expects($this->any())->method('getTimestamp')->will($this->returnValue($timestamp));
  300. $nonceMock->expects($this->any())->method('setNonce')->will($this->returnSelf());
  301. $nonceMock->expects($this->any())->method('setConsumerId')->will($this->returnSelf());
  302. $nonceMock->expects($this->any())->method('setTimestamp')->will($this->returnSelf());
  303. $nonceMock->expects($this->any())->method('save')->will($this->returnSelf());
  304. $this->_nonceFactory->expects($this->any())->method('create')->will($this->returnValue($nonceMock));
  305. }
  306. /**
  307. * \Magento\Framework\Oauth\OauthInterface::ERR_NONCE_USED
  308. *
  309. * @expectedException \Magento\Framework\Oauth\Exception
  310. */
  311. public function testGetRequestTokenNonceAlreadyUsed()
  312. {
  313. $this->_setupConsumer();
  314. $this->_makeValidExpirationPeriod();
  315. $this->_setupNonce(true);
  316. $this->_oauth->getRequestToken($this->_getRequestTokenParams(), self::REQUEST_URL);
  317. }
  318. /**
  319. * \Magento\Framework\Oauth\OauthInterface::ERR_CONSUMER_KEY_REJECTED
  320. *
  321. * @expectedException \Magento\Framework\Oauth\Exception
  322. */
  323. public function testGetRequestTokenNoConsumer()
  324. {
  325. $this->_consumerMock->expects(
  326. $this->any()
  327. )->method(
  328. 'loadByKey'
  329. )->will(
  330. $this->returnValue(new \Magento\Framework\DataObject())
  331. );
  332. $this->_oauth->getRequestToken($this->_getRequestTokenParams(), self::REQUEST_URL);
  333. }
  334. /**
  335. * @param bool $doesExist
  336. * @param string $type
  337. * @param int $consumerId
  338. * @param null $verifier
  339. * @param bool $isRevoked
  340. */
  341. protected function _setupToken(
  342. $doesExist = true,
  343. $type = \Magento\Integration\Model\Oauth\Token::TYPE_VERIFIER,
  344. $consumerId = self::CONSUMER_ID,
  345. $verifier = null,
  346. $isRevoked = false
  347. ) {
  348. $this->_tokenMock->expects(
  349. $this->any()
  350. )->method(
  351. 'getId'
  352. )->will(
  353. $this->returnValue($doesExist ? self::CONSUMER_ID : null)
  354. );
  355. $verifier = $verifier ?: $this->_oauthVerifier;
  356. $this->_tokenMock->expects($this->any())->method('load')->will($this->returnSelf());
  357. $this->_tokenMock->expects($this->any())->method('getType')->will($this->returnValue($type));
  358. $this->_tokenMock->expects($this->any())->method('createRequestToken')->will($this->returnSelf());
  359. $this->_tokenMock->expects($this->any())->method('getToken')->will($this->returnValue($this->_oauthToken));
  360. $this->_tokenMock->expects($this->any())->method('getSecret')->will($this->returnValue($this->_oauthSecret));
  361. $this->_tokenMock->expects($this->any())->method('getConsumerId')->will($this->returnValue($consumerId));
  362. $this->_tokenMock->expects($this->any())->method('getVerifier')->will($this->returnValue($verifier));
  363. $this->_tokenMock->expects($this->any())->method('convertToAccess')->will($this->returnSelf());
  364. $this->_tokenMock->expects($this->any())->method('getRevoked')->will($this->returnValue($isRevoked));
  365. $this->_tokenMock->expects($this->any())->method('loadByConsumerIdAndUserType')->will($this->returnSelf());
  366. }
  367. /**
  368. * \Magento\Framework\Oauth\OauthInterface::ERR_TOKEN_REJECTED
  369. *
  370. * @expectedException \Magento\Framework\Oauth\Exception
  371. */
  372. public function testGetRequestTokenTokenRejected()
  373. {
  374. $this->_setupConsumer();
  375. $this->_makeValidExpirationPeriod();
  376. $this->_setupNonce();
  377. $this->_setupToken(false);
  378. $signature = 'valid_signature';
  379. $this->_httpUtilityMock->expects($this->any())->method('sign')->will($this->returnValue($signature));
  380. $this->_oauth->getRequestToken(
  381. $this->_getRequestTokenParams(['oauth_signature' => $signature]),
  382. self::REQUEST_URL
  383. );
  384. }
  385. /**
  386. * \Magento\Framework\Oauth\OauthInterface::ERR_TOKEN_REJECTED
  387. *
  388. * @expectedException \Magento\Framework\Oauth\Exception
  389. */
  390. public function testGetRequestTokenTokenRejectedByType()
  391. {
  392. $this->_setupConsumer();
  393. $this->_makeValidExpirationPeriod();
  394. $this->_setupNonce();
  395. $this->_setupToken(true, \Magento\Integration\Model\Oauth\Token::TYPE_REQUEST);
  396. // wrong type
  397. $signature = 'valid_signature';
  398. $this->_httpUtilityMock->expects($this->any())->method('sign')->will($this->returnValue($signature));
  399. $this->_oauth->getRequestToken(
  400. $this->_getRequestTokenParams(['oauth_signature' => $signature]),
  401. self::REQUEST_URL
  402. );
  403. }
  404. /**
  405. * \Magento\Framework\Oauth\OauthInterface::ERR_SIGNATURE_METHOD_REJECTED
  406. *
  407. * @expectedException \Magento\Framework\Oauth\OauthInputException
  408. */
  409. public function testGetRequestTokenSignatureMethodRejected()
  410. {
  411. $this->_setupConsumer();
  412. $this->_makeValidExpirationPeriod();
  413. $this->_setupNonce();
  414. $this->_setupToken();
  415. $this->_oauth->getRequestToken(
  416. $this->_getRequestTokenParams(['oauth_signature_method' => 'wrong_method']),
  417. self::REQUEST_URL
  418. );
  419. }
  420. /**
  421. * \Magento\Framework\Oauth\OauthInterface::ERR_SIGNATURE_INVALID
  422. *
  423. * @expectedException \Magento\Framework\Oauth\Exception
  424. */
  425. public function testGetRequestTokenInvalidSignature()
  426. {
  427. $this->_setupConsumer();
  428. $this->_makeValidExpirationPeriod();
  429. $this->_setupNonce();
  430. $this->_setupToken();
  431. $this->_oauth->getRequestToken(
  432. $this->_getRequestTokenParams(['oauth_signature' => 'invalid_signature']),
  433. self::REQUEST_URL
  434. );
  435. }
  436. public function testGetRequestToken()
  437. {
  438. $this->_setupConsumer();
  439. $this->_makeValidExpirationPeriod();
  440. $this->_setupNonce();
  441. $this->_setupToken();
  442. $signature = 'valid_signature';
  443. $this->_httpUtilityMock->expects($this->any())->method('sign')->will($this->returnValue($signature));
  444. $requestToken = $this->_oauth->getRequestToken(
  445. $this->_getRequestTokenParams(['oauth_signature' => $signature]),
  446. self::REQUEST_URL
  447. );
  448. $this->assertEquals(
  449. ['oauth_token' => $this->_oauthToken, 'oauth_token_secret' => $this->_oauthSecret],
  450. $requestToken
  451. );
  452. }
  453. /**
  454. * \Magento\Framework\Oauth\OauthInterface::ERR_VERSION_REJECTED
  455. *
  456. * @expectedException \Magento\Framework\Oauth\OauthInputException
  457. */
  458. public function testGetAccessTokenVersionRejected()
  459. {
  460. $this->_oauth->getAccessToken(
  461. $this->_getAccessTokenRequiredParams(['oauth_version' => '0.0']),
  462. self::REQUEST_URL
  463. );
  464. }
  465. /**
  466. * \Magento\Framework\Oauth\OauthInterface::ERR_PARAMETER_ABSENT
  467. *
  468. * @expectedException \Magento\Framework\Oauth\OauthInputException
  469. * @expectedExceptionMessage "oauth_verifier" is required. Enter and try again.
  470. */
  471. public function testGetAccessTokenParameterAbsent()
  472. {
  473. $this->_oauth->getAccessToken(
  474. [
  475. 'oauth_version' => '1.0',
  476. 'oauth_consumer_key' => '',
  477. 'oauth_signature' => '',
  478. 'oauth_signature_method' => '',
  479. 'oauth_nonce' => '',
  480. 'oauth_timestamp' => '',
  481. 'oauth_token' => '',
  482. // oauth_verifier missing
  483. ],
  484. self::REQUEST_URL
  485. );
  486. }
  487. /**
  488. * \Magento\Framework\Oauth\OauthInterface::ERR_TOKEN_REJECTED
  489. *
  490. * @expectedException \Magento\Framework\Oauth\OauthInputException
  491. */
  492. public function testGetAccessTokenTokenRejected()
  493. {
  494. $this->_oauth->getAccessToken(
  495. $this->_getAccessTokenRequiredParams(['oauth_token' => 'invalid_token']),
  496. self::REQUEST_URL
  497. );
  498. }
  499. /**
  500. * \Magento\Framework\Oauth\OauthInterface::ERR_SIGNATURE_METHOD_REJECTED
  501. *
  502. * @expectedException \Magento\Framework\Oauth\OauthInputException
  503. */
  504. public function testGetAccessTokenSignatureMethodRejected()
  505. {
  506. $this->_oauth->getAccessToken(
  507. $this->_getAccessTokenRequiredParams(['oauth_signature_method' => 'invalid_method']),
  508. self::REQUEST_URL
  509. );
  510. }
  511. /**
  512. * \Magento\Framework\Oauth\OauthInterface::ERR_TOKEN_USED
  513. *
  514. * @expectedException \Magento\Framework\Oauth\Exception
  515. */
  516. public function testGetAccessTokenTokenUsed()
  517. {
  518. $this->_setupConsumer();
  519. $this->_setupNonce();
  520. $this->_setupToken(true, \Magento\Integration\Model\Oauth\Token::TYPE_VERIFIER);
  521. // Wrong type
  522. $this->_oauth->getAccessToken($this->_getAccessTokenRequiredParams(), self::REQUEST_URL);
  523. }
  524. /**
  525. * \Magento\Framework\Oauth\OauthInterface::ERR_TOKEN_REJECTED
  526. *
  527. * @expectedException \Magento\Framework\Oauth\Exception
  528. */
  529. public function testGetAccessTokenConsumerIdDoesntMatch()
  530. {
  531. $this->_setupConsumer();
  532. $this->_setupNonce();
  533. $this->_setupToken(true, \Magento\Integration\Model\Oauth\Token::TYPE_REQUEST, null);
  534. $this->_oauth->getAccessToken($this->_getAccessTokenRequiredParams(), self::REQUEST_URL);
  535. }
  536. /**
  537. * \Magento\Framework\Oauth\OauthInterface::ERR_VERIFIER_INVALID
  538. *
  539. * @expectedException \Magento\Framework\Oauth\Exception
  540. * @dataProvider dataProviderForGetAccessTokenVerifierInvalidTest
  541. */
  542. public function testGetAccessTokenVerifierInvalid($verifier, $verifierFromToken)
  543. {
  544. $this->_setupConsumer();
  545. $this->_setupNonce();
  546. $this->_setupToken(
  547. true,
  548. \Magento\Integration\Model\Oauth\Token::TYPE_REQUEST,
  549. self::CONSUMER_ID,
  550. $verifierFromToken
  551. );
  552. $this->_oauth->getAccessToken(
  553. $this->_getAccessTokenRequiredParams(['oauth_verifier' => $verifier]),
  554. self::REQUEST_URL
  555. );
  556. }
  557. /**
  558. * @return array
  559. */
  560. public function dataProviderForGetAccessTokenVerifierInvalidTest()
  561. {
  562. // Verifier is not a string
  563. return [[3, 3], ['wrong_length', 'wrong_length'], ['verifier', 'doesn\'t match']];
  564. }
  565. public function testGetAccessToken()
  566. {
  567. $this->_setupConsumer();
  568. $this->_setupNonce();
  569. $this->_setupToken(true, \Magento\Integration\Model\Oauth\Token::TYPE_REQUEST);
  570. $token = $this->_oauth->getAccessToken($this->_getAccessTokenRequiredParams(), self::REQUEST_URL);
  571. $this->assertEquals(
  572. ['oauth_token' => $this->_oauthToken, 'oauth_token_secret' => $this->_oauthSecret],
  573. $token
  574. );
  575. }
  576. /**
  577. * \Magento\Framework\Oauth\OauthInterface::ERR_TOKEN_REJECTED
  578. *
  579. * @expectedException \Magento\Framework\Oauth\Exception
  580. */
  581. public function testValidateAccessTokenRequestTokenRejected()
  582. {
  583. $this->_setupConsumer();
  584. $this->_setupNonce();
  585. $this->_setupToken(true, \Magento\Integration\Model\Oauth\Token::TYPE_ACCESS, null);
  586. $this->_oauth->validateAccessTokenRequest($this->_getAccessTokenRequiredParams(), self::REQUEST_URL);
  587. }
  588. /**
  589. * \Magento\Framework\Oauth\OauthInterface::ERR_TOKEN_REJECTED
  590. *
  591. * @expectedException \Magento\Framework\Oauth\Exception
  592. */
  593. public function testValidateAccessTokenRequestTokenRejectedByType()
  594. {
  595. $this->_setupConsumer();
  596. $this->_setupNonce();
  597. $this->_setupToken(true, \Magento\Integration\Model\Oauth\Token::TYPE_REQUEST);
  598. $this->_oauth->validateAccessTokenRequest($this->_getAccessTokenRequiredParams(), self::REQUEST_URL);
  599. }
  600. /**
  601. * \Magento\Framework\Oauth\OauthInterface::ERR_TOKEN_REVOKED
  602. *
  603. * @expectedException \Magento\Framework\Oauth\Exception
  604. */
  605. public function testValidateAccessTokenRequestTokenRevoked()
  606. {
  607. $this->_setupConsumer();
  608. $this->_setupNonce();
  609. $this->_setupToken(
  610. true,
  611. \Magento\Integration\Model\Oauth\Token::TYPE_ACCESS,
  612. self::CONSUMER_ID,
  613. $this->_oauthVerifier,
  614. true
  615. );
  616. $this->_oauth->validateAccessTokenRequest($this->_getAccessTokenRequiredParams(), self::REQUEST_URL);
  617. }
  618. public function testValidateAccessTokenRequest()
  619. {
  620. $this->_setupConsumer();
  621. $this->_setupNonce();
  622. $this->_setupToken(true, \Magento\Integration\Model\Oauth\Token::TYPE_ACCESS);
  623. $requiredParams = $this->_getAccessTokenRequiredParams();
  624. $this->assertEquals(
  625. 1,
  626. $this->_oauth->validateAccessTokenRequest($requiredParams, self::REQUEST_URL),
  627. "Consumer ID is invalid."
  628. );
  629. }
  630. /**
  631. * \Magento\Framework\Oauth\OauthInterface::ERR_TOKEN_REJECTED
  632. *
  633. * @expectedException \Magento\Framework\Oauth\Exception
  634. */
  635. public function testValidateAccessTokenRejectedByType()
  636. {
  637. $this->_setupConsumer();
  638. $this->_setupToken(true, \Magento\Integration\Model\Oauth\Token::TYPE_REQUEST);
  639. $this->_oauth->validateAccessToken($this->_oauthToken);
  640. }
  641. /**
  642. * \Magento\Framework\Oauth\OauthInterface::ERR_TOKEN_REVOKED
  643. *
  644. * @expectedException \Magento\Framework\Oauth\Exception
  645. */
  646. public function testValidateAccessTokenRevoked()
  647. {
  648. $this->_setupConsumer();
  649. $this->_setupToken(
  650. true,
  651. \Magento\Integration\Model\Oauth\Token::TYPE_ACCESS,
  652. self::CONSUMER_ID,
  653. $this->_oauthVerifier,
  654. true
  655. );
  656. $this->_oauth->validateAccessToken($this->_oauthToken);
  657. }
  658. /**
  659. * \Magento\Framework\Oauth\OauthInterface::ERR_TOKEN_REJECTED
  660. *
  661. * @expectedException \Magento\Framework\Oauth\Exception
  662. */
  663. public function testValidateAccessTokenNoConsumer()
  664. {
  665. $this->_setupConsumer(false);
  666. $this->_setupToken(true, \Magento\Integration\Model\Oauth\Token::TYPE_ACCESS);
  667. $this->_oauth->validateAccessToken($this->_oauthToken);
  668. }
  669. public function testValidateAccessToken()
  670. {
  671. $this->_setupConsumer();
  672. $this->_setupToken(true, \Magento\Integration\Model\Oauth\Token::TYPE_ACCESS);
  673. $this->assertEquals(1, $this->_oauth->validateAccessToken($this->_oauthToken), "Consumer ID is invalid.");
  674. }
  675. public function testBuildAuthorizationHeader()
  676. {
  677. $signature = 'valid_signature';
  678. $this->_httpUtilityMock->expects($this->any())->method('sign')->will($this->returnValue($signature));
  679. $this->_setupConsumer(false);
  680. $this->_oauthHelperMock->expects(
  681. $this->any()
  682. )->method(
  683. 'generateRandomString'
  684. )->will(
  685. $this->returnValue('tyukmnjhgfdcvxstyuioplkmnhtfvert')
  686. );
  687. $request = [
  688. 'oauth_consumer_key' => 'edf957ef88492f0a32eb7e1731e85da2',
  689. 'oauth_consumer_secret' => 'asdawwewefrtyh2f0a32eb7e1731e85d',
  690. 'oauth_token' => '7c0709f789e1f38a17aa4b9a28e1b06c',
  691. 'oauth_token_secret' => 'a6agsfrsfgsrjjjjyy487939244ssggg',
  692. 'custom_param1' => 'foo',
  693. 'custom_param2' => 'bar',
  694. ];
  695. $requestUrl = 'http://www.example.com/endpoint';
  696. $oauthHeader = $this->_oauth->buildAuthorizationHeader($request, $requestUrl);
  697. $expectedHeader = 'OAuth oauth_nonce="tyukmnjhgfdcvxstyuioplkmnhtfvert",' .
  698. 'oauth_timestamp="",' .
  699. 'oauth_version="1.0",oauth_consumer_key="edf957ef88492f0a32eb7e1731e85da2",' .
  700. 'oauth_consumer_secret="asdawwewefrtyh2f0a32eb7e1731e85d",' .
  701. 'oauth_token="7c0709f789e1f38a17aa4b9a28e1b06c",' .
  702. 'oauth_token_secret="a6agsfrsfgsrjjjjyy487939244ssggg",' .
  703. 'oauth_signature="valid_signature"';
  704. $this->assertEquals($expectedHeader, $oauthHeader, 'Generated Oauth header is incorrect');
  705. }
  706. /**
  707. * @dataProvider dataProviderMissingParamForBuildAuthorizationHeaderTest
  708. */
  709. public function testMissingParamForBuildAuthorizationHeader($expectedMessage, $request)
  710. {
  711. $this->expectException(\Magento\Framework\Oauth\OauthInputException::class);
  712. $this->expectExceptionMessage($expectedMessage);
  713. $this->expectExceptionCode(0);
  714. $requestUrl = 'http://www.example.com/endpoint';
  715. $this->_oauth->buildAuthorizationHeader($request, $requestUrl);
  716. }
  717. /**
  718. * @return array
  719. */
  720. public function dataProviderMissingParamForBuildAuthorizationHeaderTest()
  721. {
  722. return [
  723. [
  724. 'oauth_consumer_key',
  725. [ //'oauth_consumer_key' => 'edf957ef88492f0a32eb7e1731e85d',
  726. 'oauth_consumer_secret' => 'asdawwewefrtyh2f0a32eb7e1731e85d',
  727. 'oauth_token' => '7c0709f789e1f38a17aa4b9a28e1b06c',
  728. 'oauth_token_secret' => 'a6agsfrsfgsrjjjjyy487939244ssggg',
  729. 'custom_param1' => 'foo',
  730. 'custom_param2' => 'bar'
  731. ],
  732. ],
  733. [
  734. 'oauth_consumer_secret',
  735. [
  736. 'oauth_consumer_key' => 'edf957ef88492f0a32eb7e1731e85d',
  737. //'oauth_consumer_secret' => 'asdawwewefrtyh2f0a32eb7e1731e85d',
  738. 'oauth_token' => '7c0709f789e1f38a17aa4b9a28e1b06c',
  739. 'oauth_token_secret' => 'a6agsfrsfgsrjjjjyy487939244ssggg',
  740. 'custom_param1' => 'foo',
  741. 'custom_param2' => 'bar'
  742. ]
  743. ],
  744. [
  745. 'oauth_token',
  746. [
  747. 'oauth_consumer_key' => 'edf957ef88492f0a32eb7e1731e85d',
  748. 'oauth_consumer_secret' => 'asdawwewefrtyh2f0a32eb7e1731e85d',
  749. //'oauth_token' => '7c0709f789e1f38a17aa4b9a28e1b06c',
  750. 'oauth_token_secret' => 'a6agsfrsfgsrjjjjyy487939244ssggg',
  751. 'custom_param1' => 'foo',
  752. 'custom_param2' => 'bar'
  753. ]
  754. ],
  755. [
  756. 'oauth_token_secret',
  757. [
  758. 'oauth_consumer_key' => 'edf957ef88492f0a32eb7e1731e85d',
  759. 'oauth_consumer_secret' => 'asdawwewefrtyh2f0a32eb7e1731e85d',
  760. 'oauth_token' => '7c0709f789e1f38a17aa4b9a28e1b06c',
  761. //'oauth_token_secret' => 'a6agsfrsfgsrjjjjyy487939244ssggg',
  762. 'custom_param1' => 'foo',
  763. 'custom_param2' => 'bar'
  764. ]
  765. ]
  766. ];
  767. }
  768. /**
  769. * @param array $amendments
  770. * @return array
  771. */
  772. protected function _getAccessTokenRequiredParams($amendments = [])
  773. {
  774. $requiredParams = [
  775. 'oauth_consumer_key' => $this->_generateRandomString(
  776. \Magento\Framework\Oauth\Helper\Oauth::LENGTH_CONSUMER_KEY
  777. ),
  778. 'oauth_signature' => '',
  779. 'oauth_signature_method' => \Magento\Framework\Oauth\OauthInterface::SIGNATURE_SHA1,
  780. 'oauth_nonce' => '',
  781. 'oauth_timestamp' => (string)time(),
  782. 'oauth_token' => $this->_generateRandomString(\Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN),
  783. 'oauth_verifier' => $this->_oauthVerifier,
  784. ];
  785. return array_merge($requiredParams, $amendments);
  786. }
  787. /**
  788. * @param $length
  789. * @return bool|string
  790. */
  791. private function _generateRandomString($length)
  792. {
  793. return substr(
  794. str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 5)),
  795. 0,
  796. $length
  797. );
  798. }
  799. }