Skip to content

Commit cf55a2c

Browse files
committed
Fixed minor issues and code duplications for CABPI-467
1 parent 61ca3a2 commit cf55a2c

File tree

6 files changed

+48
-61
lines changed

6 files changed

+48
-61
lines changed

app/code/Magento/AdminAdobeIms/Controller/Adminhtml/OAuth/ImsCallback.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
use Magento\Backend\Model\View\Result\Redirect;
1818
use Magento\Framework\App\Action\HttpGetActionInterface;
1919

20+
/**
21+
* Callback for handling redirect from Adobe IMS
22+
*/
2023
class ImsCallback extends Auth implements HttpGetActionInterface
2124
{
2225
public const ACTION_NAME = 'imscallback';

app/code/Magento/AdminAdobeIms/Controller/Adminhtml/OAuth/ImsReauthCallback.php

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,14 @@
99

1010
use Exception;
1111
use Magento\AdminAdobeIms\Logger\AdminAdobeImsLogger;
12-
use Magento\AdminAdobeIms\Service\AdminReauthProcessService;
12+
use Magento\AdminAdobeIms\Model\Authorization\AdobeImsAdminTokenUserService;
1313
use Magento\AdminAdobeIms\Service\ImsConfig;
14-
use Magento\AdobeImsApi\Api\OrganizationMembershipInterface;
15-
use Magento\AdobeImsApi\Api\GetProfileInterface;
1614
use Magento\Backend\App\Action\Context;
1715
use Magento\Backend\Controller\Adminhtml\Auth;
1816
use Magento\Framework\App\Action\HttpGetActionInterface;
1917
use Magento\Framework\Controller\Result\Raw;
2018
use Magento\Framework\Controller\ResultFactory;
2119
use Magento\Framework\Controller\ResultInterface;
22-
use Magento\AdobeImsApi\Api\GetTokenInterface;
23-
use Magento\Framework\Exception\AuthenticationException;
2420

2521
class ImsReauthCallback extends Auth implements HttpGetActionInterface
2622
{
@@ -42,56 +38,32 @@ class ImsReauthCallback extends Auth implements HttpGetActionInterface
4238
*/
4339
private ImsConfig $adminImsConfig;
4440

45-
/**
46-
* @var OrganizationMembershipInterface
47-
*/
48-
private OrganizationMembershipInterface $organizationMembership;
49-
50-
/**
51-
* @var AdminReauthProcessService
52-
*/
53-
private AdminReauthProcessService $adminReauthProcessService;
54-
5541
/**
5642
* @var AdminAdobeImsLogger
5743
*/
5844
private AdminAdobeImsLogger $logger;
5945

6046
/**
61-
* @var GetTokenInterface
62-
*/
63-
private GetTokenInterface $token;
64-
65-
/**
66-
* @var GetProfileInterface
47+
* @var AdobeImsAdminTokenUserService
6748
*/
68-
private GetProfileInterface $profile;
49+
private AdobeImsAdminTokenUserService $adminTokenUserService;
6950

7051
/**
7152
* @param Context $context
72-
* @param GetProfileInterface $profile
7353
* @param ImsConfig $adminImsConfig
74-
* @param OrganizationMembershipInterface $organizationMembership
75-
* @param AdminReauthProcessService $adminReauthProcessService
54+
* @param AdobeImsAdminTokenUserService $adminTokenUserService
7655
* @param AdminAdobeImsLogger $logger
77-
* @param GetTokenInterface $token
7856
*/
7957
public function __construct(
8058
Context $context,
81-
GetProfileInterface $profile,
8259
ImsConfig $adminImsConfig,
83-
OrganizationMembershipInterface $organizationMembership,
84-
AdminReauthProcessService $adminReauthProcessService,
85-
AdminAdobeImsLogger $logger,
86-
GetTokenInterface $token
60+
AdobeImsAdminTokenUserService $adminTokenUserService,
61+
AdminAdobeImsLogger $logger
8762
) {
8863
parent::__construct($context);
89-
$this->profile = $profile;
9064
$this->adminImsConfig = $adminImsConfig;
91-
$this->organizationMembership = $organizationMembership;
92-
$this->adminReauthProcessService = $adminReauthProcessService;
65+
$this->adminTokenUserService = $adminTokenUserService;
9366
$this->logger = $logger;
94-
$this->token = $token;
9567
}
9668

9769
/**
@@ -119,24 +91,7 @@ public function execute(): ResultInterface
11991
}
12092

12193
try {
122-
$code = $this->getRequest()->getParam('code');
123-
124-
if ($code === null) {
125-
throw new AuthenticationException(__('An authentication error occurred. Verify and try again.'));
126-
}
127-
128-
$tokenResponse = $this->token->getTokenResponse($code);
129-
$accessToken = $tokenResponse->getAccessToken();
130-
131-
$profile = $this->profile->getProfile($accessToken);
132-
if (empty($profile['email'])) {
133-
throw new AuthenticationException(__('An authentication error occurred. Verify and try again.'));
134-
}
135-
136-
//check membership in organization
137-
$this->organizationMembership->checkOrganizationMembership($accessToken);
138-
139-
$this->adminReauthProcessService->execute($tokenResponse);
94+
$this->adminTokenUserService->processLoginRequest(true);
14095

14196
$response = sprintf(
14297
self::RESPONSE_TEMPLATE,

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
76
declare(strict_types=1);
87

98
namespace Magento\AdminAdobeIms\Model\Authorization;
@@ -84,7 +83,11 @@ public function getUserId(): ?int
8483
throw new AuthenticationException(__('An authentication error occurred. Verify and try again.'));
8584
}
8685
} else {
87-
$this->adminTokenUserService->processLoginRequest();
86+
try {
87+
$this->adminTokenUserService->processLoginRequest();
88+
} catch (\Exception $e) {
89+
throw new AuthenticationException(__('An authentication error occurred. Verify and try again.'));
90+
}
8891
}
8992

9093
$this->userId = (int) $session->getUser()->getUserId();

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Magento\AdminAdobeIms\Exception\AdobeImsAuthorizationException;
1212
use Magento\AdminAdobeIms\Service\AdminLoginProcessService;
13+
use Magento\AdminAdobeIms\Service\AdminReauthProcessService;
1314
use Magento\AdminAdobeIms\Service\ImsConfig;
1415
use Magento\AdobeIms\Exception\AdobeImsOrganizationAuthorizationException;
1516
use Magento\AdobeImsApi\Api\GetProfileInterface;
@@ -19,6 +20,8 @@
1920
use Magento\Framework\Exception\AuthenticationException;
2021

2122
/**
23+
* Adobe IMS Auth Model for getting Admin Token
24+
*
2225
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2326
*/
2427
class AdobeImsAdminTokenUserService
@@ -50,6 +53,11 @@ class AdobeImsAdminTokenUserService
5053
*/
5154
private AdminLoginProcessService $adminLoginProcessService;
5255

56+
/**
57+
* @var AdminReauthProcessService
58+
*/
59+
private AdminReauthProcessService $adminReauthProcessService;
60+
5361
/**
5462
* @var RequestInterface
5563
*/
@@ -59,6 +67,7 @@ class AdobeImsAdminTokenUserService
5967
* @param ImsConfig $adminImsConfig
6068
* @param OrganizationMembershipInterface $organizationMembership
6169
* @param AdminLoginProcessService $adminLoginProcessService
70+
* @param AdminReauthProcessService $adminReauthProcessService
6271
* @param RequestInterface $request
6372
* @param GetTokenInterface $token
6473
* @param GetProfileInterface $profile
@@ -67,13 +76,15 @@ public function __construct(
6776
ImsConfig $adminImsConfig,
6877
OrganizationMembershipInterface $organizationMembership,
6978
AdminLoginProcessService $adminLoginProcessService,
79+
AdminReauthProcessService $adminReauthProcessService,
7080
RequestInterface $request,
7181
GetTokenInterface $token,
7282
GetProfileInterface $profile
7383
) {
7484
$this->adminImsConfig = $adminImsConfig;
7585
$this->organizationMembership = $organizationMembership;
7686
$this->adminLoginProcessService = $adminLoginProcessService;
87+
$this->adminReauthProcessService = $adminReauthProcessService;
7788
$this->request = $request;
7889
$this->token = $token;
7990
$this->profile = $profile;
@@ -82,12 +93,13 @@ public function __construct(
8293
/**
8394
* Process login request to Admin Adobe IMS.
8495
*
96+
* @param bool $isReauthorize
8597
* @return void
8698
* @throws AdobeImsAuthorizationException
8799
* @throws AdobeImsOrganizationAuthorizationException
88100
* @throws AuthenticationException
89101
*/
90-
public function processLoginRequest()
102+
public function processLoginRequest(bool $isReauthorize = false): void
91103
{
92104
if ($this->adminImsConfig->enabled() && $this->request->getParam('code')
93105
&& $this->request->getModuleName() === self::ADOBE_IMS_MODULE_NAME) {
@@ -107,7 +119,11 @@ public function processLoginRequest()
107119
//check membership in organization
108120
$this->organizationMembership->checkOrganizationMembership($accessToken);
109121

110-
$this->adminLoginProcessService->execute($tokenResponse, $profile);
122+
if ($isReauthorize) {
123+
$this->adminReauthProcessService->execute($tokenResponse);
124+
} else {
125+
$this->adminLoginProcessService->execute($tokenResponse, $profile);
126+
}
111127
} catch (AdobeImsAuthorizationException $e) {
112128
throw new AdobeImsAuthorizationException(
113129
__('You don\'t have access to this Commerce instance')

app/code/Magento/AdminAdobeIms/Test/Unit/Model/Authorization/AdobeImsAdminTokenUserContextTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
use Magento\AdminAdobeIms\Model\Auth;
1111
use Magento\AdminAdobeIms\Model\Authorization\AdobeImsAdminTokenUserContext;
1212
use Magento\AdminAdobeIms\Model\Authorization\AdobeImsAdminTokenUserService;
13-
use Magento\AdobeImsApi\Api\IsTokenValidInterface;
1413
use Magento\AdminAdobeIms\Service\ImsConfig;
14+
use Magento\AdobeImsApi\Api\IsTokenValidInterface;
1515
use Magento\Authorization\Model\UserContextInterface;
1616
use Magento\Backend\Model\Auth\Session;
1717
use Magento\Framework\Exception\AuthenticationException;
@@ -101,6 +101,8 @@ public function testGetUserId()
101101
}
102102

103103
/**
104+
* Test exception with invalid access token
105+
*
104106
* @return void
105107
* @throws AuthenticationException
106108
*/
@@ -127,6 +129,8 @@ public function testGetUserType()
127129
}
128130

129131
/**
132+
* Setting up User Id
133+
*
130134
* @param int|null $userId
131135
* @return void
132136
*/

app/code/Magento/AdminAdobeIms/Test/Unit/Model/Authorization/AdobeImsAdminTokenUserServiceTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
use Magento\AdminAdobeIms\Exception\AdobeImsAuthorizationException;
1111
use Magento\AdminAdobeIms\Model\Authorization\AdobeImsAdminTokenUserService;
12-
use Magento\AdobeImsApi\Api\GetProfileInterface;
13-
use Magento\AdobeImsApi\Api\GetTokenInterface;
1412
use Magento\AdminAdobeIms\Service\AdminLoginProcessService;
1513
use Magento\AdminAdobeIms\Service\ImsConfig;
16-
use Magento\AdobeImsApi\Api\OrganizationMembershipInterface;
1714
use Magento\AdobeImsApi\Api\Data\TokenResponseInterface;
15+
use Magento\AdobeImsApi\Api\GetProfileInterface;
16+
use Magento\AdobeImsApi\Api\GetTokenInterface;
17+
use Magento\AdobeImsApi\Api\OrganizationMembershipInterface;
1818
use Magento\Framework\App\RequestInterface;
1919
use Magento\Framework\Exception\AuthenticationException;
2020
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
@@ -132,6 +132,8 @@ public function testProcessLoginRequest(string $code, array $responseData)
132132
}
133133

134134
/**
135+
* Test exception when tried to access from other module
136+
*
135137
* @return void
136138
* @param string $code
137139
* @dataProvider responseDataProvider
@@ -152,6 +154,8 @@ public function testExceptionWhenTriedToAccessFromOtherModule(string $code): voi
152154
}
153155

154156
/**
157+
* Test exception when profile not found
158+
*
155159
* @return void
156160
* @param string $code
157161
* @param array $responseData
@@ -190,6 +194,8 @@ public function testExceptionWhenProfileNotFoundBasedOnAccessToken(
190194
}
191195

192196
/**
197+
* Test exception when admin login provided with wrong info
198+
*
193199
* @return void
194200
* @param string $code
195201
* @param array $responseData

0 commit comments

Comments
 (0)