Skip to content

Commit 789791c

Browse files
committed
CABPI-467: Admin Authentication implementation based on UserContextInterface
1 parent 2e45de7 commit 789791c

File tree

6 files changed

+645
-63
lines changed

6 files changed

+645
-63
lines changed

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

Lines changed: 13 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -9,70 +9,49 @@
99
namespace Magento\AdminAdobeIms\Controller\Adminhtml\OAuth;
1010

1111
use Exception;
12-
use Magento\AdminAdobeIms\Exception\AdobeImsOrganizationAuthorizationException;
13-
use Magento\AdminAdobeIms\Exception\AdobeImsAuthorizationException;
1412
use Magento\AdminAdobeIms\Logger\AdminAdobeImsLogger;
15-
use Magento\AdminAdobeIms\Service\AdminLoginProcessService;
13+
use Magento\Authorization\Model\UserContextInterface;
1614
use Magento\AdminAdobeIms\Service\ImsConfig;
17-
use Magento\AdminAdobeIms\Service\ImsOrganizationService;
1815
use Magento\Backend\App\Action\Context;
19-
use Magento\AdminAdobeIms\Model\ImsConnection;
2016
use Magento\Backend\Controller\Adminhtml\Auth;
2117
use Magento\Backend\Model\View\Result\Redirect;
2218
use Magento\Framework\App\Action\HttpGetActionInterface;
23-
use Magento\Framework\Exception\AuthenticationException;
2419

