/** * {@inheritdoc} */ 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->setRevoked(1)->save(); } } catch (\Exception $e) { throw new LocalizedException(__('The tokens could not be revoked.')); } return true; }
/** * @magentoApiDataFixture Magento/Customer/_files/customer.php */ public function testCreateCustomerAccessToken() { $customerUserName = '******'; $password = '******'; $isTokenCorrect = false; $serviceInfo = ['rest' => ['resourcePath' => self::RESOURCE_PATH_CUSTOMER_TOKEN, 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST]]; $requestData = ['username' => $customerUserName, 'password' => $password]; $accessToken = $this->_webApiCall($serviceInfo, $requestData); $customerData = $this->customerAccountManagement->authenticate($customerUserName, $password); /** @var $this->tokenCollection \Magento\Integration\Model\ResourceModel\Oauth\Token\Collection */ $this->tokenCollection->addFilterByCustomerId($customerData->getId()); foreach ($this->tokenCollection->getItems() as $item) { /** @var $item TokenModel */ if ($item->getToken() == $accessToken) { $isTokenCorrect = true; } } $this->assertTrue($isTokenCorrect); }
/** * Make sure provided token is valid and belongs to the specified user. * * @param string $accessToken * @param string $userName * @param string $password */ private function assertToken($accessToken, $userName, $password) { $customerData = $this->customerAccountManagement->authenticate($userName, $password); /** @var $this ->tokenCollection \Magento\Integration\Model\ResourceModel\Oauth\Token\Collection */ $this->tokenCollection->addFilterByCustomerId($customerData->getId()); $isTokenCorrect = false; foreach ($this->tokenCollection->getItems() as $item) { /** @var $item TokenModel */ if ($item->getToken() == $accessToken) { $isTokenCorrect = true; } } $this->assertTrue($isTokenCorrect); }