Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added token revocation for REST API #32481

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/code/Magento/Integration/etc/webapi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@
<resource ref="anonymous"/>
</resources>
</route>
<route url="/V1/integration/customer/revoke-customer-token" method="POST">
<service class="Magento\Integration\Api\CustomerTokenServiceInterface" method="revokeCustomerAccessToken"/>
<resources>
<resource ref="self"/>
</resources>
<data>
<parameter name="customerId" force="true">%customer_id%</parameter>
</data>
</route>
</routes>
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Customer\Api;

use Exception;
use Magento\Framework\Exception\AuthenticationException;
use Magento\Framework\Webapi\Rest\Request;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\ObjectManager;
use Magento\TestFramework\TestCase\WebapiAbstract;

/**
* Test class for Magento\Integration\Api\CustomerTokenServiceInterface
*/
class AccountManagementRevokeCustomerTokenTest extends WebapiAbstract
{
const RESOURCE_PATH = '/V1/integration/customer/revoke-customer-token';
const INTEGRATION_SERVICE = 'integrationCustomerTokenServiceV1';
const SERVICE_VERSION = 'V1';

/**
* Test token revoking for authenticated customer
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testRevokeCustomerToken(): void
{
$token = $this->getCustomerToken();
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH,
'httpMethod' => Request::HTTP_METHOD_POST,
'token' => $token,
],
'soap' => [
'service' => self::INTEGRATION_SERVICE,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::INTEGRATION_SERVICE . 'RevokeCustomerAccessToken',
'token' => $token,
]
];

$requestData = [];
if (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP) {
$requestData['customerId'] = 0;
}

$this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
}

/**
* @return string
*
* @throws AuthenticationException
*/
private function getCustomerToken(): string
{
$userName = 'customer@example.com';
$password = 'password';

/** @var CustomerTokenServiceInterface $customerTokenService */
$customerTokenService = ObjectManager::getInstance()->get(CustomerTokenServiceInterface::class);

return $customerTokenService->createCustomerAccessToken($userName, $password);
}

/**
* Test token revoking for guest customer
*/
public function testRevokeCustomerTokenForGuestCustomer(): void
{
$this->expectException(Exception::class);
$requestData = [];

if (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP) {
$requestData['customerId'] = 0;
}

$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH,
'httpMethod' => Request::HTTP_METHOD_POST,
],
'soap' => [
'service' => self::INTEGRATION_SERVICE,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::INTEGRATION_SERVICE . 'RevokeCustomerAccessToken',
]
];

$this->_webApiCall($serviceInfo, $requestData);
}
}