Skip to content

Commit 60ebe86

Browse files
authored
Merge pull request #419 from magento-performance/cabpi-332-revoke-ims-token-on-force-sign-in
Cabpi 332 revoke ims token on force sign in
2 parents 4be5cf2 + 3dc8c1b commit 60ebe86

File tree

9 files changed

+343
-121
lines changed

9 files changed

+343
-121
lines changed

app/code/Magento/AdminAdobeIms/Api/ImsLogOutInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ interface ImsLogOutInterface
1919
* LogOut User from Adobe IMS Account
2020
*
2121
* @param string|null $accessToken
22+
* @param int|null $adminUserId
2223
* @return bool
2324
*/
24-
public function execute(?string $accessToken = null) : bool;
25+
public function execute(?string $accessToken = null, ?int $adminUserId = null) : bool;
2526
}

app/code/Magento/AdminAdobeIms/Model/Authorization/AdobeImsTokenUserContext.php

Lines changed: 17 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,12 @@
88

99
namespace Magento\AdminAdobeIms\Model\Authorization;
1010

11-
use Magento\AdminAdobeIms\Api\TokenReaderInterface;
12-
use Magento\AdminAdobeIms\Model\ImsConnection;
13-
use Magento\AdminAdobeIms\Model\User;
1411
use Magento\AdminAdobeIms\Service\ImsConfig;
15-
use Magento\AdobeImsApi\Api\Data\UserProfileInterface;
16-
use Magento\AdobeImsApi\Api\Data\UserProfileInterfaceFactory;
17-
use Magento\AdobeImsApi\Api\UserProfileRepositoryInterface;
1812
use Magento\Authorization\Model\UserContextInterface;
1913
use Magento\Framework\Exception\AuthenticationException;
20-
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\Framework\Exception\AuthorizationException;
15+
use Magento\Framework\Exception\CouldNotSaveException;
16+
use Magento\Framework\Exception\InvalidArgumentException;
2117
use Magento\Framework\Webapi\Request;
2218

2319
/**
@@ -28,81 +24,49 @@ class AdobeImsTokenUserContext implements UserContextInterface
2824
private const AUTHORIZATION_METHOD_HEADER_BEARER = 'bearer';
2925

3026
/**
31-
* @var int
27+
* @var int|null
3228
*/
33-
private $userId;
29+
private ?int $userId = null;
3430

3531
/**
3632
* @var bool
3733
*/
38-
private $isRequestProcessed;
34+
private bool $isRequestProcessed = false;
3935

4036
/**
4137
* @var Request
4238
*/
4339
private Request $request;
4440

45-
/**
46-
* @var TokenReaderInterface
47-
*/
48-
private TokenReaderInterface $tokenReader;
49-
50-
/**
51-
* @var ImsConnection
52-
*/
53-
private ImsConnection $imsConnection;
54-
5541
/**
5642
* @var ImsConfig
5743
*/
5844
private ImsConfig $imsConfig;
5945

6046
/**
61-
* @var UserProfileRepositoryInterface
47+
* @var AdobeImsTokenUserService
6248
*/
63-
private UserProfileRepositoryInterface $userProfileRepository;
64-
65-
/**
66-
* @var UserProfileInterfaceFactory
67-
*/
68-
private UserProfileInterfaceFactory $userProfileFactory;
69-
70-
/**
71-
* @var User
72-
*/
73-
private User $adminUser;
49+
private AdobeImsTokenUserService $tokenUserService;
7450

7551
/**
7652
* @param Request $request
77-
* @param TokenReaderInterface $tokenReader
78-
* @param ImsConnection $imsConnection
7953
* @param ImsConfig $imsConfig
80-
* @param UserProfileRepositoryInterface $userProfileRepository
81-
* @param UserProfileInterfaceFactory $userProfileFactory
82-
* @param User $adminUser
54+
* @param AdobeImsTokenUserService $tokenUserService
8355
*/
8456
public function __construct(
8557
Request $request,
86-
TokenReaderInterface $tokenReader,
87-
ImsConnection $imsConnection,
8858
ImsConfig $imsConfig,
89-
UserProfileRepositoryInterface $userProfileRepository,
90-
UserProfileInterfaceFactory $userProfileFactory,
91-
User $adminUser
59+
AdobeImsTokenUserService $tokenUserService
9260
) {
9361
$this->request = $request;
94-
$this->tokenReader = $tokenReader;
95-
$this->imsConnection = $imsConnection;
9662
$this->imsConfig = $imsConfig;
97-
$this->userProfileRepository = $userProfileRepository;
98-
$this->userProfileFactory = $userProfileFactory;
99-
$this->adminUser = $adminUser;
63+
$this->tokenUserService = $tokenUserService;
10064
}
10165

