Consumer.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Model\Oauth;
  7. use Magento\Framework\Oauth\ConsumerInterface;
  8. /**
  9. * Consumer model
  10. *
  11. * @api
  12. * @author Magento Core Team <core@magentocommerce.com>
  13. * @method string getName()
  14. * @method Consumer setName() setName(string $name)
  15. * @method Consumer setKey() setKey(string $key)
  16. * @method Consumer setSecret() setSecret(string $secret)
  17. * @method Consumer setCallbackUrl() setCallbackUrl(string $url)
  18. * @method Consumer setCreatedAt() setCreatedAt(string $date)
  19. * @method string getUpdatedAt()
  20. * @method Consumer setUpdatedAt() setUpdatedAt(string $date)
  21. * @method string getRejectedCallbackUrl()
  22. * @method Consumer setRejectedCallbackUrl() setRejectedCallbackUrl(string $rejectedCallbackUrl)
  23. * @since 100.0.2
  24. */
  25. class Consumer extends \Magento\Framework\Model\AbstractModel implements ConsumerInterface
  26. {
  27. /**
  28. * @var \Magento\Framework\Url\Validator
  29. */
  30. protected $urlValidator;
  31. /**
  32. * @var \Magento\Integration\Model\Oauth\Consumer\Validator\KeyLength
  33. */
  34. protected $keyLengthValidator;
  35. /**
  36. * @var \Magento\Integration\Helper\Oauth\Data
  37. */
  38. protected $dataHelper;
  39. /**
  40. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  41. */
  42. private $_dateHelper;
  43. /**
  44. * @param \Magento\Framework\Model\Context $context
  45. * @param \Magento\Framework\Registry $registry
  46. * @param \Magento\Integration\Model\Oauth\Consumer\Validator\KeyLength $keyLength
  47. * @param \Magento\Framework\Url\Validator $urlValidator
  48. * @param \Magento\Integration\Helper\Oauth\Data $dataHelper
  49. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  50. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  51. * @param array $data
  52. */
  53. public function __construct(
  54. \Magento\Framework\Model\Context $context,
  55. \Magento\Framework\Registry $registry,
  56. \Magento\Integration\Model\Oauth\Consumer\Validator\KeyLength $keyLength,
  57. \Magento\Framework\Url\Validator $urlValidator,
  58. \Magento\Integration\Helper\Oauth\Data $dataHelper,
  59. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  60. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  61. array $data = []
  62. ) {
  63. $this->keyLengthValidator = $keyLength;
  64. $this->urlValidator = $urlValidator;
  65. $this->dataHelper = $dataHelper;
  66. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  67. }
  68. /**
  69. * Initialize resource model
  70. *
  71. * @return void
  72. */
  73. protected function _construct()
  74. {
  75. parent::_construct();
  76. $this->_init(\Magento\Integration\Model\ResourceModel\Oauth\Consumer::class);
  77. }
  78. /**
  79. * The getter function to get the new DateTime dependency
  80. *
  81. * @return \Magento\Framework\Stdlib\DateTime\DateTime
  82. *
  83. * @deprecated 100.0.6
  84. */
  85. private function getDateHelper()
  86. {
  87. if ($this->_dateHelper === null) {
  88. $this->_dateHelper = \Magento\Framework\App\ObjectManager::getInstance()
  89. ->get(\Magento\Framework\Stdlib\DateTime\DateTime::class);
  90. }
  91. return $this->_dateHelper;
  92. }
  93. /**
  94. * BeforeSave actions
  95. *
  96. * @return $this
  97. */
  98. public function beforeSave()
  99. {
  100. $this->validate();
  101. parent::beforeSave();
  102. return $this;
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function validate()
  108. {
  109. if ($this->getCallbackUrl() || $this->getRejectedCallbackUrl()) {
  110. $this->setCallbackUrl(trim($this->getCallbackUrl()));
  111. $this->setRejectedCallbackUrl(trim($this->getRejectedCallbackUrl()));
  112. if ($this->getCallbackUrl() && !$this->urlValidator->isValid($this->getCallbackUrl())) {
  113. throw new \Magento\Framework\Exception\LocalizedException(__('Invalid Callback URL'));
  114. }
  115. if ($this->getRejectedCallbackUrl() && !$this->urlValidator->isValid($this->getRejectedCallbackUrl())) {
  116. throw new \Magento\Framework\Exception\LocalizedException(__('Invalid Rejected Callback URL'));
  117. }
  118. }
  119. $this->keyLengthValidator
  120. ->setLength(\Magento\Framework\Oauth\Helper\Oauth::LENGTH_CONSUMER_KEY)
  121. ->setName('Consumer Key');
  122. if (!$this->keyLengthValidator->isValid($this->getKey())) {
  123. $messages = $this->keyLengthValidator->getMessages();
  124. throw new \Magento\Framework\Exception\LocalizedException(__(array_shift($messages)));
  125. }
  126. $this->keyLengthValidator
  127. ->setLength(\Magento\Framework\Oauth\Helper\Oauth::LENGTH_CONSUMER_SECRET)
  128. ->setName('Consumer Secret');
  129. if (!$this->keyLengthValidator->isValid($this->getSecret())) {
  130. $messages = $this->keyLengthValidator->getMessages();
  131. throw new \Magento\Framework\Exception\LocalizedException(__(array_shift($messages)));
  132. }
  133. return true;
  134. }
  135. /**
  136. * Load consumer data by consumer key.
  137. *
  138. * @param string $key
  139. * @return $this
  140. */
  141. public function loadByKey($key)
  142. {
  143. return $this->load($key, 'key');
  144. }
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public function getKey()
  149. {
  150. return $this->getData('key');
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public function getSecret()
  156. {
  157. return $this->getData('secret');
  158. }
  159. /**
  160. * {@inheritdoc}
  161. */
  162. public function getCallbackUrl()
  163. {
  164. return $this->getData('callback_url');
  165. }
  166. /**
  167. * {@inheritdoc}
  168. */
  169. public function getCreatedAt()
  170. {
  171. return $this->getData('created_at');
  172. }
  173. /**
  174. * {@inheritdoc}
  175. */
  176. public function isValidForTokenExchange()
  177. {
  178. $expiry = $this->dataHelper->getConsumerExpirationPeriod();
  179. $currentTimestamp = $this->getDateHelper()->gmtTimestamp();
  180. $updatedTimestamp = $this->getDateHelper()->gmtTimestamp($this->getUpdatedAt());
  181. return $expiry > ($currentTimestamp - $updatedTimestamp);
  182. }
  183. }