Esempio n. 1
0
 public function testAfterSaveNoCleanupProbability()
 {
     $this->oauthDataMock->expects($this->once())->method('isCleanupProbability')->will($this->returnValue(false));
     $this->oauthDataMock->expects($this->never())->method('getCleanupExpirationPeriod');
     $this->resourceMock->expects($this->never())->method('deleteOldEntries');
     $this->assertEquals($this->nonceModel, $this->nonceModel->afterSave());
 }
Esempio n. 2
0
 /**
  * The "After save" actions
  *
  * @return $this
  */
 public function afterSave()
 {
     parent::afterSave();
     if ($this->_oauthData->isCleanupProbability()) {
         $this->getResource()->deleteOldEntries($this->_oauthData->getCleanupExpirationPeriod());
     }
     return $this;
 }
Esempio n. 3
0
 /**
  * {@inheritdoc}
  */
 public function validateConsumer($consumer)
 {
     // Must use consumer within expiration period.
     $consumerTS = strtotime($consumer->getCreatedAt());
     $expiry = $this->_dataHelper->getConsumerExpirationPeriod();
     if ($this->_date->timestamp() - $consumerTS > $expiry) {
         throw new \Magento\Framework\Oauth\Exception('Consumer key has expired');
     }
     return true;
 }
Esempio n. 4
0
 public function setUp()
 {
     $this->contextMock = $this->getMock('Magento\\Framework\\Model\\Context', ['getEventDispatcher'], [], '', false);
     $eventManagerMock = $this->getMockForAbstractClass('Magento\\Framework\\Event\\ManagerInterface', [], '', false, true, true, ['dispatch']);
     $this->contextMock->expects($this->once())->method('getEventDispatcher')->will($this->returnValue($eventManagerMock));
     $this->registryMock = $this->getMock('Magento\\Framework\\Registry', [], [], '', false);
     $this->keyLengthValidator = new KeyLength();
     $this->urlValidator = new UrlValidator();
     $this->oauthDataMock = $this->getMock('Magento\\Integration\\Helper\\Oauth\\Data', ['getConsumerExpirationPeriod'], [], '', false);
     $this->oauthDataMock->expects($this->any())->method('getConsumerExpirationPeriod')->will($this->returnValue(\Magento\Integration\Helper\Oauth\Data::CONSUMER_EXPIRATION_PERIOD_DEFAULT));
     $this->resourceMock = $this->getMock('Magento\\Integration\\Model\\ResourceModel\\Oauth\\Consumer', ['getIdFieldName', 'selectByCompositeKey', 'deleteOldEntries'], [], '', false, true, true);
     $this->resourceCollectionMock = $this->getMock('Magento\\Framework\\Data\\Collection\\AbstractDb', [], [], '', false);
     $this->consumerModel = new \Magento\Integration\Model\Oauth\Consumer($this->contextMock, $this->registryMock, $this->keyLengthValidator, $this->urlValidator, $this->oauthDataMock, $this->resourceMock, $this->resourceCollectionMock);
     $this->validDataArray = ['key' => md5(uniqid()), 'secret' => md5(uniqid()), 'callback_url' => 'http://example.com/callback', 'rejected_callback_url' => 'http://example.com/rejectedCallback'];
 }
Esempio n. 5
0
 /**
  * {@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');
     }
 }
Esempio n. 6
0
 /**
  * {@inheritdoc}
  */
 public function isValidForTokenExchange()
 {
     $expiry = $this->dataHelper->getConsumerExpirationPeriod();
     $currentTimestamp = $this->getDateHelper()->gmtTimestamp();
     $updatedTimestamp = $this->getDateHelper()->gmtTimestamp($this->getUpdatedAt());
     return $expiry > $currentTimestamp - $updatedTimestamp;
 }
Esempio n. 7
0
 /**
  * The "After save" actions
  *
  * @return $this
  */
 protected function _afterSave()
 {
     parent::_afterSave();
     // Cleanup old entries
     if ($this->_oauthData->isCleanupProbability()) {
         $this->_getResource()->deleteOldEntries($this->_oauthData->getCleanupExpirationPeriod());
     }
     return $this;
 }
Esempio n. 8
0
 public function testPostToConsumer()
 {
     $consumerId = 1;
     $key = $this->_generateRandomString(\Magento\Framework\Oauth\Helper\Oauth::LENGTH_CONSUMER_KEY);
     $secret = $this->_generateRandomString(\Magento\Framework\Oauth\Helper\Oauth::LENGTH_CONSUMER_SECRET);
     $oauthVerifier = $this->_generateRandomString(\Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN_VERIFIER);
     $consumerData = ['entity_id' => $consumerId, 'key' => $key, 'secret' => $secret];
     $this->_consumerMock->expects($this->once())->method('load')->with($this->equalTo($consumerId))->will($this->returnSelf());
     $this->_consumerMock->expects($this->once())->method('getId')->will($this->returnValue($consumerId));
     $this->_consumerMock->expects($this->once())->method('getData')->will($this->returnValue($consumerData));
     $this->_httpClientMock->expects($this->once())->method('setUri')->with('http://www.magento.com')->will($this->returnSelf());
     $this->_httpClientMock->expects($this->once())->method('setParameterPost')->will($this->returnSelf());
     $this->_tokenMock->expects($this->once())->method('createVerifierToken')->with($consumerId)->will($this->returnSelf());
     $this->_tokenMock->expects($this->any())->method('getVerifier')->will($this->returnValue($oauthVerifier));
     $this->_dataHelper->expects($this->once())->method('getConsumerPostMaxRedirects')->will($this->returnValue(5));
     $this->_dataHelper->expects($this->once())->method('getConsumerPostTimeout')->will($this->returnValue(120));
     $verifier = $this->_oauthService->postToConsumer($consumerId, 'http://www.magento.com');
     $this->assertEquals($oauthVerifier, $verifier, 'Checking Oauth Verifier');
 }
Esempio n. 9
0
 /**
  * {@inheritdoc}
  */
 public function isValidForTokenExchange()
 {
     $expiry = $this->dataHelper->getConsumerExpirationPeriod();
     return $expiry > $this->getResource()->getTimeInSecondsSinceCreation($this->getId());
 }
Esempio n. 10
0
 public function testGetConsumerPostTimeoutNonZero()
 {
     $this->_scopeConfigMock->expects($this->once())->method('getValue')->will($this->returnValue(10));
     $this->assertEquals(10, $this->_dataHelper->getConsumerPostTimeout());
 }
Esempio n. 11
0
 protected function _makeValidExpirationPeriod()
 {
     $this->_dateMock->expects($this->any())->method('timestamp')->will($this->returnValue(0));
     $this->_dataHelperMock->expects($this->once())->method('getConsumerExpirationPeriod')->will($this->returnValue(300));
 }