Skip to content

Commit 4e2aece

Browse files
committed
CABPI-324: Change Org check to use new endpoint
1 parent d21a94b commit 4e2aece

File tree

5 files changed

+92
-11
lines changed

5 files changed

+92
-11
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,15 @@ public function execute(): Redirect
102102
$tokenResponse = $this->adminImsConnection->getTokenResponse($code);
103103
$accessToken = $tokenResponse->getAccessToken();
104104

105-
//check organization assignment
106-
$this->adminOrganizationService->checkOrganizationAllocation($accessToken);
107-
108105
//get profile info to check email
109106
$profile = $this->adminImsConnection->getProfile($accessToken);
110107
if (empty($profile['email'])) {
111108
throw new AuthenticationException(__('An authentication error occurred. Verify and try again.'));
112109
}
110+
111+
//check membership in organization
112+
$this->adminOrganizationService->checkOrganizationMembership($accessToken);
113+
113114
$this->adminLoginProcessService->execute($tokenResponse, $profile);
114115
} catch (AdobeImsAuthorizationException $e) {
115116
$this->logger->error($e->getMessage());

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
use Magento\AdobeImsApi\Api\Data\TokenResponseInterface;
1616
use Magento\Framework\Exception\AuthorizationException;
1717
use Magento\Framework\Exception\InvalidArgumentException;
18+
use Magento\Framework\Exception\LocalizedException;
1819
use Magento\Framework\HTTP\Client\Curl;
1920
use Magento\Framework\HTTP\Client\CurlFactory;
2021
use Magento\Framework\Serialize\Serializer\Json;
22+
use mysql_xdevapi\Exception;
2123

2224
class ImsConnection
2325
{
@@ -233,4 +235,56 @@ public function getProfile(string $code)
233235

234236
return $this->json->unserialize($curl->getBody());
235237
}
238+
239+
/**
240+
* Check if user is a member of Adobe IMS Organization
241+
*
242+
* @param string $orgId
243+
* @param string|null $token
244+
* @return bool
245+
* @throws AuthorizationException
246+
*/
247+
public function organizationMembership(string $orgId, ?string $token): bool
248+
{
249+
$result = false;
250+
if ($token === null) {
251+
return $result;
252+
}
253+
try {
254+
$curl = $this->curlFactory->create();
255+
256+
$curl->addHeader('Content-Type', 'application/x-www-form-urlencoded');
257+
$curl->addHeader('cache-control', 'no-cache');
258+
$curl->addHeader('Authorization', 'Bearer ' . $token);
259+
260+
$curl->get(
261+
$this->adminImsConfig->getOrganizationMembershipUrl($orgId),
262+
[]
263+
);
264+
265+
if ($curl->getBody() === '') {
266+
throw new AuthorizationException(
267+
__('Could not check Organization Membership')
268+
);
269+
}
270+
271+
$response = $curl->getBody();
272+
273+
if ($response == 'true') {
274+
$result = true;
275+
} else {
276+
throw new AdobeImsOrganizationAuthorizationException(
277+
__('User is not a member of configured Adobe Organization.')
278+
);
279+
}
280+
281+
} catch (Exception $exception) {
282+
throw new LocalizedException(
283+
__('Organization Membership check can\'t be performed')
284+
);
285+
286+
}
287+
288+
return $result;
289+
}
236290
}

app/code/Magento/AdminAdobeIms/Service/ImsConfig.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ImsConfig extends Config
3434
public const XML_PATH_ADMIN_AUTH_URL_PATTERN = 'adobe_ims/integration/admin/auth_url_pattern';
3535
public const XML_PATH_ADMIN_REAUTH_URL_PATTERN = 'adobe_ims/integration/admin/reauth_url_pattern';
3636
public const XML_PATH_ADMIN_ADOBE_IMS_SCOPES = 'adobe_ims/integration/admin/scopes';
37+
public const XML_PATH_ORGANIZATION_MEMBERSHIP_URL = 'adobe_ims/integration/organization_membership_url';
3738

3839
private const OAUTH_CALLBACK_URL = 'adobe_ims_auth/oauth/';
3940

