-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENGCOM-3077: GraphQL-160: Add Revoke Customer token #195
- Loading branch information
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
app/code/Magento/CustomerGraphQl/Model/Resolver/Customer/Account/RevokeCustomerToken.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CustomerGraphQl\Model\Resolver\Customer\Account; | ||
|
||
use Magento\Authorization\Model\UserContextInterface; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\Integration\Api\CustomerTokenServiceInterface; | ||
|
||
/** | ||
* Customers Revoke Token resolver, used for GraphQL request processing. | ||
*/ | ||
class RevokeCustomerToken implements ResolverInterface | ||
{ | ||
/** | ||
* @var UserContextInterface | ||
*/ | ||
private $userContext; | ||
|
||
/** | ||
* @var CustomerTokenServiceInterface | ||
*/ | ||
private $customerTokenService; | ||
|
||
/** | ||
* @param UserContextInterface $userContext | ||
* @param CustomerTokenServiceInterface $customerTokenService | ||
*/ | ||
public function __construct( | ||
UserContextInterface $userContext, | ||
CustomerTokenServiceInterface $customerTokenService | ||
) { | ||
$this->userContext = $userContext; | ||
$this->customerTokenService = $customerTokenService; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve( | ||
Field $field, | ||
$context, | ||
ResolveInfo $info, | ||
array $value = null, | ||
array $args = null | ||
) { | ||
$customerId = (int)$this->userContext->getUserId(); | ||
|
||
if ($customerId === 0) { | ||
throw new GraphQlAuthorizationException( | ||
__( | ||
'Current customer does not have access to the resource "%1"', | ||
[\Magento\Customer\Model\Customer::ENTITY] | ||
) | ||
); | ||
} | ||
|
||
return $this->customerTokenService->revokeCustomerAccessToken($customerId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/RevokeCustomerTokenTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GraphQl\Customer; | ||
|
||
use Magento\TestFramework\ObjectManager; | ||
use Magento\TestFramework\TestCase\GraphQlAbstract; | ||
|
||
/** | ||
* Test for revoke customer token mutation | ||
*/ | ||
class RevokeCustomerTokenTest extends GraphQlAbstract | ||
{ | ||
/** | ||
* Verify customers with valid credentials | ||
* @magentoApiDataFixture Magento/Customer/_files/customer.php | ||
*/ | ||
public function testRevokeCustomerTokenValidCredentials() | ||
{ | ||
$query = <<<QUERY | ||
mutation { | ||
revokeCustomerToken | ||
} | ||
QUERY; | ||
|
||
$userName = 'customer@example.com'; | ||
$password = 'password'; | ||
/** @var CustomerTokenServiceInterface $customerTokenService */ | ||
$customerTokenService = ObjectManager::getInstance() | ||
->get(\Magento\Integration\Api\CustomerTokenServiceInterface::class); | ||
$customerToken = $customerTokenService->createCustomerAccessToken($userName, $password); | ||
|
||
$headerMap = ['Authorization' => 'Bearer ' . $customerToken]; | ||
$response = $this->graphQlQuery($query, [], '', $headerMap); | ||
$this->assertTrue($response['revokeCustomerToken']); | ||
} | ||
|
||
/** | ||
* Verify guest customers | ||
*/ | ||
public function testRevokeCustomerTokenForGuestCustomer() | ||
{ | ||
$query = <<<QUERY | ||
mutation { | ||
revokeCustomerToken | ||
} | ||
QUERY; | ||
$this->expectException(\Exception::class); | ||
$this->expectExceptionMessage( | ||
'GraphQL response contains errors: Current customer' . ' ' . | ||
'does not have access to the resource "customer"' | ||
); | ||
$this->graphQlQuery($query, [], ''); | ||
} | ||
} |