10266
/**
10367
* @inheritdoc
10468
*/
105-
public function getUserId()
69+
public function getUserId(): ?int
10670
{
10771
$this->processRequest();
10872
return $this->userId;
@@ -111,7 +75,7 @@ public function getUserId()
11175
/**
11276
* @inheritdoc
11377
*/
114-
public function getUserType()
78+
public function getUserType(): ?int
11579
{
11680
return UserContextInterface::USER_TYPE_ADMIN;
11781
}
@@ -120,6 +84,9 @@ public function getUserType()
12084
* Finds the bearer token and looks up the value.
12185
*
12286
* @return void
87+
* @throws AuthorizationException
88+
* @throws CouldNotSaveException
89+
* @throws InvalidArgumentException
12390
*/
12491
private function processRequest()
12592
{
@@ -132,28 +99,7 @@ private function processRequest()
13299
}
133100

134101
try {
135-
$tokenData = $this->tokenReader->read($bearerToken);
136-
$adobeUserId = $tokenData['adobe_user_id'] ?? '';
137-
$userProfile = $this->userProfileRepository->getByAdobeUserId($adobeUserId);
138-
139-
if ($userProfile->getId()) {
140-
$adminUserId = (int) $userProfile->getData('admin_user_id');
141-
} else {
142-
$profile = $this->imsConnection->getProfile($bearerToken);
143-
if (empty($profile['email'])) {
144-
throw new AuthenticationException(__('An authentication error occurred. Verify and try again.'));
145-
}
146-
$adminUser = $this->adminUser->loadByEmail($profile['email']);
147-
if (empty($adminUser['user_id'])) {
148-
throw new AuthenticationException(__('An authentication error occurred. Verify and try again.'));
149-
}
150-
151-
$adminUserId = (int) $adminUser['user_id'];
152-
$profile['adobe_user_id'] = $adobeUserId;
153-
154-
$userProfileInterface = $this->getUserProfileInterface($adminUserId);
155-
$this->userProfileRepository->save($this->updateUserProfile($userProfileInterface, $profile));
156-
}
102+
$adminUserId = $this->tokenUserService->getAdminUserIdByToken($bearerToken);
157103
} catch (AuthenticationException $e) {
158104
$this->isRequestProcessed = true;
159105
return;
@@ -190,43 +136,4 @@ private function getRequestedToken()
190136

191137
return $headerPieces[1];
192138
}
193-
194-
/**
195-
* Get user profile entity
196-
*
197-
* @param int $adminUserId
198-
* @return UserProfileInterface
199-
*/
200-
private function getUserProfileInterface(int $adminUserId): UserProfileInterface
201-
{
202-
try {
203-
return $this->userProfileRepository->getByUserId($adminUserId);
204-
} catch (NoSuchEntityException $exception) {
205-
return $this->userProfileFactory->create(
206-
[
207-
'data' => [
208-
'admin_user_id' => $adminUserId
209-
]
210-
]
211-
);
212-
}
213-
}
214-
215-
/**
216-
* Update user profile with the data from token
217-
*
218-
* @param UserProfileInterface $userProfileInterface
219-
* @param array $profile
220-
* @return UserProfileInterface
221-
*/
222-
private function updateUserProfile(
223-
UserProfileInterface $userProfileInterface,
224-
array $profile
225-
): UserProfileInterface {
226-
$userProfileInterface->setName($profile['name'] ?? '');
227-
$userProfileInterface->setEmail($profile['email'] ?? '');
228-
$userProfileInterface->setAdobeUserId($profile['adobe_user_id']);
229-
230-
return $userProfileInterface;
231-
}
232139
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\AdminAdobeIms\Model\Authorization;
10+
11+
use Magento\AdminAdobeIms\Api\TokenReaderInterface;
12+
use Magento\AdminAdobeIms\Model\ImsConnection;
13+
use Magento\AdminAdobeIms\Model\User;
14+
use Magento\AdobeImsApi\Api\Data\UserProfileInterface;
15+
use Magento\AdobeImsApi\Api\Data\UserProfileInterfaceFactory;
16+
use Magento\AdobeImsApi\Api\UserProfileRepositoryInterface;
17+
use Magento\Framework\Exception\AuthenticationException;
18+
use Magento\Framework\Exception\AuthorizationException;
19+
use Magento\Framework\Exception\CouldNotSaveException;
20+
use Magento\Framework\Exception\InvalidArgumentException;
21+
use Magento\Framework\Exception\NoSuchEntityException;
22+
23+
class AdobeImsTokenUserService
24+
{
25+
/**
26+
* @var TokenReaderInterface
27+
*/
28+
private TokenReaderInterface $tokenReader;
29+
30+
/**
31+
* @var UserProfileRepositoryInterface
32+
*/
33+
private UserProfileRepositoryInterface $userProfileRepository;
34+
35+
/**
36+
* @var UserProfileInterfaceFactory
37+
*/
38+
private UserProfileInterfaceFactory $userProfileFactory;
39+
40+
/**
41+
* @var User
42+
*/
43+
private User $adminUser;
44+
45+
/**
46+
* @var ImsConnection
47+
*/
48+
private ImsConnection $imsConnection;
49+
50+
/**
51+
* @param TokenReaderInterface $tokenReader
52+
* @param UserProfileRepositoryInterface $userProfileRepository
53+
* @param UserProfileInterfaceFactory $userProfileFactory
54+
* @param User $adminUser
55+
* @param ImsConnection $imsConnection
56+
*/
57+
public function __construct(
58+
TokenReaderInterface $tokenReader,
59+
UserProfileRepositoryInterface $userProfileRepository,
60+
UserProfileInterfaceFactory $userProfileFactory,
61+
User $adminUser,
62+
ImsConnection $imsConnection
63+
) {
64+
$this->tokenReader = $tokenReader;
65+
$this->userProfileRepository = $userProfileRepository;
66+
$this->userProfileFactory = $userProfileFactory;
67+
$this->adminUser = $adminUser;
68+
$this->imsConnection = $imsConnection;
69+
}
70+
71+
/**
72+
* Get adobe_user_id from token and store it for admin user
73+
*
74+
* @param string $bearerToken
75+
* @return int
76+
* @throws AuthenticationException
77+
* @throws AuthorizationException
78+
* @throws CouldNotSaveException
79+
* @throws InvalidArgumentException
80+
*/
81+
public function getAdminUserIdByToken(string $bearerToken): int
82+
{
83+
$tokenData = $this->tokenReader->read($bearerToken);
84+
85+
$adobeUserId = $tokenData['adobe_user_id'] ?? '';
86+
87+
$userProfile = $this->userProfileRepository->getByAdobeUserId($adobeUserId);
88+
89+
if ($userProfile->getId()) {
90+
$adminUserId = (int) $userProfile->getData('admin_user_id');
91+
} else {
92+
$profile = $this->getUserProfile($bearerToken);
93+
if (empty($profile['email'])) {
94+
throw new AuthenticationException(__('An authentication error occurred. Verify and try again.'));
95+
}
96+
$adminUser = $this->adminUser->loadByEmail($profile['email']);
97+
if (empty($adminUser['user_id'])) {
98+
throw new AuthenticationException(__('An authentication error occurred. Verify and try again.'));
99+
}
100+
101+
$adminUserId = (int) $adminUser['user_id'];
102+
$profile['adobe_user_id'] = $adobeUserId;
103+
104+
$userProfileInterface = $this->getUserProfileInterface($adminUserId);
105+
$this->userProfileRepository->save($this->updateUserProfile($userProfileInterface, $profile));
106+
}
107+
108+
return $adminUserId;
109+
}
110+
111+
/**
112+
* Get adobe user profile
113+
*
114+
* @param string $bearerToken
115+
* @return array
116+
* @throws AuthenticationException
117+
*/
118+
private function getUserProfile(string $bearerToken): array
119+
{
120+
try {
121+
return $this->imsConnection->getProfile($bearerToken);
122+
} catch (\Exception $exception) {
123+
throw new AuthenticationException(__('An authentication error occurred. Verify and try again.'));
124+
}
125+
}
126+
127+
/**
128+
* Get user profile entity
129+
*
130+
* @param int $adminUserId
131+
* @return UserProfileInterface
132+
*/
133+
private function getUserProfileInterface(int $adminUserId): UserProfileInterface
134+
{
135+
try {
136+
return $this->userProfileRepository->getByUserId($adminUserId);
137+
} catch (NoSuchEntityException $exception) {
138+
return $this->userProfileFactory->create(
139+
[
140+
'data' => [
141+
'admin_user_id' => $adminUserId
142+
]
143+
]
144+
);
145+
}
146+
}
147+
148+
/**
149+
* Update user profile with the data from token
150+
*
151+
* @param UserProfileInterface $userProfileInterface
152+
* @param array $profile
153+
* @return UserProfileInterface
154+
*/
155+
private function updateUserProfile(
156+
UserProfileInterface $userProfileInterface,
157+
array $profile
158+
): UserProfileInterface {
159+
$userProfileInterface->setName($profile['name'] ?? '');
160+
$userProfileInterface->setEmail($profile['email'] ?? '');
161+
$userProfileInterface->setAdobeUserId($profile['adobe_user_id']);
162+
163+
return $userProfileInterface;
164+
}
165+
}

app/code/Magento/AdminAdobeIms/Model/ImsConnection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public function getTokenResponse(string $code): TokenResponseInterface
208208
*
209209
* @param string $code
210210
* @return array|bool|float|int|mixed|string|null
211-
* @throws AuthorizationException
211+
* @throws AdobeImsTokenAuthorizationException
212212
*/
213213
public function getProfile(string $code)
214214
{

0 commit comments

Comments
 (0)