Skip to content

Commit fc21597

Browse files
ENGCOM-3077: GraphQL-160: Add Revoke Customer token #195
- Merge Pull Request magento/graphql-ce#195 from ArturoI/graphql-ce:160-customer-revoke-token - Merged commits: 1. 342c052 2. 5819c68 3. f65b6b5 4. 6a5a624 5. 61a8bc8 6. f81a0b6
2 parents 625c384 + f81a0b6 commit fc21597

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CustomerGraphQl\Model\Resolver\Customer\Account;
9+
10+
use Magento\Authorization\Model\UserContextInterface;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
use Magento\Integration\Api\CustomerTokenServiceInterface;
16+
17+
/**
18+
* Customers Revoke Token resolver, used for GraphQL request processing.
19+
*/
20+
class RevokeCustomerToken implements ResolverInterface
21+
{
22+
/**
23+
* @var UserContextInterface
24+
*/
25+
private $userContext;
26+
27+
/**
28+
* @var CustomerTokenServiceInterface
29+
*/
30+
private $customerTokenService;
31+
32+
/**
33+
* @param UserContextInterface $userContext
34+
* @param CustomerTokenServiceInterface $customerTokenService
35+
*/
36+
public function __construct(
37+
UserContextInterface $userContext,
38+
CustomerTokenServiceInterface $customerTokenService
39+
) {
40+
$this->userContext = $userContext;
41+
$this->customerTokenService = $customerTokenService;
42+
}
43+
44+
/**
45+
* @inheritdoc
46+
*/
47+
public function resolve(
48+
Field $field,
49+
$context,
50+
ResolveInfo $info,
51+
array $value = null,
52+
array $args = null
53+
) {
54+
$customerId = (int)$this->userContext->getUserId();
55+
56+
if ($customerId === 0) {
57+
throw new GraphQlAuthorizationException(
58+
__(
59+
'Current customer does not have access to the resource "%1"',
60+
[\Magento\Customer\Model\Customer::ENTITY]
61+
)
62+
);
63+
}
64+
65+
return $this->customerTokenService->revokeCustomerAccessToken($customerId);
66+
}
67+
}

app/code/Magento/CustomerGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type Query {
88
type Mutation {
99
generateCustomerToken(email: String!, password: String!): CustomerToken @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\Customer\\Account\\GenerateCustomerToken") @doc(description:"Retrieve Customer token")
1010
changeCustomerPassword(currentPassword: String!, newPassword: String!): Customer @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\Customer\\Account\\ChangePassword") @doc(description:"Changes password for logged in customer")
11+
revokeCustomerToken: Boolean @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\Customer\\Account\\RevokeCustomerToken") @doc(description:"Revoke Customer token")
1112
}
1213

1314
type CustomerToken {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Customer;
9+
10+
use Magento\TestFramework\ObjectManager;
11+
use Magento\TestFramework\TestCase\GraphQlAbstract;
12+
13+
/**
14+
* Test for revoke customer token mutation
15+
*/
16+
class RevokeCustomerTokenTest extends GraphQlAbstract
17+
{
18+
/**
19+
* Verify customers with valid credentials
20+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
21+
*/
22+
public function testRevokeCustomerTokenValidCredentials()
23+
{
24+
$query = <<<QUERY
25+
mutation {
26+
revokeCustomerToken
27+
}
28+
QUERY;
29+
30+
$userName = 'customer@example.com';
31+
$password = 'password';
32+
/** @var CustomerTokenServiceInterface $customerTokenService */
33+
$customerTokenService = ObjectManager::getInstance()
34+
->get(\Magento\Integration\Api\CustomerTokenServiceInterface::class);
35+
$customerToken = $customerTokenService->createCustomerAccessToken($userName, $password);
36+
37+
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
38+
$response = $this->graphQlQuery($query, [], '', $headerMap);
39+
$this->assertTrue($response['revokeCustomerToken']);
40+
}
41+
42+
/**
43+
* Verify guest customers
44+
*/
45+
public function testRevokeCustomerTokenForGuestCustomer()
46+
{
47+
$query = <<<QUERY
48+
mutation {
49+
revokeCustomerToken
50+
}
51+
QUERY;
52+
$this->expectException(\Exception::class);
53+
$this->expectExceptionMessage(
54+
'GraphQL response contains errors: Current customer' . ' ' .
55+
'does not have access to the resource "customer"'
56+
);
57+
$this->graphQlQuery($query, [], '');
58+
}
59+
}

0 commit comments

Comments
 (0)