Oauth.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Oauth\Helper;
  7. class Oauth
  8. {
  9. /**
  10. * #@+
  11. * Lengths of token fields
  12. */
  13. const LENGTH_TOKEN = 32;
  14. const LENGTH_TOKEN_SECRET = 32;
  15. const LENGTH_TOKEN_VERIFIER = 32;
  16. /**#@- */
  17. /**
  18. * #@+
  19. * Lengths of consumer fields
  20. */
  21. const LENGTH_CONSUMER_KEY = 32;
  22. const LENGTH_CONSUMER_SECRET = 32;
  23. /**#@- */
  24. /**
  25. * Nonce length
  26. */
  27. const LENGTH_NONCE = 32;
  28. /**
  29. * Value of callback URL when it is established or if the client is unable to receive callbacks
  30. *
  31. * @link http://tools.ietf.org/html/rfc5849#section-2.1 Requirement in RFC-5849
  32. */
  33. const CALLBACK_ESTABLISHED = 'oob';
  34. /**
  35. * @var \Magento\Framework\Math\Random
  36. */
  37. protected $_mathRandom;
  38. /**
  39. * @param \Magento\Framework\Math\Random $mathRandom
  40. */
  41. public function __construct(\Magento\Framework\Math\Random $mathRandom)
  42. {
  43. $this->_mathRandom = $mathRandom;
  44. }
  45. /**
  46. * Generate random string for token or secret or verifier
  47. *
  48. * @param int $length String length
  49. * @return string
  50. */
  51. public function generateRandomString($length)
  52. {
  53. return $this->_mathRandom->getRandomString(
  54. $length,
  55. \Magento\Framework\Math\Random::CHARS_DIGITS . \Magento\Framework\Math\Random::CHARS_LOWERS
  56. );
  57. }
  58. /**
  59. * Generate random string for token
  60. *
  61. * @return string
  62. */
  63. public function generateToken()
  64. {
  65. return $this->generateRandomString(self::LENGTH_TOKEN);
  66. }
  67. /**
  68. * Generate random string for token secret
  69. *
  70. * @return string
  71. */
  72. public function generateTokenSecret()
  73. {
  74. return $this->generateRandomString(self::LENGTH_TOKEN_SECRET);
  75. }
  76. /**
  77. * Generate random string for verifier
  78. *
  79. * @return string
  80. */
  81. public function generateVerifier()
  82. {
  83. return $this->generateRandomString(self::LENGTH_TOKEN_VERIFIER);
  84. }
  85. /**
  86. * Generate random string for consumer key
  87. *
  88. * @return string
  89. */
  90. public function generateConsumerKey()
  91. {
  92. return $this->generateRandomString(self::LENGTH_CONSUMER_KEY);
  93. }
  94. /**
  95. * Generate random string for consumer secret
  96. *
  97. * @return string
  98. */
  99. public function generateConsumerSecret()
  100. {
  101. return $this->generateRandomString(self::LENGTH_CONSUMER_SECRET);
  102. }
  103. }