@@ -376,4 +377,19 @@ public function getCertificateUrl(string $fileName): string
376377
{
377378
return $this->scopeConfig->getValue(self::XML_PATH_CERTIFICATE_PATH) . $fileName;
378379
}
380+
381+
/**
382+
* Get url to check organization membership
383+
*
384+
* @param string $orgId
385+
* @return string
386+
*/
387+
public function getOrganizationMembershipUrl(string $orgId): string
388+
{
389+
return str_replace(
390+
['#{orgId}'],
391+
[$orgId],
392+
$this->scopeConfig->getValue(self::XML_PATH_ORGANIZATION_MEMBERSHIP_URL)
393+
);
394+
}
379395
}

app/code/Magento/AdminAdobeIms/Service/ImsOrganizationService.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Magento\AdminAdobeIms\Service;
1010

1111
use Magento\AdminAdobeIms\Exception\AdobeImsOrganizationAuthorizationException;
12+
use Magento\AdminAdobeIms\Model\ImsConnection;
1213

1314
class ImsOrganizationService
1415
{
@@ -17,33 +18,40 @@ class ImsOrganizationService
1718
*/
1819
private ImsConfig $adminImsConfig;
1920

21+
/**
22+
* @var ImsConnection
23+
*/
24+
private ImsConnection $adminImsConnection;
25+
2026
/**
2127
* @param ImsConfig $adminImsConfig
28+
* @param ImsConnection $adminImsConnection
2229
*/
2330
public function __construct(
24-
ImsConfig $adminImsConfig
31+
ImsConfig $adminImsConfig,
32+
ImsConnection $adminImsConnection
2533
) {
2634
$this->adminImsConfig = $adminImsConfig;
35+
$this->adminImsConnection = $adminImsConnection;
2736
}
2837

2938
/**
30-
* Check if user is assigned to organization
39+
* Check if user is a member of Adobe Organization
3140
*
32-
* @param string $token
41+
* @param string $access_token
3342
* @return bool
3443
* @throws AdobeImsOrganizationAuthorizationException
3544
*/
36-
public function checkOrganizationAllocation(string $token): bool
45+
public function checkOrganizationMembership(string $access_token): bool
3746
{
3847
$configuredOrganization = $this->adminImsConfig->getOrganizationId();
3948

40-
//@TODO CABPI-324: Change Org check to use new endpoint
41-
if ($configuredOrganization === '' || !$token) {
49+
if ($configuredOrganization === '' || !$access_token) {
4250
throw new AdobeImsOrganizationAuthorizationException(
43-
__('User is not assigned to defined organization.')
51+
__('Can\'t check user membership in organization.')
4452
);
4553
}
4654

47-
return true;
55+
$this->adminImsConnection->organizationMembership($configuredOrganization, $access_token);
4856
}
4957
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
<openid>openid</openid>
1919
<email>email</email>
2020
<profile>profile</profile>
21+
<org.read>org.read</org.read>
2122
</scopes>
2223
</admin>
2324
<logging_enabled>0</logging_enabled>
2425
<organization_id backend_model="Magento\Config\Model\Config\Backend\Encrypted"/>
2526
<auth_url_pattern><![CDATA[https://ims-na1.adobelogin.com/ims/authorize/v2?client_id=#{client_id}&amp;redirect_uri=#{redirect_uri}&amp;locale=#{locale}&amp;scope=openid,creative_sdk,email,profile&amp;response_type=code]]></auth_url_pattern>
2627
<token_url>https://ims-na1.adobelogin.com/ims/token</token_url>
2728
<profile_url><![CDATA[https://ims-na1.adobelogin.com/ims/profile/v1?client_id=#{client_id}]]></profile_url>
29+
<organization_membership_url><![CDATA[https://graph.identity.adobe.com/#{org_id}@AdobeOrg/membership]]></organization_membership_url>
2830
<logout_url><![CDATA[https://ims-na1.adobelogin.com/ims/logout/v1?access_token=#{access_token}&amp;client_id=#{client_id}&amp;client_secret=#{client_secret}]]></logout_url>
2931
<certificate_path><![CDATA[https://static.adobelogin.com/keys/prod/]]></certificate_path>
3032
<validate_token_url><![CDATA[https://ims-na1.adobelogin.com/ims/validate_token/v1?token=#{token}&client_id=#{client_id}&type=#{token_type}]]></validate_token_url>

0 commit comments

Comments
 (0)