tokenModelFactory = $tokenModelFactory; $this->accountManagement = $accountManagement; $this->tokenModelCollectionFactory = $tokenModelCollectionFactory; $this->validatorHelper = $validatorHelper; $this->eventManager = $eventManager ?: \Magento\Framework\App\ObjectManager::getInstance() ->get(ManagerInterface::class); } /** * @inheritdoc */ public function createCustomerAccessToken($username, $password) { $this->validatorHelper->validate($username, $password); $this->getRequestThrottler()->throttle($username, RequestThrottler::USER_TYPE_CUSTOMER); try { $customerDataObject = $this->accountManagement->authenticate($username, $password); } catch (\Exception $e) { $this->getRequestThrottler()->logAuthenticationFailure($username, RequestThrottler::USER_TYPE_CUSTOMER); throw new AuthenticationException( __( 'The account sign-in was incorrect or your account is disabled temporarily. ' . 'Please wait and try again later.' ) ); } $this->eventManager->dispatch('customer_login', ['customer' => $customerDataObject]); $this->getRequestThrottler()->resetAuthenticationFailuresCount($username, RequestThrottler::USER_TYPE_CUSTOMER); return $this->tokenModelFactory->create()->createCustomerToken($customerDataObject->getId())->getToken(); } /** * Revoke token by customer id. * * The function will delete the token from the oauth_token table. * * @param int $customerId * @return bool * @throws \Magento\Framework\Exception\LocalizedException */ public function revokeCustomerAccessToken($customerId) { $tokenCollection = $this->tokenModelCollectionFactory->create()->addFilterByCustomerId($customerId); if ($tokenCollection->getSize() == 0) { throw new LocalizedException(__('This customer has no tokens.')); } try { foreach ($tokenCollection as $token) { $token->delete(); } } catch (\Exception $e) { throw new LocalizedException(__("The tokens couldn't be revoked.")); } return true; } /** * Get request throttler instance * * @return RequestThrottler * @deprecated 100.0.4 */ private function getRequestThrottler() { if (!$this->requestThrottler instanceof RequestThrottler) { return \Magento\Framework\App\ObjectManager::getInstance()->get(RequestThrottler::class); } return $this->requestThrottler; } }