/** * {@inheritdoc} */ public function postToConsumer($consumerId, $endpointUrl) { try { $consumer = $this->_consumerFactory->create()->load($consumerId); if (!$consumer->getId()) { throw new \Magento\Framework\Oauth\Exception(__('A consumer with ID %1 does not exist', $consumerId), OauthInterface::ERR_PARAMETER_REJECTED); } $consumerData = $consumer->getData(); $verifier = $this->_tokenFactory->create()->createVerifierToken($consumerId); $storeBaseUrl = $this->_storeManager->getStore()->getBaseUrl(); $this->_httpClient->setUri($endpointUrl); $this->_httpClient->setParameterPost(array('oauth_consumer_key' => $consumerData['key'], 'oauth_consumer_secret' => $consumerData['secret'], 'store_base_url' => $storeBaseUrl, 'oauth_verifier' => $verifier->getVerifier())); $maxredirects = $this->_dataHelper->getConsumerPostMaxRedirects(); $timeout = $this->_dataHelper->getConsumerPostTimeout(); $this->_httpClient->setConfig(array('maxredirects' => $maxredirects, 'timeout' => $timeout)); $this->_httpClient->request(\Magento\Framework\HTTP\ZendClient::POST); return $verifier->getVerifier(); } catch (\Magento\Framework\Model\Exception $exception) { throw $exception; } catch (\Magento\Framework\Oauth\Exception $exception) { throw $exception; } catch (\Exception $exception) { $this->_logger->logException($exception); throw new \Magento\Framework\Oauth\Exception('Unable to post data to consumer due to an unexpected error'); } }
/** * Load token object given a consumer Id. * * @param int $consumerId - The Id of the consumer. * @return \Magento\Integration\Model\Oauth\Token * @throws \Magento\Framework\Oauth\Exception */ public function getTokenByConsumerId($consumerId) { $token = $this->_tokenFactory->create()->load($consumerId, 'consumer_id'); if (!$token->getId()) { throw new \Magento\Framework\Oauth\Exception('A token with consumer ID %1 does not exist', [$consumerId]); } return $token; }
protected function setUp() { $this->_consumerFactory = $this->getMockBuilder('Magento\\Integration\\Model\\Oauth\\Consumer\\Factory')->disableOriginalConstructor()->getMock(); $this->_consumerMock = $this->getMockBuilder('Magento\\Integration\\Model\\Oauth\\Consumer')->disableOriginalConstructor()->getMock(); $this->_consumerFactory->expects($this->any())->method('create')->will($this->returnValue($this->_consumerMock)); $this->_tokenFactory = $this->getMockBuilder('Magento\\Integration\\Model\\Oauth\\Token\\Factory')->disableOriginalConstructor()->getMock(); $this->_tokenMock = $this->getMockBuilder('Magento\\Integration\\Model\\Oauth\\Token')->disableOriginalConstructor()->getMock(); $this->_tokenFactory->expects($this->any())->method('create')->will($this->returnValue($this->_tokenMock)); $this->_storeManagerMock = $this->getMockBuilder('Magento\\Store\\Model\\StoreManagerInterface')->disableOriginalConstructor()->getMockForAbstractClass(); $this->_storeMock = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock(); $this->_storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($this->_storeMock)); $this->_dataHelper = $this->getMockBuilder('Magento\\Integration\\Helper\\Oauth\\Data')->disableOriginalConstructor()->getMock(); $oauthHelperMock = $this->getMockBuilder('Magento\\Framework\\Oauth\\Helper\\Oauth')->disableOriginalConstructor()->getMock(); $tokenProviderMock = $this->getMockBuilder('Magento\\Integration\\Model\\Oauth\\Token\\Provider')->disableOriginalConstructor()->getMock(); $this->_httpClientMock = $this->getMockBuilder('Magento\\Framework\\HTTP\\ZendClient')->disableOriginalConstructor()->getMock(); $this->_loggerMock = $this->getMockBuilder('Magento\\Framework\\Logger')->disableOriginalConstructor()->setMethods(array('logException'))->getMock(); $this->_oauthService = new \Magento\Integration\Service\V1\Oauth($this->_storeManagerMock, $this->_consumerFactory, $this->_tokenFactory, $this->_dataHelper, $this->_httpClientMock, $this->_loggerMock, $oauthHelperMock, $tokenProviderMock); }
public function testCreateAccessTokenInvalidConsumerId() { $this->_consumerMock->expects($this->any())->method('load')->with(0)->will($this->returnValue($this->_consumerMock)); $this->_tokenProviderMock->expects($this->any())->method('getIntegrationTokenByConsumerId')->will($this->throwException(new \Magento\Framework\Oauth\Exception('A token with consumer ID 0 does not exist'))); $this->_tokenMock->expects($this->never())->method('delete'); $this->_tokenFactoryMock->expects($this->once())->method('create')->will($this->returnValue($this->_tokenMock)); $this->_tokenMock->expects($this->once())->method('createVerifierToken'); $this->_tokenProviderMock->expects($this->once())->method('createRequestToken'); $this->_tokenProviderMock->expects($this->once())->method('getAccessToken'); $this->assertTrue($this->_service->createAccessToken(0, false)); }
public function setUp() { $this->_consumerFactory = $this->getMockBuilder('Magento\\Integration\\Model\\Oauth\\Consumer\\Factory')->disableOriginalConstructor()->getMock(); $this->_consumerMock = $this->getMockBuilder('Magento\\Integration\\Model\\Oauth\\Consumer')->disableOriginalConstructor()->setMethods(array('getCreatedAt', 'loadByKey', 'load', 'getId', 'getSecret', 'getCallbackUrl', 'save', 'getData', '__wakeup'))->getMock(); $this->_consumerFactory->expects($this->any())->method('create')->will($this->returnValue($this->_consumerMock)); $this->_nonceFactory = $this->getMockBuilder('Magento\\Integration\\Model\\Oauth\\Nonce\\Factory')->disableOriginalConstructor()->getMock(); $this->_tokenFactory = $this->getMockBuilder('Magento\\Integration\\Model\\Oauth\\Token\\Factory')->disableOriginalConstructor()->getMock(); $this->_tokenMock = $this->getMockBuilder('Magento\\Integration\\Model\\Oauth\\Token')->disableOriginalConstructor()->setMethods(array('getId', 'load', 'getType', 'createRequestToken', 'getToken', 'getSecret', 'createVerifierToken', 'getVerifier', 'getConsumerId', 'convertToAccess', 'getRevoked', '__wakeup'))->getMock(); $this->_tokenFactory->expects($this->any())->method('create')->will($this->returnValue($this->_tokenMock)); $this->_oauthHelperMock = $this->getMockBuilder('Magento\\Framework\\Oauth\\Helper\\Oauth')->setConstructorArgs(array(new \Magento\Framework\Math\Random()))->getMock(); $this->_dataHelperMock = $this->getMockBuilder('Magento\\Integration\\Helper\\Oauth\\Data')->disableOriginalConstructor()->getMock(); $this->_httpUtilityMock = $this->getMockBuilder('Zend_Oauth_Http_Utility')->setMethods(array('sign'))->getMock(); $this->_dateMock = $this->getMockBuilder('Magento\\Framework\\Stdlib\\DateTime\\DateTime')->disableOriginalConstructor()->getMock(); $nonceGenerator = new \Magento\Integration\Model\Oauth\Nonce\Generator($this->_oauthHelperMock, $this->_nonceFactory, $this->_dateMock); $tokenProvider = new \Magento\Integration\Model\Oauth\Token\Provider($this->_consumerFactory, $this->_tokenFactory, $this->_dataHelperMock, $this->_dateMock); $this->_oauth = new \Magento\Framework\Oauth\Oauth($this->_oauthHelperMock, $nonceGenerator, $tokenProvider, $this->_httpUtilityMock); $this->_oauthToken = $this->_generateRandomString(\Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN); $this->_oauthSecret = $this->_generateRandomString(\Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN_SECRET); $this->_oauthVerifier = $this->_generateRandomString(\Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN_VERIFIER); }
/** * Load token object and validate it. * * @param string $token * @return Token * @throws \Magento\Framework\Oauth\Exception */ protected function _getToken($token) { if (!$this->validateOauthToken($token)) { throw new \Magento\Framework\Oauth\Exception('Token is not the correct length'); } $tokenObj = $this->_tokenFactory->create()->load($token, 'token'); if (!$tokenObj->getId()) { throw new \Magento\Framework\Oauth\Exception('Specified token does not exist'); } return $tokenObj; }
/** * {@inheritdoc} */ public function createAdminAccessToken($username, $password) { $this->validatorHelper->validateCredentials($username, $password); try { $this->userModel->login($username, $password); if (!$this->userModel->getId()) { /* * This message is same as one thrown in \Magento\Backend\Model\Auth to keep the behavior consistent. * Constant cannot be created in Auth Model since it uses legacy translation that doesn't support it. * Need to make sure that this is refactored once exception handling is updated in Auth Model. */ throw new AuthenticationException('Please correct the user name or password.'); } } catch (\Magento\Backend\Model\Auth\Exception $e) { throw new AuthenticationException($e->getMessage(), [], $e); } catch (\Magento\Framework\Model\Exception $e) { throw new LocalizedException($e->getMessage(), [], $e); } return $this->tokenModelFactory->create()->createAdminToken($this->userModel->getId())->getToken(); }
/** * {@inheritdoc} */ public function createCustomerAccessToken($username, $password) { $this->validateCredentials($username, $password); $customerDataObject = $this->customerAccountService->authenticate($username, $password); return $this->tokenModelFactory->create()->createCustomerToken($customerDataObject->getId())->getToken(); }