CustomerTokenService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Model;
  7. use Magento\Customer\Api\AccountManagementInterface;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Integration\Model\CredentialsValidator;
  10. use Magento\Integration\Model\Oauth\Token as Token;
  11. use Magento\Integration\Model\Oauth\TokenFactory as TokenModelFactory;
  12. use Magento\Integration\Model\ResourceModel\Oauth\Token\CollectionFactory as TokenCollectionFactory;
  13. use Magento\Integration\Model\Oauth\Token\RequestThrottler;
  14. use Magento\Framework\Exception\AuthenticationException;
  15. use Magento\Framework\Event\ManagerInterface;
  16. /**
  17. * @inheritdoc
  18. */
  19. class CustomerTokenService implements \Magento\Integration\Api\CustomerTokenServiceInterface
  20. {
  21. /**
  22. * Token Model
  23. *
  24. * @var TokenModelFactory
  25. */
  26. private $tokenModelFactory;
  27. /**
  28. * @var Magento\Framework\Event\ManagerInterface
  29. */
  30. private $eventManager;
  31. /**
  32. * Customer Account Service
  33. *
  34. * @var AccountManagementInterface
  35. */
  36. private $accountManagement;
  37. /**
  38. * @var \Magento\Integration\Model\CredentialsValidator
  39. */
  40. private $validatorHelper;
  41. /**
  42. * Token Collection Factory
  43. *
  44. * @var TokenCollectionFactory
  45. */
  46. private $tokenModelCollectionFactory;
  47. /**
  48. * @var RequestThrottler
  49. */
  50. private $requestThrottler;
  51. /**
  52. * Initialize service
  53. *
  54. * @param TokenModelFactory $tokenModelFactory
  55. * @param AccountManagementInterface $accountManagement
  56. * @param TokenCollectionFactory $tokenModelCollectionFactory
  57. * @param \Magento\Integration\Model\CredentialsValidator $validatorHelper
  58. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  59. */
  60. public function __construct(
  61. TokenModelFactory $tokenModelFactory,
  62. AccountManagementInterface $accountManagement,
  63. TokenCollectionFactory $tokenModelCollectionFactory,
  64. CredentialsValidator $validatorHelper,
  65. ManagerInterface $eventManager = null
  66. ) {
  67. $this->tokenModelFactory = $tokenModelFactory;
  68. $this->accountManagement = $accountManagement;
  69. $this->tokenModelCollectionFactory = $tokenModelCollectionFactory;
  70. $this->validatorHelper = $validatorHelper;
  71. $this->eventManager = $eventManager ?: \Magento\Framework\App\ObjectManager::getInstance()
  72. ->get(ManagerInterface::class);
  73. }
  74. /**
  75. * @inheritdoc
  76. */
  77. public function createCustomerAccessToken($username, $password)
  78. {
  79. $this->validatorHelper->validate($username, $password);
  80. $this->getRequestThrottler()->throttle($username, RequestThrottler::USER_TYPE_CUSTOMER);
  81. try {
  82. $customerDataObject = $this->accountManagement->authenticate($username, $password);
  83. } catch (\Exception $e) {
  84. $this->getRequestThrottler()->logAuthenticationFailure($username, RequestThrottler::USER_TYPE_CUSTOMER);
  85. throw new AuthenticationException(
  86. __(
  87. 'The account sign-in was incorrect or your account is disabled temporarily. '
  88. . 'Please wait and try again later.'
  89. )
  90. );
  91. }
  92. $this->eventManager->dispatch('customer_login', ['customer' => $customerDataObject]);
  93. $this->getRequestThrottler()->resetAuthenticationFailuresCount($username, RequestThrottler::USER_TYPE_CUSTOMER);
  94. return $this->tokenModelFactory->create()->createCustomerToken($customerDataObject->getId())->getToken();
  95. }
  96. /**
  97. * Revoke token by customer id.
  98. *
  99. * The function will delete the token from the oauth_token table.
  100. *
  101. * @param int $customerId
  102. * @return bool
  103. * @throws \Magento\Framework\Exception\LocalizedException
  104. */
  105. public function revokeCustomerAccessToken($customerId)
  106. {
  107. $tokenCollection = $this->tokenModelCollectionFactory->create()->addFilterByCustomerId($customerId);
  108. if ($tokenCollection->getSize() == 0) {
  109. throw new LocalizedException(__('This customer has no tokens.'));
  110. }
  111. try {
  112. foreach ($tokenCollection as $token) {
  113. $token->delete();
  114. }
  115. } catch (\Exception $e) {
  116. throw new LocalizedException(__("The tokens couldn't be revoked."));
  117. }
  118. return true;
  119. }
  120. /**
  121. * Get request throttler instance
  122. *
  123. * @return RequestThrottler
  124. * @deprecated 100.0.4
  125. */
  126. private function getRequestThrottler()
  127. {
  128. if (!$this->requestThrottler instanceof RequestThrottler) {
  129. return \Magento\Framework\App\ObjectManager::getInstance()->get(RequestThrottler::class);
  130. }
  131. return $this->requestThrottler;
  132. }
  133. }