Skip to content

Commit 32724c3

Browse files
committed
CABPI-479: Admin Authentication based on Access Token in Authorization Header
1 parent 91549b9 commit 32724c3

File tree

2 files changed

+160
-63
lines changed

2 files changed

+160
-63
lines changed

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

Lines changed: 72 additions & 21 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;
@@ -13,6 +12,8 @@
1312
use Magento\AdminAdobeIms\Service\AdminReauthProcessService;
1413
use Magento\AdminAdobeIms\Service\ImsConfig;
1514
use Magento\AdobeIms\Exception\AdobeImsOrganizationAuthorizationException;
15+
use Magento\AdobeImsApi\Api\Data\TokenResponseInterface;
16+
use Magento\AdobeImsApi\Api\Data\TokenResponseInterfaceFactory;
1617
use Magento\AdobeImsApi\Api\GetProfileInterface;
1718
use Magento\AdobeImsApi\Api\GetTokenInterface;
1819
use Magento\AdobeImsApi\Api\OrganizationMembershipInterface;
@@ -27,6 +28,7 @@
2728
class AdobeImsAdminTokenUserService
2829
{
2930
private const ADOBE_IMS_MODULE_NAME = 'adobe_ims_auth';
31+
private const AUTHORIZATION_METHOD_HEADER_BEARER = 'bearer';
3032

3133
/**
3234
* @var ImsConfig
@@ -63,6 +65,11 @@ class AdobeImsAdminTokenUserService
6365
*/
6466
private RequestInterface $request;
6567

68+
/**
69+
* @var TokenResponseInterfaceFactory
70+
*/
71+
private $tokenResponseFactory;
72+
6673
/**
6774
* @param ImsConfig $adminImsConfig
6875
* @param OrganizationMembershipInterface $organizationMembership
@@ -71,6 +78,7 @@ class AdobeImsAdminTokenUserService
7178
* @param RequestInterface $request
7279
* @param GetTokenInterface $token
7380
* @param GetProfileInterface $profile
81+
* @param TokenResponseInterfaceFactory $tokenResponseFactory
7482
*/
7583
public function __construct(
7684
ImsConfig $adminImsConfig,
@@ -79,7 +87,8 @@ public function __construct(
7987
AdminReauthProcessService $adminReauthProcessService,
8088
RequestInterface $request,
8189
GetTokenInterface $token,
82-
GetProfileInterface $profile
90+
GetProfileInterface $profile,
91+
TokenResponseInterfaceFactory $tokenResponseFactory
8392
) {
8493
$this->adminImsConfig = $adminImsConfig;
8594
$this->organizationMembership = $organizationMembership;
@@ -88,6 +97,7 @@ public function __construct(
8897
$this->request = $request;
8998
$this->token = $token;
9099
$this->profile = $profile;
100+
$this->tokenResponseFactory = $tokenResponseFactory;
91101
}
92102

93103
/**
@@ -101,29 +111,19 @@ public function __construct(
101111
*/
102112
public function processLoginRequest(bool $isReauthorize = false): void
103113
{
104-
if ($this->adminImsConfig->enabled() && $this->request->getParam('code')
114+
if ($this->adminImsConfig->enabled()
105115
&& $this->request->getModuleName() === self::ADOBE_IMS_MODULE_NAME) {
106116
try {
107-
$code = $this->request->getParam('code');
108-
109-
//get token from response
110-
$tokenResponse = $this->token->getTokenResponse($code);
111-
$accessToken = $tokenResponse->getAccessToken();
112-
113-
//get profile info to check email
114-
$profile = $this->profile->getProfile($accessToken);
115-
if (empty($profile['email'])) {
116-
throw new AuthenticationException(__('An authentication error occurred. Verify and try again.'));
117-
}
118-
119-
//check membership in organization
120-
$this->organizationMembership->checkOrganizationMembership($accessToken);
121-
122-
if ($isReauthorize) {
123-
$this->adminReauthProcessService->execute($tokenResponse);
117+
if ($this->request->getHeader('Authorization')) {
118+
$tokenResponse = $this->getRequestedToken();
119+
} elseif ($this->request->getParam('code')) {
120+
$code = $this->request->getParam('code');
121+
$tokenResponse = $this->token->getTokenResponse($code);
124122
} else {
125-
$this->adminLoginProcessService->execute($tokenResponse, $profile);
123+
throw new AuthenticationException(__('Unable to get Access Token. Please try again.'));
126124
}
125+
126+
$this->getLoggedIn($isReauthorize, $tokenResponse);
127127
} catch (AdobeImsAuthorizationException $e) {
128128
throw new AdobeImsAuthorizationException(
129129
__('You don\'t have access to this Commerce instance')
@@ -137,4 +137,55 @@ public function processLoginRequest(bool $isReauthorize = false): void
137137
throw new AuthenticationException(__('An authentication error occurred. Verify and try again.'));
138138
}
139139
}
140+
141+
/**
142+
* Get requested token using Authorization header
143+
*
144+
* @return \Magento\AdobeImsApi\Api\Data\TokenResponseInterface
145+
* @throws AuthenticationException
146+
*/
147+
private function getRequestedToken()
148+
{
149+
$authorizationHeaderValue = $this->request->getHeader('Authorization');
150+
if (!$authorizationHeaderValue) {
151+
throw new AuthenticationException(__('An authentication error occurred. Verify and try again.'));
152+
}
153+
154+
$headerPieces = explode(" ", $authorizationHeaderValue);
155+
if (count($headerPieces) !== 2) {
156+
throw new AuthenticationException(__('An authentication error occurred. Verify and try again.'));
157+
}
158+
159+
$tokenType = strtolower($headerPieces[0]);
160+
if ($tokenType !== self::AUTHORIZATION_METHOD_HEADER_BEARER) {
161+
throw new AuthenticationException(__('An authentication error occurred. Verify and try again.'));
162+
}
163+
164+
$tokenResponse['access_token'] = $headerPieces[1];
165+
return $this->tokenResponseFactory->create(['data' => $tokenResponse]);
166+
}
167+
168+
/**
169+
* Responsible for logging in to Admin Panel
170+
*
171+
* @param bool $isReauthorize
172+
* @param TokenResponseInterface $tokenResponse
173+
* @return void
174+
* @throws AuthenticationException
175+
*/
176+
private function getLoggedIn(bool $isReauthorize, TokenResponseInterface $tokenResponse)
177+
{
178+
$profile = $this->profile->getProfile($tokenResponse->getAccessToken());
179+
if (empty($profile['email'])) {
180+
throw new AuthenticationException(__('An authentication error occurred. Verify and try again.'));
181+
}
182+
183+
$this->organizationMembership->checkOrganizationMembership($tokenResponse->getAccessToken());
184+
185+
if ($isReauthorize) {
186+
$this->adminReauthProcessService->execute($tokenResponse);
187+
} else {
188+
$this->adminLoginProcessService->execute($tokenResponse, $profile);
189+
}
190+
}
140191
}

0 commit comments

Comments
 (0)