Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Generator.php
Go to the documentation of this file.
1 <?php
7 
10 
12 {
16  protected $_oauthHelper;
17 
21  protected $_nonceFactory;
22 
26  protected $_nonceLength;
27 
31  protected $_date;
32 
36  const TIME_DEVIATION = 600;
37 
44  public function __construct(
45  \Magento\Framework\Oauth\Helper\Oauth $oauthHelper,
46  \Magento\Integration\Model\Oauth\NonceFactory $nonceFactory,
47  \Magento\Framework\Stdlib\DateTime\DateTime $date,
48  $nonceLength = \Magento\Framework\Oauth\Helper\Oauth::LENGTH_NONCE
49  ) {
50  $this->_oauthHelper = $oauthHelper;
51  $this->_nonceFactory = $nonceFactory;
52  $this->_date = $date;
53  $this->_nonceLength = $nonceLength;
54  }
55 
59  public function generateNonce(ConsumerInterface $consumer = null)
60  {
61  return $this->_oauthHelper->generateRandomString($this->_nonceLength);
62  }
63 
67  public function generateTimestamp()
68  {
69  return $this->_date->timestamp();
70  }
71 
75  public function validateNonce(ConsumerInterface $consumer, $nonce, $timestamp)
76  {
77  try {
78  $timestamp = (int)$timestamp;
79  if ($timestamp <= 0 || $timestamp > time() + self::TIME_DEVIATION) {
80  throw new \Magento\Framework\Oauth\OauthInputException(
81  __('Incorrect timestamp value in the oauth_timestamp parameter')
82  );
83  }
84 
86  $nonceObj = $this->_nonceFactory->create()->loadByCompositeKey($nonce, $consumer->getId());
87 
88  if ($nonceObj->getNonce()) {
89  throw new \Magento\Framework\Oauth\Exception(
90  __(
91  'The nonce is already being used by the consumer with ID %1',
92  [$consumer->getId()]
93  )
94  );
95  }
96 
97  $nonceObj->setNonce($nonce)->setConsumerId($consumer->getId())->setTimestamp($timestamp)->save();
98  } catch (\Magento\Framework\Oauth\Exception $exception) {
99  throw $exception;
100  } catch (\Exception $exception) {
101  throw new \Magento\Framework\Oauth\Exception(__('An error occurred validating the nonce'));
102  }
103  }
104 }
validateNonce(ConsumerInterface $consumer, $nonce, $timestamp)
__()
Definition: __.php:13
generateNonce(ConsumerInterface $consumer=null)
Definition: Generator.php:59
__construct(\Magento\Framework\Oauth\Helper\Oauth $oauthHelper, \Magento\Integration\Model\Oauth\NonceFactory $nonceFactory, \Magento\Framework\Stdlib\DateTime\DateTime $date, $nonceLength=\Magento\Framework\Oauth\Helper\Oauth::LENGTH_NONCE)
Definition: Generator.php:44