RestTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * Test authentication mechanisms in REST.
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Webapi\Authentication;
  9. /**
  10. * @magentoApiDataFixture consumerFixture
  11. */
  12. class RestTest extends \Magento\TestFramework\TestCase\WebapiAbstract
  13. {
  14. /** @var \Magento\TestFramework\Authentication\Rest\OauthClient[] */
  15. protected $_oAuthClients = [];
  16. /** @var \Magento\Integration\Model\Oauth\Consumer */
  17. protected static $_consumer;
  18. /** @var \Magento\Integration\Model\Oauth\Token */
  19. protected static $_token;
  20. /** @var string */
  21. protected static $_consumerKey;
  22. /** @var string */
  23. protected static $_consumerSecret;
  24. /** @var string */
  25. protected static $_verifier;
  26. protected function setUp()
  27. {
  28. $this->_markTestAsRestOnly();
  29. parent::setUp();
  30. }
  31. /**
  32. * Create a consumer
  33. */
  34. public static function consumerFixture($date = null)
  35. {
  36. /** Clear the credentials because during the fixture generation, any previous credentials are invalidated */
  37. \Magento\TestFramework\Authentication\OauthHelper::clearApiAccessCredentials();
  38. $consumerCredentials = \Magento\TestFramework\Authentication\OauthHelper::getConsumerCredentials($date);
  39. self::$_consumerKey = $consumerCredentials['key'];
  40. self::$_consumerSecret = $consumerCredentials['secret'];
  41. self::$_verifier = $consumerCredentials['verifier'];
  42. self::$_consumer = $consumerCredentials['consumer'];
  43. self::$_token = $consumerCredentials['token'];
  44. }
  45. protected function tearDown()
  46. {
  47. parent::tearDown();
  48. $this->_oAuthClients = [];
  49. if (isset(self::$_consumer)) {
  50. self::$_consumer->delete();
  51. self::$_token->delete();
  52. }
  53. }
  54. public function testGetRequestToken()
  55. {
  56. /** @var $oAuthClient \Magento\TestFramework\Authentication\Rest\OauthClient */
  57. $oAuthClient = $this->_getOauthClient(self::$_consumerKey, self::$_consumerSecret);
  58. $requestToken = $oAuthClient->requestRequestToken();
  59. $this->assertNotEmpty($requestToken->getRequestToken(), "Request token value is not set");
  60. $this->assertNotEmpty($requestToken->getRequestTokenSecret(), "Request token secret is not set");
  61. $this->assertEquals(
  62. \Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN,
  63. strlen($requestToken->getRequestToken()),
  64. "Request token value length should be " . \Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN
  65. );
  66. $this->assertEquals(
  67. \Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN_SECRET,
  68. strlen($requestToken->getRequestTokenSecret()),
  69. "Request token secret length should be " . \Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN_SECRET
  70. );
  71. }
  72. /**
  73. * @expectedException \Exception
  74. * @expectedExceptionMessage 401 Unauthorized
  75. */
  76. public function testGetRequestTokenExpiredConsumer()
  77. {
  78. $this::consumerFixture('2012-01-01 00:00:00');
  79. $this::$_consumer->setUpdatedAt('2012-01-01 00:00:00');
  80. $this::$_consumer->save();
  81. /** @var $oAuthClient \Magento\TestFramework\Authentication\Rest\OauthClient */
  82. $oAuthClient = $this->_getOauthClient(self::$_consumerKey, self::$_consumerSecret);
  83. $oAuthClient->requestRequestToken();
  84. }
  85. /**
  86. * @expectedException \Exception
  87. * @expectedExceptionMessage 401 Unauthorized
  88. */
  89. public function testGetRequestTokenInvalidConsumerKey()
  90. {
  91. $oAuthClient = $this->_getOauthClient('invalid_key', self::$_consumerSecret);
  92. $oAuthClient->requestRequestToken();
  93. }
  94. /**
  95. * @expectedException \Exception
  96. * @expectedExceptionMessage 401 Unauthorized
  97. */
  98. public function testGetRequestTokenInvalidConsumerSecret()
  99. {
  100. $oAuthClient = $this->_getOauthClient(self::$_consumerKey, 'invalid_secret');
  101. $oAuthClient->requestRequestToken();
  102. }
  103. public function testGetAccessToken()
  104. {
  105. $oAuthClient = $this->_getOauthClient(self::$_consumerKey, self::$_consumerSecret);
  106. $requestToken = $oAuthClient->requestRequestToken();
  107. $accessToken = $oAuthClient->requestAccessToken(
  108. $requestToken->getRequestToken(),
  109. self::$_verifier,
  110. $requestToken->getRequestTokenSecret()
  111. );
  112. $this->assertNotEmpty($accessToken->getAccessToken(), "Access token value is not set.");
  113. $this->assertNotEmpty($accessToken->getAccessTokenSecret(), "Access token secret is not set.");
  114. $this->assertEquals(
  115. \Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN,
  116. strlen($accessToken->getAccessToken()),
  117. "Access token value length should be " . \Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN
  118. );
  119. $this->assertEquals(
  120. \Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN_SECRET,
  121. strlen($accessToken->getAccessTokenSecret()),
  122. "Access token secret length should be " . \Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN_SECRET
  123. );
  124. }
  125. /**
  126. * @expectedException \Exception
  127. * @expectedExceptionMessage 401 Unauthorized
  128. */
  129. public function testGetAccessTokenInvalidVerifier()
  130. {
  131. $oAuthClient = $this->_getOauthClient(self::$_consumerKey, self::$_consumerSecret);
  132. $requestToken = $oAuthClient->requestRequestToken();
  133. $oAuthClient->requestAccessToken(
  134. $requestToken->getRequestToken(),
  135. 'invalid verifier',
  136. $requestToken->getRequestTokenSecret()
  137. );
  138. }
  139. /**
  140. * @expectedException \Exception
  141. * @expectedExceptionMessage 401 Unauthorized
  142. */
  143. public function testGetAccessTokenConsumerMismatch()
  144. {
  145. $oAuthClientA = $this->_getOauthClient(self::$_consumerKey, self::$_consumerSecret);
  146. $requestTokenA = $oAuthClientA->requestRequestToken();
  147. $oauthVerifierA = self::$_verifier;
  148. self::consumerFixture();
  149. $oAuthClientB = $this->_getOauthClient(self::$_consumerKey, self::$_consumerSecret);
  150. $oAuthClientB->requestRequestToken();
  151. $oAuthClientB->requestAccessToken(
  152. $requestTokenA->getRequestToken(),
  153. $oauthVerifierA,
  154. $requestTokenA->getRequestTokenSecret()
  155. );
  156. }
  157. /**
  158. * @expectedException \Exception
  159. * @expectedExceptionMessage 400 Bad Request
  160. */
  161. public function testAccessApiInvalidAccessToken()
  162. {
  163. $oAuthClient = $this->_getOauthClient(self::$_consumerKey, self::$_consumerSecret);
  164. $requestToken = $oAuthClient->requestRequestToken();
  165. $accessToken = $oAuthClient->requestAccessToken(
  166. $requestToken->getRequestToken(),
  167. self::$_verifier,
  168. $requestToken->getRequestTokenSecret()
  169. );
  170. $accessToken->setAccessToken('invalid');
  171. $oAuthClient->validateAccessToken($accessToken);
  172. }
  173. protected function _getOauthClient($consumerKey, $consumerSecret)
  174. {
  175. if (!isset($this->_oAuthClients[$consumerKey])) {
  176. $credentials = new \OAuth\Common\Consumer\Credentials($consumerKey, $consumerSecret, TESTS_BASE_URL);
  177. $this->_oAuthClients[$consumerKey] = new \Magento\TestFramework\Authentication\Rest\OauthClient(
  178. $credentials
  179. );
  180. }
  181. return $this->_oAuthClients[$consumerKey];
  182. }
  183. }