Skip to content

Commit 269e2d9

Browse files
authored
Merge pull request #421 from magento-performance/CABPI-327-tokens-cleanup
CABPI-327 - [BUG] Magento token remains valid after IMS is enabled
2 parents cad8536 + ca9caf5 commit 269e2d9

File tree

5 files changed

+82
-2
lines changed

5 files changed

+82
-2
lines changed

app/code/Magento/AdminAdobeIms/Console/Command/AdminAdobeImsEnableCommand.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Magento\AdminAdobeIms\Console\Command;
1010

1111
use Magento\AdminAdobeIms\Model\ImsConnection;
12+
use Magento\AdminAdobeIms\Service\UpdateTokensService;
1213
use Magento\AdminAdobeIms\Service\ImsCommandOptionService;
1314
use Magento\AdminAdobeIms\Service\ImsConfig;
1415
use Magento\Framework\App\Cache\Type\Config;
@@ -61,23 +62,31 @@ class AdminAdobeImsEnableCommand extends Command
6162
*/
6263
private TypeListInterface $cacheTypeList;
6364

65+
/**
66+
* @var UpdateTokensService
67+
*/
68+
private UpdateTokensService $updateTokensService;
69+
6470
/**
6571
* @param ImsConfig $imsConfig
6672
* @param ImsConnection $imsConnection
6773
* @param ImsCommandOptionService $imsCommandOptionService
6874
* @param TypeListInterface $cacheTypeList
75+
* @param UpdateTokensService $cleanupTokensService
6976
*/
7077
public function __construct(
7178
ImsConfig $imsConfig,
7279
ImsConnection $imsConnection,
7380
ImsCommandOptionService $imsCommandOptionService,
74-
TypeListInterface $cacheTypeList
81+
TypeListInterface $cacheTypeList,
82+
UpdateTokensService $updateTokensService
7583
) {
7684
parent::__construct();
7785
$this->imsConfig = $imsConfig;
7886
$this->imsConnection = $imsConnection;
7987
$this->imsCommandOptionService = $imsCommandOptionService;
8088
$this->cacheTypeList = $cacheTypeList;
89+
$this->updateTokensService = $updateTokensService;
8190

8291
$this->setName('admin:adobe-ims:enable')
8392
->setDescription('Enable Adobe IMS Module.')
@@ -171,6 +180,7 @@ private function enableModule(
171180
if ($testAuth) {
172181
$this->imsConfig->enableModule($clientId, $clientSecret, $organizationId);
173182
$this->cacheTypeList->cleanType(Config::TYPE_IDENTIFIER);
183+
$this->updateTokensService->execute();
174184
return true;
175185
}
176186

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Magento\AdminAdobeIms\Service;
11+
12+
use Magento\Authorization\Model\UserContextInterface;
13+
use Magento\JwtUserToken\Api\Data\Revoked;
14+
use Magento\JwtUserToken\Api\RevokedRepositoryInterface;
15+
use Magento\User\Model\ResourceModel\User\Collection;
16+
use Magento\User\Model\ResourceModel\User\CollectionFactory;
17+
18+
class UpdateTokensService
19+
{
20+
/**
21+
* @var RevokedRepositoryInterface
22+
*/
23+
private RevokedRepositoryInterface $revokedRepo;
24+
25+
/**
26+
* @var Collection
27+
*/
28+
private Collection $adminUserCollection;
29+
30+
/**
31+
* @param RevokedRepositoryInterface $revokedRepo
32+
* @param CollectionFactory $adminUserCollectionFactory
33+
*/
34+
public function __construct(RevokedRepositoryInterface $revokedRepo, CollectionFactory $adminUserCollectionFactory)
35+
{
36+
$this->revokedRepo = $revokedRepo;
37+
$this->adminUserCollection = $adminUserCollectionFactory->create();
38+
}
39+
40+
/**
41+
* Token invalidation for the admin users
42+
*
43+
* return @void
44+
*/
45+
public function execute()
46+
{
47+
$adminUsers = $this->adminUserCollection->getItems();
48+
foreach ($adminUsers as $adminUser) {
49+
//Invalidating all tokens issued before current datetime.
50+
$this->revokedRepo->saveRevoked(
51+
new Revoked((int) UserContextInterface::USER_TYPE_ADMIN, (int) $adminUser->getId(), time())
52+
);
53+
}
54+
}
55+
}

app/code/Magento/AdminAdobeIms/Test/Unit/Command/AdminAdobeImsEnableCommandTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Exception;
1212
use Magento\AdminAdobeIms\Console\Command\AdminAdobeImsEnableCommand;
1313
use Magento\AdminAdobeIms\Model\ImsConnection;
14+
use Magento\AdminAdobeIms\Service\UpdateTokensService;
1415
use Magento\AdminAdobeIms\Service\ImsCommandOptionService;
1516
use Magento\AdminAdobeIms\Service\ImsConfig;
1617
use Magento\Framework\App\Cache\Type\Config;
@@ -53,6 +54,11 @@ class AdminAdobeImsEnableCommandTest extends TestCase
5354
*/
5455
private $typeListInterface;
5556

57+
/**
58+
* @var UpdateTokensService
59+
*/
60+
private $updateTokensService;
61+
5662
/**
5763
* @var QuestionHelper
5864
*/
@@ -71,6 +77,7 @@ protected function setUp(): void
7177
$this->imsConnectionMock = $this->createMock(ImsConnection::class);
7278
$this->imsCommandOptionService = $this->createMock(ImsCommandOptionService::class);
7379
$this->typeListInterface = $this->createMock(TypeListInterface::class);
80+
$this->updateTokensService = $this->createMock(UpdateTokensService::class);
7481

7582
$this->questionHelperMock = $this->getMockBuilder(QuestionHelper::class)
7683
->disableOriginalConstructor()
@@ -83,6 +90,7 @@ protected function setUp(): void
8390
'imsConnection' => $this->imsConnectionMock,
8491
'imsCommandOptionService' => $this->imsCommandOptionService,
8592
'cacheTypeList' => $this->typeListInterface,
93+
'updateTokenService' => $this->updateTokensService
8694
]
8795
);
8896
}
@@ -128,11 +136,16 @@ public function testAdminAdobeImsModuleEnableWillClearCacheWhenSuccessful(
128136
->method('cleanType')
129137
->with(Config::TYPE_IDENTIFIER);
130138

139+
$this->updateTokensService
140+
->expects($cleanMethodCallExpection)
141+
->method('execute');
142+
131143
$outputMock->expects($this->once())
132144
->method('writeln')
133145
->with($outputMessage, null)
134146
->willReturnSelf();
135147

148+
136149
$this->enableCommand->setHelperSet($this->getHelperSet());
137150
$this->enableCommand->run($inputMock, $outputMock);
138151
}

app/code/Magento/AdminAdobeIms/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"magento/module-authorization": "*",
1717
"magento/module-store": "*",
1818
"magento/module-email": "*",
19-
"magento/module-integration": "*"
19+
"magento/module-integration": "*",
20+
"magento/module-jwt-user-token": "*"
2021
},
2122
"suggest": {
2223
"magento/module-theme": "*"

app/code/Magento/AdminAdobeIms/etc/module.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<module name="Magento_Store"/>
1919
<module name="Magento_Email"/>
2020
<module name="Magento_Integration"/>
21+
<module name="Magento_JwtUserToken"/>
2122
</sequence>
2223
</module>
2324
</config>

0 commit comments

Comments
 (0)