Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Token.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Framework\Oauth\Exception as OauthException;
10 use Magento\Framework\Oauth\Helper\Oauth as OauthHelper;
12 
44 {
48  const TYPE_REQUEST = 'request';
49 
50  const TYPE_ACCESS = 'access';
51 
52  const TYPE_VERIFIER = 'verifier';
53 
57  protected $_oauthHelper;
58 
62  protected $_oauthData;
63 
67  protected $_consumerFactory;
68 
72  protected $_urlValidator;
73 
77  protected $_keyLengthFactory;
78 
94  public function __construct(
95  \Magento\Framework\Model\Context $context,
96  \Magento\Framework\Registry $registry,
97  \Magento\Integration\Model\Oauth\Consumer\Validator\KeyLengthFactory $keyLengthFactory,
98  \Magento\Framework\Url\Validator $urlValidator,
99  \Magento\Integration\Model\Oauth\ConsumerFactory $consumerFactory,
100  \Magento\Integration\Helper\Oauth\Data $oauthData,
101  OauthHelper $oauthHelper,
102  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
103  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
104  array $data = []
105  ) {
106  parent::__construct($context, $registry, $resource, $resourceCollection, $data);
107  $this->_keyLengthFactory = $keyLengthFactory;
108  $this->_urlValidator = $urlValidator;
109  $this->_consumerFactory = $consumerFactory;
110  $this->_oauthData = $oauthData;
111  $this->_oauthHelper = $oauthHelper;
112  }
113 
119  protected function _construct()
120  {
121  $this->_init(\Magento\Integration\Model\ResourceModel\Oauth\Token::class);
122  }
123 
129  public function afterSave()
130  {
131  parent::afterSave();
132 
133  // Cleanup old entries
134  if ($this->_oauthData->isCleanupProbability()) {
135  $this->_getResource()->deleteOldEntries($this->_oauthData->getCleanupExpirationPeriod());
136  }
137  return $this;
138  }
139 
146  public function createVerifierToken($consumerId)
147  {
148  $tokenData = $this->getResource()->selectTokenByType($consumerId, self::TYPE_VERIFIER);
149  $this->setData($tokenData ? $tokenData : []);
150  if (!$this->getId()) {
151  $this->setData(
152  [
153  'consumer_id' => $consumerId,
154  'type' => self::TYPE_VERIFIER,
155  'token' => $this->_oauthHelper->generateToken(),
156  'secret' => $this->_oauthHelper->generateTokenSecret(),
157  'verifier' => $this->_oauthHelper->generateVerifier(),
158  'callback_url' => OauthHelper::CALLBACK_ESTABLISHED,
159  'user_type' => UserContextInterface::USER_TYPE_INTEGRATION, //As of now only integrations use Oauth
160  ]
161  );
162  $this->validate();
163  $this->save();
164  }
165  return $this;
166  }
167 
174  public function convertToAccess()
175  {
176  if (self::TYPE_REQUEST != $this->getType()) {
177  throw new OauthException(__('Cannot convert to access token due to token is not request type'));
178  }
180  }
181 
188  public function createAdminToken($userId)
189  {
190  $this->setAdminId($userId);
192  }
193 
200  public function createCustomerToken($userId)
201  {
202  $this->setCustomerId($userId);
204  }
205 
213  public function createRequestToken($entityId, $callbackUrl)
214  {
215  $callbackUrl = !empty($callbackUrl) ? $callbackUrl : OauthHelper::CALLBACK_ESTABLISHED;
216  $this->setData(
217  [
218  'entity_id' => $entityId,
219  'type' => self::TYPE_REQUEST,
220  'token' => $this->_oauthHelper->generateToken(),
221  'secret' => $this->_oauthHelper->generateTokenSecret(),
222  'callback_url' => $callbackUrl,
223  ]
224  );
225  $this->validate();
226  $this->save();
227 
228  return $this;
229  }
230 
237  public function __toString()
238  {
239  return http_build_query(['oauth_token' => $this->getToken(), 'oauth_token_secret' => $this->getSecret()]);
240  }
241 
248  public function validate()
249  {
250  if (OauthHelper::CALLBACK_ESTABLISHED != $this->getCallbackUrl() && !$this->_urlValidator->isValid(
251  $this->getCallbackUrl()
252  )
253  ) {
254  $messages = $this->_urlValidator->getMessages();
255  throw new OauthException(__(array_shift($messages)));
256  }
257 
259  $validatorLength = $this->_keyLengthFactory->create();
260  $validatorLength->setLength(OauthHelper::LENGTH_TOKEN_SECRET);
261  $validatorLength->setName('Token Secret Key');
262  if (!$validatorLength->isValid($this->getSecret())) {
263  $messages = $validatorLength->getMessages();
264  throw new OauthException(__(array_shift($messages)));
265  }
266 
267  $validatorLength->setLength(OauthHelper::LENGTH_TOKEN);
268  $validatorLength->setName('Token Key');
269  if (!$validatorLength->isValid($this->getToken())) {
270  $messages = $validatorLength->getMessages();
271  throw new OauthException(__(array_shift($messages)));
272  }
273 
274  if (null !== ($verifier = $this->getVerifier())) {
275  $validatorLength->setLength(OauthHelper::LENGTH_TOKEN_VERIFIER);
276  $validatorLength->setName('Verifier Key');
277  if (!$validatorLength->isValid($verifier)) {
278  $messages = $validatorLength->getMessages();
279  throw new OauthException(__(array_shift($messages)));
280  }
281  }
282  return true;
283  }
284 
290  public function getVerifier()
291  {
292  return $this->getData('verifier');
293  }
294 
301  protected function saveAccessToken($userType)
302  {
303  $this->setUserType($userType);
304  $this->setType(self::TYPE_ACCESS);
305  $this->setToken($this->_oauthHelper->generateToken());
306  $this->setSecret($this->_oauthHelper->generateTokenSecret());
307  return $this->save();
308  }
309 
317  public function loadByConsumerIdAndUserType($consumerId, $userType)
318  {
319  $tokenData = $this->getResource()->selectTokenByConsumerIdAndUserType($consumerId, $userType);
320  $this->setData($tokenData ? $tokenData : []);
321  return $this;
322  }
323 
330  public function loadByAdminId($adminId)
331  {
332  $tokenData = $this->getResource()->selectTokenByAdminId($adminId);
333  $this->setData($tokenData ? $tokenData : []);
334  return $this;
335  }
336 
343  public function loadByCustomerId($customerId)
344  {
345  $tokenData = $this->getResource()->selectTokenByCustomerId($customerId);
346  $this->setData($tokenData ? $tokenData : []);
347  return $this;
348  }
349 
356  public function loadByToken($token)
357  {
358  return $this->load($token, 'token');
359  }
360 }
getData($key='', $index=null)
Definition: DataObject.php:119
loadByConsumerIdAndUserType($consumerId, $userType)
Definition: Token.php:317
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Integration\Model\Oauth\Consumer\Validator\KeyLengthFactory $keyLengthFactory, \Magento\Framework\Url\Validator $urlValidator, \Magento\Integration\Model\Oauth\ConsumerFactory $consumerFactory, \Magento\Integration\Helper\Oauth\Data $oauthData, OauthHelper $oauthHelper, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Token.php:94
createRequestToken($entityId, $callbackUrl)
Definition: Token.php:213