Generator.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Model\Oauth\Nonce;
  7. use Magento\Framework\Oauth\ConsumerInterface;
  8. use Magento\Framework\Oauth\NonceGeneratorInterface;
  9. class Generator implements NonceGeneratorInterface
  10. {
  11. /**
  12. * @var \Magento\Framework\Oauth\Helper\Oauth
  13. */
  14. protected $_oauthHelper;
  15. /**
  16. * @var \Magento\Integration\Model\Oauth\NonceFactory
  17. */
  18. protected $_nonceFactory;
  19. /**
  20. * @var int
  21. */
  22. protected $_nonceLength;
  23. /**
  24. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  25. */
  26. protected $_date;
  27. /**
  28. * Possible time deviation for timestamp validation in seconds.
  29. */
  30. const TIME_DEVIATION = 600;
  31. /**
  32. * @param \Magento\Framework\Oauth\Helper\Oauth $oauthHelper
  33. * @param \Magento\Integration\Model\Oauth\NonceFactory $nonceFactory
  34. * @param \Magento\Framework\Stdlib\DateTime\DateTime $date
  35. * @param int $nonceLength - Length of the generated nonce
  36. */
  37. public function __construct(
  38. \Magento\Framework\Oauth\Helper\Oauth $oauthHelper,
  39. \Magento\Integration\Model\Oauth\NonceFactory $nonceFactory,
  40. \Magento\Framework\Stdlib\DateTime\DateTime $date,
  41. $nonceLength = \Magento\Framework\Oauth\Helper\Oauth::LENGTH_NONCE
  42. ) {
  43. $this->_oauthHelper = $oauthHelper;
  44. $this->_nonceFactory = $nonceFactory;
  45. $this->_date = $date;
  46. $this->_nonceLength = $nonceLength;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function generateNonce(ConsumerInterface $consumer = null)
  52. {
  53. return $this->_oauthHelper->generateRandomString($this->_nonceLength);
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function generateTimestamp()
  59. {
  60. return $this->_date->timestamp();
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function validateNonce(ConsumerInterface $consumer, $nonce, $timestamp)
  66. {
  67. try {
  68. $timestamp = (int)$timestamp;
  69. if ($timestamp <= 0 || $timestamp > time() + self::TIME_DEVIATION) {
  70. throw new \Magento\Framework\Oauth\OauthInputException(
  71. __('Incorrect timestamp value in the oauth_timestamp parameter')
  72. );
  73. }
  74. /** @var \Magento\Integration\Model\Oauth\Nonce $nonceObj */
  75. $nonceObj = $this->_nonceFactory->create()->loadByCompositeKey($nonce, $consumer->getId());
  76. if ($nonceObj->getNonce()) {
  77. throw new \Magento\Framework\Oauth\Exception(
  78. __(
  79. 'The nonce is already being used by the consumer with ID %1',
  80. [$consumer->getId()]
  81. )
  82. );
  83. }
  84. $nonceObj->setNonce($nonce)->setConsumerId($consumer->getId())->setTimestamp($timestamp)->save();
  85. } catch (\Magento\Framework\Oauth\Exception $exception) {
  86. throw $exception;
  87. } catch (\Exception $exception) {
  88. throw new \Magento\Framework\Oauth\Exception(__('An error occurred validating the nonce'));
  89. }
  90. }
  91. }