RequestThrottler.php 2.9 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\Token;
  7. use Magento\Integration\Model\Oauth\Token\RequestLog\ReaderInterface as RequestLogReader;
  8. use Magento\Integration\Model\Oauth\Token\RequestLog\WriterInterface as RequestLogWriter;
  9. use Magento\Integration\Model\Oauth\Token\RequestLog\Config as RequestLogConfig;
  10. use Magento\Framework\Exception\AuthenticationException;
  11. /**
  12. * Model for OAuth admin/customer token requests throttling.
  13. */
  14. class RequestThrottler
  15. {
  16. /**#@+
  17. * Web API user type
  18. */
  19. const USER_TYPE_CUSTOMER = 2;
  20. const USER_TYPE_ADMIN = 3;
  21. /**#@-*/
  22. /**#@-*/
  23. private $requestLogReader;
  24. /**
  25. * @var RequestLogWriter
  26. */
  27. private $requestLogWriter;
  28. /**
  29. * @var RequestLogConfig
  30. */
  31. private $requestLogConfig;
  32. /**
  33. * Initialize dependencies.
  34. *
  35. * @param RequestLogReader $requestLogReader
  36. * @param RequestLogWriter $requestLogWriter
  37. * @param RequestLogConfig $requestLogConfig
  38. */
  39. public function __construct(
  40. RequestLogReader $requestLogReader,
  41. RequestLogWriter $requestLogWriter,
  42. RequestLogConfig $requestLogConfig
  43. ) {
  44. $this->requestLogReader = $requestLogReader;
  45. $this->requestLogWriter = $requestLogWriter;
  46. $this->requestLogConfig = $requestLogConfig;
  47. }
  48. /**
  49. * Throw exception if user account is currently locked because of too many failed authentication attempts.
  50. *
  51. * @param string $userName
  52. * @param int $userType
  53. * @return void
  54. * @throws AuthenticationException
  55. */
  56. public function throttle($userName, $userType)
  57. {
  58. $count = $this->requestLogReader->getFailuresCount($userName, $userType);
  59. if ($count >= $this->requestLogConfig->getMaxFailuresCount()) {
  60. throw new AuthenticationException(
  61. __(
  62. 'The account sign-in was incorrect or your account is disabled temporarily. '
  63. . 'Please wait and try again later.'
  64. )
  65. );
  66. }
  67. }
  68. /**
  69. * Reset count of failed authentication attempts.
  70. *
  71. * Unlock user account and make generation of OAuth tokens possible for this account again.
  72. *
  73. * @param string $userName
  74. * @param int $userType
  75. * @return void
  76. */
  77. public function resetAuthenticationFailuresCount($userName, $userType)
  78. {
  79. $this->requestLogWriter->resetFailuresCount($userName, $userType);
  80. }
  81. /**
  82. * Increment authentication failures count and lock user account if the limit is reached.
  83. *
  84. * Account will be locked until lock expires.
  85. *
  86. * @param string $userName
  87. * @param int $userType
  88. * @return void
  89. */
  90. public function logAuthenticationFailure($userName, $userType)
  91. {
  92. $this->requestLogWriter->incrementFailuresCount($userName, $userType);
  93. }
  94. }