2520
class ImsCallback extends Auth implements HttpGetActionInterface
2621
{
2722
public const ACTION_NAME = 'imscallback';
2823

29-
/**
30-
* @var ImsConnection
31-
*/
32-
private ImsConnection $adminImsConnection;
33-
3424
/**
3525
* @var ImsConfig
3626
*/
3727
private ImsConfig $adminImsConfig;
3828

3929
/**
40-
* @var ImsOrganizationService
41-
*/
42-
private ImsOrganizationService $adminOrganizationService;
43-
44-
/**
45-
* @var AdminLoginProcessService
30+
* @var AdminAdobeImsLogger
4631
*/
47-
private AdminLoginProcessService $adminLoginProcessService;
32+
private AdminAdobeImsLogger $logger;
4833

4934
/**
50-
* @var AdminAdobeImsLogger
35+
* @var UserContextInterface
5136
*/
52-
private AdminAdobeImsLogger $logger;
37+
private UserContextInterface $userContext;
5338

5439
/**
5540
* @param Context $context
56-
* @param ImsConnection $adminImsConnection
5741
* @param ImsConfig $adminImsConfig
58-
* @param ImsOrganizationService $adminOrganizationService
59-
* @param AdminLoginProcessService $adminLoginProcessService
6042
* @param AdminAdobeImsLogger $logger
43+
* @param UserContextInterface $userContext
6144
*/
6245
public function __construct(
6346
Context $context,
64-
ImsConnection $adminImsConnection,
6547
ImsConfig $adminImsConfig,
66-
ImsOrganizationService $adminOrganizationService,
67-
AdminLoginProcessService $adminLoginProcessService,
68-
AdminAdobeImsLogger $logger
48+
AdminAdobeImsLogger $logger,
49+
UserContextInterface $userContext
6950
) {
7051
parent::__construct($context);
71-
$this->adminImsConnection = $adminImsConnection;
7252
$this->adminImsConfig = $adminImsConfig;
73-
$this->adminOrganizationService = $adminOrganizationService;
74-
$this->adminLoginProcessService = $adminLoginProcessService;
7553
$this->logger = $logger;
54+
$this->userContext = $userContext;
7655
}
7756

7857
/**
@@ -92,40 +71,11 @@ public function execute(): Redirect
9271
}
9372

9473
try {
95-
$code = $this->getRequest()->getParam('code');
96-
97-
if ($code === null) {
98-
throw new AuthenticationException(__('An authentication error occurred. Verify and try again.'));
99-
}
100-
101-
//get token from response
102-
$tokenResponse = $this->adminImsConnection->getTokenResponse($code);
103-
$accessToken = $tokenResponse->getAccessToken();
104-
105-
//get profile info to check email
106-
$profile = $this->adminImsConnection->getProfile($accessToken);
107-
if (empty($profile['email'])) {
108-
throw new AuthenticationException(__('An authentication error occurred. Verify and try again.'));
74+
if ($this->userContext->getUserId()
75+
&& $this->userContext->getUserType() === UserContextInterface::USER_TYPE_ADMIN
76+
) {
77+
return $resultRedirect;
10978
}
110-
111-
//check membership in organization
112-
$this->adminOrganizationService->checkOrganizationMembership($accessToken);
113-
114-
$this->adminLoginProcessService->execute($tokenResponse, $profile);
115-
} catch (AdobeImsAuthorizationException $e) {
116-
$this->logger->error($e->getMessage());
117-
118-
$this->imsErrorMessage(
119-
'You don\'t have access to this Commerce instance',
120-
AdobeImsAuthorizationException::ERROR_MESSAGE
121-
);
122-
} catch (AdobeImsOrganizationAuthorizationException $e) {
123-
$this->logger->error($e->getMessage());
124-
125-
$this->imsErrorMessage(
126-
'Unable to sign in with the Adobe ID',
127-
AdobeImsOrganizationAuthorizationException::ERROR_MESSAGE
128-
);
12979
} catch (Exception $e) {
13080
$this->logger->error($e->getMessage());
13181

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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\Model\Auth;
12+
use Magento\AdminAdobeIms\Model\ImsConnection;
13+
use Magento\AdminAdobeIms\Service\ImsConfig;
14+
use Magento\Authorization\Model\UserContextInterface;
15+
use Magento\Framework\Exception\AuthenticationException;
16+
17+
/**
18+
* A user context determined by Adobe IMS tokens for Admin Panel.
19+
*/
20+
class AdobeImsAdminTokenUserContext implements UserContextInterface
21+
{
22+
/**
23+
* @var int|null
24+
*/
25+
private ?int $userId = null;
26+
27+
/**
28+
* @var bool
29+
*/
30+
private bool $isRequestProcessed = false;
31+
32+
/**
33+
* @var ImsConfig
34+
*/
35+
private ImsConfig $adminImsConfig;
36+
37+
/**
38+
* @var Auth
39+
*/
40+
protected Auth $auth;
41+
42+
/**
43+
* @var ImsConnection
44+
*/
45+
private ImsConnection $adminImsConnection;
46+
47+
/**
48+
* @var AdobeImsAdminTokenUserService
49+
*/
50+
private AdobeImsAdminTokenUserService $adminTokenUserService;
51+
52+
/**
53+
* @param ImsConfig $adminImsConfig
54+
* @param Auth $auth
55+
* @param ImsConnection $adminImsConnection
56+
* @param AdobeImsAdminTokenUserService $adminTokenUserService
57+
*/
58+
public function __construct(
59+
ImsConfig $adminImsConfig,
60+
Auth $auth,
61+
ImsConnection $adminImsConnection,
62+
AdobeImsAdminTokenUserService $adminTokenUserService
63+
) {
64+
$this->adminImsConfig = $adminImsConfig;
65+
$this->auth = $auth;
66+
$this->adminImsConnection = $adminImsConnection;
67+
$this->adminTokenUserService = $adminTokenUserService;
68+
}
69+
70+
/**
71+
* @inheritdoc
72+
*/
73+
public function getUserId(): ?int
74+
{
75+
if (!$this->adminImsConfig->enabled() || $this->isRequestProcessed) {
76+
return $this->userId;
77+
}
78+
79+
$session = $this->auth->getAuthStorage();
80+
81+
if (!empty($session->getAdobeAccessToken())) {
82+
$isTokenValid = $this->adminImsConnection->validateToken($session->getAdobeAccessToken());
83+
if (!$isTokenValid) {
84+
throw new AuthenticationException(__('An authentication error occurred. Verify and try again.'));
85+
}
86+
} else {
87+
$this->adminTokenUserService->processLoginRequest();
88+
}
89+
90+
$this->userId = (int) $session->getUser()->getUserId();
91+
$this->isRequestProcessed = true;
92+
93+
return $this->userId;
94+
}
95+
96+
/**
97+
* @inheritdoc
98+
*/
99+
public function getUserType(): ?int
100+
{
101+
return UserContextInterface::USER_TYPE_ADMIN;
102+
}
103+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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\Exception\AdobeImsAuthorizationException;
12+
use Magento\AdminAdobeIms\Exception\AdobeImsOrganizationAuthorizationException;
13+
use Magento\AdminAdobeIms\Model\ImsConnection;
14+
use Magento\AdminAdobeIms\Service\AdminLoginProcessService;
15+
use Magento\AdminAdobeIms\Service\ImsConfig;
16+
use Magento\AdminAdobeIms\Service\ImsOrganizationService;
17+
use Magento\Framework\App\RequestInterface;
18+
use Magento\Framework\Exception\AuthenticationException;
19+
20+
/**
21+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
22+
*/
23+
class AdobeImsAdminTokenUserService
24+
{
25+
private const ADOBE_IMS_MODULE_NAME = 'adobe_ims_auth';
26+
27+
/**
28+
* @var ImsConfig
29+
*/
30+
private ImsConfig $adminImsConfig;
31+
32+
/**
33+
* @var ImsConnection
34+
*/
35+
private ImsConnection $adminImsConnection;
36+
37+
/**
38+
* @var ImsOrganizationService
39+
*/
40+
private ImsOrganizationService $adminOrganizationService;
41+
42+
/**
43+
* @var AdminLoginProcessService
44+
*/
45+
private AdminLoginProcessService $adminLoginProcessService;
46+
47+
/**
48+
* @var RequestInterface
49+
*/
50+
private RequestInterface $request;
51+
52+
/**
53+
* @param ImsConfig $adminImsConfig
54+
* @param ImsConnection $adminImsConnection
55+
* @param ImsOrganizationService $adminOrganizationService
56+
* @param AdminLoginProcessService $adminLoginProcessService
57+
* @param RequestInterface $request
58+
*/
59+
public function __construct(
60+
ImsConfig $adminImsConfig,
61+
ImsConnection $adminImsConnection,
62+
ImsOrganizationService $adminOrganizationService,
63+
AdminLoginProcessService $adminLoginProcessService,
64+
RequestInterface $request
65+
) {
66+
$this->adminImsConfig = $adminImsConfig;
67+
$this->adminImsConnection = $adminImsConnection;
68+
$this->adminOrganizationService = $adminOrganizationService;
69+
$this->adminLoginProcessService = $adminLoginProcessService;
70+
$this->request = $request;
71+
}
72+
73+
/**
74+
* Process login request to Admin Adobe IMS.
75+
*
76+
* @return void
77+
* @throws AuthenticationException
78+
* @throws AdobeImsAuthorizationException
79+
*/
80+
public function processLoginRequest()
81+
{
82+
if ($this->adminImsConfig->enabled() && $this->request->getParam('code')
83+
&& $this->request->getModuleName() === self::ADOBE_IMS_MODULE_NAME) {
84+
try {
85+
$code = $this->request->getParam('code');
86+
87+
//get token from response
88+
$tokenResponse = $this->adminImsConnection->getTokenResponse($code);
89+
$accessToken = $tokenResponse->getAccessToken();
90+
91+
//get profile info to check email
92+
$profile = $this->adminImsConnection->getProfile($accessToken);
93+
if (empty($profile['email'])) {
94+
throw new AuthenticationException(__('An authentication error occurred. Verify and try again.'));
95+
}
96+
97+
//check membership in organization
98+
$this->adminOrganizationService->checkOrganizationMembership($accessToken);
99+
100+
$this->adminLoginProcessService->execute($tokenResponse, $profile);
101+
} catch (AdobeImsAuthorizationException $e) {
102+
throw new AdobeImsAuthorizationException(
103+
__('You don\'t have access to this Commerce instance')
104+
);
105+
} catch (AdobeImsOrganizationAuthorizationException $e) {
106+
throw new AdobeImsOrganizationAuthorizationException(
107+
__('Unable to sign in with the Adobe ID')
108+
);
109+
}
110+
} else {
111+
throw new AuthenticationException(__('An authentication error occurred. Verify and try again.'));
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)